Note: Answers are in blue typeface.
procedure: Breadth-first-search(w) { initialize list L0 to contin vertex w # 2 mem I/O i = 0 # 1 mem I/O while not(isEmpty(Li)) do # n-1 comps { create Li+1 = empty list # 1 mem I/O for each vertex v in Li do # n iterations max. { for each edge e incident on v do # m iter's max. { if e is unexplored then # 1 comp { find w such that e = (v,w) # n-1 comp's max. if w is unexplored then # 1 comp { label e as a DISCOVERED edge # 1 mem I/O insert w into Li+1 } } # 3 mem I/O else label e as a CROSS edge # 1 mem I/O }}} i = i + 1 # 1 incr. }
Each edge will be visited twice, when labelled as DISCOVERY and CROSS respectively. This entails visiting all the vertices of the graph. Thus, the minimum work will be O(n + m) comparisons. Since m = O(n2), the work is actually O(n2). The quadratic work requirement is especially noticeable in a dense graph. If an adjacency matrix representation is used, then the total work will be O(n2) in the best case, since all rows and columns will have to be examined in order to find edges incident on all vertices v.
procedure: Depth-first-search(v) { for each edge e incident on v do # m iter's max. { if e is unvisited then # 1 comp { find w such that e = (v,w) # n-1 comp's max. if w is unvisited then # 1 comp { label e as a DISCOVERED edge # 1 mem I/O DFS(w) }} # one call to DFS else label e as a BACK edge # 1 mem I/O } }
Each node is marked and visited at least once, and is checked for each incident edge. The time complexity of DFS is dependent on the implementation, like BFS. With an adjaceny-list representation of the graph, work can be O(n + m) comparisons, as discussed for BFS. If an adjacency matrix representation is used, then the total work will be O(n2), since all rows and columns will have to be examined in order to find edges incident on v.
The order of traversal is a b c f e d
, which can
vary depending on the rotations of subtrees. For example,
a b c f d e
is also valid.
(b) V = {a,b,c,d,e,f}, E = {(d,a), (b,c), (a,b), (e,b), (c,e), (b,d)}.
a b c d
e
. The comment in Question 3a) applies.
Analyze the complexity of each case ((a) and (b), above) by
constructing a work budget for each step of the traversal
(2 points total).
Answer: The number of comparisons N is the key work
bottleneck in a compute-bound system, and it is proportional to
the number of edges encountered at each vertex. Simply sum the
degree of each vertex to find N for both a) and b).
b f c e d
. Part b) - One possible
order of traversal is b c d e a
. Vertex
f
is unreachable. The comment in Question 3
applies to both Parts a) and b).
a b f c e d
. Part b) - One possible
order of traversal is a b c e d
. Vertex
f
is unreachable. The comment in Question 3
applies to both Parts a) and b).
b f c e d
. Vertex
a
is unreachable. Part b) - One possible
order of traversal is b c e d a
. Vertex
f
is unreachable. The comment in Question 3
applies to both Parts a) and b).
Copyright © 1999 by Mark S. Schmalz.