-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrims.cpp
More file actions
62 lines (57 loc) · 1.69 KB
/
Prims.cpp
File metadata and controls
62 lines (57 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<vector>
#include<utility> // required to use pair
#include<limits> // required for numeric_limits
#include<algorithm> // required for using find()
using namespace std;
int prims(vector<vector<int> >&,vector<int>&,vector<pair<int,int> >&);
int main(){
int size;
vector<vector<int> > graph;
vector<int> visited;
vector<pair<int,int> > answer;
cout<<"enter the number of vertices: ";
cin>>size;
graph.resize(size);
cout<<"enter the graph:\n";
for(int j=0;j<size;j++){
for(int i=0;i<size;i++){
graph[j].resize(size);
cin>>graph[j][i];
}
}
int cost = prims(graph,visited,answer);
cout<<"the edges in the minimum spanning tree are: \n";
for(int i=0;i<answer.size();i++){
cout<<'('<<answer[i].first<<","<<answer[i].second<<") ";
}
cout<<"\nthe total cost is : "<<cost<<endl;
return 0;
}
int prims(vector<vector<int> >&graph,vector<int>&visited,vector<pair<int,int> >&answer){
pair<int,int> temp;
int cost = 0;
int prevWeight = numeric_limits<int>::max();
visited.push_back(0);
//spanning tree will always have n-1 edges(n is the number of vertices)
for(int k=0;k<graph.size()-1;k++){
for(int i=0;i<visited.size();i++){
for(int j=0;j<graph.size();j++){
//find the smallest edge
if(graph[visited[i]][j] != 0 && (find(visited.begin(),visited.end(),j) == visited.end())){
if(graph[visited[i]][j] < prevWeight){
temp = make_pair(visited[i],j);
prevWeight = graph[visited[i]][j];
}
}
}
}
//push smallest edge into answer vector
visited.push_back(temp.second);
answer.push_back(temp);
cost += graph[temp.first][temp.second];
//reset prevWeight to a large value
prevWeight = numeric_limits<int>::max();
}
return cost;
}