0% found this document useful (0 votes)
54 views16 pages

Graph-Based Algorithms: CSE373: Design and Analysis of Algorithms

The document discusses graph-based algorithms for finding shortest paths in graphs. It defines shortest path problems on weighted and unweighted graphs, and describes variants like single-source, single-destination, and all-pairs shortest path problems. It explains the optimal substructure property of shortest paths and representations using distance labels (d[]) and predecessors (π[]). Dijkstra's algorithm is presented for solving the single-source shortest path problem on graphs with non-negative edge weights. It uses initialization, relaxation of edges, and two sets (S,Q) where Q is a min-priority queue to iteratively determine shortest paths from the source.

Uploaded by

Sazedur Rahman
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)
54 views16 pages

Graph-Based Algorithms: CSE373: Design and Analysis of Algorithms

The document discusses graph-based algorithms for finding shortest paths in graphs. It defines shortest path problems on weighted and unweighted graphs, and describes variants like single-source, single-destination, and all-pairs shortest path problems. It explains the optimal substructure property of shortest paths and representations using distance labels (d[]) and predecessors (π[]). Dijkstra's algorithm is presented for solving the single-source shortest path problem on graphs with non-negative edge weights. It uses initialization, relaxation of edges, and two sets (S,Q) where Q is a min-priority queue to iteratively determine shortest paths from the source.

Uploaded by

Sazedur Rahman
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/ 16

Graph-Based Algorithms

CSE373: Design and Analysis of Algorithms


Shortest Path Problems
Modeling problems as graph problems:
Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
Goal: find a shortest path between two vertices (cities)
Shortest Path Problems
What is shortest path ?
shortest length between two vertices for an unweighted graph:
smallest cost between two vertices for a weighted graph:

B 210 B

A A
450
60 190

C unweighted C weighted
graph graph
200 130
D D
E E
Shortest Path Problems
• Input:
t x
• Directed graph G = (V, E) 6
3 9
• Weight func on w : E → R 3
4
2 1
s 0
• Weight of path p = v0, v1, . . . , vk 3
2 7
k 5
w( p )   w(vi 1 , vi ) 5 11
i 1
6
y z

• Shortest-path weight from u to v:


p
δ(u, v) = min w(p) : u v if there exists a path from u to v

∞ otherwise
Variants of Shortest Paths
Single-source shortest path
Given G = (V, E), find a shortest path from a given source vertex s to each
vertex v  V

Single-destination shortest path


Find a shortest path to a given destination vertex t from each vertex v
Reverse the direction of each edge  single-source

Single-pair shortest path


Find a shortest path from u to v for given vertices u and v
Solve the single-source problem

All-pairs shortest-paths
Find a shortest path from u to v for every pair of vertices u and v
Optimal Substructure of Shortest Paths
Given:
vj
A weighted, directed graph G = (V, E)
pij pjk
A weight function w: E  R, v1
p1i
A shortest path p = v1, v2, . . . , vk from v1 to vk pij’ vk

A subpath of p: pij = vi, vi+1, . . . , vj, with 1  i  j  k vi

Then: pij is a shortest path from vi to vj


p1i pij pjk
Proof: p = v1 vi vj vk
 w(p) = w(p1i) + w(pij) + w(pjk)
Assume  pij’ from vi to vj with w(pij’) < w(pij)
 w(p’) = w(p1i) + w(pij’) + w(pjk) < w(p) contradiction!
Shortest-Path Representation
For each vertex v  V:
d[v] = δ(s, v): a shortest-path estimate
Ini ally, d[v]=∞
Reduces as algorithms progress
[v] = predecessor of v on a shortest path from s
If no predecessor, [v] = NIL
 induces a tree—shortest-path tree
Shortest paths & shortest path trees are not unique
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v  V
2. do d[v] ← 
3. [v] ← NIL
4. d[s] ← 0

All the shortest-paths algorithms start with INITIALIZE-


SINGLE-SOURCE
Relaxation
Relaxing an edge (u, v) = testing whether we can improve
the shortest path to v found so far by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
 update d[v] and [v]
s s
u v u v
2 2
5 9 5 6

RELAX(u, v, w) RELAX(u, v, w)

u v u v
2 2
5 7 5 6

After relaxation: d[v]  d[u] + w(u, v)


RELAX(u, v, w)
1. if d[v] > d[u] + w(u, v)
2. then d[v] ← d[u] + w(u, v)
3. [v] ← u

All the single-source shortest-paths algorithms


start by calling INIT-SINGLE-SOURCE
then relax edges
The algorithms differ in the order and how many times they
relax each edge
Dijkstra’s Algorithm
Single-source shortest path problem:
No negative-weight edges: w(u, v) > 0  (u, v)  E

Maintains two sets of vertices:


S = vertices whose final shortest-path weights have already been
determined
Q = vertices in V – S: min-priority queue
Keys in Q are estimates of shortest-path weights (d[v])

Repeatedly select a vertex u  V – S, with the minimum


shortest-path estimate d[v]
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) t 1 x
 
2. S ←  10 9
2 3 4 6
s 0
3. Q ← V[G]
5 7

4. while Q   
2

y z
5. do u ← EXTRACT-MIN(Q) t 1 x

10 
6. S ← S  {u} 10 9
2 3 4 6
s 0
7. for each vertex v  Adj[u] 5 7
 
8. do RELAX(u, v, w) 5
y
2
z
Example
t 1 x t 1 x
8
10 
14 8 13
14
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 
7 5 7
2 2
y z y z

t x t 1 x
1
8 13
9 8 9
10 9 10 9

2 4 2 3 4 6
s 0 3 6 s 0

7 5 7
5
5 7 5 7
2 2
y z y z
Dijkstra’s Pseudo Code
Graph G, weight function w, root s

relaxing
edges
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S← 
3. Q ← V[G] O(V) build min-heap
4. while Q   Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w) O(E) times; O(lgV)
Running time: O(VlgV + ElgV) = O(ElgV)
Dijkstra’s Running Time

Q T(Extract T(Decrease- Total


-Min) Key)
array O(V) O(1) O(V 2)
binary heap O(lg V) O(lg V) O(E lg V)

You might also like