-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRepresentation.cpp
More file actions
62 lines (46 loc) · 1004 Bytes
/
GraphRepresentation.cpp
File metadata and controls
62 lines (46 loc) · 1004 Bytes
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<bits/stdc++.h>
#define V 5
using namespace std;
//Adjacency Matrix Representation of directed Graph
//Initialization of Graph
void init(int arr[][V]) {
for (int i = 0; i < V; i++) {
for (int j = 0 ; j < V; j++) {
arr[i][j] = 0;
}
}
}
//Add Edge set arr[src][dest] = 1
void addEdge(int arr[][V], int src, int dest) {
arr[src][dest] = 1;
// arr[dest][src] = 1; //For Unidirected Graph
}
void printAdjMartix(int arr[][V]) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
cout << " " << arr[i][j];
}
cout << "\n";
}
}
int main() {
// int V = 5;
int adjMatrix[V][V];
init(adjMatrix);
addEdge(adjMatrix, 0, 1);
addEdge(adjMatrix, 0, 2);
addEdge(adjMatrix, 0, 3);
addEdge(adjMatrix, 1, 2);
addEdge(adjMatrix, 2, 3);
addEdge(adjMatrix, 3, 4);
addEdge(adjMatrix, 4, 0);
printAdjMartix(adjMatrix);
return 0;
}
/* Output
0 1 1 1 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 0
*/