-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0014_AllPairsShortestPathsJohnson.cc
217 lines (192 loc) · 5.25 KB
/
0014_AllPairsShortestPathsJohnson.cc
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
#include "../Headers/0003_Graph/0014_AllPairsShortestPathsJohnson.h"
#include<climits>
using namespace std;
namespace AllPairsShortestPathsJohnson
{
Node::Node(int data)
{
this->data = data;
this->distance = INT_MAX;
this->parent = nullptr;
this->potentialWeight = 0;
}
Edge::Edge(Node* nodeU, Node* nodeV, int weight)
{
this->nodeU = nodeU;
this->nodeV = nodeV;
this->weight = weight;
}
// Graph Private Member Methods
Node* Graph::MakeOrFindNode(int data)
{
Node* node = nullptr;
if (this->_nodeMap.find(data) == this->_nodeMap.end())
{
node = new Node(data);
this->_nodeMap[data] = node;
}
else
{
node = this->_nodeMap[data];
}
return node;
}
void Graph::PushAugmentedDirectedEdges(Node* sourceNode, Node* nodeV, int weight)
{
this->_augmentedAdjlist[sourceNode].push_back(nodeV);
this->_augmentedEdgeList.push_back(new Edge(sourceNode, nodeV, weight));
}
void Graph::InitializeSingleSource(Node* sourceNode)
{
for (auto& iterator : this->_nodeMap)
{
iterator.second->distance = INT_MAX;
iterator.second->parent = nullptr;
}
sourceNode->distance = 0;
}
void Graph::RelaxBellmanFord(Edge* edge)
{
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
{
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
edge->nodeV->parent = edge->nodeU;
}
}
bool Graph::BellmanFord(Node* source)
{
this->InitializeSingleSource(source);
for (int i = 0; i < this->_nodeMap.size() - 1; i++)
{
for (auto& edge : this->_augmentedEdgeList)
{
this->RelaxBellmanFord(edge);
}
}
for (auto& edge : this->_augmentedEdgeList)
{
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
{
return false;
}
}
return true;
}
void Graph::RelaxDijkstra(Edge* edge)
{
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
{
this->_operationalSet.erase(edge->nodeV);
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
edge->nodeV->parent = edge->nodeU;
this->_operationalSet.insert(edge->nodeV);
}
}
void Graph::Dijkstra(Node* source)
{
this->InitializeSingleSource(source);
for (auto& node : this->_nodeMap)
{
this->_operationalSet.insert(node.second);
}
while (!this->_operationalSet.empty())
{
Node* nodeU = *(this->_operationalSet.begin());
this->_operationalSet.erase(nodeU);
for (auto& edge : this->_edgeMap[nodeU])
{
this->RelaxDijkstra(edge);
}
}
}
void Graph::GetShortestPath(int source, int destination, vector<int>& path)
{
if (this->_predecessorMatrix[source - 1][destination - 1] != source)
{
int predecessor = this->_predecessorMatrix[source - 1][destination - 1];
this->GetShortestPath(source, predecessor, path);
path.push_back(predecessor);
}
}
// Graph Public Member Methods
void Graph::PushDirectedEdge(int dataU, int dataV, int weight)
{
Node* nodeU = this->MakeOrFindNode(dataU);
Node* nodeV = this->MakeOrFindNode(dataV);
this->_adjlist[nodeU].push_back(nodeV);
Edge* edge = new Edge(nodeU, nodeV, weight);
this->_edgeMap[nodeU].push_back(edge);
this->_edgeList.push_back(edge);
}
bool Graph::FindAllPairsShortestPathsJohnsonAlgorithm()
{
// Creating the graph G'
this->_augmentedAdjlist = this->_adjlist;
this->_augmentedEdgeList = this->_edgeList;
// Source Node s
Node* source = new Node(0);
this->_nodeMap[0] = source;
// Creating all the augmented edges in G'.E = G.E U {(s, v) : v in G.V
for (auto& node : this->_nodeMap)
{
if (node.second != source)
{
this->PushAugmentedDirectedEdges(source, node.second, 0);
}
}
if (this->BellmanFord(source) == false)
{
return false;
}
else
{
this->_nodeMap.erase(0);
for (auto& node : this->_nodeMap)
{
node.second->potentialWeight = node.second->distance;
}
for (auto& edge : this->_edgeList)
{
edge->weight = edge->weight + edge->nodeU->potentialWeight - edge->nodeV->potentialWeight;
}
this->_noOfVertices = (int)this->_nodeMap.size();
this->_shortestPathMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, -1));
this->_predecessorMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, -1));
for (auto& iteratorU : this->_nodeMap)
{
Node* nodeU = iteratorU.second;
this->Dijkstra(nodeU);
for (auto& iteratorV : this->_nodeMap)
{
Node* nodeV = iteratorV.second;
this->_shortestPathMatrix[nodeU->data - 1][nodeV->data - 1] = nodeV->distance + nodeV->potentialWeight - nodeU->potentialWeight;
this->_predecessorMatrix[nodeU->data - 1][nodeV->data - 1] = nodeV->parent != nullptr ? nodeV->parent->data : -1;
}
}
return true;
}
}
vector<vector<int>> Graph::GetAllPairsShortestPathsDistanceMatrix()
{
return this->_shortestPathMatrix;
}
vector<vector<int>> Graph::GetAllPairsShortestPathsPathMatrix()
{
vector<vector<int>> result;
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (i != j)
{
vector<int> path = {};
path.push_back(i + 1);
this->GetShortestPath(i + 1, j + 1, path);
path.push_back(j + 1);
result.push_back(path);
}
}
}
return result;
}
}