0% found this document useful (0 votes)
34 views

Recitation 1 Graph Search: BFS and DFS: 1.1 Announcements

This document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of running BFS and DFS on graphs, tracing the process. It also analyzes the implementations of BFS and DFS, discussing their work and span in terms of the number of vertices, edges, and diameter of the graph. Key points covered include classifying DFS tree edges, computing DFS numberings, modifying DFS for undirected graphs, running BFS on an example graph, and analyzing the asymptotic time complexity of BFS.

Uploaded by

Ahmed Said
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Recitation 1 Graph Search: BFS and DFS: 1.1 Announcements

This document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of running BFS and DFS on graphs, tracing the process. It also analyzes the implementations of BFS and DFS, discussing their work and span in terms of the number of vertices, edges, and diameter of the graph. Key points covered include classifying DFS tree edges, computing DFS numberings, modifying DFS for undirected graphs, running BFS on an example graph, and analyzing the asymptotic time complexity of BFS.

Uploaded by

Ahmed Said
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Recitation 1

Graph Search: BFS and DFS

1.1 Announcements
BridgeLab has been released, and is worth 140 points. The written section is due at Friday
at 5pm, while the programming portion is due Sunday night.

ShortLab will be released on Friday.

Please fill out a midterm survey about the course so that we may improve. The link to the
survey is on Piazza.

1
2 RECITATION 1. GRAPH SEARCH: BFS AND DFS

1.2 DFS Trees and Numberings

Task 1.1. Starting at vertex 1, execute DFS on the following graph, visiting vertices in
increasing order. Trace the process by doing each of the following.

1. Draw the resulting DFS tree. Draw tree edges as solid lines, and include non tree
edges in your drawing as dashed lines.

2. Classify each non tree edge as one of forward, back, or cross.

3. Label each vertex with its discovery and finish times.

4 7 8

3 2 6

1 5 9

10

In the following diagram, back edges are red, forward edges are blue, and cross edges are green.

Vertex Discovery Finish


2 5 1 0 19
2 1 6
3 2 5
4 3 4
3 6 10 5 7 18
6 8 15
7 9 10
8 11 14
4 7 8 9 12 13
10 16 17

Built: October 23, 2017


1.2. DFS TREES AND NUMBERINGS 3

Task 1.2. Suppose DFS is run on a directed graph, and consider some edge (x, y).
Using the discovery and finish times of x and y, attempt to classify this edge as one of
tree, forward, back, or cross.

Write d[v] and f [v] for the discovery and finish time of v, respectively.

Numbering Possible Edge Type


d[x] < d[y] < f [y] < f [x] tree, forward
d[y] < d[x] < f [x] < f [y] back
d[y] < f [y] < d[x] < f [x] cross

1.2.1 Higher-Order DFS

Recall the following code from the textbook:

Algorithm 1.3. Directed, generalized DFS.


1 directedDFS (revisit,discover,finish) (G,0 ,s) =
2 let
3 DFS p ((X ,),v) =
4 if (v X) then (X ,revisit (,v ,p)) else
5 let
6 0 = discover (,v ,p)
7 X 0 = X {v}
8 (X 00 ,00 ) = iterate (DFS v) (X 0 ,0 ) (NG+ (v))
9 000 = finish (0 ,00 ,v ,p)
10 in
11 (X 00 ,000 )
12 end
13 in
14 DFS s (({},0 ),s)
15 end

Task 1.4. Define 0 , revisit, discover, and finish to calculate DFS number-
ings.

Algorithm 1.5. Time-stamping with generalized directed DFS.


1 0 = ({},{},0)
2 revisit (,_,_) =
3 discover ((D,F ,c),v ,_) = (D {v 7 c},F ,c + 1)
4 finish (_,(D,F ,c),v ,_) = (D,F {v 7 c},c + 1)

Built: October 23, 2017


4 RECITATION 1. GRAPH SEARCH: BFS AND DFS

Task 1.6. Modify the given generalized DFS code to work with undirected graphs.

(Hint: We only want to traverse each edge once! Try implementing


undirected cycle detection with the above algorithm and see where it fails.)

The problem with running the above code on an undirected graph is that every every child
will revisit its parent in the DFS tree, creating m back edges. Hence, when attempting undi-
rected cycle detection, every edge will be considered a cycle. We can fix this problem by
omitting the parent from the neighbors of each child.

Algorithm 1.7. Undirected, generalized DFS.


1 undirectedDFS (revisit,discover,finish) (G,0 ,s) =
2 let
3 DFS p ((X ,),v) =
4 if (v X) then (X ,revisit (,v ,p)) else
5 let
6 0 = discover (,v ,p)
7 X 0 = X {v}
8 (X 00 ,00 ) = iterate (DFS v) (X 0 ,0 ) (NG+ (v) \ p)
9 000 = finish (0 ,00 ,v ,p)
10 in
11 (X 00 ,000 )
12 end
13 in
14 DFS s (({},0 ),s)
15 end

Built: October 23, 2017


1.3. BFS 5

1.3 BFS

1.3.1 An Example

0 1

2 3

4 5

Task 1.8. Run BFS on the example graph above, starting at vertex 1. Draw the resulting
BFS tree. Draw tree edges as solid lines and non-tree edges as dashed lines.

Note that we could have chosen (5, 3) as a


2 5 tree edge instead of (2, 3). Either edge is
valid; as long as we dont choose both as
tree edges, were golden!

0 4 3

Built: October 23, 2017


6 RECITATION 1. GRAPH SEARCH: BFS AND DFS

1.3.2 Implementation

Consider the following code, which computes the BFS tree of an enumerated graph represented
by an adjacency sequence. For brevity, well write NONE as and (SOME x) as x .

Algorithm 1.9. Computing BFS trees on adjacency sequences.


1 fun BFS (G,s) =
2 let
3 fun BFS (Xi ,Fi ) =
4 if |Fi | = 0 then STSeq.toSeq Xi else
5 let
6 val Ni =


7 Seq.flatten (u, v ) : u G[v] | Xi [u] = : v Fi
8 val Xi+1 =
STSeq.inject (Xi ,Ni )
9 val Fi+1 = u : (u, v) Ni | Xi+1 [u] = v
10 in
11 BFS (Xi+1 ,Fi+1 )
12 end
13

14 val init = STSeq.fromSeq : 0 i < |G|
15 val X0 = STSeq.update (init, (s, s ))
16 val F0 = hsi
17 in
18 BFS (X0 ,F0 )
19 end

Task 1.10. Execute this code on the example graph given in the first section, starting
with vertex 1 as the source. Trace the process by writing down the values Xi , Fi , and
Ni for i = 0, 1, 2, 3.

i Xi Fi Ni




0 , 1, , , , 1 (2, 1 ), (5, 1 )




1 , 1, 1, , , 1 2, 5 (0, 2 ), (4, 2 ), (3, 2 ), (3, 5 )




2 2, 1, 1, 5, 2, 1 0, 4, 3



3 2, 1, 1, 5, 2, 1 (nonexistent)

Built: October 23, 2017


1.3. BFS 7

Task 1.11. Analyze the work and span of this implementation in terms of n (the number
of vertices), m (the number of edges), and d (the diameter of the graph).

+ d+
P
Lets break down the code, line-by-line. We write ||F || = vF (1 G (v)).

Line 7: O(||Fi ||) work, O(log n) span.

Line 8: O(||Fi ||) work, O(1) span.

Line 9: O(||Fi ||) work, O(log n) span.

Line 14: O(n) work, O(1) span.

Lines 15,16: O(1) work, O(1) span.

There are two important observations to make here:

1. no vertex is ever in a frontier more than once, and

2. the number of rounds of BFS is upper bounded by d + 1. (There could be a vertex d hops
away from the source, and each round progresses by exactly one hop. The +1 comes
from the final round which verifies that the frontier is empty, then exits).

We can now show that


d
X X
||Fi || (1 + d+
G (v)) = n + m.
i=0 v

Therefore the total work is


d1
!
X
O n+ ||Fi || = O(n + m)
i=0

and the span is O(d log n).

Built: October 23, 2017


8 RECITATION 1. GRAPH SEARCH: BFS AND DFS

Built: October 23, 2017

You might also like