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 Dont 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 cant 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 in order found. Breadth-First Search Stack: Handle most recently found Depth-First Search Priority Queue: Handle node that seems to be closest to s. Shortest (Weighted) Paths:

22 22 Breadth First Search & Algorithm

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

39 39 BFS What order are the nodes found? i.e. Breadth First Search Algorithm So far, the nodes have been found in order of length from s.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

60 60 BFS What order are the nodes found? 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. To prove path is shortest: Prove there is a path of this length. Prove there are no shorter paths. Give a path (witness)

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

62 62 BFS What order are the nodes found? 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 ? Prove there are no shorter paths. Otherwise, we would have found it already.

63 63 BFS Data structure for storing tree: For each node v, store (v) to be parent of v.

64 64 Basic Steps: s u Path to u v & there is an edge from u to v Parent of v is (v) (v) = u. BFS

65 65

66 66 Graph Search Handle some foundNotHandled node Queue: Handle in order found. Breadth-First Search Stack: Handle most recently found Depth-First Search Priority Queue: Handle node that seems to be closest to s. Shortest (Weighted) Paths:

67 67 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.

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

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

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

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

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

73 73 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.

74 74 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?

75 75 Basic Steps: u The shortest of handled paths to u has length d(u) Dijkstra's & there is an edge from u to v ww 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).

76 76 …

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

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

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

80 80 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)= Definition of handled paths and d(v)

81 81 Exit

82 82 s c b Dijkstra's a d f ij h e g d= d=0d=

83 83 s c b Dijkstra's a d f ij h e g d=1d=1 d= d=10 d=0d=0 d=40 d= d= d= 3

84 84 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=10 d=0d=0 d=31 d= d=

85 85 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=10 d=0d=0 d=31 d=4d=4 d=

86 86 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=10 d= d=10 d=0d=0 d=6d=6 d=4d=4 d=5d=

87 87 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=10 d=0d=0 d=6d=6 d=4d=4 d=5d=

88 88 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=9d=9 d=0d=0 d=6d=6 d=4d=4 d=5d=

89 89 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=15 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=

90 90 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=

91 91 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=

92 92 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=

93 93 s c b Dijkstra's a d f ij h e g d=1d=1 d=3d=3 d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d= DONE 3

94 94 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.

95 95 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)

96 96 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.

97 97 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) & d(u) are the length of the shortest handled paths to u and u.

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

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

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

101 101 Dijkstra's

102 102 Dijkstra's

103 103 Graph Search Handle some foundNotHandled node Queue: Handle in order found. Breadth-First Search Stack: Handle most recently found Depth-First Search Priority Queue: Handle node that seems to be closest to s. Shortest (Weighted) Paths:

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

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

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

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

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

109 109 DFS

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

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

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

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

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,0

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,1 h,0

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,1 h,1 k,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,1 h,1 Tree Edge Path on Stack

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,1

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 i,0

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,2 i,1 Cross Edge to handled node

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 c,2 i,2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

154 154

155 155 Recursive Depth First Search Get help from friends

156 156

157 157 Linear Order underwear pants socks shoes underwear pants socks shoes socks underwear pants shoes

158 158 Linear Order underwear pants socks shoes ?

159 159 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 ?

160 160 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) (n 2 ) Algorithm: Find a sink. Put it last in order. Delete & Repeat ….. l

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

180 180 End


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