Lectures On Graph Series
Lectures On Graph Series
Lectures On Graph Series
Ananda Guna
April 8, 2003
1
Announcements
Homework #5 is due Tuesday April
15th.
Quiz #3 feedback is enabled.
Final Exam is Tuesday May 8th at
8AM
Recap
Dijkstras algorithm
S = {1}
for i = 2 to n do D[i] = C[1,i] if there is an edge
from 1 to i, infinity otherwise
for i = 1 to n-1
{ choose a vertex w in V-S such that D[w] is min
add w to S (where S is the set of visited nodes)
for each vertex v in V-S do
D[v] = min(D[v], D[w]+c[w,v])
}
Where |V| = n
Quiz break
Would it be better to use an
adjacency list or an adjacency matrix
for Dijkstras algorithm?
What is the running time of
Dijkstras algorithm, in terms of |V|
and |E| in each case?
Complexity of Dijkstra
Adjacency matrix version Dijkstra finds
shortest path from one vertex to all others
in O(|V|2) time
If |E| is small compared to |V|2, use a
priority queue to organize the vertices in
V-S, where V is the set of all vertices and
S is the set that has already been
explored
So total of |E| updates each at a cost of O(log
|V|)
So total time is O(|E| log|V|)
8
Returns a boolean:
TRUE if and only if there is no negativeweight cycle reachable from the source: a
simple cycle <v0, v1,,vk>, where v0=vk and
k
weight( v
i 1
i 1
,v i ) 0
FALSE otherwise
If it returns TRUE, it also produces the shortest
paths
10
Example
Example ctd..
How can we fill out the rows?
50
15
50
80
15
45
12
Example ctd..
Can we get ith row from i-1th row?
for v != start,
d[v][i] = MIN d[x][i-1] + len(x,v)
x->v
We know minimum path to come to x
using < i nodes.So for all x that
can reach v, find the minimum such
sum (in blue) among all x
Assume d[start][i] = 0 for all i
13
0
0
50
15
50
80
15
45
3
4
0
0
25
25
80
55
15
15
45
45
75
75
25 55 15 45 65
14
Key features
If the graph contains no negative-weight
cycles reachable from the source vertex,
after |V| - 1 iterations all distance estimates
represent shortest pathswhy?
15
Correctness
Case 1: Graph G=(V,E) doesnt contain any negativeweight cycles reachable from the source vertex s
Consider a shortest path p = < v0, v1,..., vk>, which
must have k |V| - 1 edges
By induction:
D(s) = 0 after initialization
Assume D(vi-1) is a shortest path after iteration (i-1)
Since edge (vi-1,vi) is updated on the ith pass, D(vi)
must then reflect the shortest path to vi.
Since we perform |V| - 1 iterations, D(vi) for all
reachable vertices vi must now represent shortest
paths
The algorithm will return true because on the |V|th
iteration, no distances will change
16
Correctness
Case 2: Graph G=(V,E) contains a negative-weight cycle
< v0, v1,..., vk> reachable from the source vertex s
Proof by contradiction:
Assume the algorithm returns TRUE
Thus, D(vi-1) + weight(vi-1, vi) D(vi) for i = 1,
,k
Summing the inequalities for the cycle:
k
D( v
i 1
i 1
i 1
i 1
) weight ( v i 1 , v i ) D( v i )
17
Performance
Initialization: O(|V|)
Path update and cycle check:
|V| calls checking |E| edges, O(|VE|)
Overall cost:
O(|VE|)
18
20
Floyds Algorithm
A[i][j]
A[i][j]
Graph
adjacency
matrix
21
Floyd ctd..
To find shortest paths that uses 2 or
fewer edges find A2, where
multiplication defined as min of sums
instead sum of products
That is (A2)ij = min{ Aik + Akj | k =1..n}
This operation is O(n3)
Using A2 you can find A4 and then A8 and so on
Therefore to find An we need log n operations
Therefore this algorithm is O(log n* n3)
We will consider another algorithm next
22
Floyd-Warshall Algorithm
This algorithm uses nxn matrix A to
compute the lengths of the shortest paths
using a dynamic programming technique.
Let A[i,j] = c[i,j] for all i,j & ij
If (i,j) is not an edge, set A[i,j]=infinity
and A[i,i]=0
Ak[i,j] =
min (Ak-1[i,j] , Ak-1[i,k]+ Ak-1[k,j])
Where Ak is the matrix after k-th iteration and path from i to j does not pass
through a vertex higher than k
23
2
3
3
5
Ak[i,j] =
min (Ak-1[i,j] , Ak-1[i,k]+ Ak-1[k,j])
Where Ak is the matrix after k-th iteration and path from i to j does not pass
through a vertex higher than k
24
Floyd-Warshall Implementation
initialize A[i,j] = C[i,j]
initialize all A[i,i] = 0
for k from 1 to n
for i from 1 to n
for j from 1 to n
if (A[i,j] > A[i,k]+A[k,j])
A[i,j] = A[i,k]+A[k,j];
25
Questions
Question: What is the
asymptotic run time of Dijkstra
(adjacency matrix version)?
O(n2)
26
27
Central office
28
Central office
Expensive!
29
Central office
31
Applications of MST
Any time you want to visit all vertices in a
graph at minimum cost (e.g., wire routing
on printed circuit boards, sewer pipe layout,
road planning)
Internet content distribution
$$$, also a hot research topic
Idea: publisher produces web pages, content
distribution network replicates web pages to
many locations so consumers can access at
higher speed
MST may not be good enough!
content distribution on minimum cost tree may take a
long time!
32
b
6
4
5
b
6
4
5
33
Prims Algorithm
Let V ={1,2,..,n} and U be the set of
vertices that makes the MST and T be the
MST
Initially : U = {1} and T =
while (U V)
let (u,v) be the lowest cost edge such that
u U and v V-U
T = T {(u,v)}
U = U {v}
34
b
6
e a b c d
4
5
Vertex Parent
e
-
e
35
Prims Algorithm
While P is not empty:
1. Select the next vertex u to add to the tree
u = P.deleteMin()
2. Update the weight of each vertex w adjacent to
u which is not in the tree (i.e., w P)
If weight(u,w) < D(w),
a. parent(w) = u
b. D(w) = weight(u,w)
c. Update the priority queue to reflect
new distance for w
36
Prims algorithm
b
6
d b c a
4
5
4 5 5
Vertex Parent
e
b
e
c
e
d
e
37
Prims algorithm
b
6
a c b
4
5
2 4 5
Vertex Parent
e
b
e
c
d
d
e
a
d
38
Prims algorithm
b
6
c b
4
5
4 5
Vertex Parent
e
b
e
c
d
d
e
a
d
39
Prims algorithm
b
6
4
5
Vertex Parent
e
b
e
c
d
d
e
a
d
40
Prims algorithm
b
6
4
5
Vertex Parent
e
b
e
c
d
d
e
a
d
41
42
43
forest:
44
Kruskals algorithm
Initialization
a. Create a set for each vertex v V
b. Initialize the set of safe edges A
comprising the MST to the empty set
c. Sort edges by increasing weight
9
b
6
4
5
45
Kruskals algorithm
For each edge (u,v) E in increasing order
while more than one set remains:
If u and v, belong to different sets
a. A = A {(u,v)}
b. merge the sets containing u and v
Return A
Kruskals algorithm
9
b
6
E=
4
5
Forest
{a}, {b}, {c}, {d}, {e}
{a,d}, {b}, {c}, {e}
{a,d,c}, {b}, {e}
{a,d,c,e}, {b}
{a,d,c,e,b}
{(a,d)}
{(a,d), (c,d)}
{(a,d), (c,d), (d,e)}
{(a,d), (c,d), (d,e), (b,e)}
47
48
Greedy Approach
Like Dijkstras algorithm, both Prims and
Kruskals algorithms are greedy
algorithms
49
Thursday
P vs NP
Models of Hard Problems
Work on Homework 5
50