-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
72 lines (62 loc) · 1.47 KB
/
Graph.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Author: Hemant Tripathi
*/
#include<iostream>
#include<vector>
using namespace std;
void addEdge(vector<int> adj[], int i, int j);
int main() {
bool isExit = false;
int totalVertex;
cout << "Enter the size of graph (i.e. total vertices)" << endl;
cin >> totalVertex;
vector<int> adj[totalVertex];
while(1) {
if(isExit) {
break;
}
cout << "Select an option:"<<endl;
cout << "1. Add two vertex" << endl;
cout << "2. Show graph" << endl;
cout << "3. Exit" << endl;
int option;
cin >> option;
switch(option) {
case 1:
int i,j;
cout << "Enter first vertex number between 0 to "<<totalVertex-1<<endl;
cin >> i;
if(i < 0 || i >= totalVertex) {
cout << "Vertex is out of range"<<endl;
break;
}
cout << "Enter second vertex number between 0 to "<<totalVertex-1<<endl;
cin >> j;
if(j < 0 || j >= totalVertex) {
cout << "Vertex is out of range" << endl;
break;
}
addEdge(adj, i, j);
break;
case 2:
cout << "Generating all routes of graph" << endl;
for(int i=0; i<totalVertex; i++) {
cout << "Adjacency List of vertex "<<i<<endl<<"Head";
for(auto j:adj[i]) {
cout << " -> " << j;
}
cout << endl;
}
break;
case 3:
cout << "Terminating program" << endl;
isExit = true;
break;
}
}
}
void addEdge(vector<int> adj[], int i, int j) {
adj[i].push_back(j);
adj[j].push_back(i);
cout << "Vertex " << i << " and Vertex " << j << " joined together!";
}