-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDirectedGraph.cpp
233 lines (191 loc) · 4.38 KB
/
DirectedGraph.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//Copyright (C) Suresh Kumar Srivastava - All Rights Reserved
//DSA Masterclass courses are available on CourseGalaxy.com
//DirectedGraph.cpp : Program for topological sorting of directed acyclic graph.
#include<iostream>
#include<string>
#include<queue>
using namespace std;
static const int maxSize = 30;
class Vertex
{
public:
string name;
public:
Vertex(string name)
{
this->name = name;
}
};//End of class Vertex
class DirectedGraph
{
private:
int nVertices;
int nEdges;
int adj[maxSize][maxSize];
Vertex *vertexList[maxSize];
private:
int getIndex(string vertexName);
bool isAdjacent(int u, int v);
int getIndegree(int vertex);
public:
DirectedGraph();
~DirectedGraph();
void insertVertex(string vertexName);
void insertEdge(string source, string destination);
void display();
void topologicalOrder();
};//End of class DirectedGraph
DirectedGraph::DirectedGraph()
{
nVertices = 0;
nEdges = 0;
for(int i=0; i<maxSize; i++)
{
for(int j=0; j<maxSize; j++)
{
adj[i][j] = 0;
}
}
}//End of DirectedGraph()
DirectedGraph::~DirectedGraph()
{
for(int i=0; i<nVertices; i++)
{
delete vertexList[i];
}
}//End of ~DirectedGraph()
void DirectedGraph::insertVertex(string vertexName)
{
vertexList[nVertices++] = new Vertex(vertexName);
}//End of insertVertex()
int DirectedGraph::getIndex(string vertexName)
{
for(int i=0; i<nVertices; i++)
{
if(vertexName == vertexList[i]->name)
return i;
}
throw exception("Invalid Vertex");
}//End of getIndex()
void DirectedGraph::insertEdge(string source, string destination)
{
int u = getIndex(source);
int v = getIndex(destination);
if(u == v)
cout << "Not a valid edge\n";
else if(adj[u][v] != 0)
cout << "Edge already present\n";
else
{
adj[u][v] = 1;
nEdges++;
}
}//End of insertEdge()
void DirectedGraph::display()
{
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
cout << adj[i][j] << " ";
cout <<"\n";
}
}//End of display()
bool DirectedGraph::isAdjacent(int u, int v)
{
return (adj[u][v] != 0);
}//End of isAdjacent()
//Returns number of edges coming to a vertex
int DirectedGraph::getIndegree(int vertex)
{
int indegree = 0;
for(int v=0; v<nVertices; v++)
if (adj[v][vertex])
indegree++;
return indegree;
}//End of getIndegree()
void DirectedGraph::topologicalOrder()
{
int topoOrder[maxSize], indegree[maxSize];
queue<int> q;
int v, count;
//Get the indegree of each vertex
for(v=0; v<nVertices; v++)
{
indegree[v] = getIndegree(v);
if(indegree[v] == 0)
q.push(v);
}
count=0;
while(!q.empty() && count<nVertices)
{
v = q.front();
q.pop();
topoOrder[++count] = v; //Add vertex v to topoOrder array
//Delete all the edges going from vertex v
for(int i=0; i<nVertices; i++)
{
if(adj[v][i] == true)
{
adj[v][i] = false;
indegree[i] = indegree[i]-1;
if(indegree[i] == 0)
q.push(i);
}
}
}//End of while
if(count < nVertices)
{
throw exception("Graph contains cycle. Topological order is not possible.");
}
cout << "Vertices in topological order are :\n";
for(int i=1; i<=count; i++)
cout << topoOrder[i] << " ";
cout << "\n";
}//End of topologicalOrder()
int main()
{
DirectedGraph dGraph;
try
{
//Creating the graph, inserting the vertices and edges
//Graph without cycle
dGraph.insertVertex("0");
dGraph.insertVertex("1");
dGraph.insertVertex("2");
dGraph.insertVertex("3");
dGraph.insertVertex("4");
dGraph.insertVertex("5");
dGraph.insertVertex("6");
dGraph.insertEdge("0","1");
dGraph.insertEdge("0","5");
dGraph.insertEdge("1","4");
dGraph.insertEdge("1","5");
dGraph.insertEdge("2","1");
dGraph.insertEdge("2","3");
dGraph.insertEdge("3","1");
dGraph.insertEdge("3","4");
dGraph.insertEdge("4","5");
dGraph.insertEdge("6","4");
dGraph.insertEdge("6","5");
//Graph with cycle
//dGraph.insertVertex("0");
//dGraph.insertVertex("1");
//dGraph.insertVertex("2");
//dGraph.insertVertex("3");
//dGraph.insertVertex("4");
//dGraph.insertEdge("0","1");
//dGraph.insertEdge("0","2");
//dGraph.insertEdge("1","3");
//dGraph.insertEdge("2","4");
//dGraph.insertEdge("3","0");
//dGraph.insertEdge("3","4");
//Display the graph
dGraph.display();
dGraph.topologicalOrder();
}//End of try
catch(exception e)
{
cout << e.what() << "\n";
}
return 0;
}//End of main()