Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<cstdio>
  5. #include<string.h>
  6. #include<cstdlib>
  7. #include<limits.h>
  8.  
  9.  
  10. using namespace std;
  11. /*struct list{
  12. int index;
  13.  
  14.  
  15. }graph[15][15];*/
  16.  
  17. //int V;
  18.  
  19. int minKey(int key[], bool mstSet[],int size)
  20. {
  21. // Initialize min value
  22. int min = INT_MAX, min_index;
  23.  
  24. for (int v = 0; v < size; v++)
  25. if (mstSet[v] == false && key[v] < min)
  26. min = key[v], min_index = v;
  27.  
  28. return min_index;
  29. }
  30.  
  31. // A utility function to print the constructed MST stored in parent[]
  32. int printMST(int parent[], int n, int graph[15][15])
  33. {
  34. // printf("Edge Weight\n");
  35.  
  36. int sum=0;
  37. for (int i = 1; i < n; i++)
  38. {
  39. sum+=graph[i][parent[i]];
  40. }
  41. cout<<sum<<endl;
  42. for (int i = 1; i < n; i++)
  43. printf("(%d ,%d, %d) \n", parent[i], i, graph[i][parent[i]]);
  44. }
  45.  
  46. // Function to construct and print MST for a graph represented using adjacency
  47. // matrix representation
  48. void primMST(int graph[15][15],int size)
  49. {
  50.  
  51.  
  52. int parent[size]; // Array to store constructed MST
  53. int key[size]; // Key values used to pick minimum weight edge in cut
  54. bool mstSet[size]; // To represent set of vertices not yet included in MST
  55.  
  56. // Initialize all keys as INFINITE
  57. for (int i = 0; i < size; i++)
  58. key[i] = INT_MAX, mstSet[i] = false;
  59.  
  60. // Always include first 1st vertex in MST.
  61. key[0] = 0; // Make key 0 so that this vertex is picked as first vertex
  62. parent[0] = -1; // First node is always root of MST
  63.  
  64. // The MST will have V vertices
  65. for (int count = 0; count < size-1; count++)
  66. {
  67. // Pick thd minimum key vertex from the set of vertices
  68. // not yet included in MST
  69. int u = minKey(key, mstSet,size);
  70.  
  71. // Add the picked vertex to the MST Set
  72. mstSet[u] = true;
  73.  
  74. // Update key value and parent index of the adjacent vertices of
  75. // the picked vertex. Consider only those vertices which are not yet
  76. // included in MST
  77. for (int v = 0; v < size; v++)
  78.  
  79. // graph[u][v] is non zero only for adjacent vertices of m
  80. // mstSet[v] is false for vertices not yet included in MST
  81. // Update the key only if graph[u][v] is smaller than key[v]
  82. if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
  83. parent[v] = u, key[v] = graph[u][v];
  84. }
  85.  
  86. // print the constructed MST
  87. printMST(parent, size, graph);
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93.  
  94. // the following lines are just to get the number of Vertices
  95. FILE *f;
  96.  
  97. int Arr[15][15];
  98. int graph;
  99. char filename[20];
  100. char insert[1000];
  101. string file_name;
  102. int Arr_siz=0;
  103.  
  104. cin>> filename;
  105.  
  106.  
  107. f=fopen(filename,"r");
  108. int size=0;
  109. int Nodes=0;
  110. while(~fscanf(f,"%c",&insert[size]))
  111. {
  112. if(insert[size]=='\n')
  113. {
  114. Nodes++;
  115. }
  116. size++;
  117. }// end of vertices counting
  118.  
  119.  
  120. file_name=(string)filename; // cast the Array of Char to string type in order to reopen it with fstream function
  121.  
  122. ifstream infile;
  123. infile.open(file_name.c_str());
  124. if(infile.fail()){
  125. cerr<<"input error"<<endl;
  126.  
  127. }
  128. string Char_in;
  129.  
  130.  
  131.  
  132.  
  133. int i;
  134.  
  135. for( i=0;i<Nodes+1;i++)
  136. {
  137. for(int j=0;j<Nodes+1;j++)
  138. {
  139. if(!infile.eof()){
  140.  
  141. infile>>Char_in;
  142.  
  143. if(Char_in!="X")
  144. {
  145. Arr[i][j]= atoi(Char_in.c_str()); // convert the weight from string into intergers
  146. }
  147.  
  148.  
  149. }
  150.  
  151.  
  152. }
  153. }
  154. Nodes+=1;
  155.  
  156.  
  157. primMST( Arr,Nodes);// call Prim's function to compute the weight and the path
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement