27 Graph Shortest Paths

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE

Graph Algorithms
Shortest Paths

Dr. Shahbaz Khan


Department of Computer Science and Engineering,
Indian Institute of Technology Roorkee
shahbaz.khan@cs.iitr.ac.in
Single Source Shortest Path (Unweighted)

Given a directed acyclic graph G(V,E) having m edges and n vertices,


compute the length of the shortest path from a given source s.

● Formulation:
dist(s,v) = min(dist(s,u)+1)
for all (u,v) in E

● Order of Computation:
Break dependence cycle: Closest Vertex First

● Complexity: O(m+n)

2
Single Source Shortest Path (Unweighted)

Given a directed acyclic graph G(V,E) having m edges and n vertices,


compute the length of the shortest path from a given source s.
Order of Computation: BFS Breadth First Search
Proof: (Greedy Choice) #define pii pair<int,int>
(Arrange by shortest distance)
ShortestPath(int s){
for(int i=0; i<n; i++) dist[i] = INF;
queue<pii> Q;
Q.push(pii(0,s));
while(Q.empty()){
(c,u) = Q.pop(); // Earliest Pushed
if(dist[u] != INF) continue;
dist[u] = c;
forEach((u,v) in E){
Q.push( pii(c+1, v) );
Complexity: O(m+n)
}}}

3
Single Source Longest Path (Unweighted)

Given a directed acyclic graph G(V,E) having m edges and n vertices,


compute the length of the longest path from a given source s.

● Formulation:
dist(s,v) = max(dist(s,u)+1)
for all (u,v) in E

● Order of Computation:
Unclear without repeating edge/vertices

● Complexity:
Exponential (Reduction from HamCycle)

4
Single Source Shortest Path (No negative edges)

Given a graph G(V,E) having m edges and n vertices, compute the


length of the shortest path from a given source s.

● Formulation: 5 5
5
14
dist(s,v) = min(dist(s,u)+w(u,v)) 2 2
5
for all (u,v) \in E 10
10
5
10 10

● Order of Computation:
Break dependence cycle, Closest Vertex First

● Complexity: Trivial O((m+n)log n)


Improved O(m+nlog n)
(Using Fibonacci Heaps)

5
Single Source Shortest Path (No negative edges)

Proof Sketch: Induction Dijikstra’s Shortest Paths


Invariant 1: #define pii pair<int,int>
Each v with dist[v]!= INF, is shortest distance
ShortestPath(int s){
for(int i=0; i<n; i++) dist[i] = INF;
Invariant 2: priority_queue<pii, greater<pii>> PQ;
Each v with dist[v]=INF in heap have key PQ.push(pii(0,s));
min(dist[u]+w(u,v)) while(!PQ.empty()){
among all assigned vertex u having (u,v)\in E (c,u) = PQ.ExtractMin(); // top()
if(dist[u] != INF) continue;
dist[u] = c;
forEach((u,v) in E){
PQ.push( pii(c+w(u,v),v) );
}}}

6
Single Source Shortest Path (No negative edges)

Proof Sketch: Contradiction Dijikstra’s Shortest Paths


Assume solution incorrect. #define pii pair<int,int>
Closest vertex having wrong distance => x
ShortestPath(int s){
for(int i=0; i<n; i++) dist[i] = INF;
priority_queue<pii, greater<pii>> PQ;
PQ.push(pii(0,s));
while(!PQ.empty()){
(c,u) = PQ.ExtractMin(); // top()
if(dist[u] != INF) continue;
dist[u] = c;
forEach((u,v) in E){
PQ.push( pii(c+w(u,v),v) );
}}}

7
Single Source Shortest Path (No negative cycles)

PROOF SKETCH: Bellman Ford Shortest Paths


Invariant: ShortestPath(int s){
Initialize dist[s]=0 and
After i iterations, any shortest dist[i] = INF for i != s;
distance to a vertex v containing
at most i edges are stored in dist[v] forEach((u,v) in E){
if(dist[v] > dist[u]+w(u,v))
dist[v]=dist[u]+w(u,v);
}
}

10
Single Source Shortest Path (No negative cycles)

Detecting Negative Cycles? Bellman Ford Shortest Paths


ShortestPath(int s){
Initialize dist[s]=0 and
dist[i] = INF for i != s;

forEach((u,v) in E){
if(dist[v] > dist[u]+w(u,v))
dist[v]=dist[u]+w(u,v);
}
}

Find single source shortest walks using


BELLMAN FORD Algorithm
In graph having negative cycles

11
Single Source Longest Path (No positive cycles)

Given a graph G(V,E) having m edges and n vertices, compute the


length of the longest path from a given source s.

Can we use BELLMAN FORD Algorithm


exchaning min with max operator?

13
Single Source Shortest/Longest Path (General)

Given a graph G(V,E) having m edges and n vertices, compute the


length of the shortest/longest path from a given source s.

Problem?

Recall Travelling salesman Problem

14
Single Source Shortest/Longest Path (Summary)

Graph Shortest Path Shortest Path Longest Path Longest Path


(No repitition) (With Repitition) (No Repitition) (With Repitition)
DAG

Unweighted

No opp weight
SP = Only Pos
LP = Only Neg
No opp cycle
SP = Only Pos
LP = Only Neg
General Case

15
All Pairs Shortest Paths in a Graph (APSP)

Given a graph G(V,E) having m edges and n vertices, compute the


length of the shortest path from all vertices in the graph.

Can we use improve over


repeating SSSP Algorithms
from each vertex?

25

You might also like