forked from JohnJordan0098/Data-Structures-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacency matrice.cpp
More file actions
137 lines (69 loc) · 1.96 KB
/
Adjacency matrice.cpp
File metadata and controls
137 lines (69 loc) · 1.96 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX 20
/* Adjacency Matrix Class */
class AdjacencyMatrix
{
private:
int n;
int **adj;
bool *visited;
public:
AdjacencyMatrix(int n)
{
this->n = n;
visited = new bool [n];
adj = new int* [n];
for (int i = 0; i < n; i++)
{
adj[i] = new int [n];
for(int j = 0; j < n; j++)
{
adj[i][j] = 0;
}
}
}
/* Adding Edge to Graph */
void add_edge(int origin, int destin)
{
if( origin > n || destin > n || origin < 0 || destin < 0)
{
cout<<"Invalid edge!\n";
}
else
{
adj[origin - 1][destin - 1] = 1;
}
}
/* Print the graph */
void display()
{
int i,j;
for(i = 0;i < n;i++)
{
for(j = 0; j < n; j++)
cout<<adj[i][j]<<" ";
cout<<endl;
}
}
};
/* Main */
int main()
{
int nodes, max_edges, origin, destin;
cout<<"Enter number of nodes: ";
cin>>nodes;
AdjacencyMatrix am(nodes);
max_edges = nodes * (nodes - 1);
for (int i = 0; i < max_edges; i++)
{
cout<<"Enter edge (-1 -1 to exit): ";
cin>>origin>>destin;
if((origin == -1) && (destin == -1))
break;
am.add_edge(origin, destin);
}
am.display();
return 0;
}