Recitation 1 Graph Search: BFS and DFS: 1.1 Announcements
Recitation 1 Graph Search: BFS and DFS: 1.1 Announcements
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.
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
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.
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.
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.
Task 1.4. Define 0 , revisit, discover, and finish to calculate DFS number-
ings.
Task 1.6. Modify the given generalized DFS code to work with undirected graphs.
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.
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.
0 4 3
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 .
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)
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)).
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).