-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPP_Graph.cpp
191 lines (147 loc) · 3.81 KB
/
CPP_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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<iostream>
#include <queue>
#include <stack>
using namespace std;
struct Node{
int dest;
Node* next;
};
struct List{
struct Node* head;
};
struct Graph{
int V;
struct List* array;
};
struct Node* new_Node(int dest){
Node* newNode = new Node;
newNode->dest = dest;
newNode->next = NULL;
return newNode;
};
struct Graph * createGraph(int V){
struct Graph* graph = new Graph;
graph->V = V;
graph->array = new List[V];
int i;
for(i=0;i<V;i++){
graph->array[i].head = NULL;
}
return graph;
}
void addEdge(struct Graph* graph, int src, int dest){
struct Node* newNode = new_Node(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
//newNode = new_Node(src);
//newNode->next = graph->array[dest].head;
//graph->array[dest].head = newNode;
}
void printGraph(struct Graph* graph){
int v;
for(v=0;v<graph->V;v++){
struct Node* pCrawl = graph->array[v].head;
cout << "\nAdj list of vertex " << v;
while(pCrawl){
cout << " " << pCrawl->dest;
pCrawl = pCrawl->next;
}
cout << "\n";
}
}
void BFS(struct Graph* G, int s){
bool *visited = new bool[G->V];
for(int i=0; i < G->V; i++){
visited[i] = false;
}
queue<int> Q;
visited[s] = true;
Q.push(s);
struct Node* temp = NULL;
while(!Q.empty()){
s = Q.front();
cout << s << " ";
temp = G->array[s].head;
Q.pop();
while(temp != NULL){
if(!visited[temp->dest]){
visited[temp->dest] = true;
Q.push(temp->dest);
}
temp = temp->next;
}
}
}
void DFS(struct Graph* G, int s){
bool* visited = new bool[G->V];
for(int i=0; i<G->V;i++){
visited[i] = false;
}
stack<int> stk;
visited[s] = true;
stk.push(s);
struct Node* temp = NULL;
while(!stk.empty()){
s = stk.top();
cout << " " << s;
temp = G->array[s].head;
stk.pop();
while(temp != NULL){
if(!visited[temp->dest]){
visited[temp->dest] = true;
stk.push(temp->dest);
}
temp = temp->next;
}
}
}
void topologicalsort_util(struct Graph* G, int v, bool visited[], stack<int> &stk){
visited[v] = true;
struct Node* temp = G->array[v].head;
while(temp != NULL){
cout << "\nIn TU while";
if(!visited[temp->dest]){
cout << "\nIn TU while if " << temp->dest;
topologicalsort_util(G,temp->dest,visited,stk);
}
temp = temp->next;
}
cout << "\nIn TU while if stkpush " << v;
stk.push(v);
}
void TopologicalSort(struct Graph* G){
stack <int> stk;
bool *visited = new bool[G->V];
for(int i=0; i< G->V;i++){
visited[i] = false;
}
for(int i=0; i<G->V; i++){
if(visited[i] == false){
cout << "\nIn T " << i;
topologicalsort_util(G,i,visited,stk);
}
}
while(stk.empty() == false){
cout << " " << stk.top();
stk.pop();
}
}
int main(){
int V=5;
struct Graph* graph = createGraph(V);
addEdge(graph,0,1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printGraph(graph);
cout << "\nDFS = ";
DFS(graph,0);
cout << "\nBFS = ";
BFS(graph,0);
cout << "\ntopo: ";
TopologicalSort(graph);
return 0;
}