-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDirectedGraph.cpp
183 lines (150 loc) · 3.18 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
//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 using warshall's algorithm.
#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 warshallsAlgorithm();
};//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::warshallsAlgorithm()
{
int P[maxSize][maxSize];
//Initializing P(-1)
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
P[i][j] = adj[i][j];
}
}
//P0,P1......Pn-1
for(int k=0; k<nVertices; k++)
{
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
{
P[i][j] = (P[i][j] || (P[i][k] && P[k][j]));
}
}
//Display P
cout << "P" << k << " :\n";
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
cout << P[i][j] << " ";
cout <<"\n";
}
}//End of for
}//End of warshallsAlgorithm()
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.warshallsAlgorithm();
}//End of try
catch(exception e)
{
cout << e.what() << "\n";
}
return 0;
}//End of main()