-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
40 lines (34 loc) · 837 Bytes
/
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
#include <iostream>
#include <vector>
using namespace std;
void addEdge(vector<int> adj[], int src, int dest) { adj[src].push_back(dest); }
void printAdjacencyList(vector<int> adj[]) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < adj[i].size(); j++) {
cout << i << " -> " << adj[i][j] << endl;
}
}
}
void transpose(vector<int> adj[], vector<int> trans[]) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < adj[i].size(); j++) {
trans[adj[i][j]].push_back(i);
}
}
}
int main() {
vector<int> adj[5];
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 0, 3);
addEdge(adj, 2, 0);
addEdge(adj, 3, 2);
addEdge(adj, 4, 1);
addEdge(adj, 4, 3);
printAdjacencyList(adj);
cout << endl;
vector<int> trans[5];
transpose(adj, trans);
printAdjacencyList(trans);
return 0;
}