-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
47 lines (47 loc) · 1.98 KB
/
main.cpp
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
#include <iostream>
#include "Graph.h"
#define randomVertix 0
using namespace std;
int main() {
int numberOfVertices;
unsigned long src;
int sum=0;
int x;
cout<<"------------------------Prim Algorithm Test--------------------"<<endl;
cout<<"Enter number of vertices of the graph : ";
cin>>numberOfVertices;
cout<<"Enter the adjacency matrix to find the MST from"<<endl;
vector<vector<int>> matrix((unsigned long) numberOfVertices);
for (int i = 0; i < numberOfVertices; ++i)
for (int j = 0; j < numberOfVertices; ++j){
cin>>x;
matrix.at((unsigned long) i).push_back(x);
}
//Instantiate a Graph object with adjacency matrix
Graph graph(numberOfVertices,matrix);
vector<GraphEdge>*vector1=graph.primAlgorithm(graph.getVertices().at(randomVertix));
cout<<"------------------------Prim Algorithm Results--------------------"<<endl;
for (auto && item:*vector1 ) {
cout<<item.getNode1()->getNodeIndex()<<","<<item.getNode2()->getNodeIndex()<<","<<item.getWeight()<<endl;
sum+=item.getWeight();
}
cout<<"Minimum spanning tree's total weight is "<<sum<<endl;
delete vector1;
cout<<"------------------------Dijkstra Algorithm Test--------------------"<<endl;
cout<<"Enter number of vertices of the graph : ";
cin>>numberOfVertices;
cout<<"Enter the adjacency matrix to find the shortest path from"<<endl;
vector<vector<int>> matrix2((unsigned long) numberOfVertices);
for (int i = 0; i < numberOfVertices; ++i)
for (int j = 0; j < numberOfVertices; ++j){
cin>>x;
matrix2.at((unsigned long) i).push_back(x);
}
//Instantiate a Graph object with adjacency matrix
Graph graph2(numberOfVertices,matrix2);
cout<<"Enter the source vertex's index"<<endl;
cin>>src;
cout<<"------------------------Dijkstra Algorithm Results--------------------"<<endl;
graph2.dijkstraAlgorithm(graph2.getVertices().at(src));
return 0;
}