SlideShare a Scribd company logo
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

More Related Content

Viewers also liked

Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationzukun
 
Practica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias HumanasPractica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias HumanasNanettt
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
Ankita Karia
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 

Viewers also liked (13)

Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notation
 
Practica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias HumanasPractica - Organización de la Facultad de Letras y Ciencias Humanas
Practica - Organización de la Facultad de Letras y Ciencias Humanas
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
lecture 5
lecture 5lecture 5
lecture 5
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
4 recursion
4 recursion4 recursion
4 recursion
 

Similar to 5 searching

Link Prediction in the Real World
Link Prediction in the Real WorldLink Prediction in the Real World
Link Prediction in the Real World
Balaji Ganesan
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
iftakhar8
 
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
Dr. C.V. Suresh Babu
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
SigSegVSquad
 
Graph 3
Graph 3Graph 3
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
KarunaBiswas3
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
chandrashekarr799
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
artificial intelligence
artificial intelligence artificial intelligence
artificial intelligence
ilias ahmed
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Shortest path
Shortest pathShortest path
Shortest path
Ruchika Sinha
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
Umme habiba
 
informed_search.pdf
informed_search.pdfinformed_search.pdf
informed_search.pdf
SankarTerli
 
Data structure
Data structureData structure
Data structure
lalithambiga kamaraj
 
dijkstraC.ppt
dijkstraC.pptdijkstraC.ppt
dijkstraC.ppt
ManoRanjani30
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
HODElex
 

Similar to 5 searching (20)

Link Prediction in the Real World
Link Prediction in the Real WorldLink Prediction in the Real World
Link Prediction in the Real World
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Graph 3
Graph 3Graph 3
Graph 3
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
artificial intelligence
artificial intelligence artificial intelligence
artificial intelligence
 
Unit ii-ppt
Unit ii-pptUnit ii-ppt
Unit ii-ppt
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Shortest path
Shortest pathShortest path
Shortest path
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
informed_search.pdf
informed_search.pdfinformed_search.pdf
informed_search.pdf
 
Data structure
Data structureData structure
Data structure
 
dijkstraC.ppt
dijkstraC.pptdijkstraC.ppt
dijkstraC.ppt
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 

Recently uploaded

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 

Recently uploaded (20)

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 

5 searching