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

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

The time complexity of Dijkstra's algorithm is: O(V^2) Where V is the number of vertices. The analysis is: - Step 1 takes O(V) time - Step 3 takes O(V) time to build the min priority queue - The while loop in step 4 runs V times, as each iteration removes a vertex from Q - Step 5 takes O(TE) time where TE is the time to extract min from the heap/priority queue - Step 7 iterates through the adjacency list of each vertex, taking O(E) time total where E is edges - Each relaxation in step 8 takes O(TD) time where TD is the time
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

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

The time complexity of Dijkstra's algorithm is: O(V^2) Where V is the number of vertices. The analysis is: - Step 1 takes O(V) time - Step 3 takes O(V) time to build the min priority queue - The while loop in step 4 runs V times, as each iteration removes a vertex from Q - Step 5 takes O(TE) time where TE is the time to extract min from the heap/priority queue - Step 7 iterates through the adjacency list of each vertex, taking O(E) time total where E is edges - Each relaxation in step 8 takes O(TD) time where TD is the time
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture 15

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
3
• Weight function w : E → R 4
2 1
s 0
2 7
• Weight of path p = v0, v1, k. . . , vk 5 3
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:
A weighted, directed graph G = (V, E) vj
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)

Adding w(p1i) + w(pjk) in both sides of this inequality:

 w(p’) = w(p1i) + w(pij’) + w(pjk) < w(p1i) + w(pij) + w(pjk) = w(p)


So there is a path p’ from v1 to vk which is shorter than the shortest path p between them; but this
contradicts our initial assumption that p is the shortest path.
Shortest-Path Idea
• Recall: d(u,v)  total weight/cost of the shortest path from u to v.
• All SSSP algorithms maintain a field d[u] for every vertex u. d[u]
will be an estimate of d(s,u). As the algorithm progresses, we will
refine d[u] until, at termination,
d[u] = d(s,u).
• Whenever we discover a new shortest path to u, we update d[u].
In fact, d[u] will always be an overestimate of d(s,u):
d[u] ³ d(s,u)
• We’ll use p[u] to point to the parent (or predecessor) of u on the
shortest path from s to u. We update p[u] when we update d[u].
• At the end, p will induce a tree, called shortest path tree.
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
(pronounced “DIKE-stra”)
Algorithm
Single-source shortest path problem:
No negative-weight edges: w(u, v) > 0 (u, v)  E

Maintains two sets of vertices:


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

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


shortest-path estimate d[v] and RELAXs its incident edges.
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) t 1 x
 
2. K ←  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. K ← K  {u} 10 9
2 3 4 6
s 0
7. for each vertex v  Adj[u] 5 7

5 
8. do RELAX(u, v, w) 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
2
7
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
Practice
Dijkstra (G, w, s) – Time Complexity
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)

2. K ← 
3. Q ← V[G] (V) time to build min-heap

4. while Q   Executed (V) times

5. do u ← EXTRACT-MIN(Q) V times, (TE) each time

6. K ← K  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w) (E) times, (TD) each time

Total running time: (V + V  TE + E  TD )


Dijkstra’s Time complexity (cont.)
• 1. Priority queue is an array.
EXTRACT-MIN in (V) time, DECREASE-KEY in (1)
Total time: (V + VV + E) = (V2)
• 2. (“Modified Dijkstra”)
Priority queue is a binary (standard) heap.
EXTRACT-MIN and DECREASE-KEY takes (lgV) time each
Total time: (VlgV + ElgV) = (ElgV)
• 3. Priority queue is Fibonacci heap. (Of theoretical interest
only.)
EXTRACT-MIN in (lgV),
DECREASE-KEY in (1) (amortized)
Total time: (VlgV+E)
6-Graphs
Why Does Dijkstra’s Algorithm Work?
• It works when all edge weights are  0.
• Why?
• It maintains a set K containing all vertices whose
shortest paths from the source vertex s are known
(i.e. d[u] = d(s,u) for all u in K).
• Now look at the “frontier” F of K — i.e., all vertices
adjacent to a vertex in K.
F
the rest
K of the
graph
s
Dijkstra’s: Theorem
• After the end of each iteration
of while loop in Dijkstra’s
algorithm the following is true K min(4+2, 6+1) = 6
for each frontier vertex u: d[u] 2
is the weight of the shortest 4 76
path to u going through only Current u 1 Next u
s
vertices in K. 8
6 3 9
• The algorithm picks the frontier
vertex u with the smallest value min(4+8, 6+3) = 9
of d[u] in the next iteration.

• Claim: If u is chosen (via the


Extract-Min opertaion in line 5)
in the beginning of an iteration
then d[u] = d(s,u)
Dijkstra’s: Proof
• By construction, d[u] is the weight of the shortest path to u
going through only vertices in K.
• If there exist another path p to u that contains some
vertices not in K, then that path must leave K, go to a node
v on the frontier and then reach u from there (via a sub-
path pvu).
shortest path to u

u
s pvu
K
v another path to u, via v
Dijkstra’s: Proof (Contd.)
• The weight of this path, w(p) = d[v]+w(pvu) ³ d[v], since
w(pvu) ³ 0 (because edge weights are non-negative).

• But d[v] ³ d[u], otherwise u wouldn’t be chosen in line 5.

\w(p) ³ d[u]. So there is no path to u which is shorter than


d[u], i.e., d[u] is the shortest path distance of u from s, i.e.,
• d[u] = d(s,u)
shortest path to u

u d[u] £ d[v]
s pvu
K
v another path to u, via v

You might also like