Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<cstdio>
- #include<string.h>
- #include<cstdlib>
- #include<limits.h>
- using namespace std;
- /*struct list{
- int index;
- }graph[15][15];*/
- //int V;
- int minKey(int key[], bool mstSet[],int size)
- {
- // Initialize min value
- int min = INT_MAX, min_index;
- for (int v = 0; v < size; v++)
- if (mstSet[v] == false && key[v] < min)
- min = key[v], min_index = v;
- return min_index;
- }
- // A utility function to print the constructed MST stored in parent[]
- int printMST(int parent[], int n, int graph[15][15])
- {
- // printf("Edge Weight\n");
- int sum=0;
- for (int i = 1; i < n; i++)
- {
- sum+=graph[i][parent[i]];
- }
- cout<<sum<<endl;
- for (int i = 1; i < n; i++)
- printf("(%d ,%d, %d) \n", parent[i], i, graph[i][parent[i]]);
- }
- // Function to construct and print MST for a graph represented using adjacency
- // matrix representation
- void primMST(int graph[15][15],int size)
- {
- int parent[size]; // Array to store constructed MST
- int key[size]; // Key values used to pick minimum weight edge in cut
- bool mstSet[size]; // To represent set of vertices not yet included in MST
- // Initialize all keys as INFINITE
- for (int i = 0; i < size; i++)
- key[i] = INT_MAX, mstSet[i] = false;
- // Always include first 1st vertex in MST.
- key[0] = 0; // Make key 0 so that this vertex is picked as first vertex
- parent[0] = -1; // First node is always root of MST
- // The MST will have V vertices
- for (int count = 0; count < size-1; count++)
- {
- // Pick thd minimum key vertex from the set of vertices
- // not yet included in MST
- int u = minKey(key, mstSet,size);
- // Add the picked vertex to the MST Set
- mstSet[u] = true;
- // Update key value and parent index of the adjacent vertices of
- // the picked vertex. Consider only those vertices which are not yet
- // included in MST
- for (int v = 0; v < size; v++)
- // graph[u][v] is non zero only for adjacent vertices of m
- // mstSet[v] is false for vertices not yet included in MST
- // Update the key only if graph[u][v] is smaller than key[v]
- if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
- parent[v] = u, key[v] = graph[u][v];
- }
- // print the constructed MST
- printMST(parent, size, graph);
- }
- int main()
- {
- // the following lines are just to get the number of Vertices
- FILE *f;
- int Arr[15][15];
- int graph;
- char filename[20];
- char insert[1000];
- string file_name;
- int Arr_siz=0;
- cin>> filename;
- f=fopen(filename,"r");
- int size=0;
- int Nodes=0;
- while(~fscanf(f,"%c",&insert[size]))
- {
- if(insert[size]=='\n')
- {
- Nodes++;
- }
- size++;
- }// end of vertices counting
- file_name=(string)filename; // cast the Array of Char to string type in order to reopen it with fstream function
- ifstream infile;
- infile.open(file_name.c_str());
- if(infile.fail()){
- cerr<<"input error"<<endl;
- }
- string Char_in;
- int i;
- for( i=0;i<Nodes+1;i++)
- {
- for(int j=0;j<Nodes+1;j++)
- {
- if(!infile.eof()){
- infile>>Char_in;
- if(Char_in!="X")
- {
- Arr[i][j]= atoi(Char_in.c_str()); // convert the weight from string into intergers
- }
- }
- }
- }
- Nodes+=1;
- primMST( Arr,Nodes);// call Prim's function to compute the weight and the path
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement