MSTLinkedlist - i++; } } } java.util.Arrays.sort(edgeArr);...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public LinkedList<Edge> mst() { Collection<Vertex> vertexList = h.values(); int i = 0; int numedges = 0; for (Vertex v: vertexList) { v.number = i; i++; numedges = numedges + v.edgeList.size(); } /* numedges is now twice the number of edges, so divide by 2 */ numedges = numedges/2; Edge [] edgeArr = new Edge [numedges]; i = 0; /* Create array of edges */ for (Vertex v: vertexList) { for (Edge e: v.edgeList) { if (e.origin == v) { // making sure edge is inserted only once edgeArr[i] = e;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: i++; } } } java.util.Arrays.sort(edgeArr); /*Initialize output and disjoint sets structure*/ LinkedList&lt;Edge&gt; mstEdges = new LinkedList&lt;Edge&gt;(); DisjointSets ds = new DisjointSets(h.size()); int j1, j2; /* The main kruskal loop */ for (i = 0; i &lt; edgeArr.length; i++) { Edge e = edgeArr[i]; j1 = e.origin.number; j2 = e.destination.number; if ( ds.find(j1) != ds.find(j2)) { mstEdges.add(e); ds.union(ds.find(j1), ds.find(j2)); } } return mstEdges; }...
View Full Document

Ask a homework question - tutors are online