androkali

p1

Feb 25th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include<stdio.h>
  2. #define MAX 10
  3. #define TEMP 0
  4. #define PERM 1
  5. #define FALSE 0
  6. #define TRUE 1
  7. #define infinity 9999
  8.  
  9. struct node
  10. {
  11. int predecessor;
  12. int dist; /*Distance from predecessor */
  13. int status;
  14. };
  15.  
  16. struct edge
  17. {
  18. int u;
  19. int v;
  20. };
  21.  
  22. int adj[MAX][MAX];
  23. int n;
  24.  
  25. main()
  26. {
  27. int i,j;
  28. int path[MAX];
  29. int wt_tree,count;
  30. struct edge tree[MAX];
  31. clrscr();
  32.  
  33. create_graph();
  34. printf("Adjacency matrix is :\n");
  35. display();
  36.  
  37. count = maketree(tree,&wt_tree);
  38.  
  39. printf("Weight of spanning tree is : %d\n", wt_tree);
  40. printf("Edges to be included in spanning tree are : \n");
  41. for(i=1;i<=count;i++)
  42. {
  43. printf("%d->",tree[i].u);
  44. printf("%d\n",tree[i].v);
  45. }
  46. getch();
  47. }/*End of main()*/
  48.  
  49. create_graph()
  50. {
  51. int i,max_edges,origin,destin,wt;
  52.  
  53. printf("\n\nEnter number of vertices : ");
  54. scanf("%d",&n);
  55. max_edges=n*(n-1)/2;
  56.  
  57. for(i=1;i<=max_edges;i++)
  58. {
  59. printf("\nEnter two vertices for %d edge:(Enter 0 0 for Quit) ",i);
  60. scanf("%d %d",&origin,&destin);
  61. if((origin==0) && (destin==0))
  62. break;
  63. printf("Enter weight for this edge : ");
  64. scanf("%d",&wt);
  65. if( origin > n || destin > n || origin<=0 || destin<=0)
  66. {
  67. printf("Invalid edge!\n");
  68. i--;
  69. }
  70. else
  71. {
  72. adj[origin][destin]=wt;
  73. adj[destin][origin]=wt;
  74. }
  75. }/*End of for*/
  76. if(i<n-1)
  77. {
  78. printf("Spanning tree is not possible\n");
  79. exit(1);
  80. }
  81. }/*End of create_graph()*/
  82.  
  83. display()
  84. {
  85. int i,j;
  86. for(i=1;i<=n;i++)
  87. {
  88. for(j=1;j<=n;j++)
  89. printf("%3d",adj[i][j]);
  90. printf("\n");
  91. }
  92. }/*End of display()*/
  93.  
  94. int maketree(struct edge tree[MAX],int *weight)
  95. {
  96. struct node state[MAX];
  97. int i,k,min,count,current,newdist;
  98. int m;
  99. int u1,v1;
  100. *weight=0;
  101. /*Make all nodes temporary*/
  102. for(i=1;i<=n;i++)
  103. {
  104. state[i].predecessor=0;
  105. state[i].dist = infinity;
  106. state[i].status = TEMP;
  107. }
  108. /*Make first node permanent*/
  109. state[1].predecessor=0;
  110. state[1].dist = 0;
  111. state[1].status = PERM;
  112.  
  113. /*Start from first node*/
  114. current=1;
  115. count=0; /*count represents number of nodes in tree */
  116. while( all_perm(state) != TRUE ) /*Loop till all the nodes become PERM*/
  117. {
  118. for(i=1;i<=n;i++)
  119. {
  120. if ( adj[current][i] > 0 && state[i].status == TEMP )
  121. {
  122. if( adj[current][i] < state[i].dist )
  123. {
  124. state[i].predecessor = current;
  125. state[i].dist = adj[current][i];
  126. }
  127. }
  128. }/*End of for*/
  129.  
  130. /*Search for temporary node with minimum distance
  131. and make it current node*/
  132. min=infinity;
  133. for(i=1;i<=n;i++)
  134. {
  135. if(state[i].status == TEMP && state[i].dist < min)
  136. {
  137. min = state[i].dist;
  138. current=i;
  139. }
  140. }/*End of for*/
  141.  
  142. state[current].status=PERM;
  143.  
  144. /*Insert this edge(u1,v1) into the tree */
  145. u1=state[current].predecessor;
  146. v1=current;
  147. count++;
  148. tree[count].u=u1;
  149. tree[count].v=v1;
  150. /*Add wt on this edge to weight of tree */
  151. *weight=*weight+adj[u1][v1];
  152. }/*End of while*/
  153. return (count);
  154. }/*End of maketree()*/
  155.  
  156. /*This function returns TRUE if all nodes are permanent*/
  157. int all_perm(struct node state[MAX] )
  158. {
  159. int i;
  160. for(i=1;i<=n;i++)
  161. if( state[i].status == TEMP )
  162. return FALSE;
  163. return TRUE;
  164. }/*End of all_perm()*/
Add Comment
Please, Sign In to add comment