-
Notifications
You must be signed in to change notification settings - Fork 7
/
ShortestPathDirectedWeighted.cpp
164 lines (140 loc) · 3.64 KB
/
ShortestPathDirectedWeighted.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
/// SHORTEST PATH, OF EACH NODE, FROM A GIVEN NODE
///-> USING WEIGHTD ADJLIST
///->TOPOLOGICAL SORT
///->USE T.S. MINIMIZE DISTANCE
#include <bits/stdc++.h>
using namespace std;
// class tampleting
template <typename T>
class Giraff
{
public:
// adJList of WEIGHTED graph
// it has map of NODES to LIST of PAIR of AdjElements and WEIGHTS
unordered_map<T, vector<pair<T, T>>> adjList;
vector<T> nodeList;
int size;
Giraff(int n)
{
// nodeList = nodes;
nodeList.resize(n);
for (int i = 0; i < n; i++)
{
nodeList[i] = i;
}
size=n;
}
void add_edge(T u, T v, T w, bool isDir = 0)
{
adjList[u].push_back(make_pair(v, w));
}
void display_graph()
{
for (auto i : adjList)
{
cout << endl;
cout << i.first << "->";
for (auto j : i.second)
{
cout << "[" << j.first << "," << j.second << "]; ";
}
}
}
//step 1: topological sort with dfs
void dfs(T i, stack<T> &ans, vector<bool> &visited)
{
visited[i] = true;
for (auto j : adjList[i])
{
if (!visited[j.first])
dfs(j.first, ans, visited);
}
ans.push(i);
}
vector<T> TopoSort()
{
// ans stack
stack<T> ans;
vector<bool> visited(size,false);
for (auto i : adjList)
{
if (visited[i.first]==false)
{
dfs(i.first, ans, visited);
}
}
// ret vector
vector<T> ret;
while (!ans.empty())
{
ret.push_back(ans.top());
ans.pop();
}
return ret;
}
//step 2: Now using TopoSort find distance vector
void distances(int node)
{
map<T, int> distance;
for (auto i : nodeList)
{
//step 2.1: setting distance of each node as MAX
distance[i] = INT_MAX;
}
//step 2.2: get and traverse TS
vector<T> TS = TopoSort();
for (auto i : TS)
{
if (i == node)
{
distance[i] = 0;
for (auto j : adjList[node])
{
distance[j.first] = j.second;
}
}
else if (distance[i] != INT_MAX)
{
for (auto j : adjList[i])
{
int newDist = distance[i] + j.second;
if (newDist < distance[j.first])
distance[j.first] = newDist;
}
}
}
// Print distance;
cout << endl
<< " Distances: " << endl;
for (auto i : distance)
{
cout << i.first << " at distance " << i.second << " from " << node << endl;
}
}
};
int main()
{
cout << "Give the map:";
//cout << "\n Number of edges: nodes";
int e = 9, n = 6, u, v, w;
// cin >> e >> n;
Giraff<int> G1(n);
/*for (int i = 0; i < n; i++)
{
cin >> u >> v >> w;
G1.add_edge(u, v, w);
}*/
G1.add_edge(1, 3, 6);
G1.add_edge(1, 2, 2);
G1.add_edge(0, 1, 5);
G1.add_edge(0, 2, 3);
G1.add_edge(3, 4, -1);
G1.add_edge(2, 4, 4);
G1.add_edge(2, 5, 2);
G1.add_edge(2, 3, 7);
G1.add_edge(4, 5, -2);
G1.display_graph();
cout << endl;
G1.distances(1);
///!!Run sucessfully!!
}