Lecture 3: Graph Search Techniques

Lecture 3:
Graph Search
Techniques
(Reading: AM&O Section 3.4)
The (s, t)-Path Problem
Input: directed network G = (N, A), source
node s, sink node t
Output: path from s to t (if there is one)
A labeling scheme to find an (s, t)-path: we
will give a label to every node in G that
can be reached from s by a directed paths.
This labeling must satisfy
1. s will be labeled
2. If v is labeled, w is unlabeled, and
(v, w) is an arc of G, then w will be
labeled.
We call an arc (v, w) satisfying (2) admissible
A First Implementation of the
Labeling Scheme
FINDPATH0:
set Label(s) = TRUE, Label(v) = FALSE for v ̸= s
while Label()=TRUE for at least one new node
begin
for each arc (v, w) of G
if (v, w) is admissible then set Label(w)=TRUE
end
if Label(t)=TRUE then return PATH FOUND
else return NO PATH FOUND
Example Network
t
Input file is available in the Network Archives under search example.net.
For the various searches: See the Search Animation in the Lectures list on the course website.
Finding the Path
In order to actually produce the path, we add
another function, Pred(v) which, for any
labeled node v ̸= s, tells through what arc
v was labeled. We modify FINDPATH0 by
adding after setting Label(v)=TRUE the
statement
set Pred(v)=(v, w)
To recover the path (if there is one), we construct it backwards from t by using the
statements:
set v1 = t, i = 1
while vi ̸= s {vi+1 = tail of Pred(vi), i = i+1}
return vi, vi−1, . . . , v1
The set of arcs P red(v) for all labeled nodes
v ̸= s forms a directed tree rooted at s,
called a search tree. The form of the
search tree will depend upon the order in
which admissible arcs are processed.
Complexity of PATHFIND0
Complexity is O(nm), since
(a) The number of while loops is at most n,
(b) At most m arcs are scanned during each
while loop
(c) The path can be recovered in O(n) steps
FINDPATH0 is also Ω(nm) (exercise)
The label-scan technique for graph search:
LIST = list of labeled, unscanned nodes
CurrentArc(v) = current arc in A(v) being
considered
Pred(v) = previous arc to v on path from s.
We set Pred(s) = 0 and Pred(v) = −1
if x has not been labeled. Thus we can
dispense with Label(v).
A Better (in fact Best) Implementation
FINDPATH:
initialize: construct the forward-star
structure
set LIST = {s}
set Pred(s) = 0, Pred(v) = −1 for all v ̸= s
set CurrentArc(v) = First(v), for all v ∈ N
while LIST ̸= ∅ do
select a node v from LIST
comment: scan A(v)
if CurrentArc(v) ̸= 0 then
let (v, w) = Currentarc(v)
if CurrentArc(v) is admissible then
set Pred(w) = (v, w)
add node w to LIST
end if
CurrentArc(v) = Next(CurrentArc(v))
else remove node v from LIST
comment: v has been scanned
end if
end while
if Pred(t) ̸= −1 then return PATH FOUND
(path can be returned using Pred() )
else return NO (s, t)-PATH FOUND
Complexity of FINDPATH
(a) The number of steps to initialize the scan
of a node and to return the path is O(n),
(b) Now at most m arcs are scanned during
the entire course of the procedure.
Thus the total complexity of FINDPATH is
O(n + m) (linear time)
Scanning Orders
As it stands now, FINDPATH does not specify in what order nodes are to be chosen
from LIST. In many cases the order nodes
are scanned has a critical effect on how
the algorithm functions.
Two scanning orders:
depth-first search: LIST is processed in LIFO
order (as a stack)
breadth-first search: LIST is processed in FIFO
order (as a queue)
(Refer to Search Animation slides)
(s, t)-Cuts
It is easy to demonstrate that there exists
an (s, t)-path, by simply giving such a path.
This is called a certificate of the correctness of the “yes” answer, which can be
checked in O(n) time. Is there a similar
certificate for the fact that there is no
(s, t)-path?
cut: set (X, X̄) of arcs of G, where ∅ ̸= X =
̸ N
is a set of nodes of G and X̄ = N \ X,
defined
(X, X̄) = {(v, w) ∈ A : v ∈ X, w ∈ X̄}
(X, X̄) is an (s, t)-cut if s ∈ X and t ∈ X̄.
(Refer to Search Animation slides)
Duality Lemma for (s, t)-Paths: For any directed graph G and nodes s and t, there
exists an (s, t)-path in G if and only if
there does not exist an (s, t)-cut containing no arcs.
Proof: The necessity of the lemma is clear,
since any (s, t)-path starts at x ∈ X and
end at t ∈ X̄, and so at least one arc of
the path must pass from X to X̄, contradicting the fact that (X, X̄) is empty.
The sufficiency of the lemma can be obtained by using FINDPATH. Suppose the
algorithm fails to label t. Let X be the
set of nodes v with Pred(v) ≥ 0. Then
by the labeling scheme, s ∈ X, t ∈ X̄, and
there are no arcs in (X, X̄).
The Archive Procedure
DisplayPathCut
Input: Directed source-sink graph G with node
labels G.node(i).pred.
Output: A printout on the command line of
either
If Pred(t) ̸= −1: The nodes of an (s, t)path,
If Pred(t) = −1: The (s, t) cut (X, X̄)
from nodes i with Pred(i) ̸= −1 to
nodes j with Pred(j) = −1
An error message if any nodes are mislabeled.
Duality and Acyclic Graphs
acyclic graph: graph containing no (directed)
cycles.
topological ordering: numbering Order(v) of
the nodes of G so that for any arc (v, w),
Order(v) <Order(w)
Duality Lemma for Acyclic Graphs: A graph
G is acyclic if and only if it admits a topological ordering.
One way to find topological orderings:
for (i=n,n-1,. . . ,1)
find a node v having empty forward star
(if none, then there must exist a cycle)
set Order(v) = i and delete v from G
Using FINDPATH to Find Cycles
Assume that there are paths from s to all
other nodes.
search model: depth-first search
modification of FINDPATH: When
CurrArc(v)=(v, w) is chosen, check and
see if w is on LIST (labeled but not scanned).
If so return CYCLE FOUND; the cycle
obtained by adding (v, w) to the current
search tree will be directed.
dual object: numerical labels on the nodes,
with Order(v) representing the topological ordering
finding dual object:
(Nodes are numbered backwards.)
Start with CurrOrder = n, and at the
point v is removed from LIST set Order(v)
= CurrOrder and decrement CurrOrder.
(See Animation slides.)
Proof of Correctness: Exercise
Shortest Cardinality Paths
Suppose we want to determine the (s, t)-path
in G with the fewest number of arcs.
search model: breadth-first search
modification of FINDPATH: no modification.
dual object: numerical labels on the nodes,
with d(v) = the number of arcs in a shortest (cardinality) (s, v)-path.
finding dual object: Initialize d(s) = 0, and
at the point w is labeled from v, set d(w)
= d(v)+1.
Proof of Correctness: (To be proven later.)
(Refer to Search Animation slides)
Bipartite Graphs
bipartite graph: Undirected graph for which
the nodes can be partitioned into sets S
and T such that A ⊆ S × T , i.e., all arcs
have an endpoint each of S and T
Duality Lemma for Bipartite Graphs: A graph
G is bipartite if and only if there does not
exist an odd cardinality cycle in G.
Using FINDPATH to
Determine Bipartiteness
We assume that G is connected.
search model: Any LIST discipline will work.
modification of FINDPATH: Give the labels
ODD or EVEN to the nodes of G. Start
with any node s, and set Label(s) = EVEN.
Now when CurrArc(v)=(v, w) is chosen,
check and see if Label(v)=Label(w). If
so, return NOT BIPARTITE. Otherwise,
set Label(w) to the opposite label as Label(v).
dual object: odd cycle.
finding dual object: When (v, w) above is found
with Label(v)=Label(w), then follow Pred()
backwards from v and w until a common
node is found. The union of the two portions of the paths found, plus (v, w), constitutes an odd cycle.
Example
Using breadth-first search from s = 1. Green
are even labels, red are odd labels.
2
8
4
5
3
7
6
9
10
1
A Bipartite Graph
2
5
3
8
4
7
6
9
10
1
A Nonbipartite Graph