Advertisement
xdxdxd123

Untitled

Jun 17th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. Problem definition:
  2. Give the program that implement Kruskal’s algorithm.
  3. Input:
  4.  
  5. First line is N, denotes the amount of test case, then there are Ns graph data with an option number (determine whether output the selected edges or not).
  6.  
  7. Each graph is undirected and connected, it is composed of V (the number of vertices, <= 1000), E (the number of edges, <=10000), then followed by Es edges which are denoted by pair of vertex and weight (e.g., 2 4 10 means an edge connecting vertices 2 and 4, and its weight is 10).
  8.  
  9. The first data of each measurement on behalf of the test vertex number, edge number and option number.
  10.  
  11.  
  12. The first data’s weight of each graph is option number. It could be 1 or 2, output the selected edge and the sum of all minimuspanning tree’s weight if it is 1, or only the sum if it is 2.
  13. We restrict that selected node of Prim always start from 0, and there is no “tree edge” with same weight.
  14.  
  15. Output:
  16.  
  17. If option is 1:
  18. The selected edges which forms the spanning tree. Order is important! The sum of all edges weight in minimum spanning tree. Note that the edge should put smaller node first, e.g., if the edge (4, 2) is selected, it should be output by 2 4, not 4 2
  19.  
  20. If option is 2:
  21. The sum of all edges weight in minimum spanning tree.
  22.  
  23. Example:
  24. Input:
  25.  
  26. 2
  27.  
  28. 5 7 1
  29.  
  30. 0 2 1
  31.  
  32. 2 1 6
  33.  
  34. 4 2 7
  35.  
  36. 1 4 2
  37.  
  38. 1 3 5
  39.  
  40. 3 0 3
  41.  
  42. 3 2 4
  43.  
  44. 6 12 2
  45.  
  46. 1 0 5
  47.  
  48. 0 4 1
  49.  
  50. 4 5 10
  51.  
  52. 4 3 4
  53.  
  54. 3 0 9
  55.  
  56. 0 5 2
  57.  
  58. 2 0 8
  59.  
  60. 2 1 3
  61.  
  62. 5 2 11
  63.  
  64. 2 3 6
  65.  
  66. 3 5 7
  67.  
  68. 1 5 12
  69.  
  70.  
  71. Output: (Kruskal’s algorithm)
  72.  
  73. 0 2
  74.  
  75. 1 4
  76.  
  77. 0 3
  78.  
  79. 1 3
  80.  
  81. 11
  82.  
  83. 15
  84.  
  85. Please using c++ programming language and follow my INPUT and OUTPUT formats!!!!! Thank you .
  86.  
  87. Expert Answer
  88. BhanuP
  89. BhanuP Chegg expert answered this Was this answer helpful?
  90. 0
  91. 0
  92. 710 answers
  93. Program in C++
  94.  
  95. #include<iostream>
  96. #include<conio.h>
  97. #include<stdlib.h>
  98. using namespace std;
  99. int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p;
  100.  
  101. main()
  102. {
  103. int dup1,dup2;
  104. cout<<"enter no of vertices";
  105. cin >> n;
  106. cout <<"enter no of edges";
  107. cin >>m;
  108. cout <<"EDGE Cost";
  109. for(k=1;k<=m;k++)
  110. {
  111. cin >>i >>j >>c;
  112. cost[i][j]=c;
  113. cost[j][i]=c;
  114. }
  115. for(i=1;i<=n;i++)
  116. for(j=1;j<=n;j++)
  117. if(cost[i][j]==0)
  118. cost[i][j]=31999;
  119. visit=1;
  120. while(visit<n)
  121. {
  122. v=31999;
  123. for(i=1;i<=n;i++)
  124. for(j=1;j<=n;j++)
  125. if(cost[i][j]!=31999 && cost[i][j]<v && cost[i][j]!=-1 )
  126. {
  127. int count =0;
  128. for(p=1;p<=n;p++)
  129. {
  130. if(visited[p]==i || visited[p]==j)
  131. count++;
  132. }
  133. if(count >= 2)
  134. {
  135. for(p=1;p<=n;p++)
  136. if(cost[i][p]!=31999 && p!=j)
  137. dup1=p;
  138. for(p=1;p<=n;p++)
  139. if(cost[j][p]!=31999 && p!=i)
  140. dup2=p;
  141.  
  142. if(cost[dup1][dup2]==-1)
  143. continue;
  144. }
  145. l=i;
  146. k=j;
  147. v=cost[i][j];
  148. }
  149. cout <<"edge from " <<l <<"-->"<<k;
  150. cost[l][k]=-1;
  151. cost[k][l]=-1;
  152. visit++;
  153. int count=0;
  154. count1=0;
  155. for(i=1;i<=n;i++)
  156. {
  157. if(visited[i]==l)
  158. count++;
  159. if(visited[i]==k)
  160. count1++;
  161. }
  162. if(count==0)
  163. visited[++vst]=l;
  164. if(count1==0)
  165. visited[++vst]=k;
  166. }
  167. }
  168.  
  169. Comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement