# 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 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 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 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 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 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 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 s a c h k f i l m j e b g d Graph Search

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

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

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

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 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 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 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 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 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 Breadth First Search & Algorithm

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

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

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 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 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 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 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 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 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 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 BFS s a c h k f i l m j e b g d Found Not Handled Queue j h i l

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

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

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

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

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

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 BFS s a c h k f i l m j e b g d Found Not Handled Queue

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

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 BFS Data structure for storing tree: For each node v, store (v) to be parent of v.

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

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 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 Dijkstra's Shortest-Weighted Paths s u v 100 1 1 1 1 w r Length of shortest path from s to u? 4

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

70 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

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

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

76 …

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

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

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

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 Exit

82 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=0d=0 30 1 2 3 3

83 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=10 d=0d=0 d=40 d= d=30 30 1 15 2 2 3 d= 3

84 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=10 d=0d=0 d=31 d= d=30 30 15 2 2 3 3

85 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=10 d=0d=0 d=31 d=4d=4 d=18 30 15 2 2 3 3

86 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=10 d= d=10 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

87 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=7d=7 d=10 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

88 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=7d=7 d=9d=9 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

89 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=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

90 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=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

91 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=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

92 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=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

93 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=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

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 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 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 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 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 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 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 Dijkstra's

102 Dijkstra's

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 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 DFS s a c h k f i l m j e b g d Found Not Handled Stack

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

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

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

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

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

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1

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 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack

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 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,2

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 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 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3

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

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

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 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,1

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 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,3

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

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

154

155 Recursive Depth First Search Get help from friends

156

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

158 Linear Order underwear pants socks shoes ?

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

Similar presentations