C++ Code For Classic Algorithms
Dijkstra’s Algo :
// calculates the shortest path from a chosen source to every other
node
//doesn't work when weights are negative
// TC-> O((N+E)logN)
vector<int>dijkstra(int n,int source,vector<vector< pair<int,int> > >
&graph)
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int>
> > pq;// min-heap ; In pair => (dist,node)
vector<int> distTo(n+1,INT_MAX); // 1-indexed array for
calculating shortest paths;
distTo[source] = 0;
[Link](make_pair(0,source)); // (dist,node)
while( ![Link]() )
int dist = [Link](). rst;
int prev = [Link]().second;
[Link]();
for(auto it : graph[prev]) //graph stored in the form g[1]-
>(node,wt),(node,wt)...
int next = it. rst;
int nextDist = [Link];
if( distTo[next] > distTo[prev] + nextDist)
distTo[next] = distTo[prev] + nextDist;
[Link](make_pair(distTo[next], next));
return distTo;
fi
fi
Bellman Ford :
//this also gives shortest path from a chosen source to every other node
but won't work when negative edge weights are there
//so advantage compared to dijkstra is that in -ve weight case ,it will
atleast detect a negative cycle
//detects negative cycles in Directed graph
//can also work for Undirected graph ..just convert it to directed by
making a single edge
//b/w 2 nodes split into two each having weight as the previous weight.
//TC->O( N-1 * E )
struct node {
int u;
int v;
int wt;
node(int rst, int second, int weight) {
u = rst;
v = second;
wt = weight;
};
void bellman(int N,int src,vector<node>&edges)
int inf = 10000000;
vector<int> dist(N, inf); //dist is of size N here just coz nodes
numbered from 0...N
dist[src] = 0;
for(int i = 1;i<=N-1;i++) // Relax all the edges exactly N-1 times
for(auto it: edges)
if(dist[it.u] + [Link] < dist[it.v])
dist[it.v] = dist[it.u] + [Link];
int = 0;
for(auto it: edges) // Try relaxing for one last time
if(dist[it.u] + [Link] < dist[it.v])
fl
fi
fi
cout << "Negative Cycle";
= 1;
break;
if(! ) //Negative cycle wasn't found and the shortest distance from
src to every node can be returned
for(int i = 0;i<N;i++)
cout << i << " " << dist[i] << endl;
Kosaraju’s Algo for nding Strongly Connected Components :
// Kosaraju helps us nd all the SCC in a directed graph
// [Link] all nodes in order of nishing time (Topo Sort)
// [Link] the graph
// [Link] according to nishing time on the Transposed Graph
void dfs(int node, stack<int> &st, vector<int> &vis,
vector<vector<int>>&graph) //for topo sort
vis[node] = 1;
for(auto it: graph[node])
if(!vis[it])
dfs(it, st, vis, graph);
[Link](node);
void revDfs(int node, vector<int> &vis, vector<vector<int>>&transpose)
cout << node << " ";
vis[node] = 1;
for(auto it: transpose[node])
if(!vis[it])
revDfs(it, vis, transpose);
fl
fl
fi
fi
fi
fi
void KosarajuSCC(int n,vector<vector<int>>&graph)
stack<int> st;
vector<int> vis(n, 0);
for(int i = 0;i<n;i++)
if(!vis[i])
dfs(i, st, vis, graph);
vector<vector<int>> transpose(n);
for(int i = 0;i<n;i++)
vis[i] = 0;
for(auto it: graph[i])
transpose[it].push_back(i);
while(![Link]())
int node = [Link]();
[Link]();
if(!vis[node])
cout << "SCC: ";
revDfs(node, vis, transpose);
cout <<"\n";