0% found this document useful (0 votes)
8 views26 pages

Lec 11 - Graph Algorithms

The document covers key concepts in graph algorithms, specifically focusing on shortest-path problems, including single-source and all-pairs shortest-paths. It details Dijkstra's algorithm for finding the shortest path in weighted graphs and the Floyd-Warshall algorithm for all-pairs shortest paths, highlighting their applications and complexities. The content is structured as a lecture outline for an Algorithm Design and Analysis course.

Uploaded by

laptop2112006
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)
8 views26 pages

Lec 11 - Graph Algorithms

The document covers key concepts in graph algorithms, specifically focusing on shortest-path problems, including single-source and all-pairs shortest-paths. It details Dijkstra's algorithm for finding the shortest path in weighted graphs and the Floyd-Warshall algorithm for all-pairs shortest paths, highlighting their applications and complexities. The content is structured as a lecture outline for an Algorithm Design and Analysis course.

Uploaded by

laptop2112006
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/ 26

CS311

Algorithm Design and


Analysis
LECTURE 11 : GRAPH ALGORITHMS (3)
Contents
▪ Shortest-path problem
▪ Single-Source Shortest-Path
▪ Relaxation
▪ Dijkstra’s algorithm
▪ All-Pairs Shortest Path Problem
▪ Floyd-Warshall algorithm

ALGORITHM DESIGN AND ANALYSIS 2


Shortest-Path Problem
▪ Shortest path = a path with the minimum weight.
▪ Applications
➢Static/Dynamic network routing
➢Robot motion planning
➢Map/Route generation in traffic
▪ The input to a shortest-paths problem is a weighted, directed graph G = (V, E),
with a weight function W: E →R (assigning real values to edges).
▪ Weight of path p = v1 →v2 →… →vk is
k −1
w( p) =  w(vi , vi +1 )
i =1

ALGORITHM DESIGN AND ANALYSIS 3


Single-Source Shortest-Path problems
▪ Given a graph G = (V, E) , find a shortest path from a given source vertex s ϵ V to every
vertex v ϵ V .
▪ The algorithm for the single-source problem can solve many other problems, including
the following variants:

➢Single-destination shortest-paths problem: Find a shortest path


to a given destination vertex t from each vertex v .
➢Single-pair shortest-paths problem : Find a shortest path from u
to v for given vertices u and v.
➢All-pairs shortest-paths problem: Find a shortest path from u to v
for every pair of vertices u and v.

ALGORITHM DESIGN AND ANALYSIS 4


Shortest-path Estimate
For each vertex v ϵ V , the single-source shortest paths algorithms maintain an
attribute v.d, represents the weight of a shortest path from source s to v .

ALGORITHM DESIGN AND ANALYSIS 5


Relaxation

ALGORITHM DESIGN AND ANALYSIS 6


Dijkstra’s algorithm
▪ Dijkstra’s algorithm solves the single-source shortest-paths problem on a weighted,
directed graph, but it requires nonnegative weights on all edges.
▪ The running time of Dijkstra’s algorithm is lower than that of the Bellman-Ford
algorithm.
▪ Greedy, similar to Prim's algorithm for MST.
▪ Uses Q, a priority queue ADT keyed by v.d, which is re-organized whenever some d
decreases.
▪ Basic idea
✓Maintain a set S of solved vertices whose SP from s has been determined.
✓Repeatedly: Selects u ϵ V–S with the minimum SP estimate (greedy choice).
✓Then, adds u into S , and relaxes all edges leaving u .

ALGORITHM DESIGN AND ANALYSIS 7


Dijkstra’s algorithm

ALGORITHM DESIGN AND ANALYSIS 8


Dijkstra’s algorithm

V d π
S 0 NIL
Q = {s , t , y , x , z} T  NIL

S= Y  NIL
X  NIL
z  NIL

ALGORITHM DESIGN AND ANALYSIS 9


Dijkstra’s algorithm

V d π
S 0 NIL
Extract-MIN(Q)  s T 
10 S
NIL
Q = {s, t , y , x , z} Y 
5 S
NIL
S = {s} X  NIL
z  NIL

ALGORITHM DESIGN AND ANALYSIS 10


Dijkstra’s algorithm

V d π
Extract-MIN(Q)  y S 0 NIL
Q = { y, t , x, z } T 8
10 Y
S
S = {s, y} Y 5 S
X 
14 Y
NIL
Zz 
7 Y
NIL

ALGORITHM DESIGN AND ANALYSIS 11


Dijkstra’s algorithm

V d π
Extract-MIN(Q)  z S 0 NIL
Q = { z, t , x } T 8 Yy
S = {s, y, z} Y 5 S
X 13
14 YZ
z 7 Yy

ALGORITHM DESIGN AND ANALYSIS 12


Dijkstra’s algorithm

V d π
Extract-MIN(Q)  t S 0 NIL
Q = { t , x} T 8 y
S = {s, y, z, t} Y 5 S
X 9
13 ZT
z 7 y

ALGORITHM DESIGN AND ANALYSIS 13


Dijkstra’s algorithm

V d π
Extract-MIN(Q)  x S 0 NIL
Q={x} T 8 y
S = {s, y, z, t, x} Y 5 S
X 9 T
z 7 y

ALGORITHM DESIGN AND ANALYSIS 14


Dijkstra’s algorithm Analysis
O(V)
Q is a Min-heap

O(V lg V)
The total running time is
O(V lg V)
O(E lg V)
V
in the typical case that
E O(E lg V)
E = Ω (V)
O(lg V)

ALGORITHM DESIGN AND ANALYSIS 15


All-Pairs Shortest Path Problem
▪ The input is a weighted, directed graph G =(V , E) with a weight function W that
maps edges to real-valued weights.

▪ The goal is to find, for every pair of vertices u,v ϵ V , a shortest (least-weight)
path from u to v.

▪ We can solve it by running a single-source shortest-paths algorithm V times,


once with each vertex as the source.

ALGORITHM DESIGN AND ANALYSIS 16


All-Pairs Shortest Path Problem
▪ If all edge weights are nonnegative, we can use Dijkstra’s algorithm:

➢ The priority queue implemented as Min-heap, the running time is

O(VE lg V).

▪Is there an algorithm that guarantee a much better asymptotic running time??

ALGORITHM DESIGN AND ANALYSIS 17


All-Pairs Shortest Path Problem
▪ Represent the graph by an adjacency matrix.
▪ The input is an n X n matrix W = (wij) representing the edge weights of an n-vertex, where

Example :

ALGORITHM DESIGN AND ANALYSIS 18


Floyd-Warshall algorithm
▪ The Floyd-Warshall algorithm presents a dynamic-programming solution to the all-
pairs shortest path problem runs in Θ(V3) time.
▪ Let dij(k) be the weight of a shortest path from vertex i to vertex j for which all
intermediate vertices belong to the set {1, 2, …, k}.
▪ When k = 0 , a path from vertex i to vertex j with no intermediate vertex. The path has
at most one edge.

ALGORITHM DESIGN AND ANALYSIS 19


Floyd-Warshall algorithm
The matrix dij(k) is defined recursively by:

Because for any path, all intermediate vertices belong to the set {1, 2, … , n) ,
the matrix D(n) = dij(n) gives the final answer.
Example:
d3,2(1) is the min (d3,2 (0) , d3,1(0) + d1,2(0) )
∞ 12

ALGORITHM DESIGN AND ANALYSIS 20


Floyd-Warshall algorithm
D(1):

d2,3 (1) is the min (d2,3 (0) , d2,1(0) + d1,3(0) )


d2,4 (1) is the min (d2,4 (0) , d2,1(0) + d1,4(0) )
d3,2 (1) is the min (d3,2 (0) , d3,1(0) + d1,2(0) )

ALGORITHM DESIGN AND ANALYSIS 21


Floyd-Warshall algorithm
D(2):

D(1):

ALGORITHM DESIGN AND ANALYSIS 22


Floyd-Warshall algorithm
D(3):

D(4):
D(3)

ALGORITHM DESIGN AND ANALYSIS 23


Floyd-Warshall algorithm

Final
Answer

ALGORITHM DESIGN AND ANALYSIS 24


Floyd-Warshall algorithm

O(1)

▪ The running time of the Floyd-Warshall algorithm is determined by the triply nested
for loops.
▪ The algorithm runs in Θ(n3) time.

ALGORITHM DESIGN AND ANALYSIS 25


Questions

ALGORITHM DESIGN AND ANALYSIS 26

You might also like