0% found this document useful (0 votes)
60 views

C++ Code For Classic Algorithms: Dijkstra's Algo

The document provides C++ code for classic graph algorithms: 1) Dijkstra's algorithm calculates the shortest path from a source node to all other nodes in O((N+E)logN) time and does not work for graphs with negative edge weights. 2) Bellman-Ford algorithm also finds shortest paths but can detect negative cycles, running in O(N*E) time. 3) Kosaraju's algorithm finds strongly connected components in a graph by performing a depth-first search on the transpose of the input graph after performing a topological sort.

Uploaded by

Praveen Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

C++ Code For Classic Algorithms: Dijkstra's Algo

The document provides C++ code for classic graph algorithms: 1) Dijkstra's algorithm calculates the shortest path from a source node to all other nodes in O((N+E)logN) time and does not work for graphs with negative edge weights. 2) Bellman-Ford algorithm also finds shortest paths but can detect negative cycles, running in O(N*E) time. 3) Kosaraju's algorithm finds strongly connected components in a graph by performing a depth-first search on the transpose of the input graph after performing a topological sort.

Uploaded by

Praveen Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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;

pq.push(make_pair(0,source)); // (dist,node)

while( !pq.empty() )

int dist = pq.top(). rst;

int prev = pq.top().second;

pq.pop();

for(auto it : graph[prev]) //graph stored in the form g[1]-


>(node,wt),(node,wt)...

int next = it. rst;

int nextDist = it.second;

if( distTo[next] > distTo[prev] + nextDist)

distTo[next] = distTo[prev] + nextDist;

pq.push(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] + it.wt < dist[it.v])

dist[it.v] = dist[it.u] + it.wt;

int = 0;

for(auto it: edges) // Try relaxing for one last time

if(dist[it.u] + it.wt < 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

// 1.sort all nodes in order of nishing time (Topo Sort)

// 2.Transpose the graph

// 3.DFS 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);

st.push(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(!st.empty())

int node = st.top();

st.pop();

if(!vis[node])

cout << "SCC: ";

revDfs(node, vis, transpose);

cout <<"\n";

You might also like