You are on page 1of 3

import java.util.*; import java.io.

*; public class KruskalMST { public static void main(String[] args) throws IOException { int count=0, c=0, min=Integer.MAX_VALUE, max=Integer.MIN_VALUE, a=0, j=0, ind ex1=0, index2=0; boolean addSortedList = false, addConnection = false; String input, special;//holds each line of text from the file to be sent to g etList LinkedList sorted = new LinkedList();//list to store all the edges sorted, as cending by value LinkedList MSTEdges = new LinkedList();//list to hold the edges needed to spa n all nodes PrintWriter write = new PrintWriter("output.txt");//opens the output file to be written to File fileName = new File("input.txt");//opens the input file Scanner test = new Scanner(fileName);//test scanner to count the number of li nes while(test.hasNextLine())//tests to see how many lines are in the file. Each line represents one adjacency list { count++; test.nextLine(); } LinkedList lists[] = new LinkedList[count];//creates and array of LinkedList' s using the number of lines found in the file Scanner scan = new Scanner(fileName);//creates new scanner to read the text f ile while(scan.hasNextLine())//reads a line from the text file, splits is by comm as, and puts each value into a seperate node of a linked list { lists[c] = new LinkedList(); input = scan.nextLine(); String[] splits = input.split(", "); for(int i=0; i<splits.length; i++) { lists[c].add(splits[i]); } c++; } for(int i=0; i<count; i++)//nested loop to find min and max values for the ed ges, used later in the program { c=2; while(c<lists[i].size()) { //eval = Integer.parseInt(lists[i].get(c).toString()); if(Integer.parseInt(lists[i].get(c).toString())<min) min=Integer.parseInt(lists[i].get(c).toString()); if(Integer.parseInt(lists[i].get(c).toString())>max) max=Integer.parseInt(lists[i].get(c).toString()); c += 2; } } /* The following nested loop structure is used to create the sorted list of edges.

Probably should be in a seperate function if I had time to split it up... purpose is to examine the edge-weights in each adjacency list. */ while(j<=(max-min))//first while-loop checks for minimum weights first and it erates through all weights from min to max. { for(int i=0; i<count; i++)//iterates through all adjacency lists { c=2; while(c<lists[i].size())//iterates through each list individually { a=0; if(Integer.parseInt(lists[i].get(c).toString()) == (min+j))//if stat ement to find the lowest remaining edge { addSortedList=true; while(a<sorted.size())//makes sure the edge hasn't already been a dded in opposite order (edge A-B could also be listed as B-A) { if((sorted.get(a).equals(lists[i].get(c-1)) && sorted.get(a+1) .equals(lists[i].get(0))) || (sorted.get(a).equals(lists[i].get(0)) && sorted.get(a+1).equa ls(lists[i].get(c-1)))) addSortedList = false; a+=2; } } if(addSortedList)//if an edge meets all conditions then it is added to the sorted list { sorted.add(lists[i].get(0)); sorted.add(lists[i].get(c-1)); addSortedList = false; } c+=2; } } j++; } String parentArray[] = new String[count];//declares array to hold parent node s String nodeArray[] = new String[count];//array to hold nodes, corresponds to parentArray for(int i=0; i<count; i++)//initializes parent and node array's { parentArray[i]=lists[i].getFirst().toString(); nodeArray[i]=parentArray[i]; } /* next nested structure should also probably be a seperate function... */ while(sorted.size()>0)//iterates through each edge in the sorted list to crea te MST { for(int i=0; i<nodeArray.length; i++)//determines the index of the next tw o nodes in the list { if(nodeArray[i].equals(sorted.get(0).toString())) index1=i;

if(nodeArray[i].equals(sorted.get(1).toString())) index2=i; } if(parentArray[index1].equals(parentArray[index2]))//if the edges share th e same parent they cannot be //connected because they will create a cycle. So they are removed right he re and the program will move on to the next edge { addConnection = false; sorted.removeFirst(); sorted.removeFirst(); } else//changes the parent nodes to adjust for new connections { special = parentArray[index2]; for(int i=0; i<parentArray.length; i++) if(parentArray[i]==special) parentArray[i]=parentArray[index1]; parentArray[index2]=parentArray[index1]; MSTEdges.add(sorted.removeFirst() + "-" + sorted.removeFirst());//remov es nodes from the sorted list and adds them to the MST list } } for(int i=0; i<MSTEdges.size(); i++)//iterates through the MST list printing each connection on a new line of the output file write.println(MSTEdges.get(i)); write.close();//closes the output file } }