-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathssp.cpp
More file actions
206 lines (194 loc) · 6.15 KB
/
ssp.cpp
File metadata and controls
206 lines (194 loc) · 6.15 KB
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
#include "bits/stdc++.h"
using namespace std;
const long double INF = 1e18;
const int MAXN = 1e5 + 5;
int N, M; // Number of nodes, Number of edges
vector <int> edges[MAXN];
vector <double> cost[MAXN];
int indegree[MAXN];
double dist[MAXN];
int shortest_path_tree_parent[MAXN];
set < pair < int, int > > S;
// This function is responsible for reading in the graph.
void initGraph(string filename)
{
char pr_type[3]; //problem type;
ifstream file(filename);
string line_inf;
getline(file, line_inf);
sscanf(line_inf.c_str(), "%*c %3s %d %d", pr_type, &N, &M);
for(string line; getline(file, line); )
{
switch(line[0]){
case 'c':
case '\n':
case 'n':
case '\0':
break;
case 'p':
case 'a': {
int tail; int head;
double weight;
sscanf(line.c_str(), "%*c %d %d %lf", &tail, &head, &weight);
edges[tail].push_back(head);
cost[tail].push_back(weight);
indegree[head]++;
break;
}
default: break;
}
}
}
// Computes Topological ordering of given graph and stores the order in vector topological_ordering(defined globally).
vector <int> topological_ordering;
void toposort(){
int tmp_indegree[N + 1];
int put[N + 1];
for(int i = 1; i <= N; ++i){
tmp_indegree[i] = indegree[i];
put[i] = 1;
}
topological_ordering.clear();
for(int i = 1; i <= N; ++i){
S.insert({tmp_indegree[i], i});
}
while(S.empty() == false){
int node = S.begin() -> second;
S.erase(S.begin());
put[node] = 0;
for(int j = 0; j < edges[node].size(); ++j){
if(put[edges[node][j]] == 0)
continue;
tmp_indegree[edges[node][j]]--;
S.erase(S.find({tmp_indegree[edges[node][j]] + 1, edges[node][j]}));
S.insert({tmp_indegree[edges[node][j]], edges[node][j]});
}
topological_ordering.push_back(node);
}
}
// Construct the initial Shortest Path tree
void init_shortest_path_tree()
{
toposort();
int l = topological_ordering.size();
for(int i = 1; i <= N; ++i)
dist[i] = INF;
dist[topological_ordering[0]] = 0;
shortest_path_tree_parent[topological_ordering[0]] = -1; // Source
for(int i = 0; i < l; ++i){
int node = topological_ordering[i];
for(int j = 0; j < edges[node].size(); ++j){
double updated_dist = dist[node] + cost[node][j];
if(dist[edges[node][j]] - updated_dist > 0.0000001){
dist[edges[node][j]] = updated_dist;
shortest_path_tree_parent[edges[node][j]] = node;
}
}
}
}
// This function transforms the edge weights to positive so that Djikstra's Algorithm can be applied
void update_allgraph_weights()
{
for(int i = 1; i <= N; ++i){
for(int j = 0; j < edges[i].size(); ++j){
cost[i][j] = cost[i][j] + dist[i] - dist[edges[i][j]];
}
}
for(int i = 1; i <= N; ++i)
dist[i] = 0;
}
// This function extracts the shortest path and stores it in shortest_path vector globally
vector <int> shortest_path;
void extract_shortest_path(){
shortest_path.clear();
int curr = N;
while(curr != 1){
shortest_path.push_back(curr);
curr = shortest_path_tree_parent[curr];
}
shortest_path.push_back(curr);
}
// This function flips edges along shortest path extracted.
void flip_path()
{
int l = shortest_path.size();
for(int i = 1; i < l; ++i){
// Find edge index
vector <int> :: iterator it = find(edges[shortest_path[i]].begin(), edges[shortest_path[i]].end(), shortest_path[i - 1]);
int idx = it - edges[shortest_path[i]].begin();
// Erase edges
long double c = cost[shortest_path[i]][idx];
edges[shortest_path[i]].erase(edges[shortest_path[i]].begin() + idx);
cost[shortest_path[i]].erase(cost[shortest_path[i]].begin() + idx);
indegree[shortest_path[i - 1]]--;
// Add reverse edge. Do not add reverse edge from top sinker to source (otherwise results in formation of cycles)
if(shortest_path[i] != 1){
edges[shortest_path[i - 1]].push_back(shortest_path[i]);
cost[shortest_path[i - 1]].push_back(-c);
indegree[shortest_path[i]]++;
}
}
}
void updateShortestPathTree()
{
for(int i = 1; i <= N; ++i)
dist[i] = INF;
dist[1] = 0;
multimap < double, int > K;
K.insert({0, 1});
while(K.empty() == false){
int node = (K.begin())->second;
double c = (K.begin())->first;
if(node == N)
break;
K.erase(K.begin());
for(int j = 0; j < edges[node].size(); ++j){
long double upd_dist = dist[node] + cost[node][j];
if(-0.0000001 > upd_dist - dist[edges[node][j]]){
dist[edges[node][j]] = upd_dist;
shortest_path_tree_parent[edges[node][j]] = node;
K.insert({dist[edges[node][j]], edges[node][j]});
}
}
}
for(int i = 1; i <= N; ++i){
if(dist[i] > dist[N])
dist[i] = dist[N];
}
}
int main(int argc, char * argv[])
{
ofstream out("Shortest_Paths.txt");
char* in_file = argv[2];
initGraph(in_file);
init_shortest_path_tree();
double total_cost = dist[N];
update_allgraph_weights();
extract_shortest_path();
int l = shortest_path.size();
for(int i = l - 1; i >= 0; --i)
out << shortest_path[i] << " ";
out << endl;
flip_path();
double curr_cost = total_cost;
int c = 1;
while(true)
{
updateShortestPathTree();
curr_cost += dist[N];
total_cost += curr_cost;
cout << "Iteration " << (c++) << ": "<< curr_cost << endl;
if (curr_cost > -0.0000001) {
break;
}
update_allgraph_weights();
extract_shortest_path();
int l = shortest_path.size();
for(int i = l - 1; i >= 0; --i)
out << shortest_path[i] << " ";
out << endl;
flip_path();
}
cout << total_cost << endl;
return 0;
}