Question: Implement the prim(int r) function as discussed in Lecture 18 for JAVA. (Note: You should return an integer that indicates the total weight of the
Implement the prim(int r) function as discussed in Lecture 18 for JAVA. (Note: You should return an integer that indicates the total weight of the minimum spanning tree. ) Below is the psescode, and skeleton code to modify for the above question/solution.
public class Graph2 {
public int n; /umber of vertice
public int[][] A; //the adjacency matrix
public Graph2 () {
n = 0;
A = null;
}
public Graph2 (int _n, int[][] _A) {
this.n = _n;
this.A = _A;
}
public int prim (int r) { // input answer
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 9;
int A[][] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
Graph2 g = new Graph2(n, A);
System.out.println(g.prim(0));
}
}


MST-KRUSKAL (G, w) 2 for each vertex v E G.V 3MAKE-SET(v) 4 sort the edges of G.E into nondecreasing order by weight iw 5 for each edge (u, v) e G.E, taken in nondecreasing order by weight if FIND-SET(u)FIND-SET(v) A AU(u, v)) UNION(u, v) 9 return A
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
