Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First.

Similar presentations


Presentation on theme: "1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First."— Presentation transcript:

1 1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First Search Linear Order

2 2 Graph a c b Node ~ city or computer Edge ~ road or network Undirected or Directed A surprisingly large number of problems in computer science can be expressed as a graph theory problem.

3 3 Graph Search Specification: Reachability-from-single-source s : The input is a graph G (either directed or undirected) and a source node s. : Output all the nodes u that are reachable by a path in G from s.

4 4 Graph Search Basic Steps: s u Suppose you know that u is reachable from s v & there is an edge from u to v You know that v is reachable from s Build up a set of reachable nodes.

5 5 Graph Search s reachable f u j d v w e h d b t d v w e h d b t How do we keep track of all of this information? How do we avoid cycling?

6 6 s a c h k f i l m j e b g d Graph Search

7 7 s a c h k f i l m j e b g d

8 8 s a c h k f i l m j e b g d

9 9 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search l s a c h k f i l m j e b g d

10 10 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search i.e. find its neighbors Don’t re-find a node. s a c h k f i l m j e b g d Handle some foundNotHandled node

11 11 s a c h k f i l m j e b g d We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Handle some foundNotHandled node i.e. find its neighbors

12 12 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search # of found nodes. measure progress # of handled nodes. Might not increase. s a c h k f i l m j e b g d

13 13 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Node s is foundNotHandled Other nodes notFound s a c h k f i l m j e b g d

14 14 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search All nodes found. Exit When can’t make any more progress. No. Might not find all. When all found nodes are have been handled. Handle some foundNotHandled node s a c h k f i l m j e b g d

15 15

16 16 Graph Search We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. a c h k f i m j e b g d l Exit All found nodes are handled. Exit : Output all the nodes u that are reachable by a path in G from s. Output Found nodes

17 17 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Exit a c h k f i m j e b g d l All found nodes are handled. Exit Found nodes are reachable from s. Reachable nodes have been found. :

18 18 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Exit a c h k f i m j e b g d l All found nodes are handled. Exit Reachable nodes have been Found. : Found = handled handled notfound [A  B] = [  B   A] notFound nodes not reachable.

19 19 Graph Search Specification of Reachability-from-single-source s : The input is a graph G (either directed or undirected) and a source node s. : Output all the nodes u that are reachable by a path in G from s. EndingInitial ConditionsMake Progress Maintain Loop InvDefine Exit ConditionDefine Step Define Measure of Progress Define Loop Invariants Define Problem 79 km to school Exit 79 km75 km Exit 0 kmExit

20 20 Graph Search # of handled nodes. Handle some foundNotHandled node Time = O(n) f u j O(n) iterations, but iteration takes more than O(1) O(n) neighbors = O(n 2 ) Could be fewer? Each edge visited, times.2 = O(E) Size =O(E) Linear time.

21 21 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search Priority Queue: –Handle node that seems to be closest to s (weighted). –Dijkstra's Shortest-Weighted Paths Stack: –Handle node Found most recently Likely farthest from s. –Depth-First Search

22 22 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search So far, the nodes have been found in order of length from s.

23 23 BFS s a c h k f i l m j e b g d Found Not Handled Queue

24 24 BFS s a c h k f i l m j e b g d Found Not Handled Queue s d=0

25 25 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

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

27 27 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

28 28 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

29 29 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

30 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 j c f m e d=1 d=2

31 31 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

32 32 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

33 33 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

34 34 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

35 35 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

36 36 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

37 37 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

38 38 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

39 39 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

40 40 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

41 41 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

42 42 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

43 43 BFS So far, the nodes have been found in order of length from s.

44 44 BFS So far, the nodes have been found in order of length from s. : Finds a shortest path from s to each node v and its length. When we find v, we know there isn't a shorter path to it because ? Otherwise, we would have found it already.

45 45 BFS Data structure for storing a path to each node: For each node v, store  (v) to be parent of v.

46 46 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. BFS Parent of v is  (v) = u.

47 47

48 48 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search Priority Queue: –Handle node that seems to be closest to s (weighted). –Dijkstra's Shortest-Weighted Paths Stack: –Handle node Found most recently Likely farthest from s. –Depth-First Search

49 49 Dijkstra's Shortest-Weighted Paths Specification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s : The input is a graph G (either directed or undirected) with positive edge weights and a source node s. : Finds a shortest weighted path from s to each node v and its length.

50 50 Dijkstra's Shortest-Weighted Paths The king wanted to know the shortest path from his castle s to each node (home) v. So that people could come and go quickly and so that he could guard these home and paths. s D( ) = length = 8. 5 3  ( ) = previous node in path

51 51 Dijkstra's Shortest-Weighted Paths Rome was not built in a day. He decided to handle the nodes in terms of their distance from s. He put a wall around these handled nodes. s

52 52 Dijkstra's Shortest-Weighted Paths We have the answer for handled nodes (i.e. closest) i.e. D(u) and  (u) give shortest path from s to u. Exit When all the nodes are handled, done! s

53 53 Dijkstra's Shortest-Weighted Paths When a node has been handled: He put a knight on each handled node to guard all edges (roads) leading out of the node. An edge is handled if it comes out of a handled node. i.e. all edges in and leaving city. s

54 54 Dijkstra's Shortest-Weighted Paths Unhandled edges are unguarded, so we don’t want to travel along them, even if they make a shorter path. s

55 55 Dijkstra's Shortest-Weighted Paths s A path is handled if has handled edges. i.e. path travels from s between the handled nodes within the walls and then one last edge out at gate. For unhandled nodes,  (u) and d(u) values give the shortest handled path from s. d( )=8. d( )=20. d( )= 

56 56 Dijkstra's Shortest-Weighted Paths s d( )=8. 5 d( ) + w(, ) = 8 + 5 = 13 This red path to has length This is better but unhandled and hence unguarded. d( )=20.

57 57 Dijkstra's Shortest-Weighted Paths s d( )=8. 5 Theorem: If has the smallest d value, then its shortest path from s is handled. Proof: Any unhandled path must leave at a different gate, and hence already has gone further than d( ). d( )=20. D

58 58 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 Consider the unhandled node with the smallest d( ). d( )=20. Priority Queue (FoundNotHandled): Handle node that seems to be closest to s.

59 59 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 Consider the unhandled node with the smallest d( ). Handled it. Handle out going edges. Update its neighbor’s d values. d( )=20.

60 60 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( ) + w(, ) = 8 + 5 = 13 This red path to has length This is better but was unhandled. d( )=20. But with handled, this edge is now handled so we are good.

61 61 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( ) + w(, ) = 8 + 5 = 13 This red path to has length d( )=20. But with handled, this edge is now handled so we are good. 13  ( ) This is better but was unhandled.

62 62 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( )=13.  ( ) We have the answer for handled nodes (i.e. closest) i.e. D(u) and  (u) give shortest path from s to u. Done!

63 63 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( )=13.  ( ) Done! For unhandled nodes,  (u) and d(u) values give the shortest handled path from s.

64 64 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( )=13.  ( ) EndingInitial ConditionsMake Progress Maintain Loop Inv Define Exit Condition Define Step Define Measure of Progress Define Loop Invariants Define Problem 79 km to school Exit 79 km75 km Exit 0 kmExit Done!

65 65

66 66 Time = O(E) Each edge visited, times. 2 ? Dijkstra's This number of times following an edge. Must update Priority Queue. Takes time O(log(n)) Time = O(E log(n)) For each update, d(v) = min( d(v), d(u)+w ). Heap

67 67 Dijkstra's u v s Handled nodesFound nodes Handled Edges? Definition of handled paths and d(v)

68 68 Dijkstra's u v s Handled nodesFound nodes Handled Edges Handled Paths? Definition of handled paths and d(v)

69 69 Dijkstra's u v s Handled nodesFound nodes Definition of handled paths and d(v) Handled Edges Handled Paths?

70 70 Dijkstra's u v s Handled nodes d(v) is the length of the shortest handled paths to v. For handled u, d(u) is the length of the shortest paths to u. NotFound d(v’)=d(v’)=  Definition of handled paths and d(v)

71 71 u The shortest of handled paths to u has length d(u) Dijkstra's & there is an edge from u to v w<u,v>w<u,v> v s The shortest of handled paths to v has length d(v) The shortest handled path to v has length min( d(v), d(u)+w ). Handle node that “seems” to be closest to s. Basic Steps:

72 72 s c b Dijkstra's a d f ij h e g 40 1 10 2 15 1 2 6 8 1 2 30 3 d=d= d=d= d=d= d=d= d=d= d=d= d=d= d=0d=0 d=d= d=d= d=d= 1 2 3 3

73 73 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 6 8 1 2 30 3 d=1d=1 d=d= d=d= d=d= d=d= d=10 d=0d=0 d=40 d=d= d=30 30 1 15 2 2 3 d=d= 3

74 74 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=d= d=d= d=10 d=0d=0 d=31 d=d= d=30 30 15 2 2 3 3

75 75 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=d= d=d= d=10 d=0d=0 d=31 d=4d=4 d=18 30 15 2 2 3 3

76 76 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=10 d=d= d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

77 77 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=d= d=10 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

78 78 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=d= d=9d=9 d=0d=0 d=6d=6 d=4d=4 d=5d=5 1 15 2 3 3

79 79 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=15 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

80 80 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

81 81 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

82 82 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

83 83 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 DONE 3

84 84 Dijkstra's

85 85 Dijkstra's

86 86 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search Priority Queue: –Handle node that seems to be closest to s (weighted). –Dijkstra's Shortest-Weighted Paths Stack: –Handle node Found most recently Likely farthest from s. –Depth-First Search

87 87 DFS Breadth first search makes a lot of sense for dating in general actually. It suggests dating a bunch of people casually before getting serious rather than having a series of five year relationships.

88 88 DFS s a c h k f i l m j e b g d Found Not Handled Stack

89 89 DFS s a c h k f i l m j e b g d Found Not Handled Stack s

90 90 DFS s a c h k f i l m j e b g d Found Not Handled Stack a b g d

91 91 DFS s a c h k f i l m j e b g d Found Not Handled Stack a m g d e

92 92 DFS

93 93 DFS The nodes in the stack form a path starting at s.

94 94 DFS s a c h k f i l m j e b g d Found Not Handled Stack

95 95 DFS s a c h k f i l m j e b g d Found Not Handled Stack s,0

96 96 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,0

97 97 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

98 98 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

99 99 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

100 100 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

101 101 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

102 102 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

103 103 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

104 104 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

105 105 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

106 106 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

107 107 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

108 108 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

109 109 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

110 110 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 Forms a cycle

111 111 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

112 112 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

113 113 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

114 114 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

115 115 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

116 116 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

117 117 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

118 118 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

119 119 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

120 120 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

121 121 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1

122 122 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,2

123 123 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack

124 124 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,1

125 125 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,2

126 126 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

127 127 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

128 128 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3

129 129 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack

130 130 DFS s a c h k f i l m j e b g d s,3 Found Not Handled Stack

131 131 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,0

132 132 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,1

133 133 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,2

134 134 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,3

135 135 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack

136 136 DFS s a c h k f i l m j e b g d Found Not Handled Stack done

137 137

138 138 Recursive Depth First Search Get help from friends

139 139

140 140 Linear Order underwear pants socks shoes underwear pants socks shoes socks underwear pants shoes

141 141 Linear Order underwear pants socks shoes ?

142 142 Linear Order a b h c i d j e k f l g : A Directed Acyclic Graph (DAG) : Find one valid linear order ….. l Algorithm: Find a sink. Put it last in order. Delete & Repeat ?

143 143 Linear Order a b h c i d j e k f l g : A Directed Acyclic Graph (DAG) : Find one valid linear order (n)(n2)(n)(n2) Algorithm: Find a sink. Put it last in order. Delete & Repeat ….. l

144 144 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

145 145 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 take off stack add to backwards order ….. f

146 146 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g l When take off stack add to backwards order l,f

147 147 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e l When take off stack add to backwards order g,l,f

148 148 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d l When take off stack add to backwards order e,g,l,f

149 149 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order d,e,g,l,f

150 150 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l When take off stack add to backwards order d,e,g,l,f j k

151 151 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l When take off stack add to backwards order k,d,e,g,l,f j

152 152 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l When take off stack add to backwards order j,k,d,e,g,l,f

153 153 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order i,j,k,d,e,g,l,f

154 154 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS b l When take off stack add to backwards order c i,j,k,d,e,g,l,f

155 155 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS b l When take off stack add to backwards order c,i,j,k,d,e,g,l,f

156 156 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order b,c,i,j,k,d,e,g,l,f

157 157 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS a l When take off stack add to backwards order h b,c,i,j,k,d,e,g,l,f

158 158 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS a l When take off stack add to backwards order h,b,c, i,j,k,d,e,g,l,f

159 159 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order a,h,b,c,i,j,k,d,e,g,l,f done

160 160 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 …

161 161 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 …

162 162 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. done

163 163 End

164 164 Breadth First Search & Algorithm

165 165 BFS s a c h k f i l m j e b g d Found Not Handled Queue

166 166 BFS s a c h k f i l m j e b g d Found Not Handled Queue s

167 167 BFS s a c h k f i l m j e b g d Found Not Handled Queue a b g d

168 168 BFS s a c h k f i l m j e b g d Found Not Handled Queue b g d c f

169 169 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

170 170 BFS s a c h k f i l m j e b g d Found Not Handled Queue b c f m e j

171 171 BFS s a c h k f i l m j e b g d Found Not Handled Queue c f m e j

172 172 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

173 173 BFS s a c h k f i l m j e b g d Found Not Handled Queue m e j h i

174 174 BFS s a c h k f i l m j e b g d Found Not Handled Queue e j h i l

175 175 BFS s a c h k f i l m j e b g d Found Not Handled Queue j h i l

176 176 BFS s a c h k f i l m j e b g d Found Not Handled Queue h i l

177 177 BFS s a c h k f i l m j e b g d Found Not Handled Queue i l k

178 178 BFS s a c h k f i l m j e b g d Found Not Handled Queue l k

179 179 BFS s a c h k f i l m j e b g d Found Not Handled Queue k

180 180 BFS s a c h k f i l m j e b g d Found Not Handled Queue

181 181 Dijkstra's Shortest-Weighted Paths s u v 100 1 1 1 1 w r Length of shortest path from s to u? 4

182 182 So far, the nodes have been found in order of length from s. BFS s u v 100 1 1 1 1 w r Which node is found first? Is the same true for Dijkstra's Alg?

183 183 So far, the nodes have been found in order of length from s. s u v 100 1 1 1 1 w r It has the longest path from s. Which node is found first? Is the same true for Dijkstra's Alg? BFS

184 184 So far, the nodes have been found in order of length from s. s u v 100 1 1 1 1 w r In what order do we handle the foundNotHandled nodes? handled Dijkstra's Handle node that “seems” to be closest to s.

185 185 To prove path is shortest: Prove there is a path of this length. Prove there are no shorter paths. Give a path (witness) Dijkstra's So far, the nodes have been handled in order of length from s. :Finds a shortest weighted path from s to each node v and its length. Desired

186 186 To prove path is shortest: Prove there is a path of this length. Prove there are no shorter paths. Dijkstra's When we handle v, we know there isn't a shorter path to it because ? Otherwise, we would have handled it already. :Finds a shortest weighted path from s to each node v and its length. So far, the nodes have been handled in order of length from s.

187 187 Dijkstra's Handle node that “seems” to be closest to s. Need to keep approximate shortest distances. Path that we have “seen so far” will be called handled paths. Let d(v) the length of the shortest such path to v. Basic Steps: u v s Which is further?

188 188 Basic Steps: u The shortest of handled paths to u has length d(u) Dijkstra's & there is an edge from u to v w<u,v>w<u,v> v s The shortest of handled paths to v has length d(v) The shortest known path to v has length min( d(v), d(u)+w ). Updating d(u).

189 189 Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(v) is the length of the shortest handled path to v.

190 190 Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(u) is the length of the shortest handled path to u. d(u)d(u)

191 191 Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(u)d(u) d(u) is the length of the shortest handled path to u. First not handled node in path.

192 192 Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(u)d(u) First not handled node in path. d(u’) d(u) & d(u’) are the length of the shortest handled paths to u and u’.

193 193 Dijkstra's d(v) is the length of the shortest handled path to v. For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). d(v) is the length of the shortest handled path to v. d(u)d(u) d(v)d(v) w d(v) = min( d(v), d(u)+w ).


Download ppt "1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First."

Similar presentations


Ads by Google