-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDirectedGraph.cpp
238 lines (201 loc) · 4.21 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
233
234
235
236
237
//Copyright (C) Suresh Kumar Srivastava - All Rights Reserved
//DSA Masterclass courses are available on CourseGalaxy.com
//DirectedGraph.cpp : Program to find out the path matrix by powers of adjacency matrix.
#include<iostream>
#include<string>
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);
public:
DirectedGraph();
~DirectedGraph();
void insertVertex(string vertexName);
void insertEdge(string source, string destination);
void display();
void pathMatrix();
};//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()
void DirectedGraph::pathMatrix()
{
int adjp[maxSize][maxSize], x[maxSize][maxSize], temp[maxSize][maxSize];
int path[maxSize][maxSize];
//Initialize x
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
x[i][j] = 0;
}
}
//Initially adjp and x is equal to adj
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
x[i][j] = adjp[i][j] = adj[i][j];
}
}
//Get the matrix x by adding all the adjp
for(int p=2; p<=nVertices; p++)
{
//adjp(1...n) x adj
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
temp[i][j]=0;
for(int k=0; k<nVertices; k++)
{
temp[i][j] = temp[i][j] + adjp[i][k]*adj[k][j];
}
}
}
//Now adjp will be equal to temp
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
adjp[i][j] = temp[i][j];
}
}
//x = adjp1 + adjp2 + ...... + adjpn
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
x[i][j] = x[i][j] + adjp[i][j];
}
}
}//End of for
//Display x
cout << "x matrix is :\n";
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
cout << x[i][j] << " ";
}
cout <<"\n";
}
//Assign values to path matrix
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
if(x[i][j] == 0)
path[i][j] = 0;
else
path[i][j] = 1;
}
}
//Display path matrix
cout << "Path matrix is :\n";
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
cout << path[i][j] << " ";
cout <<"\n";
}
}//End of pathMatrix()
int main()
{
DirectedGraph dGraph;
try
{
//Creating the graph, inserting the vertices and edges
dGraph.insertVertex("0");
dGraph.insertVertex("1");
dGraph.insertVertex("2");
dGraph.insertVertex("3");
dGraph.insertEdge("0","1");
dGraph.insertEdge("0","3");
dGraph.insertEdge("1","0");
dGraph.insertEdge("1","2");
dGraph.insertEdge("1","3");
dGraph.insertEdge("2","3");
dGraph.insertEdge("3","0");
dGraph.insertEdge("3","2");
//Display the graph
dGraph.display();
cout << "\n";
cout << "Find the path matrix :\n";
dGraph.pathMatrix();
}//End of try
catch(exception e)
{
cout << e.what() << "\n";
}
return 0;
}//End of main()