Advertisement
Guest User

Untitled

a guest
Jun 8th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <memory.h>
  5.  
  6. // Create the edge type
  7. struct Edge
  8. {
  9. int from;
  10. int to;
  11. int weight;
  12. bool inTheCut;
  13. };
  14. typedef Edge* pEdge;
  15.  
  16.  
  17. // adjacency list
  18. struct ListIt
  19. {
  20. int value;
  21. ListIt *p_next;
  22. };
  23.  
  24. // typedef for the pointer type
  25. typedef ListIt* pListIt;
  26.  
  27. pEdge edges;
  28. pEdge edgesPr;
  29.  
  30. int n,m;
  31.  
  32. int *tree;
  33. int *ancient;
  34. int **distanceMatrix;
  35.  
  36.  
  37. pListIt* adjacencyList; // Here we save the relations.... A 100000 is the maximum vertexes
  38.  
  39.  
  40. void read();
  41. void print();
  42. void Kruskal();
  43. int Prim(int at);
  44. bool add(pListIt &dest, int val);
  45. int compareEdges(const void* el1, const void* el2);
  46. int min_vag();
  47.  
  48. int main()
  49. {
  50. read();
  51. printf("\n");
  52. print();
  53.  
  54. //Kruskal();
  55.  
  56. printf("\n\n With Prims Technique: \n\n");
  57. Prim(1);
  58.  
  59. return 0;
  60. }
  61.  
  62. //************************************
  63. // Method: read - public access
  64. // Returns: void
  65. //
  66. // Purpose: Read in the data from In.txt
  67. //************************************
  68. void read()
  69. {
  70. FILE *f;
  71. freopen_s(&f, "In.txt", "r", stdin);
  72. freopen_s(&f, "Out.txt", "w", stdout);
  73.  
  74. scanf_s("%d", &n);
  75. scanf_s("%d", &m);
  76.  
  77. tree = (int*) calloc(n+1, sizeof(int));
  78. ancient = (int*) calloc(n+1, sizeof(int));
  79. edges = (pEdge)malloc(m*sizeof(Edge));
  80. adjacencyList = (pListIt*) calloc(n+1,sizeof(pListIt));
  81.  
  82. distanceMatrix = (int**) calloc(n+1, sizeof(int*));
  83. for (int j = 1; j <=n; ++j)
  84. {
  85. distanceMatrix[j] = (int*) calloc(n+1, sizeof(int));
  86. }
  87.  
  88.  
  89. int from,to,distance;
  90.  
  91. for(int i =0; i< m; ++i)
  92. {
  93. scanf_s("%d%d%d", &from, &to, & distance);
  94.  
  95. add(adjacencyList[from], to);
  96. add(adjacencyList[to], from);
  97.  
  98. edges[i].from = from;
  99. edges[i].to = to;
  100. edges[i].weight = distance;
  101. }
  102. }
  103.  
  104. //************************************
  105. // Method: print - public access
  106. // Parameter:
  107. // Returns: void
  108. //
  109. // Purpose: Print the list of edges
  110. //************************************
  111. void print()
  112. {
  113. printf("\n The list of the edges:\n ");
  114. for(int i =0; i < m; ++i)
  115. {
  116. if(!(i % 1)) // how many in a row... for now 1 edge per row
  117. {
  118. printf("\n");
  119. }
  120. printf(" %d ~ %d -> %d ", edges[i].from, edges[i].to, edges[i].weight);
  121. }
  122.  
  123. printf("\n");
  124.  
  125. }
  126.  
  127. void Kruskal()
  128. {
  129. int i = 0, overallWeight = 0 , j =0;
  130. int fromTree = 0, toTree = 0, changeNr = 0;
  131.  
  132.  
  133. // sort the edge list
  134. qsort((void*)edges,m,sizeof(Edge),compareEdges);
  135.  
  136.  
  137. printf("\n");
  138. print();
  139. printf("\n");
  140.  
  141. printf("\nThe list of the edges with the algorithm of Kruskall:\n");
  142.  
  143.  
  144. // build up the individual list
  145. for ( i =1; i <= n; ++i)
  146. tree[i] = i;
  147.  
  148.  
  149. for ( i= 0; i < m && changeNr < n; ++i)
  150. {
  151. fromTree = tree[edges[i].from];
  152. toTree = tree[edges[i].to];
  153.  
  154. if( fromTree != toTree)
  155. {
  156. ++changeNr;
  157. overallWeight += edges[i].weight;
  158. printf(" %d) %d ~ %d -> %d \n", changeNr, edges[i].from, edges[i].to, edges[i].weight);
  159.  
  160. //Union of the trees
  161. for ( j = 0; j < m; ++j)
  162. if(tree[j] == toTree)
  163. tree[j] = fromTree;
  164. }
  165. }
  166. printf( " \n The minimal weight %d ", overallWeight);
  167.  
  168. }
  169.  
  170. //************************************
  171. // Method: compareEdges - public access
  172. // Parameter:
  173. // const void * edge1
  174. // const void * edge2
  175. // Returns: int
  176. //
  177. // Purpose: Compare two edge types according to their weight
  178. //************************************
  179. int compareEdges(const void* edge1, const void * edge2)
  180. {
  181. pEdge first = (pEdge)edge1;
  182. pEdge second = (pEdge)edge2;
  183.  
  184. return first->weight - second->weight;
  185. }
  186.  
  187. //************************************
  188. // Method: Prim - public access
  189. // Parameter:
  190. // int at
  191. // Returns: int
  192. //
  193. // Purpose: Prims Technique to generate the minimal spanning tree
  194. //************************************
  195. int Prim(int at)
  196. {
  197. int i = 0;
  198.  
  199. memset(tree, 0, (n+1)*sizeof(int));
  200.  
  201. tree[at] = 1;
  202. ancient[at] = 0 ;
  203.  
  204.  
  205. // sort the edge list
  206. qsort((void*)edges,m,sizeof(Edge),compareEdges);
  207.  
  208.  
  209. // build up the tree
  210. for (i = 0; i < m; ++i)
  211. {
  212. distanceMatrix[edges[i].from][edges[i].to ] = i;
  213. distanceMatrix[edges[i].to ][edges[i].from] = i;
  214.  
  215. if (edges[i].from == at || edges[i].to == at)
  216. {
  217. edges[i].inTheCut = true;
  218. }
  219. else
  220. {
  221. edges[i].inTheCut = false;
  222. }
  223. }
  224.  
  225. int overallWeight = 0;
  226. int curent = 0;
  227. int addedVertex = 1;
  228. int neighborVertex = 0;
  229. int neighborEdge = 0;
  230. pListIt p = NULL;
  231. int j = 0;
  232.  
  233. for(i = 1; i < n; ++i)
  234. {
  235. for ( j =0 ; j < m; ++j)
  236. {
  237. if (edges[j].inTheCut)
  238. {
  239. curent = j;
  240. break;
  241. }
  242. }
  243.  
  244.  
  245. printf( " %d) %d ~ %d -> %d \n",i, edges[curent].from,edges[curent].to, edges[curent].weight);
  246.  
  247. overallWeight += edges[curent].weight;
  248.  
  249. if (tree[edges[curent].from] == 1)
  250. {
  251. addedVertex = edges[curent].to;
  252. ancient[addedVertex] = edges[curent].from;
  253. }
  254.  
  255. tree[addedVertex] = 1;
  256.  
  257. for (p = adjacencyList[addedVertex]; p != NULL; p = p -> p_next)
  258. {
  259. neighborVertex = p->value;
  260. neighborEdge = distanceMatrix[addedVertex][neighborVertex];
  261.  
  262. if (tree[neighborVertex] == 1)
  263. edges[neighborEdge].inTheCut = false; // reset if both ends are there
  264. else
  265. edges[neighborEdge].inTheCut = true; // set if only one is there
  266.  
  267. }
  268.  
  269. }
  270.  
  271. printf( "\n\n The overall Weight: %d " , overallWeight);
  272. return 0;
  273. }
  274.  
  275.  
  276. // Here we need to make sure that the items are added in // correct order
  277. bool add(pListIt &dest, int val)
  278. {
  279. //create the item
  280. pListIt p;
  281. p = (pListIt) malloc(sizeof(ListIt));
  282. p -> value = val;
  283.  
  284. if(!dest) // first item addition
  285. {
  286. p -> p_next = NULL;
  287. dest = p;
  288. }
  289. else
  290. {
  291. pListIt find = dest;
  292. pListIt at = NULL;
  293.  
  294. // first find the first greater number, insert before
  295. while(find && find->value <= val)
  296. {
  297. if( find->value == val) // do not add a duplicate
  298. {
  299. free(p);
  300. return false;
  301. }
  302.  
  303. at = find;
  304. find = find->p_next;
  305. }
  306.  
  307. // insert at
  308. if (at) // insert at a valid point
  309. {
  310. p->p_next = at->p_next;
  311. at->p_next = p;
  312. }
  313. else // insert at the start
  314. {
  315. p->p_next = dest;
  316. dest = p;
  317. }
  318. }
  319.  
  320. return true;
  321. _getch();
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement