Question
Hello, first of all thank you so much! Anything will be helpful. I'm trying to write a java code with Prim's Algorithm using minimum spanning
Hello, first of all thank you so much! Anything will be helpful.
I'm trying to write a java code with Prim's Algorithm using minimum spanning tree java code. I was supposed to the code using priority queue but for now I won't use it neither imported programs like queue and LinkNode I had.
For now I just want my code to work...is there a way yo solve this:
package graph; import java.util.*; import ds.ListNode; import ds.Queue; public class Graph2 { public int n; //number 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; } //***** Please dont change code above, I wasn't supposed to change it*** ^o^/ public int prim (int r) { int parent[] = new int[n]; int key[] = new int[n]; boolean [] setNumber = new boolean[n]; //Initialize all keys to infinite for(int i = 0; i < n; i++) { key[i] = Integer.MAX_VALUE; setNumber[i] = false; } key[0] = 0; parent[0] = -1; // First node is always root of MST // The MST will have vertices-1 edges for (int count = 0; count < n - 1; count++) { int u = minKey(key,setNumber); setNumber[u] = true; // Add the picked vertex to the MST Set for (int v = 0; v < n; v++) { if (A[u][v] != 0 && setNumber[v] && A[u][v] < key[v]) { parent[v] = u; key[v] = A[u][v]; } } } int weight = 0; for (int i = 1; i < n; i++) { weight = A[i][parent[i]];//The answer I want is in A[i][parent[i] but sum all the weights. } return weight; } public int minKey(int[] key, boolean [] setNumber) { int min = Integer.MAX_VALUE; int minIndex = -1; for (int v = 0; v < n; v++) { if (!setNumber[v] && key[v] <= min) { min = key[v]; minIndex = v; } } return minIndex; } ///Finish of copy code //***** Please dont change code below, I wasn't supposed to change it*** ^o^/ /** * @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)); } }
I went to this link that provided me the code, I saw that it had the same numbers I have in my homework but I don't know to to print my solution.
https://github.com/VamsiSangam/theoryofprogramming/blob/master/Graph%20Theory/Prim's%20Algorithm/Java/PrimsAlgorithm.java
This is the code I used that wanted to teach how Prim works. But my question is how can I implement that into here?
I compiled the code in the link and the answers are: 4,4,7,9,2,1,8,2 . But I need to return an integer that indicates the total weight of the minimum spanning tree, which would be 37. So I have to add a code to sum all of those numbers.
Anything please let me know~
If this takes too much of your time, I'm sorry but thanks for trying. It's okay, you can help others. Hints would be helpful.
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