Depth-First and Breadth-First Search - Combinatorial Computing | MAT 3770, Study notes of Mathematics

Material Type: Notes; Professor: Cleave; Class: Combinatorial Computing; Subject: Mathematics; University: Eastern Illinois University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-rfc-1
koofers-user-rfc-1 🇺🇸

4

(1)

10 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mat 3770
Week 8
Spring 2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Depth-First and Breadth-First Search - Combinatorial Computing | MAT 3770 and more Study notes Mathematics in PDF only on Docsity!

  • Mat - Week
    • Spring

Section 3.2 DepthñFirst and BreadthñFirst Search

I (^) Tree-based searches abound in applications, and are amenable to computerized solutions.

I (^) Depending upon the application, we may wish to nd

  1. one solution
  2. all solutions, or
  3. an optimal solution

I (^) To organize the enumeration of possible solutions:

  1. let the sequential choices be internal nodes in a rooted tree
  2. the solutions and ìdead endsî be the leaves

I (^) The big challenge is to be sure to check that all possible ways to generate a solution are investigated: that the enumeration of possibilities is complete.

Example: DFS(G, f)

I (^) numbers will indicate visitation order

I (^) dotted lines will represent unmarked edges

I (^) use alphabetic order to choose among next nodes to visit

a

b

c

d f^

i

g

h

DFS Algorithm

Given : An undirected graph G = (V , E ), and a vertex v ∈ V Goal : Visit all vertices and edges of G starting with v.

Procedure DFS (G, v)

// PRE: G is connected

// POST: every vertex and edge of G

// is visited

begin

Mark (v)

Visit (v) // do application processing

for all unmarked edges <v, w> do

if w is unmarked then

MarkEdge (v, w)

DFS (G, w)

VisitEdge (v, w) // process if necessary

end

Proof:

I (^) Recall:

deg(v) = 2jE j, i.e., the sum of the degrees of all nodes is twice the number of edges.

I (^) For any v, DFS(G , v) is called exactly once ó when v is unmarked.

I (^) So, if < v, w > is an edge, it's visited and marked at that time by DFS(G , v) or DFS(G , w), whichever node is visited rst.

I Note also, this means the FOR loop is executed a total

of 2jE j times over all calls of DFS.

I (^) As for marked edges: Claim : DFS(G , v) marks edges which form a tree with root v. [Proof by induction on number of unmarked vertices in G .]

Lemma

If T is the tree formed by DFS (G , v), then for each edge e ∈ EG , either I (^) e ∈ T , or I (^) e connects a vertex in T to some ancestor or descendent in T. I.e., there are no cross–edges in T.

Proof. Let e = < u, W >. if u is marked rst, then w is marked during DFS (G , u). So, by the previous lemma (vertices are visited only once), w is a descendent of u.

DFS Drawback

I (^) Suppose we don't want to visit all the vertices ó we merely want to nd one with a certain property: i.e., we want to be able to stop.

I (^) Idea : nonñrecursive version of DFS using a stack.

I (^) We'll assume we're not interesting in visiting every edge.

Nonñrecursive DFS Algorithm

Procedure DFS2 (G, v)

// PRE: G is connected or we only visit

// subgraph in which v is found

// POST: every vertex of G is visited

begin

CreateStack(S)

Mark(v)

Push(S, v)

while not Empty(S)

x = Pop(S)

while x has unvisited neighbor (w)

Mark(w)

Push(S, w)

end

DFS in DiGraphs

Works the same as for undirected graphs, but there are differences with respect to the DFS tree ó it may not be a tree, but a forest.

There are four possible types of edges in the DFS forest:

I (^) the tree edges: of the DFS tree

I (^) back edges: edges to ancestors in the tree

I (^) forward edges: edges to descendents in the tree

I (^) cross edges: all others

DiGraph Example: DFS(G, f)

a

b

c

d f^

i

g

h

Lemma

Suppose there is a directed path v 1 ; v 2 ; : : : ; vn in G such that v 1 has lowest DFSnumber in fv 1 ; : : : ; vn g. Then, ∀ 1 < i  n, vi is a descendent of v 1.

Proof:

Note v 1 and all descendents have DFSnumbers in the interval [a..b], and DFSnumber(v 1 ) = a.

Proof will be by induction on the number of nodes in the path.

BC Let n = 2, and v 1 → v 2. By the last lemma, v 2 is a descendent of v 1.

IH Assume for directed paths with n 1 nodes where v 1 has lowest DFSnumber, that v 2 ; : : : ; vn 1 are descendents of v 1.

IS Show lemma is true for paths with n nodes. If vn is not a descendent of v 1 , and the DFSnumber(v 1 ) > DFSnumber(vn ), then DFSnumber(vn ) >DFSnumber(vn 1 ) since vn 1 is closer to v 1 in the path (and hence will be processed before vn ).

Application: Acyclic Digraphs

I (^) Suppose a program consists of many procedures.

I (^) Let us say procedure X depends on Procedure Y if there are procedures X = X 1 , X 2 ,... , Xn = Y such that Xi calls Xi + 1 ∀ i < n.

I (^) Question: Does any procedure depend on itself?

I (^) Why care?

I (^) As programmers, this is a sort of recursion I (^) As compiler writers, we can allocate one big activation record for all the procedures if none depend on themselves.

Directed Cycles and Backedges

I (^) Consider procedures as vertices of a directed graph, and X → X′^ if X calls X′

I (^) We want to know: does G contain any directed cycles?

I (^) Theorem. let G = (V, E) be a directed graph, and let T be a DFS tree (forest) of G. Then G contains a directed cycle IFF T contains a back edge.