Question
You are provided one skeleton program named Graph2.java. Implement the prim(int r) function for minimum spanning tree using PRIORITY QUEUE . You should return an
You are provided one skeleton program named Graph2.java. Implement the prim(int r) function for minimum spanning tree using PRIORITY QUEUE. You should return an integer that indicates the total weight of the minimum spanning tree. In order to associate each node with a weight and place it into the queue, you may need to create another class.
PLEASE TRY TO FOLLOW THE PSEUDO CODE AND THE INSTRUCTIONS. Thank you
package graph;
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) {
}
/**
* @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));
}
}
Minimum Spanning Tree MST-PRIM (G, w, r) Key idea: 1 for each u e G. V 2 Take r as the root of the tree. In each iteration, add one edge to the tree until it covers all the nodes 6 while Q 7 u ExTRACT-MIN ) 8 for each v E G.Adj[u] The min-priority queue Q includes all nodes that are not in the tree yet, where each node's key value is the minimum weight of any edge ifv e Q and w(u, v)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started