prims algorith and MST

hi, i have a project to turn in for a class, and for the life of me i cannot figure out what is wrong. I;ve spent the better part of the last couple days trying to figure it out, and its a no go. My professor affords me no help, and my book implements graphs with adjacent matricies, not linked lists. I've read through a few books on java and data structures, but they didn't help my linked list implementation, nor my understanding of the concept.

I am trying to implement prim's algorith using linked lists, and thus far i have this code, maybe someone can help me, as i have almost giving up all hope.

import java.io.*;

import java.util.*;

publicclass Graph{

Vertex_Node head;

int size;

publicvoid find_mst(){

clearMarks();

find_mst(head);

}

publicvoid find_mst(Vertex_Node v){

Edge_Node e = v.GetEdge_Head();int size_tree = 0; Edge_Node min = e;

v.SetMarked(true); v.Setin_mst(true); size_tree++;

while ( size_tree < size){

min = smallest(v);

min.Setin_mst(true);

v.SetMarked(true);

v.Setin_mst(true);

min.GetTarget().Setin_mst(true);

size_tree++;

v = v.GetNext();

}

}

public Edge_Node smallest(Vertex_Node v){

Edge_Node e = v.GetEdge_Head(), min = v.GetEdge_Head();

while(e !=null){

if(e.Getin_mst()){

e = e.GetNext();

}

else{

if(e.GetWeight() < min.GetWeight()){

min = e;

}

}

e = e.GetNext();

}

return min;

}

public Edge_Node cycle(int in_tree){

int visited = 0;

Edge_Node e = head.GetEdge_Head(); Edge_Node min = e;

Vertex_Node v = head;

while(visited < in_tree){

if(v.Getin_mst()){

System.out.println("min cycle = " + v.name);

min = smallest(v);

visited++;

if(min.GetTarget().Getin_mst()){

v = e.GetTarget();

}

}

}

return min;

}

publicvoid output_mst_edges(){

clearMarks();

output_mst_edges(head);

}

publicvoid output_mst_edges(Vertex_Node v){

Edge_Node e;

if (v ==null){

}

else{

while( v !=null){

v.SetMarked(true);

e = v.GetEdge_Head();

while (e !=null){

if (e.Getin_mst()){

System.out.print(e.GetWeight() +" ");

}

e = e.GetNext();

}

v = v.GetNext();

}

}

System.out.println();

}

publicvoid clearMarks(){

Vertex_Node v = head;

while (v !=null){

v.SetMarked(false);

v = v.GetNext();

}

}

publicvoid output_mst_weight(){

clearMarks();

System.out.println("Wt. = " + get_mst_weight(head));

}

publicint get_mst_weight(Vertex_Node v){

Edge_Node e;

int weight = 0;

if (v ==null || v.GetMarked())

weight = 0;

else{

v.SetMarked(true);

e = v.GetEdge_Head();

while (e !=null){

if (e.Getin_mst())

weight += e.GetWeight();

weight += get_mst_weight(e.GetTarget());

e = e.GetNext();

}

}

return weight;

}

public Vertex_Node input(String fileName)throws IOException{

String inputLine, vertexName, sourceName, targetName;

Vertex_Node source = null, target;

Edge_Node e;

int weight;

StringTokenizer input;

BufferedReader inFile =new BufferedReader(new FileReader(fileName));

inputLine = inFile.readLine();

while (inputLine !=null){

input =new StringTokenizer(inputLine);

sourceName = input.nextToken();

source = findVertex(sourceName);

if (source ==null){

head =new Vertex_Node(sourceName, head);

source = head;

size++;

}

targetName = input.nextToken();

target = findVertex(targetName);

if (target ==null){

head =new Vertex_Node(targetName, head);

target = head;

size++;

}

if (input.hasMoreTokens()){

weight = Integer.parseInt(input.nextToken());

// put edge in one direction -- after checking for repeat

e = source.edge_head;

while (e !=null){

if (e.target == target){

System.out.print("Multiple edges from " + source.name +" to ");

System.out.println(target.name +".");

break;

}

e = e.next;

}

source.edge_head =new Edge_Node(target, source.edge_head, weight);

// put edge in the other direction

e = target.edge_head;

while (e !=null){

if (e.target == source){

System.out.print("Multiple edges from " + target.name +" to ");

System.out.println(source.name +".");

break;

}

e = e.next;

}

target.edge_head =new Edge_Node(source, target.edge_head, weight);

}

inputLine = inFile.readLine();

}

inFile.close();

return source;

}

public Vertex_Node findVertex(String s){

Vertex_Node pt = head;

while (pt !=null && s.compareTo(pt.name) != 0)

pt = pt.next;

return pt;

}

}

class Vertex_Node{

String name;

int distance;

boolean marked;

boolean in_mst;

Edge_Node edge_head;

Vertex_Node next;

public Vertex_Node(String target, Vertex_Node v){

name = target;

next = v;

}

public Vertex_Node GetNext(){

return next;

}

publicboolean GetMarked(){

return marked;

}

publicvoid SetMarked(boolean b){

marked = b;

}

public Edge_Node GetEdge_Head(){

return edge_head;

}

publicvoid SetDistance(int d){

distance = d;

}

publicboolean Getin_mst(){

return in_mst;

}

publicvoid Setin_mst(boolean b){

in_mst = b;

}

}

class Edge_Node{

int weight;

boolean in_mst;

Edge_Node next;

Vertex_Node target;

public Edge_Node(Vertex_Node v, Edge_Node e,int w){

next = e;

target = v;

weight = w;

}

publicboolean Getin_mst(){

return in_mst;

}

publicint GetWeight(){

return weight;

}

public Vertex_Node GetTarget(){

return target;

}

public Edge_Node GetNext(){

return next;

}

publicvoid Setin_mst(boolean b){

in_mst = b;

}

}

thank you for any help.

[13864 byte] By [lance_c_12a] at [2007-11-26 12:39:44]
# 1

You didn't say what your problem was with that code.

If it doesn't compile, then tell us that, and tell us what error messages appear, and what lines of code they refer to.

If it crashes when you run it, then tell us that, and paste in the stack trace and tell us what line of code it crashed at.

If it doesn't do what you think it is supposed to do, then tell us what you think it is supposed to do and what it actually does.

Just dumping a pile of code is useless.

DrClapa at 2007-7-7 16:10:14 > top of Java-index,Java Essentials,Java Programming...
# 2

sorry, yeah i was ahead of myself.

it seems to count nodes twice, or i think it ALWAYS finds the smallest connected node, even if i set it as in the MST -- via Setin_mst(true)

i get a node list for the mst which seems to count nodes twice...

but i cannot fix it. no matter what i try.

for example

i get

6 5 5 8 4

Wt. = 28

and that has the edge of weight 5 twice, there is only one there...

my main (which i omitted sorry again) is

import java.io.*;

public class MSTTest {

public static void main(String[] args) throws IOException {

Vertex_Node startVertex;

Graph g;

int i;

for (i = 1; i <= 3; i++) {

g = new Graph();

g.input("MSTTest"+i+".txt");

g.find_mst();

System.out.println("Graph #" + i + ".");

if (i <= 3)

g.output_mst_edges();

g.output_mst_weight();

System.out.println();

}

}

}

and the 3 text files are

MSTTest1.txt

a b 5

b c 7

c a 3

MSTTest2.txt

Albany Helena 12

Helena Io 18

Io LAX 32

Albany Barstow 9

Helena Colinga 19

Io Jasper 41

LAX Kobe 18

Barstow Colinga 7

Jasper Colinga 45

Jasper Kobe 15

Gothum Barstow 22

Colinga Denver 29

Jasper EchoPark 14

Kobe Folsum 10

Denver Gothum 39

Denver EchoPark 20

EchoPark Folsum 5

and finally

MSTTest3.txt

e f 4

d e 12

c f 15

b e 10

a d 8

b c 5

a b 6

lance_c_12a at 2007-7-7 16:10:14 > top of Java-index,Java Essentials,Java Programming...