1 searching in a graph jeff edmonds york university cosc 3101 lecture 5 generic search breadth first...
TRANSCRIPT
1
Searching in a Graph
Jeff EdmondsYork University COSC 3101LectureLecture 55
Generic SearchBreadth First SearchDijkstra's Shortest Paths AlgorithmDepth First SearchLinear 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•<preCond>: The input is a graph G (either directed or undirected) and a source node s. •<postCond>: Output all the nodes u that are reachable by a path in G from s.
4
Graph Search
Basic Steps:
su
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
fuj reachable
dvw reachable
ehd reachable
btd
vw
ehd
bt
•How do we keep track of all of this information?
•How do we avoid cycling?
6
s
a
c
hk
fi
lm
j
eb
gd
Graph Search
7
Graph Search
s
a
c
hk
fi
lm
j
eb
gd
8
Graph Search
s
a
c
hk
fi
lm
j
eb
gd
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
hk
fi
lm
j
eb
gd
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 neighborsDon’t re-find a node.
s
a
c
hk
fi
lm
j
eb
gd
Handle some foundNotHandled node
11
s
a
c
hk
fi
lm
j
eb
gd
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 nodei.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.measureprogress
# of handled nodes.Might not increase.
s
a
c
hk
fi
lm
j
eb
gd
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 foundNotHandledOther nodes notFound
s
a
c
hk
fi
lm
j
eb
gd
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
hk
fi
lm
j
eb
gd
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
hk
fi
m
j
eb
gd
lExit All found nodes are handled.
Exit
•<postCond>: 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
hk
fi
m
j
eb
gd
lAll found nodes are handled.
Exit Found nodes are reachable from s. Reachable nodes have been found.
•<postCond>:
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
hk
fi
m
j
eb
gd
lAll found nodes are handled.
Exit Reachable nodes have been Found. •<postCond>:
Found = handled
handled
notfound
[A B] = [B A]notFound nodes not reachable.
19
Graph Search
Specification of Reachability-from-single-source s•<preCond>: The input is a graph G (either directed or undirected) and a source node s. •<postCond>: 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
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
20
Graph Search
# of handled nodes.
Handle some foundNotHandled node
Time = O(n)
fuj
O(n) iterations, but iteration takes more than O(1)
O(n) neighbors= O(n2)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
<preCond> & <postCond>
Algorithm
23
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
24
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
s
25
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
a
bgd
26
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
bgd
cf
27
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
bg
cfme
28
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
bcfmej
29
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
cfmej
30
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
fmejh
i
31
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
mejh
i
32
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
ejh
il
33
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
jh
il
34
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
h
il
35
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
ilk
36
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
lk
37
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
k
38
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot 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
gd
FoundNot Handled
Queue
41
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
s
d=0
d=0
42
BFS FoundNot Handled
Queue
d=0a
bgd
d=1
s
a
c
h
k
f
i
l
m
j
e
b
gd
d=0d=1
43
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
a
bgd
d=0d=1
d=1
44
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
bgd
cf
d=0d=1
d=2
d=1
d=2
45
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
bg
cfme
d=0d=1
d=2
d=1
d=2
46
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queued=0
d=1
d=2
b
j
cfme
d=1
d=2
47
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queued=0
d=1
d=2
j
cfme
d=1
d=2
48
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
cfmej
d=0d=1
d=2
d=2
49
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
fmejh
i
d=0d=1
d=2
d=3
d=2
d=3
50
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
mejh
i
d=0d=1
d=2
d=3
d=2
d=3
51
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
ejh
il
d=0d=1
d=2
d=3
d=2
d=3
52
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
jh
il
d=0d=1
d=2
d=3
d=2
d=3
53
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
h
il
d=0d=1
d=2
d=3
d=2
d=3
54
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
h
d=0d=1
d=2
d=3
il
d=3
55
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
ilk
d=0d=1
d=2
d=3d=4
d=3
d=4
56
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
lk
d=0d=1
d=2
d=3d=4
d=3
d=4
57
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
k
d=0d=1
d=2
d=3d=4
d=3
d=4
58
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queue
k
d=0d=1
d=2
d=3d=4
d=4
59
BFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Queued=0
d=1
d=2
d=3d=4
d=4d=5
60
BFS
What order are the nodes found?
So far, the nodes have been found in order of length from s.
<postCond>: Finds a shortest path from sto 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:
su
The shortest path to uhas 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.
<postCond>: Finds a shortest path from sto 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
BFSData structure for storing tree: For each node v, store (v) to be parent of v.
64
Basic Steps:
su
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
•<preCond>: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. •<postCond>: Finds a shortest weighted path from s to each node v and its length.
68
Dijkstra's Shortest-Weighted Paths
s
u
v
100
11
1
1w
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
11
1
1w
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
11
1
1w
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
11
1
1w
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'sSo far, the nodes have been handled in order of length from s.
<postCond>: 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.
<postCond>: 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 calledhandled paths.•Let d(v) the length of the shortest such path to v.
Basic Steps:
uvs
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>
vs
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<u,v> ).
Updating d(u).
76
…
77
Dijkstra's
uvs
Handled nodes Found nodes
Handled Edges?
Definition of handled paths and d(v)
78
Dijkstra's
uvs
Handled nodes Found nodes
Handled Edges
Handled Paths?
Definition of handled paths and d(v)
79
Dijkstra's
uvs
Handled nodes Found nodes
Definition of handled paths and d(v)
Handled Edges
Handled Paths?
80
Dijkstra's
uvs
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
i j
h
e
g
40
110
2
15
1
2
6
8
1
230
3
d=
d=
d= d=
d=
d=
d=
d=0
d=
d=
d=
30
12
3
3
83
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1 6
8
1
230
3
d=1
d= d=
d=
d=
d=10
d=0
d=40
d=
d=30
30
1152
2
3
d=
3
84
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=
d=
d=10
d=0
d=31
d=
d=30
30
152
2
3
3
85
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=
d=
d=10
d=0
d=31
d=4
d=18
30
152
2
3
3
86
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=10
d=
d=10
d=0
d=6
d=4
d=5
30
1152
3
3
87
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=
d=10
d=0
d=6
d=4
d=5
30
1152
3
3
88
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=
d=9
d=0
d=6
d=4
d=5
30
1152
3
3
89
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=15
d=8
d=0
d=6
d=4
d=5
30
1152
3
3
90
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=10
d=8
d=0
d=6
d=4
d=5
30
1152
3
3
91
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=10
d=8
d=0
d=6
d=4
d=5
30
1152
3
3
92
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=10
d=8
d=0
d=6
d=4
d=5
30
1152
3
3
93
s
c
b
Dijkstra's
a
d
f
i j
h
e
g
40
110
2
1
1
6
8
1
230
3
d=1
d=3
d= d=
d=7
d=10
d=8
d=0
d=6
d=4
d=5
30
1152
3
DONE3
94
Dijkstra'sFor 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'sFor 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)
96
Dijkstra'sFor 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) is the length of the shortest handled
path to u.
First not handled node in path.
97
Dijkstra'sFor 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)
First not handled node in path.d(u’)
d(u) & d(u’) are the length of the shortest handled
paths to u and u’.
98
Dijkstra'sd(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(v)
w<u,v>
d(v) = min( d(v), d(u)+w<u,v> ).
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<u,v> ).
Heap
100
Dijkstra's Shortest-Weighted PathsSpecification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s
•<preCond>: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. •<postCond>: 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
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
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
gd
FoundNot Handled
Stack
106
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Stack
s
107
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Stack
a
bgd
108
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Stack
a
mgd
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
gd
FoundNot Handled
Stack <node,# edges>
112
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Stack <node,# edges>
s,0
113
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,0
114
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,0
115
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,1h,0
116
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,1h,1k,0
117
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,1h,1
Tree Edge
Path on Stack
118
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,1
119
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,0
120
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,1
Cross Edgeto handled node
121
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,2
122
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,3l,0
123
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,3l,1
124
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,3
125
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,0
126
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,1j,0
127
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,1j,1
Back Edgeto node on Stack
Forms a cycle
128
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,1j,2m,0
129
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,1j,2m,1
130
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,1j,2
131
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4g,1
132
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,4
133
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,5f,0
134
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,5f,1
135
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2i,5
136
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,2
137
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1c,3
138
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,1
139
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
a,2
140
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,1
FoundNot Handled
Stack <node,# edges>
141
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,2
FoundNot Handled
Stack <node,# edges>
d,1
142
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,2
FoundNot Handled
Stack <node,# edges>
d,2
143
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,2
FoundNot Handled
Stack <node,# edges>
d,3e,0
144
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,2
FoundNot Handled
Stack <node,# edges>
d,3e,1
145
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,2
FoundNot Handled
Stack <node,# edges>
d,3
146
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,2
FoundNot Handled
Stack <node,# edges>
147
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,3
FoundNot Handled
Stack <node,# edges>
148
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,4
FoundNot Handled
Stack <node,# edges>
b,0
149
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,4
FoundNot Handled
Stack <node,# edges>
b,1
150
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,4
FoundNot Handled
Stack <node,# edges>
b,2
151
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,4
FoundNot Handled
Stack <node,# edges>
b,3
152
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
s,4
FoundNot Handled
Stack <node,# edges>
153
DFS
s
a
c
h
k
f
i
l
m
j
e
b
gd
FoundNot Handled
Stack <node,# edges>
done
154
155
RecursiveDepth First
Search
Get help from friends
156
157
Linear Order
underwear
pants
socks
shoes
underwearpantssocksshoes
socks underwearpantsshoes
158
Linear Order
underwear
pants
socks
shoes
?
159
Linear Order
a
b h
c i
d j
e k
f l
g
<preCond>: A Directed Acyclic Graph (DAG)
<postCond>: 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
<preCond>: A Directed Acyclic Graph (DAG)
<postCond>: Find one valid linear order
(n)(n2)
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
FoundNot Handled
Stack
Alg: DFS
degf
l
….. f
162
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
degl
l
When take off stackadd to backwards order
….. f
163
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
deg
l
When take off stackadd to backwards order
l,f
164
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
del
When take off stackadd to backwards order
g,l,f
165
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
dl
When take off stackadd to backwards order
e,g,l,f
166
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
l
When take off stackadd to backwards order
d,e,g,l,f
167
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
il
When take off stackadd to backwards order
d,e,g,l,f
jk
168
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
il
When take off stackadd to backwards order
k,d,e,g,l,f
j
169
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
il
When take off stackadd to backwards order
j,k,d,e,g,l,f
170
Linear Order
a
b h
c i
d j
e k
f
g
FoundNot Handled
Stack
Alg: DFS
l
When take off stackadd 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
FoundNot Handled
Stack
Alg: DFS
bl
When take off stackadd 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
FoundNot Handled
Stack
Alg: DFS
bl
When take off stackadd 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
FoundNot Handled
Stack
Alg: DFS
l
When take off stackadd 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
FoundNot Handled
Stack
Alg: DFS
al
When take off stackadd 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
FoundNot Handled
Stack
Alg: DFS
al
When take off stackadd 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
FoundNot Handled
Stack
Alg: DFS
l
When take off stackadd to backwards order
a,h,b,c,i,j,k,d,e,g,l,f done
177
Linear OrderFound
Not HandledStack
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 vv…u…
Consider each edge
v
…
u
…
178
Linear OrderFound
Not HandledStack
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 vv…u…
Consider each edge
u
…
v
…
179
Linear OrderFound
Not HandledStack
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 vu…v…
Consider each edge
u
…
v
…
The nodes in the stack form a path starting at s.
done
180
End