Download presentation
Presentation is loading. Please wait.
1
Graph Search Algorithms
COSC 3101E, PROF. J. ELDER
2
Graph a b Node ~ city or computer Edge ~ road or data cable c
Undirected or Directed A surprisingly large number of computational problems can be expressed as graph problems. COSC 3101E, PROF. J. ELDER
3
Directed and Undirected Graphs
(c) The subgraph of the graph in part (a) induced by the vertex set {1,2,3,6}. (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. 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. COSC 3101E, PROF. J. ELDER
4
Trees A tree is a connected, acyclic, undirected graph.
Forest Graph with Cycle A tree is a connected, acyclic, undirected graph. A forest is a set of trees (not necessarily connected) COSC 3101E, PROF. J. ELDER
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). COSC 3101E, PROF. J. ELDER
6
End of Lecture 10 Oct 16, 2007
7
Representations: Undirected Graphs
Adjacency List Adjacency Matrix COSC 3101E, PROF. J. ELDER
8
Representations: Directed Graphs
Adjacency List Adjacency Matrix COSC 3101E, PROF. J. ELDER
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 COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
11
Found Not Handled Queue
BFS Found Not Handled Queue First-In First-Out (FIFO) queue stores ‘just discovered’ vertices s b a e d g c f j i h m k l COSC 3101E, PROF. J. ELDER
12
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s b a s d=0 e d g c f j i h m k l COSC 3101E, PROF. J. ELDER
13
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=0 e a d=1 d d g g c f b j i h m k l COSC 3101E, PROF. J. ELDER
14
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a a d=1 d e d g g b c f j i h m k l COSC 3101E, PROF. J. ELDER
15
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 d e d g g b c f c d=2 j f d=2 i h m k l COSC 3101E, PROF. J. ELDER
16
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 e d g g b c f c d=2 j f d=2 m i e h m k l COSC 3101E, PROF. J. ELDER
17
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 e d g b c f c d=2 j f d=2 m i e h j m k l COSC 3101E, PROF. J. ELDER
18
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 e d g c f c d=2 j f d=2 m i e h j m k l COSC 3101E, PROF. J. ELDER
19
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a c d=2 e d f m g e c f j j d=2 i h m k l COSC 3101E, PROF. J. ELDER
20
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d f m g e c f j j h d=3 d=2 i i h m d=3 k l COSC 3101E, PROF. J. ELDER
21
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d m g e c f j j h d=3 d=2 i i h m d=3 k l COSC 3101E, PROF. J. ELDER
22
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d g e c f j j h d=3 d=2 i i h l m d=3 k l COSC 3101E, PROF. J. ELDER
23
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d g c f j j h d=3 d=2 i i h l m d=3 k l COSC 3101E, PROF. J. ELDER
24
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d g c f j h d=3 d=2 i i h l m d=3 k l COSC 3101E, PROF. J. ELDER
25
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a h d=3 i e d l g c f j d=2 i h m d=3 k l COSC 3101E, PROF. J. ELDER
26
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=3 i e d l g k d=4 c f j d=2 i h m d=3 k d=4 l COSC 3101E, PROF. J. ELDER
27
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=3 e d l g k d=4 c f j d=2 i h m d=3 k d=4 l COSC 3101E, PROF. J. ELDER
28
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=3 e d g k d=4 c f j d=2 i h m d=3 k d=4 l COSC 3101E, PROF. J. ELDER
29
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a k d=4 e d g c f j d=2 i h m d=3 k d=4 l COSC 3101E, PROF. J. ELDER
30
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=4 e d=5 d g c f j d=2 i h m d=3 k d=4 l COSC 3101E, PROF. J. ELDER
31
Breadth-First Search Algorithm
BLACK RED GRAY 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. COSC 3101E, PROF. J. ELDER
32
Breadth-First-Search is Greedy
Vertices are handled: in order of their discovery (FIFO queue) Smallest d values first COSC 3101E, PROF. J. ELDER
33
Correctness d Basic Steps: s v & there is an edge from u to v u
The shortest path to u has length d There is a path to v with length d+1. COSC 3101E, PROF. J. ELDER
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 d v u COSC 3101E, PROF. J. ELDER
35
Correctness Two-step proof: On exit: COSC 3101E, PROF. J. ELDER
36
u v s COSC 3101E, PROF. J. ELDER
37
BLACK s v u RED BLACK RED GRAY COSC 3101E, PROF. J. ELDER
38
x s v u Contradiction! COSC 3101E, PROF. J. ELDER
39
Correctness COSC 3101E, PROF. J. ELDER
40
Progress? On every iteration one vertex is processed (turns gray).
BLACK RED GRAY COSC 3101E, PROF. J. ELDER
41
Running Time BLACK RED BLACK RED GRAY COSC 3101E, PROF. J. ELDER
42
Optimal Substructure Property
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. u v s shortest path COSC 3101E, PROF. J. ELDER
43
Recovering the Shortest Path
For each node v, store predecessor of v in (v). s v u (v) Predecessor of v is (v) = u. COSC 3101E, PROF. J. ELDER
44
Recovering the Shortest Path
COSC 3101E, PROF. J. ELDER
45
Colours are actually not required
COSC 3101E, PROF. J. ELDER
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 COSC 3101E, PROF. J. ELDER
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). COSC 3101E, PROF. J. ELDER
48
Found Not Handled Stack <node,# edges>
DFS Note: Stack is Last-In First-Out (LIFO) Found Not Handled Stack <node,# edges> d f s / b a e d g c f j i h m k l COSC 3101E, PROF. J. ELDER
49
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ b a e d g c f j i s,0 h m k l COSC 3101E, PROF. J. ELDER
50
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ 2/ b a e d g c f j i a,0 s,1 h m k l COSC 3101E, PROF. J. ELDER
51
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ 2/ 3/ b a e d g c f j c,0 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
52
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ 2/ 3/ 4/ b a e d g c f h,0 j c,1 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
53
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ 2/ 3/ 5/ 4/ b a e d g c k,0 f h,1 j c,1 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
54
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ 2/ 3/ 5/6 4/ b a e d g c f h,1 j c,1 Path on Stack i a,1 s,1 h m Tree Edge k l COSC 3101E, PROF. J. ELDER
55
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s / 1/ 2/ 3/ 5/6 4/7 b a e d g c f j c,1 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
56
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 5/6 4/7 b a e d g c f i,0 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
57
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,1 j
Cross Edge to handled node: d[h]<d[i] s 8/ 1/ / 2/ 3/ 5/6 4/7 b a e d g c f i,1 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
58
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 5/6 4/7 b a e d g c f i,2 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
59
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 9/ 5/6 4/7 b a e d g c l,0 f i,3 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
60
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 9/ 5/6 4/7 b a e d g c l,1 f i,3 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
61
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 9/10 5/6 4/7 b a e d g c f i,3 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
62
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/ 9/10 5/6 4/7 b a e d g c g,0 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
63
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/ 12/ 9/10 5/6 4/7 b a e d g j,0 c g,1 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
64
DFS Found Not Handled Stack <node,# edges> s b a e d g j,1 c g,1
Back Edge to node on Stack: s 8/ 1/ / 2/ 3/ 11/ 12/ 9/10 5/6 4/7 b a e d g j,1 c g,1 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
65
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/ 12/ 13/ 9/10 5/6 4/7 b a e d m,0 g j,2 c g,1 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
66
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/ 12/ 13/ 9/10 5/6 4/7 b a e d m,1 g j,2 c g,1 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
67
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/ 12/ 13/14 9/10 5/6 4/7 b a e d g j,2 c g,1 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
68
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/ 12/15 13/14 9/10 5/6 4/7 b a e d g c g,1 f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
69
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f i,4 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
70
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 17/ 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f,0 f i,5 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
71
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 17/ 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f,1 f i,5 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
72
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/ 1/ / 2/ 3/ 17/18 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f i,5 j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
73
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/ 3/ 17/18 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j c,2 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
74
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> Forward Edge: d[f]>d[c] s 8/19 1/ / 2/ 3/ 17/18 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j c,3 i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
75
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/ 3/19 17/18 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j i a,1 s,1 h m k l COSC 3101E, PROF. J. ELDER
76
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/ 3/19 17/18 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j i a,2 s,1 h m k l COSC 3101E, PROF. J. ELDER
77
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j i s,1 h m k l COSC 3101E, PROF. J. ELDER
78
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j i d,0 s,2 h m k l COSC 3101E, PROF. J. ELDER
79
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j i d,1 s,2 h m k l COSC 3101E, PROF. J. ELDER
80
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 b a e d g c f j i d,2 s,2 h m k l COSC 3101E, PROF. J. ELDER
81
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 22/ b a e d g c f j e,0 i d,3 s,2 h m k l COSC 3101E, PROF. J. ELDER
82
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 22/ b a e d g c f j e,1 i d,3 s,2 h m k l COSC 3101E, PROF. J. ELDER
83
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 8/19 1/ / 2/20 3/19 17/18 21/ 11/16 12/15 13/14 9/10 5/6 4/7 22/23 b a e d g c f j i d,3 s,2 h m k l COSC 3101E, PROF. J. ELDER
84
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i s,2 h m k l COSC 3101E, PROF. J. ELDER
85
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i s,3 h m k l COSC 3101E, PROF. J. ELDER
86
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i b,0 s,4 h m k l COSC 3101E, PROF. J. ELDER
87
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i b,1 s,4 h m k l COSC 3101E, PROF. J. ELDER
88
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i b,2 s,4 h m k l COSC 3101E, PROF. J. ELDER
89
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i b,3 s,4 h m k l COSC 3101E, PROF. J. ELDER
90
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s 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 b a e d g c f j i s,4 h m k l COSC 3101E, PROF. J. ELDER
91
End of Lecture 11 Oct 18, 2007
92
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> Tree Edges Back Edges Forward Edges s Finished! 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 Cross Edges b a e d g c f j i h m k l COSC 3101E, PROF. J. ELDER
93
Classification of Edges in DFS
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). Back edges are those edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree. Forward edges are non-tree edges (u, v) connecting a vertex u to a descendant v in a depth-first tree. 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 COSC 3101E, PROF. J. ELDER
94
Classification of Edges in DFS
Tree edges: Edge (u, v) is a tree edge if v was black when (u, v) traversed. Back edges: (u, v) is a back edge if v was red when (u, v) traversed. Forward edges: (u, v) is a forward edge if v was gray when (u, v) traversed and d[v] > d[u]. Cross edges (u,v) is a cross edge if v was gray when (u, v) traversed and d[v] < d[u]. Classifying edges can help to identify properties of the graph, e.g., a graph is acyclic iff DFS yields no back edges. 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 COSC 3101E, PROF. J. ELDER
95
Undirected Graphs In a depth-first search of an undirected graph, every edge is either a tree edge or a back edge. Why? COSC 3101E, PROF. J. ELDER
96
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 COSC 3101E, PROF. J. ELDER
97
Depth-First Search Algorithm
BLACK RED BLACK GRAY COSC 3101E, PROF. J. ELDER
98
Depth-First Search Algorithm
BLACK RED BLACK GRAY COSC 3101E, PROF. J. ELDER
99
End of Lecture 12 Oct 23, 2007
100
Topological Sorting (e.g., putting tasks in linear order)
An application of Depth-First Search
101
Linear Order underwear socks pants shoes underwear pants socks shoes
socks underwear pants shoes COSC 3101E, PROF. J. ELDER
102
Linear Order underwear socks pants shoes Too many video games?
COSC 3101E, PROF. J. ELDER
103
Linear Order a b h c i d j e k g f l ….. l (V) (V2)
Precondition: A Directed Acyclic Graph (DAG) Post Condition: Find one valid linear order b h c i d j e k Algorithm: Find a terminal node (sink). Put it last in sequence. Delete from graph & repeat g We can do better! (V) (V2) f l ….. l COSC 3101E, PROF. J. ELDER
104
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f g f l e d ….. f COSC 3101E, PROF. J. ELDER
105
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g l g l e f d When node is popped off stack, insert at front of linearly-ordered “to do” list. Linear Order: ….. f COSC 3101E, PROF. J. ELDER
106
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g g f l e d Linear Order: l,f COSC 3101E, PROF. J. ELDER
107
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l e d Linear Order: g,l,f COSC 3101E, PROF. J. ELDER
108
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l d Linear Order: e,g,l,f COSC 3101E, PROF. J. ELDER
109
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l Linear Order: d,e,g,l,f COSC 3101E, PROF. J. ELDER
110
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g k j f l i Linear Order: d,e,g,l,f COSC 3101E, PROF. J. ELDER
111
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g j f l i Linear Order: k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
112
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l i Linear Order: j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
113
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l Linear Order: i,j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
114
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g c f l b Linear Order: i,j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
115
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l b Linear Order: c,i,j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
116
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l Linear Order: b,c,i,j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
117
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g h f l a Linear Order: b,c,i,j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
118
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l a Linear Order: h,b,c,i,j,k,d,e,g,l,f COSC 3101E, PROF. J. ELDER
119
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l Linear Order: a,h,b,c,i,j,k,d,e,g,l,f Done! COSC 3101E, PROF. J. ELDER
120
Found Not Handled Stack
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. Consider each edge v … u … u v u… v… COSC 3101E, PROF. J. ELDER
121
Found Not Handled Stack
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. Consider each edge u … v … u v u… v… COSC 3101E, PROF. J. ELDER
122
Found Not Handled Stack
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 Consider each edge u … The nodes in the stack form a path starting at s. v … u v u… v… COSC 3101E, PROF. J. ELDER
123
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f l Linear Order: a,h,b,c,i,j,k,d,e,g,l,f Done! COSC 3101E, PROF. J. ELDER
124
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’? 7 1 5 v v 2 s s 2 3 COSC 3101E, PROF. J. ELDER
125
Single-Source (Weighted) Shortest Paths
126
The Problem What is the shortest driving route from Toronto to Ottawa? (e.g. MAPQuest, Microsoft Streets and Trips) Input: COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
130
Optimal substructure Lemma: Any subpath of a shortest path is a shortest path Proof: Cut and paste. COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
133
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 COSC 3101E, PROF. J. ELDER
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 COSC 3101E, PROF. J. ELDER
135
General single-source shortest-path strategy
Start by calling INIT-SINGLE-SOURCE Relax Edges Algorithms differ in the order in which edges are taken and how many times each edge is relaxed. COSC 3101E, PROF. J. ELDER
136
Example: Single-source shortest paths in a directed acyclic graph (DAG)
Since graph is a DAG, we are guaranteed no negative-weight cycles. COSC 3101E, PROF. J. ELDER
137
Algorithm COSC 3101E, PROF. J. ELDER
138
Example COSC 3101E, PROF. J. ELDER
139
Example COSC 3101E, PROF. J. ELDER
140
Example COSC 3101E, PROF. J. ELDER
141
Example COSC 3101E, PROF. J. ELDER
142
Example COSC 3101E, PROF. J. ELDER
143
Example COSC 3101E, PROF. J. ELDER
144
Correctness: Path relaxation property (Lemma 24.15)
COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
148
Dijkstra’s algorithm: Analysis
Using minheap, queue operations takes O(logV) time COSC 3101E, PROF. J. ELDER
149
Key: Example COSC 3101E, PROF. J. ELDER
150
Example COSC 3101E, PROF. J. ELDER
151
Example COSC 3101E, PROF. J. ELDER
152
Example COSC 3101E, PROF. J. ELDER
153
Example COSC 3101E, PROF. J. ELDER
154
Example COSC 3101E, PROF. J. ELDER
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. COSC 3101E, PROF. J. ELDER
156
Correctness of Dijkstra’s Algorithm: Upper Bound Property
Proof: COSC 3101E, PROF. J. ELDER
157
Correctness of Dijkstra’s Algorithm
Handled COSC 3101E, PROF. J. ELDER
158
Correctness of Dijkstra’s Algorithm
Handled COSC 3101E, PROF. J. ELDER
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. ? COSC 3101E, PROF. J. ELDER
160
End of Lecture 13 Oct 25, 2007
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.