Note that there are some explanatory texts on larger screens.

plurals
  1. POoutput problems of prims algorithm
    primarykey
    data
    text
    <p>this is the code of prim's algorithm which i learnt from someone, <a href="http://sourcecodesforfree.blogspot.in/2013/05/10-prims-algorithm.html" rel="nofollow">http://sourcecodesforfree.blogspot.in/2013/05/10-prims-algorithm.html</a></p> <p>here is the previous one...i ran the previous code ,and it will give me the Minimum cost of spanning tree. and i want to change the weight between two nodes to random, and it is a complete weighted graph at the mean time. i need to know what's wrong with my code.. sometimes i could get the correct when i input 3 as the number of the nodes, after i tried for several times, i found that the code is always adding the w[1][2] with w[1][3], then output the answer. and i guess the problem occurs while assigning the weight. because the previous code asked me to input the weight as well as between which two nodes.</p> <pre><code>import java.util.*; import java.io.*; public class integrated { public static Scanner br = new Scanner(System.in);//this is used to listen to any input from the users static int w [][];//it is the weight between two edges.. static int n;//it is the number of vertices. //static declaration can be used in every method. public static void main (String[] args) throws IOException { System.out.println("Integrated Algorithm"); System.out.println("\nEnter the number of the vertices: "); n = br.nextInt(); w = new int[n+1][n+1]; for(int i=1;i&lt;n;i++) { for(int j=1;j&lt;=n;j++) { if((i!=j)&amp;&amp;(i&lt;j)) { w[i][j] = w[j][i] = 1+(int)(Math.random()*9);//this step is to give each edge random weight. } } } System.out.println(n+" by "+n+" Matrix:"); Graph();//it is used to call a new method which can draw a n by n matrix with random weight. Prim(); } static void Graph() { for(int i=1;i&lt;=n;i++) { for(int j=1;j&lt;=n;j++) { System.out.print(" "+w[i][j]+" "); } System.out.println(); } } static void Prim() { int d[] = new int[n+1];//it is the distance between two nodes. int total,current,mincost=0; int visited[] = new int[n+1];//the node which was visited just now. int near[] = new int[n+1];//the closest node nears to the current node. for(int i=1;i&lt;=n;i++) { near[i]=visited[i]=0; } for(int i=1;i&lt;=n;i++) { d[i]=32767;//initiate the distance between two nodes as 32767, but its not the real distance between two nodes in matrix. } current = 1;//initiate the first and starting node as 1. visited[current]=1;//and initiate the visited[current] equal to 1. d[current]=0; total=1;//this is just a number, which is used to do the iterations. System.out.println("Minimum Spanning Tree: "); while(total!=n) { for(int i=1;i&lt;=n;i++) { if(w[i][current]!=0&amp;&amp;visited[i]!=1)//if we want to keep moving, we have to meet the conditions, here weight of current and nodes is not equal to zero and ignore the current node to make the comparisons. if(w[i][current]&lt;d[i])//and weight of i and current nodes have to be less than d[i], which is initiated by me before. { d[i]=w[i][current];//if it passed the 'if', then d[i] would be covered by the shortest edge, which the starting node was current. near[i]=current; } } int min=32767;// for(int i=1;i&lt;=n;i++) { if(visited[i]!=1&amp;&amp;d[i]&lt;mincost) { min=d[i]; current=i;//after the comparisons, they select the closest node to cover the current. System.out.println(near[i]+" to "+current); } } visited[current]=1;//after each iteration, the current changes as well, so the visited[current] should be changed as the changes of current. total++;//after all the steps has been finished in one iteration, total is increased by one. } for(int i=1;i&lt;=n;i++) mincost+=d[i]; System.out.println("minimum cost= "+mincost); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload