-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdjacencyMatrix.cpp
More file actions
77 lines (65 loc) · 1.82 KB
/
AdjacencyMatrix.cpp
File metadata and controls
77 lines (65 loc) · 1.82 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
using namespace std;
void scangraph(vector<vector<int>>& adjMat, char Dir) {
int V, E;
cout << "Enter How many vertices? ";
cin >> V;
adjMat.resize(V + 1, vector<int>(V + 1, 0));
cout << "Enter Number of edges: ";
cin >> E;
int sr, ds, wt;
for (int i = 0; i < E; i++) {
cout << "Enter source, destination, and weight of the edge " << i + 1 << ": ";
cin >> sr >> ds >> wt;
if (sr >= 1 && sr <= V && ds >= 1 && ds <= V)
{
adjMat[sr][ds] = wt;
if (Dir == 'U')
adjMat[ds][sr] = wt;
}
else
{
cout << "Enter correct vertices for the edge." << endl;
i--;
}
}
}
void displayg(const vector<vector<int>>& adjMat) {
cout << "Adjacency Matrix is: " << endl;
for (size_t i = 1; i < adjMat.size(); ++i) {
for (size_t j = 1; j < adjMat[i].size(); ++j)
{
cout << adjMat[i][j] << " ";
}
cout << endl;
}
}
int main() {
int ch, cont;
char Dir;
vector<vector<int>> adjMat;
do {
cout << endl << "Menu";
cout << endl << "1. Create graph using adjacency matrix";
cout << endl << "2. Display Graph";
cout << endl << "Enter choice: ";
cin >> ch;
switch(ch) {
case 1:
cout << "Graph is directed/undirected (D/U): ";
cin >> Dir;
scangraph(adjMat, Dir);
break;
case 2:
displayg(adjMat);
break;
default:
cout << "Invalid choice." << endl;
break;
}
cout << endl << "Do you want to continue? (1 for continue): ";
cin >> cont;
} while(cont == 1);
return 0;
}