-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPDag.cpp
132 lines (104 loc) · 2.96 KB
/
SPDag.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
/*
@Chiranjeevi
Date: 17-07-17
Finding shortest path in DAGs
Uses reverse Topological order for relaxing edges
Like Topological sort and unlike Dijkstra's algorithm this works even if negative edges are present
Running time (V+E)
*/
#include <iostream>
#include <stack>
#include <vector>
#include <climits>
using namespace std;
struct node{
int src;
int dst;
int wgt;
node *next;
};
node *newNode(int src, int dst, int wgt){
node *temp=new node;
temp->src=src;
temp->dst=dst;
temp->wgt=wgt;
temp->next=NULL;
return temp;
}
void addEdge(vector<node *> &gph, int src, int dst, int wgt){
node *ptr=gph[src];
if(ptr==NULL)
gph[src]=newNode(src, dst, wgt);
else{
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newNode(src, dst, wgt);
}
}
void createGraph(vector<node *> &gph, int E){
int src, dst, wgt;
for(int i=0; i<E; i++){
cin>>src>>dst>>wgt;
addEdge(gph, src, dst, wgt);
}
}
void topologicalSort(int vertex, vector<node *> &gph, vector<bool> &mark, stack<int> &rev_topo){
node *ptr=gph[vertex];
while(ptr!=NULL){
if(!mark[ptr->dst]){
mark[ptr->dst]=true;
topologicalSort(ptr->dst, gph, mark, rev_topo);
}
ptr=ptr->next;
}
rev_topo.push(vertex);
}
void getReverseTopoOrder(vector<node *> &gph, stack<int> &rev_topo){
int V=gph.size();
vector<bool> mark(V, false);
for(int i=0; i<V; i++){
if(!mark[i]){
mark[i]=true;
topologicalSort(i, gph, mark, rev_topo);
}
}
}
void relaxEdges(node *ptr, vector<node *> &gph, vector<int> &dist, vector<int> &edge_to){
while(ptr!=NULL){
if(ptr->wgt+dist[ptr->src] < dist[ptr->dst]){
dist[ptr->dst]=ptr->wgt+dist[ptr->src];
edge_to[ptr->dst]=ptr->src;
}
ptr=ptr->next;
}
}
void getShortestPath(vector<node *> &gph, stack<int> &rev_topo, vector<int> &dist, vector<int> &edge_to){
int vertex;
node *ptr;
dist[rev_topo.top()]=0;
while(!rev_topo.empty()){
vertex=rev_topo.top();
rev_topo.pop();
ptr=gph[vertex];
relaxEdges(ptr, gph, dist, edge_to);
}
}
void printSPT(vector<int> &dist, vector<int> &edge_to){
int V=dist.size();
cout<<"vertex\t"<<"dist\t"<<"edge_to\t"<<endl;
for(int i=0; i<V; i++)
cout<<i<<"\t"<<dist[i]<<"\t"<<edge_to[i]<<endl;
}
int main(){
int V,E;
cin>>V>>E;
vector<node *> gph(V, NULL);
stack<int> rev_topo;
vector<int> dist(V, INT_MAX);
vector<int> edge_to(V, -1);
createGraph(gph, E);
getReverseTopoOrder(gph, rev_topo);
getShortestPath(gph, rev_topo, dist, edge_to);
printSPT(dist, edge_to);
return 0;
}