4102 BFS Dijkstra Oct 22 2009 abhi shelat
breadth first search
bfs(g, a) 1 2 a b 1 2 d c e f g 2 h
bfs theorem
Theorem 1 (CLRS, p. 599) Let G =(V, E) be a graph and suppose that BFS is run on G from vertex s V. Then, when BFS terminates, d v = δ(s, v) for all v V where δ(s, v) = if there is no path from s to v in G.
claim:
lemma: when bfs terminates,
lemma: then suppose is enqueued before during bfs
breadth first search
Theorem 1 (CLRS, p. 599) Let G =(V, E) be a graph and suppose that BFS is run on G from vertex s V. Then, when BFS terminates, d v = δ(s, v) for all v V where δ(s, v) = if there is no path from s to v in G.
breadth first search
dfsall(g =(V, E)) 1 for each v V set d v 2 Set t 0 and S while s V such that d s = 4 do push(s, s) 5 while S is not empty 6 do u pop(s) 7 if d u = or u=s 8 then 9 t t + 1 10 d u t 11 for each v Adj(u) such that d v = 12 do push(s, v)
dfs(g, a)
partial order
http://scienceblogs.com/goodmath/2007/10/making_graph_algorithms_fast_u.php
http://scienceblogs.com/goodmath/2007/10/making_graph_algorithms_fast_u.php
strongly connected component definition: maximal set of vertices such that for every pair (a) u is connected to v (b) v is connected to u
example
dfs allows one to compute the strongly connected component of a graph.
unit edge weight 1 2 1 2 2
shortest paths b 8 d 8 g 10 2 a 9 e 5 9 i 12 11 c 1 f 6 h image:www.princegeorgeva.org, thefranciscofamily.org, www.rightdriveacademy.co.uk, www.ccscambridge.org, www.drawingcoach.com, www.pastoral.org.uk, www.daasgallery.com
bfs b 8 d 8 g 7 2 9 e 5 9 i 11 a 12 c 1 f 6 h
b a c
b a c
b a c
shortest paths b 8 d 8 g 10 2 a 9 e 5 9 i 12 11 c 1 f 6 h image:www.princegeorgeva.org, thefranciscofamily.org, www.rightdriveacademy.co.uk, www.ccscambridge.org, www.drawingcoach.com, www.pastoral.org.uk, www.daasgallery.com
shortest paths b 8 d 8 g 10 2 a 9 e 5 9 i 12 11 c 1 f 6 h alarmclock model image:www.princegeorgeva.org, thefranciscofamily.org, www.rightdriveacademy.co.uk, www.ccscambridge.org, www.drawingcoach.com, www.pastoral.org.uk, www.daasgallery.com
new data structure
binary heap full tree, key value <= to key of children
binary heap full tree, key value <= to key of children 10 5 11 1 6 9 2 44
binary heap full tree, key value <= to key of children 10 5 11 1 6 9 2 44 8
binary heap full tree, key value <= to key of children 10 5 11 8 6 9 2 44 1
binary heap full tree, key value <= to key of children how to extractmin? 8 5 11 10 6 9 2 44 1
binary heap full tree, key value <= to key of children how to extractmin? 1 8 5 11 10 6 9 2 44 1
binary heap full tree, key value <= to key of children how to extractmin? 5 8 1 11 10 6 9 2 44 1
binary heap full tree, key value <= to key of children how to extractmin? 5 8 6 11 10 1 9 2 44 1
binary heap full tree, key value <= to key of children how to decreasekey? 5 8 6 11 10 1 9 2 44
binary heap full tree, key value <= to key of children how to decreasekey? 5 7 6 11 8 1 9 2 44
implementation use a priority queue to keep track of light edges insert: makequeue: extractmin: decreasekey:
@/!(=, 1, ))!"# "%42 % = {)} $" >A%B >A)B C < 9DE:9:9(<, )) %&'() < = $" % F9E:9:9(<)!"# "%42 v 7>?A%B $" '! >AvB = *&)+ >AvB >A%B + 5 9DE:9:9(<,v)
algorithm
Dijkstra(G =(V,E),s) 1 for all v V 2 do d u π u nil 4 d s 0 5 Q makequeue(v ) use d u as key 6 while Q = 7 do u extractmin(q) 8 for each v Adj (u) 9 do if d v >d u + w(u, v) 10 then d v d u + w(u, v) 11 π v u 12 decreasekey(q, v)
@/!(=, 1, ))!"# "%42 % = {)} $" >A%B >A)B C < 9DE:9:9(<, )) %&'() < = $" % F9E:9:9(<)!"# "%42 v 7>?A%B $" '! >AvB = *&)+ >AvB >A%B + 5 9DE:9:9(<,v) Dijkstra(G =(V,E),s) 1 for all v V 2 do d u π u nil 4 d s 0 5 Q makequeue(v ) use d u as key 6 while Q = 7 do u extractmin(q) 8 for each v Adj (u) 9 do if d v >d u + w(u, v) 10 then d v d u + w(u, v) 11 π v u 12 decreasekey(q, v)
b 8 d 8 g 10 7 2 a 9 e 5 9 i 12 11 c 1 f 6 h
b 8 d 8 g 10 7 2 a 9 0 e 5 9 i 12 11 c 1 f 6 h
b 10 8 d 8 g 10 7 2 a 9 0 e 5 9 i 12 11 c 1 12 f 6 h
b 10 8 d 18 8 g 10 7 2 a 9 0 e 5 9 i 12 11 c 1 12 f 6 h
b 10 8 d 18 8 g 10 7 2 a 9 0 e 15 5 9 i 12 11 c 1 12 f 1 6 h
b 10 8 d 18 8 g 10 7 2 a 9 0 e 15 5 9 i 12 11 c 1 12 f 6 1 19 h
b 10 8 d 18 8 g 26 10 7 2 a 9 0 e 15 5 9 i 12 11 c 1 12 f 6 1 19 h
b 10 8 d 18 8 g 26 10 7 2 a 9 0 e 15 5 9 i 0 12 11 c 1 12 f 6 1 19 h
b 10 8 d 18 8 g 26 10 7 2 a 9 0 e 15 5 9 i 0 28 12 11 c 1 12 f 6 1 19 h
running time Dijkstra(G =(V,E),s) 1 for all v V 2 do d u π u nil 4 d s 0 5 Q makequeue(v ) use d u as key 6 while Q = 7 do u extractmin(q) 8 for each v Adj (u) 9 do if d v >d u + w(u, v) 10 then d v d u + w(u, v) 11 π v u 12 decreasekey(q, v)
why does dijkstra work?
steps along the way triangle inequality: (u, v) E, δ(s, v) δ(s, u)+w(u, v) upper bound: d v δ(s, v) no paths: δ(s, v) = = d v =