Advertisement
Guest User

Untitled

a guest
Apr 9th, 2014
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int numVertices,numEdges;
  5. int *parent,*weight,numTrees;
  6. int *bestEdgeNum;
  7.  
  8. struct edge {
  9. int tail,head,weight;
  10. };
  11. typedef struct edge edgeType;
  12. edgeType *edgeTab;
  13.  
  14. int find(int x)
  15. {
  16. int i,j,root;
  17.  
  18. for (i=x;parent[i]!=i; i=parent[i]);
  19. root=i;
  20. // path compression
  21. for (i=x; parent[i]!=i;j=parent[i],parent[i]=root,i=j);
  22. return root;
  23. }
  24.  
  25. void makeEquivalent(int i,int j)
  26. {
  27. if (weight[i]>weight[j])
  28. {
  29. parent[j]=i;
  30. weight[i]+=weight[j];
  31. }
  32. else
  33. {
  34. parent[i]=j;
  35. weight[j]+=weight[i];
  36. }
  37. numTrees--;
  38. }
  39.  
  40. int main()
  41. {
  42. int i,MSTweight=0;
  43. int root1,root2;
  44. int usefulEdges;
  45.  
  46. cout << "Enter the number of Vertices\n";
  47. cin >> numVertices;
  48. cout << "Enter the number of Edges\n";
  49. cin >> numEdges;
  50.  
  51. edgeTab = new edgeType[numEdges];
  52. parent = new int[numVertices];
  53. weight = new int[numVertices];
  54. bestEdgeNum = new int[numVertices];
  55.  
  56. if (!edgeTab || !parent || !weight || !bestEdgeNum)
  57. {
  58. cout << "error\n";
  59. }
  60.  
  61. cout << "Enter the undirected edge weights for the graph in the format u v w\n";
  62. for (i=0;i<numEdges;i++)
  63. {
  64. cin >> edgeTab[i].tail >> edgeTab[i].head >> edgeTab[i].weight;
  65. }
  66. for (i=0;i<numVertices;i++)
  67. {
  68. parent[i]=i;
  69. weight[i]=1;
  70. }
  71. numTrees=numVertices; // Each vertex is initially in its own subtree
  72. usefulEdges=numEdges; // An edge is useful if the two vertices are separate
  73. while (numTrees>1 && usefulEdges>0)
  74. {
  75. for (i=0;i<numVertices;i++)
  76. bestEdgeNum[i]=(-1);
  77. usefulEdges=0;
  78. for (i=0;i<numEdges;i++)
  79. {
  80. root1=find(edgeTab[i].tail);
  81. root2=find(edgeTab[i].head);
  82. if (root1==root2)
  83. cout << edgeTab[i].tail <<" , " << edgeTab[i].head << " : "
  84. << edgeTab[i].weight << " is useless\n";
  85. else
  86. {
  87. usefulEdges++;
  88. if (bestEdgeNum[root1] == -1||edgeTab[bestEdgeNum[root1]].weight>edgeTab[i].weight)
  89. bestEdgeNum[root1]=i; // Have a new best edge from this component
  90.  
  91. if (bestEdgeNum[root2]==(-1)|| edgeTab[bestEdgeNum[root2]].weight>edgeTab[i].weight)
  92. bestEdgeNum[root2]=i; // Have a new best edge from this component
  93. }
  94. }
  95. for (i=0;i<numVertices;i++)
  96. if (bestEdgeNum[i]!=(-1))
  97. {
  98. root1=find(edgeTab[bestEdgeNum[i]].tail);
  99. root2=find(edgeTab[bestEdgeNum[i]].head);
  100. if (root1==root2)
  101. continue; // This round has already connected these components.
  102. MSTweight+=edgeTab[bestEdgeNum[i]].weight;
  103. cout << edgeTab[bestEdgeNum[i]].tail << " " << edgeTab[bestEdgeNum[i]].head << " " << edgeTab[bestEdgeNum[i]].weight << "included in MST\n";
  104. makeEquivalent(root1,root2);
  105. }
  106. cout << "numTrees is " << numTrees << endl;
  107.  
  108. }
  109. if (numTrees!=1)
  110. cout << "MST does not exist\n";
  111. cout << "Sum of weights of spanning edges is " << MSTweight << endl;
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement