Searching in a Graph
Jeff Edmonds
York University COSC 3101LectureLecture 55
Generic Search
Breadth First Search
Dijkstra's Shortest Paths Algorithm
Depth First Search
Linear Order
Network Flow
Linear Programming
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.
Graph Search
Specification: Reachability-from-single-source s
•Preconditions:
The input is a graph G
(either directed or undirected)
and a source node s.
•Postconditions:
Output all the nodes u that are
reachable by a path in G from s.
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.
Graph Search
s
reachable
f
u
k reachable
d
v
w reachable
e
h
d reachable
b
t
reachable
d
v
w reachable
e
h
d reachable
b
t
•How do we keep track of
all of this information?
•How do we avoid cycling?
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Graph Search
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Graph Search
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Graph Search
s
a
c
h
k
f
i
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
l
s
a
c
h
k
f
i
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
l
Handle some foundNotHandled node
i.e. find its neighbors
Don’t refind a node.
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
s
a
c
h
k
f
i
m
j
e
b
g
d
l
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.
s
a
c
h
k
f
i
m
j
e
b
g
d
l
measure
progress
# of handled nodes.
Might not increase.
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
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Other nodes NotFound
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
a
c
h
k
f
i
m
j
e
b
g
d
l
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
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
•Postconditions:
Output all the nodes u that are
reachable by a path in G from s.
Output Found nodes
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.
•Postconditions:
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.
•Postconditions:
found = handled
handled
Notfound
[A ⇒ B] = [¬B ⇒ ¬A]
NotFound nodes not reachable.
Graph Search
Specification of Reachability-from-single-source s
•Preconditions:
The input is a graph G
(either directed or undirected)
and a source node s.
•Postconditions:
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
Graph Search
# of handled nodes.
Handle some foundNotHandled node
Time = O(n)
f
u
k
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.
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:
Breadth First Search
Pre & Post Conditions
Algorithm
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
s
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
a
b
g
d
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
b
g
d
c
f
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
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
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
c
f
m
e
j
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
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
m
e
j
h
i
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
e
j
h
i
l
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
j
h
i
l
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
h
i
l
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
i
l
k
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
l
k
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
k
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
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.
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
BFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Queue
s
d=0
d=0
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
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
d=1
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
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
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
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
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
d=2
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
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
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
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
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
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
d=3
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
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
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
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=4
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=4
d=5
BFS
What order are the nodes found?
So far, the nodes have been
found in order of length from s.
Post Conditions: 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)
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
BFS
What order are the nodes found?
So far, the nodes have been
found in order of length from s.
Post Conditions: 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.
BFS
Data structure for storing tree:
For each node v, store π(v) to be parent of v.
Basic Steps:
s
u
Path to u
v
& there is an edge
from u to v
Parent of v is
π(v)
π(v) = u.
BFS
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:
Dijkstra's Shortest-Weighted Paths
Specification: Dijkstra's Shortest-Weighted Paths
Reachability-from-single-source s
•Preconditions:
The input is a graph G
(either directed or undirected)
with positive edge weights
and a source node s.
•Postconditions:
Finds a shortest weighted path from s
to each node v and its length.
Dijkstra's Shortest-Weighted Paths
s
u
v
100
1
1
1
1
w
r
Length of shortest
path from s to u?
4
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?
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
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.
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.
Post Conditions: Finds a shortest weighted 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.
Dijkstra's
When we handle v, we know
there isn't a shorter path to it because ?
Otherwise, we would have handled it already.
Post Conditions: 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.
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> ).
Handle node that seems to be closest to s.
Need to keep approximate shortest distances.
…
Dijkstra's
The shortest of handled paths
to v has length d(v)
u
vs
Dijkstra's
u
vs
Handled nodes Found nodes
Handled Edges?
The shortest of handled paths
to v has length d(v)
Dijkstra's
u
vs
Handled Nodes Found Nodes
Handled Edges
The shortest of handled paths
to v has length d(v)
Handled Paths?
Dijkstra's
u
vs
Handled Nodes Found Nodes
Handled Edges
The shortest of handled paths
to v has length d(v)
Handled Paths
Dijkstra's
u
vs
Handled Nodes
The shortest of handled paths
to v has length d(v)
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’)= ∞
Exit
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.
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)
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) is the length
of the shortest handled
path to u.
First not handled
node in path.
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)
First not handled
node in path.d(u’)
d(u) & d(u’) are the length
of the shortest handled
paths to u and u’.
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(v)
w<u,v>
d(v) = min( d(v), d(u)+w<u,v> ).
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
Dijkstra's Shortest-Weighted Paths
Specification: Dijkstra's Shortest-Weighted Paths
Reachability-from-single-source s
•Preconditions:
The input is a graph G
(either directed or undirected)
with positive edge weights
and a source node s.
•Postconditions:
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
Dijkstra's
Dijkstra's
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:
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
s
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
a
b
g
d
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
a
m
g
d
e
DFS
DFS
The nodes in the stack form a path starting at s.
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
<node,# edges>
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
<node,# edges>
s,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,1
h,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,1
h,1
k,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,1
h,1
Tree Edge
Path on Stack
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,1
Cross Edge
to handled node
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,2
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,3
l,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,3
l,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,3
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,1
j,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,1
j,1
Back Edge
to node on Stack
Forms a cycle
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,1
j,2
m,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,1
j,2
m,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,1
j,2
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
g,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,4
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,5
f,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,5
f,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
i,5
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,2
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
c,3
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
a,2
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,1
Found
Not Handled
Stack
<node,# edges>
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,2
Found
Not Handled
Stack
<node,# edges>
d,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,2
Found
Not Handled
Stack
<node,# edges>
d,2
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,2
Found
Not Handled
Stack
<node,# edges>
d,3
e,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,2
Found
Not Handled
Stack
<node,# edges>
d,3
e,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,2
Found
Not Handled
Stack
<node,# edges>
d,3
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,2
Found
Not Handled
Stack
<node,# edges>
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,3
Found
Not Handled
Stack
<node,# edges>
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,4
Found
Not Handled
Stack
<node,# edges>
b,0
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,4
Found
Not Handled
Stack
<node,# edges>
b,1
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,4
Found
Not Handled
Stack
<node,# edges>
b,2
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,4
Found
Not Handled
Stack
<node,# edges>
b,3
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
s,4
Found
Not Handled
Stack
<node,# edges>
DFS
s
a
c
h
k
f
i
l
m
j
e
b
g
d
Found
Not Handled
Stack
<node,# edges>
Linear Order
Ingredients:
•Instances: The possible inputs to the problem.
•Solutions for Instance: Each instance has an
exponentially large set of solutions.
•Cost of Solution: Each solution has an easy to
compute cost or value.
Specification
•Preconditions: The input is one instance.
•Postconditions: An valid solution with optimal
cost. (minimum or maximum)
Optimization Problems
Specification: Network Flow
•Preconditions:
The input is a network G
directed edges with capacities,
a source node s, and a sink node t.
•Postconditions:
Finds a Maximum Flow F.
Network Flow
Network Flow
No leaks, not extra flow
Value of flow
= flow out of s
= flow into t
= flow across any cut
Network Flow
Network Flow
•Given Flow F
•Construct Augmenting Graph GF
•Find path P using BFS, DFS,
or generic search algorithm
•Let w be the max amount flow
can increase along path P.
•Increase flow along path P by w.
i.e newF = oldF + w × P
Network Flow
Stop
Hill Climbing
We have a valid solution.
(not necessarily optimal)
Take a step that goes up.
measure
progress
Value of our solution.
Problems:
Exit
Cant take a step that goes up.
Running time?
Initially have the “zero” solution
Local Max
Global Max
Can our Network Flow
Algorithm get stuck
in a local maximum?
Make small local changes to your solution to
construct a slightly better solution.
Network Flow
Previous Input
Previous Output
Same Input
Yes! Our Network Flow
Algorithm can get stuck.
Worse Output
Need only one example.
Network Flow
Network Flow
Network Flow
Network Flow
Network Flow
Previous Input
Previous Output
Same Input
Yes! Our Network Flow
Algorithm can get stuck.
Worse Output
Need only one example.
Hill Climbing
Avoiding getting stuck
in a local maximum
Good ExecutionBad Execution
•Made better choices of direction
•Hard
•Back up an retry
•Exponential time
•Define a bigger step
Hill Climbing
Different
Solutions
Current
Solution
Alg defines
to where alg can step
i.e. what small local
changes can be made to
current solution
This defines
the topography
Hill Climbing
Different
Solutions
Current
Solution
Define a slightly
bigger step
This defines
the topography
Perhaps removes some local maxima
Network Flow
Previous Input
Previous Output
Same Input
Yes! Our Network Flow
Algorithm can get stuck.
Worse Output
Need only one example.
Mistake?
Putting 2 through
this edge
Network Flow
u v
0/75
0/10
Flow Graph
u v
75
10
Augmentation Graph
75-21=54
21+10=31
u v
21/75
10
Flow Graph
u v
Augmentation Graph
2
Network Flow
Network Flow
Network Flow
Network Flow
Network Flow
Network Flow
Hill Climbing
Network Flow
Linear Programming
A Hotdog
A combination of pork,A combination of pork,
grain, and sawdust, …grain, and sawdust, …
Constraints:Constraints:
•Amount of moistureAmount of moisture
•Amount of protean,Amount of protean,
•……
The Hotdog Problem
Given today’s prices,
what is a fast algorithm
to find the cheapest
hotdog?
There are deep ideas within the simplicity.
=
AbstractionAbstraction
Goal: Understand and think about complex
things in simple ways.
There are deep ideas within the simplicity.
Rudich www.discretemath.com
Abstract Out Essential Details
Cost: 29, 8, 1, 2
Amount to add: x1, x2, x3, x4
pork
grain
water
sawdust
3x1 + 4x2 – 7x3 + 8x4 ≥ 12
2x1 - 8x2 + 4x3 - 3x4 ≥ 24
-8x1 + 2x2 – 3x3 - 9x4 ≥ 8
x1 + 2x2 + 9x3 - 3x4 ≥ 31
Constraints:Constraints:
•moisturemoisture
•protean,protean,
•……
29x1 + 8x2 + 1x3 + 2x4Cost of Hotdog:Cost of Hotdog:
3x1 + 4x2 – 7x3 + 8x4 ≥ 12
2x1 - 8x2 + 4x3 - 3x4 ≥ 24
-8x1 + 2x2 – 3x3 - 9x4 ≥ 8
x1 + 2x2 + 9x3 - 3x4 ≥ 31
29x1 + 8x2 + 1x3 + 2x4
Subject to:Subject to:
Minimize:Minimize:
Abstract Out Essential Details
A Fast Algorithm
For decades people thought
that there was no fast
algorithm.
Then one was found!
Theoretical Computer Science
finds new algorithms every day.
3x1 + 4x2 – 7x3 + 8x4 ≥ 12
2x1 - 8x2 + 4x3 - 3x4 ≥ 24
-8x1 + 2x2 – 3x3 - 9x4 ≥ 8
x1 + 2x2 + 9x3 - 3x4 ≥ 31
29x1 + 8x2 + 1x3 + 2x4
Subject to:Subject to:
Minimize:Minimize:
≈
End

5 searching