Download presentation
Presentation is loading. Please wait.
Published byBrook Simon Modified over 9 years ago
1
CSE 3101 1 Graph Search Algorithms
2
CSE 3101 2 Graph a c b Node ~ city or computer Edge ~ road or data cable Undirected or Directed A surprisingly large number of computational problems can be expressed as graph problems.
3
CSE 3101 3 Directed and Undirected Graphs (c) The subgraph of the graph in part (a) induced by the vertex set {1,2,3,6}. (a)A directed graph G = (V, E), where V = {1,2,3,4,5,6} and E = {(1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3)}. The edge (2,2) is a self-loop. (b) An undirected graph G = (V,E), where V = {1,2,3,4,5,6} and E = {(1,2), (1,5), (2,5), (3,6)}. The vertex 4 is isolated.
4
CSE 3101 4 Trees TreeForestGraph with Cycle A tree is a connected, acyclic, undirected graph. A forest is a set of trees (not necessarily connected)
5
CSE 3101 5 Running Time of Graph Algorithms Running time often a function of both |V| and |E|. For convenience, drop the |. | in asymptotic notation, e.g. O(V+E).
6
End of Lecture 10 April 6, 2009
7
CSE 3101 7 Representations: Undirected Graphs Adjacency List Adjacency Matrix
8
CSE 3101 8 Representations: Directed Graphs Adjacency List Adjacency Matrix
9
CSE 3101 9 Breadth-First Search Goal: To recover the shortest paths from a source node s to all other reachable nodes v in a graph. –The length of each path and the paths themselves are returned. Notes: –There are an exponential number of possible paths –This problem is harder for general graphs than trees because of cycles! s ?
10
CSE 3101 10 Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouring vertices: –Undiscovered vertices are coloured black –Just discovered vertices (on the wavefront) are coloured red. –Previously discovered vertices (behind wavefront) are coloured grey.
11
CSE 3101 11 BFS s a c h k f i l m j e b g d Found Not Handled Queue First-In First-Out (FIFO) queue stores ‘just discovered’ vertices
12
CSE 3101 12 BFS s a c h k f i l m j e b g d Found Not Handled Queue s d=0
13
CSE 3101 13 BFS Found Not Handled Queue d=0 a b g d d=1 s a c h k f i l m j e b g d d=0 d=1
14
CSE 3101 14 BFS s a c h k f i l m j e b g d Found Not Handled Queue a b g d d=0 d=1
15
CSE 3101 15 BFS s a c h k f i l m j e b g d Found Not Handled Queue b g d c f d=0 d=1 d=2 d=1 d=2
16
CSE 3101 16 BFS s a c h k f i l m j e b g d Found Not Handled Queue b g c f m e d=0 d=1 d=2 d=1 d=2
17
CSE 3101 17 BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 b j c f m e d=1 d=2
18
CSE 3101 18 BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 j c f m e d=1 d=2
19
CSE 3101 19 BFS s a c h k f i l m j e b g d Found Not Handled Queue c f m e j d=0 d=1 d=2
20
CSE 3101 20 BFS s a c h k f i l m j e b g d Found Not Handled Queue f m e j h i d=0 d=1 d=2 d=3 d=2 d=3
21
CSE 3101 21 BFS s a c h k f i l m j e b g d Found Not Handled Queue m e j h i d=0 d=1 d=2 d=3 d=2 d=3
22
CSE 3101 22 BFS s a c h k f i l m j e b g d Found Not Handled Queue e j h i l d=0 d=1 d=2 d=3 d=2 d=3
23
CSE 3101 23 BFS s a c h k f i l m j e b g d Found Not Handled Queue j h i l d=0 d=1 d=2 d=3 d=2 d=3
24
CSE 3101 24 BFS s a c h k f i l m j e b g d Found Not Handled Queue h i l d=0 d=1 d=2 d=3 d=2 d=3
25
CSE 3101 25 BFS s a c h k f i l m j e b g d Found Not Handled Queue h d=0 d=1 d=2 d=3 i l
26
CSE 3101 26 BFS s a c h k f i l m j e b g d Found Not Handled Queue i l k d=0 d=1 d=2 d=3 d=4 d=3 d=4
27
CSE 3101 27 BFS s a c h k f i l m j e b g d Found Not Handled Queue l k d=0 d=1 d=2 d=3 d=4 d=3 d=4
28
CSE 3101 28 BFS s a c h k f i l m j e b g d Found Not Handled Queue k d=0 d=1 d=2 d=3 d=4 d=3 d=4
29
CSE 3101 29 BFS s a c h k f i l m j e b g d Found Not Handled Queue k d=0 d=1 d=2 d=3 d=4
30
CSE 3101 30 BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 d=3 d=4 d=5
31
CSE 3101 31 Breadth-First Search Algorithm Q is a FIFO queue. Each vertex assigned finite d value at most once. Q contains vertices with d values {i, …, i, i+1, …, i+1} d values assigned are monotonically increasing over time. BLACK RED BLACK RED GRAY
32
CSE 3101 32 Breadth-First-Search is Greedy Vertices are handled: – in order of their discovery (FIFO queue) –Smallest d values first
33
CSE 3101 33 Basic Steps: s u The shortest path to u has length d v & there is an edge from u to v There is a path to v with length d+1. Correctness d
34
CSE 3101 34 Correctness Vertices are discovered in order of their distance from the source vertex s. When we discover v, how do we know there is not a shorter path to v? –Because if there was, we would already have discovered it! s u v d
35
CSE 3101 35 Correctness Two-step proof: On exit:
36
CSE 3101 36 u v s
37
CSE 3101 37 BLACK RED BLACK RED GRAY s u v
38
CSE 3101 38 s u v x Contradiction!
39
CSE 3101 39 Correctness
40
CSE 3101 40 Progress? On every iteration one vertex is processed (turns gray). BLACK RED BLACK RED GRAY
41
CSE 3101 41 Running Time BLACK RED GRAY BLACK
42
CSE 3101 42 The shortest path problem has the optimal substructure property: –Every subpath of a shortest path is a shortest path. The optimal substructure property –is a hallmark of both greedy and dynamic programming algorithms. –allows us to compute both shortest path distance and the shortest paths themselves by storing only one d value and one predecessor value per vertex. Optimal Substructure Property u v s shortest path
43
CSE 3101 43 Recovering the Shortest Path For each node v, store predecessor of v in (v). s u v Predecessor of v is (v) (v) = u.
44
CSE 3101 44 Recovering the Shortest Path
45
CSE 3101 45 Colours are actually not required
46
CSE 3101 46 Depth First Search (DFS) Idea: –Continue searching “deeper” into the graph, until we get stuck. –If all the edges leaving v have been explored we “backtrack” to the vertex from which v was discovered. Does not recover shortest paths, but can be useful for extracting other properties of graph, e.g., –Topological sorts –Detection of cycles –Extraction of strongly connected components
47
CSE 3101 47 Depth-First Search Explore every edge, starting from different vertices if necessary. As soon as vertex discovered, explore from it. Keep track of progress by colouring vertices: –Black: undiscovered vertices –Red: discovered, but not finished (still exploring from it) –Gray: finished (found everything reachable from it).
48
CSE 3101 48 DFS s a c h k f i l m j e b g d Found Not Handled Stack / / / / / / / / / / / / / / d f Note: Stack is Last-In First-Out (LIFO)
49
CSE 3101 49 DFS s a c h k f i l m j e b g d Found Not Handled Stack s,0 / 1/ / / / / / / / / / / / /
50
CSE 3101 50 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,0 / 1/ / 2/ / / / / / / / / / /
51
CSE 3101 51 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,0 / 1/ / 2/ 3/ / / / / / / / / /
52
CSE 3101 52 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,0 / 1/ / 2/ 3/ / / / / / / / 4/ /
53
CSE 3101 53 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,1 k,0 / 1/ / 2/ 3/ / / / / / / 5/ 4/ /
54
CSE 3101 54 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,1 Tree Edge Path on Stack / 1/ / 2/ 3/ / / / / / / 5/6 4/ /
55
CSE 3101 55 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 / 1/ / 2/ 3/ / / / / / / 5/6 4/7 /
56
CSE 3101 56 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,0 8/ 1/ / 2/ 3/ / / / / / / 5/6 4/7 /
57
CSE 3101 57 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,1 Cross Edge to handled node: d[h]<d[i] 8/ 1/ / 2/ 3/ / / / / / / 5/6 4/7 /
58
CSE 3101 58 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,2 8/ 1/ / 2/ 3/ / / / / / / 5/6 4/7 /
59
CSE 3101 59 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,3 l,0 8/ 1/ / 2/ 3/ / / / / / 9/ 5/6 4/7 /
60
CSE 3101 60 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,3 l,1 8/ 1/ / 2/ 3/ / / / / / 9/ 5/6 4/7 /
61
CSE 3101 61 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,3 8/ 1/ / 2/ 3/ / / / / / 9/10 5/6 4/7 /
62
CSE 3101 62 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,0 8/ 1/ / 2/ 3/ / / 11/ / / 9/10 5/6 4/7 /
63
CSE 3101 63 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,0 8/ 1/ / 2/ 3/ / / 11/ 12/ / 9/10 5/6 4/7 /
64
CSE 3101 64 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,1 Back Edge to node on Stack: 8/ 1/ / 2/ 3/ / / 11/ 12/ / 9/10 5/6 4/7 /
65
CSE 3101 65 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,2 m,0 8/ 1/ / 2/ 3/ / / 11/ 12/ 13/ 9/10 5/6 4/7 /
66
CSE 3101 66 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,2 m,1 8/ 1/ / 2/ 3/ / / 11/ 12/ 13/ 9/10 5/6 4/7 /
67
CSE 3101 67 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,2 8/ 1/ / 2/ 3/ / / 11/ 12/ 13/14 9/10 5/6 4/7 /
68
CSE 3101 68 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 8/ 1/ / 2/ 3/ / / 11/ 12/15 13/14 9/10 5/6 4/7 /
69
CSE 3101 69 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 8/ 1/ / 2/ 3/ / / 11/16 12/15 13/14 9/10 5/6 4/7 /
70
CSE 3101 70 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,5 f,0 8/ 1/ / 2/ 3/ 17/ / 11/16 12/15 13/14 9/10 5/6 4/7 /
71
CSE 3101 71 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,5 f,1 8/ 1/ / 2/ 3/ 17/ / 11/16 12/15 13/14 9/10 5/6 4/7 /
72
CSE 3101 72 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,5 8/ 1/ / 2/ 3/ 17/18 / 11/16 12/15 13/14 9/10 5/6 4/7 /
73
CSE 3101 73 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 8/19 1/ / 2/ 3/ 17/18 / 11/16 12/15 13/14 9/10 5/6 4/7 /
74
CSE 3101 74 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,3 8/19 1/ / 2/ 3/ 17/18 / 11/16 12/15 13/14 9/10 5/6 4/7 / Forward Edge
75
CSE 3101 75 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 8/19 1/ / 2/ 3/19 17/18 / 11/16 12/15 13/14 9/10 5/6 4/7 /
76
CSE 3101 76 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,2 8/19 1/ / 2/ 3/19 17/18 / 11/16 12/15 13/14 9/10 5/6 4/7 /
77
CSE 3101 77 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack 8/19 1/ / 2/20 3/19 17/18 / 11/16 12/15 13/14 9/10 5/6 4/7 /
78
CSE 3101 78 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,0 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 /
79
CSE 3101 79 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,1 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 /
80
CSE 3101 80 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,2 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 /
81
CSE 3101 81 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3 e,0 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 22/
82
CSE 3101 82 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3 e,1 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 22/
83
CSE 3101 83 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 22/23
84
CSE 3101 84 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack 8/19 1/ / 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
85
CSE 3101 85 DFS s a c h k f i l m j e b g d s,3 Found Not Handled Stack 8/19 1/ / 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
86
CSE 3101 86 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,0 8/19 1/ 25/ 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
87
CSE 3101 87 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,1 8/19 1/ 25/ 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
88
CSE 3101 88 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,2 8/19 1/ 25/ 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
89
CSE 3101 89 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,3 8/19 1/ 25/ 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
90
CSE 3101 90 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack 8/19 1/ 25/26 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
91
CSE 3101 91 DFS s Found Not Handled Stack Finished! Tree Edges Back Edges a c h k f i l m j e b g d Cross Edges 8/19 1/27 25/26 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23 Forward Edges
92
CSE 3101 92 Classification of Edges in DFS 1.Tree edges are edges in the depth-first forest G π. Edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v). 2.Back edges are those edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree. 3.Forward edges are non-tree edges (u, v) connecting a vertex u to a descendant v in a depth-first tree. 4.Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other. s a c h k f i l m j e b g d 8/19 1/27 25/26 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23
93
CSE 3101 93 Classification of Edges in DFS 1.Tree edges: Edge (u, v) is a tree edge if v was black when (u, v) traversed. 2.Back edges: (u, v) is a back edge if v was red when (u, v) traversed. 3.Forward edges: (u, v) is a forward edge if v was gray when (u, v) traversed and d[v] > d[u]. 4.Cross edges (u,v) is a cross edge if v was gray when (u, v) traversed and d[v] < d[u]. s a c h k f i l m j e b g d 8/19 1/27 25/26 2/20 3/19 17/18 21/24 11/16 12/15 13/14 9/10 5/6 4/7 22/23 Classifying edges can help to identify properties of the graph, e.g., a graph is acyclic iff DFS yields no back edges.
94
CSE 3101 94 Undirected Graphs In a depth-first search of an undirected graph, every edge is either a tree edge or a back edge. Why?
95
CSE 3101 95 Undirected Graphs Suppose that (u,v) is a forward edge or a cross edge in a DFS of an undirected graph. (u,v) is a forward edge or a cross edge when v is already handled (grey) when accessed from u. This means that all vertices reachable from v have been explored. Since we are currently handling u, u must be red. Clearly v is reachable from u. Since the graph is undirected, u must also be reachable from v. Thus u must already have been handled: u must be grey. Contradiction! u v
96
End of Lecture 11 Apr 8, 2009
97
CSE 3101 97 Depth-First Search Algorithm RED BLACK GRAY BLACK
98
CSE 3101 98 Depth-First Search Algorithm BLACK RED BLACK GRAY BLACK
99
Topological Sorting (e.g., putting tasks in linear order) An application of Depth-First Search
100
CSE 3101 100 Linear Order underwear pants socks shoes underwear pants socks shoes socks underwear pants shoes
101
CSE 3101 101 Linear Order underwear pants socks shoes Too many video games?
102
CSE 3101 102 Linear Order a b h c i d j e k f l g Precondition: A Directed Acyclic Graph (DAG) Post Condition: Find one valid linear order Algorithm: Find a terminal node (sink). Put it last in sequence. Delete from graph & repeat ….. l (V) (V 2 ) We can do better!
103
CSE 3101 103 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g f l ….. f
104
CSE 3101 104 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g l l When node is popped off stack, insert at front of linearly-ordered “to do” list. ….. f Linear Order:
105
CSE 3101 105 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g l l,f Linear Order:
106
CSE 3101 106 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e l g,l,f Linear Order:
107
CSE 3101 107 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d l e,g,l,f Linear Order:
108
CSE 3101 108 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l d,e,g,l,f Linear Order:
109
CSE 3101 109 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l d,e,g,l,f j k Linear Order:
110
CSE 3101 110 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l k,d,e,g,l,f j Linear Order:
111
CSE 3101 111 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l j,k,d,e,g,l,f Linear Order:
112
CSE 3101 112 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l i,j,k,d,e,g,l,f Linear Order:
113
CSE 3101 113 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS b l c i,j,k,d,e,g,l,f Linear Order:
114
CSE 3101 114 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS b l c,i,j,k,d,e,g,l,f Linear Order:
115
CSE 3101 115 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l b,c,i,j,k,d,e,g,l,f Linear Order:
116
CSE 3101 116 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS a l h b,c,i,j,k,d,e,g,l,f Linear Order:
117
CSE 3101 117 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS a l h,b,c, i,j,k,d,e,g,l,f Linear Order:
118
CSE 3101 118 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l a,h,b,c,i,j,k,d,e,g,l,f Done! Linear Order:
119
CSE 3101 119 Linear Order Found Not Handled Stack Proof: Case 1: u goes on stack first before v. Because of edge, v goes on before u comes off v comes off before u comes off v goes after u in order. u v v…u… Consider each edge v … u …
120
CSE 3101 120 Linear Order Found Not Handled Stack Proof: Case 1: u goes on stack first before v. Case 2: v goes on stack first before u. v comes off before u goes on. v goes after u in order. u v v…u… Consider each edge u … v …
121
CSE 3101 121 Linear Order Found Not Handled Stack Proof: Case 1: u goes on stack first before v. Case 2: v goes on stack first before u. v comes off before u goes on. Case 3: v goes on stack first before u. u goes on before v comes off. Panic: u goes after v in order. Cycle means linear order is impossible u v u…v… Consider each edge u … v … The nodes in the stack form a path starting at s.
122
CSE 3101 122 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l a,h,b,c,i,j,k,d,e,g,l,f Done! Linear Order:
123
Shortest Paths Revisited
124
CSE 3101 124 3 Back to Shortest Path BFS finds the shortest paths from a source node s to every vertex v in the graph. Here, the length of a path is simply the number of edges on the path. But what if edges have different ‘costs’? s v 2 s v 2 5 1 7
125
Single-Source (Weighted) Shortest Paths
126
CSE 3101 126 The Problem What is the shortest driving route from Toronto to Ottawa? (e.g. MAPQuest, Google Maps) Input:
127
CSE 3101 127 Example Single-source shortest path search induces a search tree rooted at s. This tree, and hence the paths themselves, are not necessarily unique.
128
CSE 3101 128 Shortest path variants Single-source shortest-paths problem: – the shortest path from s to each vertex v. (e.g. BFS) Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each vertex v. Single-pair shortest-path 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.
129
CSE 3101 129 Negative-weight edges OK, as long as no negative-weight cycles are reachable from the source. –If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = −∞ for all v on the cycle. –But OK if the negative-weight cycle is not reachable from the source. –Some algorithms work only if there are no negative-weight edges in the graph.
130
CSE 3101 130 Optimal substructure Lemma: Any subpath of a shortest path is a shortest path Proof: Cut and paste.
131
CSE 3101 131 Cycles Shortest paths can’t contain cycles: –Already ruled out negative-weight cycles. –Positive-weight: we can get a shorter path by omitting the cycle. –Zero-weight: no reason to use them assume that our solutions won’t use them.
132
CSE 3101 132 Output of a single-source shortest-path algorithm For each vertex v in V: –d[v] = δ(s, v). Initially, d[v]=∞. Reduce as algorithm progresses. But always maintain d[v] ≥ δ(s, v). Call d[v] a shortest-path estimate. –π[v] = predecessor of v on a shortest path from s. If no predecessor, π[v] = NIL. π induces a tree — shortest-path tree.
133
CSE 3101 133 Initialization All shortest-paths algorithms start with the same initialization: INIT-SINGLE-SOURCE(V, s) for each v in V do d[v]←∞ π[v] ← NIL d[s] ← 0
134
CSE 3101 134 Relaxing an edge Can we improve shortest-path estimate for v by going through u and taking (u,v)? RELAX(u, v,w) if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) π[v]← u
135
CSE 3101 135 General single-source shortest-path strategy 1.Start by calling INIT-SINGLE-SOURCE 2.Relax Edges Algorithms differ in the order in which edges are taken and how many times each edge is relaxed.
136
CSE 3101 136 Example: Single-source shortest paths in a directed acyclic graph (DAG) Since graph is a DAG, we are guaranteed no negative-weight cycles.
137
CSE 3101 137 Algorithm
138
CSE 3101 138 Example
139
CSE 3101 139 Example
140
CSE 3101 140 Example
141
CSE 3101 141 Example
142
CSE 3101 142 Example
143
CSE 3101 143 Example
144
CSE 3101 144 Correctness: Path relaxation property (Lemma 24.15)
145
CSE 3101 145 Correctness of DAG Shortest Path Algorithm Because we process vertices in topologically sorted order, edges of any path are relaxed in order of appearance in the path. – Edges on any shortest path are relaxed in order. – By path-relaxation property, correct.
146
CSE 3101 146 Example: Dijkstra’s algorithm Applies to general weighted directed graph (may contain cycles). But weights must be non-negative. Essentially a weighted version of BFS. –Instead of a FIFO queue, uses a priority queue. –Keys are shortest-path weights (d[v]). Maintain 2 sets of vertices: –S = vertices whose final shortest-path weights are determined. –Q = priority queue = V-S.
147
CSE 3101 147 Dijkstra’s algorithm Dijkstra’s algorithm can be viewed as greedy, since it always chooses the “lightest” vertex in V − S to add to S.
148
CSE 3101 148 Dijkstra’s algorithm: Analysis Analysis: Using minheap, queue operations takes O(logV) time
149
CSE 3101 149 Example Key:
150
CSE 3101 150 Example
151
CSE 3101 151 Example
152
CSE 3101 152 Example
153
CSE 3101 153 Example
154
CSE 3101 154 Example
155
CSE 3101 155 Correctness of Dijkstra’s algorithm Loop invariant: d[v] = δ(s, v) for all v in S. –Initialization: Initially, S is empty, so trivially true. –Termination: At end, Q is empty S = V d[v] = δ(s, v) for all v in V. –Maintenance: Need to show that –d[u] = δ(s, u) when u is added to S in each iteration. –d[u] does not change once u is added to S.
156
CSE 3101 156 Correctness of Dijkstra’s Algorithm: Upper Bound Property Upper Bound Property: Proof:
157
CSE 3101 157 Correctness of Dijkstra’s Algorithm Handled
158
CSE 3101 158 Correctness of Dijkstra’s Algorithm Handled
159
CSE 3101 159 Correctness of Dijkstra’s algorithm Loop invariant: d[v] = δ(s, v) for all v in S. –Maintenance: Need to show that –d[u] = δ(s, u) when u is added to S in each iteration. –d[u] does not change once u is added to S. ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.