2. Instructor
Prof. Amrinder Arora
amrinder@gwu.edu
Please copy TA on emails
Please feel free to call as well
Available for study sessions
Science and Engineering Hall
GWU
Algorithms Graph Traversal Techniques 2
LOGISTICS
4. Suppose you have two jugs, one capable of holding 5
cups, and one capable of holding 8 cups.
[The jugs are irregularly shaped and without
markings, so you can't determine how much water is
in either jug unless it is completely full or completely
empty.]
You also have a faucet, and as much water as you'd
like.
Can you get 3 cups?
Can you obtain 1 cup? 2 cups? 4 cups? 6 cups? 7
cups?
Algorithms Graph Traversal Techniques 4
PUZZLES
5. Where can we go from here:
(x,y)
a. (0,y) / (x,0) Empty first/second
b. (5,y) / (x,8) Fill first/second
c. (5,x+y-5) Second to First, x+y > 5
d. (x+y,0) Second to First
e. (x+y-8,8) First to Second, x+y > 8
f. (0,x+y) First to Second
Algorithms Graph Traversal Techniques 5
PUZZLES (CONT.)
6. (0,0)
1. (0,8) // b
2. (5,3) // c
3. (0,3) // a
4. (3,0) // d
5. (3,8) // a
6. (5,6) // c
7. (0,6) // a
8. (5,1) // c
9. (0,1) // a
Algorithms Graph Traversal Techniques 6
PUZZLES (CONT.)
7. While ()
Make a transition step
Reach a new state
If new state is what you were looking for “Eureka Eureka”
This also works if trying to change state in life
[Or in the US.]
Algorithms Graph Traversal Techniques 7
POSSIBLE SOLUTION
8. A graph search (or traversal) technique visits every
node exactly once in a systematic fashion.
Basic use case is the search
Two basic techniques:
Depth-First Search (DFS)
Breadth-First Search (BFS)
Algorithms Graph Traversal Techniques 8
GRAPH TRAVERSAL TECHNIQUES
10. Edges of input graph G = (V,E) can be classified
in context of the forest G’ produced by the
traversal of G
Tree edges (aka Discovery Edge): Edge (u,v) if v first
discovered by exploring edge (u,v)
Back edges: Edge (u,v) connecting a vertex u to an
ancestor v. Self loops are also back edges.
Forward edge: Edge (u,v) connecting a vertex u to a
descendent v.
Cross edges: All other edges
Algorithms Graph Traversal Techniques 10
CLASSIFICATION OF EDGES
11. 1) DFS follows the following rules: Select an unvisited
node s, visit it, and treat as the current node
2) Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3) If the current node has no unvisited neighbors,
backtrack to the its parent, and make that the new
current node
Repeat the steps 2 and 3 until no more nodes can be
visited.
4) If there are still unvisited nodes, repeat from step 1.
[Use of backtracking suggests that a stack is a good
data structure for DFS implementation]
Algorithms Graph Traversal Techniques 11
DEPTH FIRST SEARCH (DFS)
12. Procedure DFS(input: graph G)
Stack T; Integer s,x;
while (G has an unvisited node) do
s := an unvisited node
visit(v)
T.push(v)
while (T is not empty) do
x := T.top()
if (x has an unvisited neighbor y) then
visit(y)
T.push(y)
else
T.pop()
endif
endwhile
endwhile
Algorithms Graph Traversal Techniques 12
DFS IMPLEMENTATION
13. dfs (Graph G) {
// all vertices of G are first painted white
while there is a white node in G {
dfs-visit(G, u)
}
}
dfs-visit (Graph G, Vertex u) {
the vertex u is painted gray
u.d = time++ // u has now been discovered
for all white successors v of u {
dfs-visit(G, v)
}
u is painted black
u.f = time++ // Exploration of u has finished
}
Algorithms Graph Traversal Techniques 13
DFS – ALTERNATE ALGORITHMIC VIEW
15. Every node is visited once. Also, every edge (x,y) is
"crossed" twice: one time when node y is checked
from x to see if it is visited (if not visited, then y
would be visited from x), and another time, when we
back track from y to x.
Therefore, the time of DFS is O(n+|E|), or O(n+m)
If the graph is connected, the time is O(m) because
the graph has at least n-1 edges, and so n+m <= 2m
+1, implying that n+m is O(m).
Algorithms Graph Traversal Techniques 15
DFS TIME COMPLEXITY
16. If the start of node u is marked as “(u”, and the end
as “u)”, then the overall parenthetical expression is
well-formed. For example: (u (v (z z) (w w) v) u)
Proof: Immediate from the recursive nature of the
algorithm
dfs-visit(u)
u.d This gets logged as “(u”
Recursive calls to dfs-visit of other nodes
u.f This gets logged as “u)”
In other words: [d,f] intervals are properly nested
Algorithms Graph Traversal Techniques 16
PARENTHESIS THEOREM
17. Theorem: In DFS, every edge of undirected graph G is
either a tree edge or a back edge. (In other words, no
forward or cross edges exist in G’ produced by DFS
traversal of G).
(Theorem – book section 7.2.3.)
Discussion Points:
Can a “forward” edge exist in DFS traversal?
Can a “cross” edge exist in DFS traversal?
Let (x,y) be a cross edge, that is, x and y are in separate subtrees of
the DFS tree. Assume x was visited before y.
When a search for unvisited neighbors of x was conducted and none
found, x was backtracked from, never to return to x again.
Why did we skip y at that time?
Since y is a neighbor of x and y is not visited at time t, y would have to be visited from x
before the algorithm backtracks from x. That would make y a descendent of x.
Contradiction.
Therefore, no such cross edge (x,y) can exist in a DFS tree.
Algorithms Graph Traversal Techniques 17
DFS EDGE CLASSIFICATION THEOREM
18. Use a counter to count the number of time the outer while-
loop iterates in the DFS algorithm, the counter value at the
end will be equal to the number of connected components of
the input graph G.
This is because the body of the outer loop, that is, every
iteration, fully traverses the connected component that
contains the node v.
If the counter value is 1, then the graph is connected. That is,
the DFS algorithm becomes a connectedness-testing
algorithm that tells if a graph is connected, in O(n+|E|) time
If the counter value is > 1, the algorithm will indicate that the
graph is disconnected, and the nodes visited in each iteration
constitute a separate connected component. In other terms,
the DFS algorithm identifies the various connected
components.
Algorithms Graph Traversal Techniques 18
FIRST APPLICATION OF DFS: CONNECTIVITY
19. Given: A uniformly weighted graph (all edges have weight
w = 1)
In that case, all spanning trees are of the same weight
(because all trees of n nodes have exactly n-1 edges)
Thus, to find a minimum spanning tree in such graphs, it
suffices to find any spanning tree.
DFS yields a spanning tree (if the input graph is
connected, otherwise, it is a spanning forest). That tree is
then a minimum spanning tree. The time to compute the
tree is O(|E|), which is better than the O(|E| log |E|)
time MST algorithm for general weighted graphs.
Algorithms Graph Traversal Techniques 19
SECOND APPLICATION OF DFS: MINIMUM
SPANNING TREES IN UNIFORMLY WEIGHTED
GRAPHS
20. Definition: A node in a connected graph is called an
“articulation point” if the deletion of that node
disconnects the graph.
Definition: A connected graph is called biconnected if
it has no articulation points. That is, the deletion of
any single node leaves the graph connected.
In the case of networks, an articulation point is referred to as a
single point of failure.
The Biconnectivity Problem:
Input: a connected graph G
Problem: Determine whether or not the graph is biconnected.
If not biconnected, find all the articulation points.
Algorithms Graph Traversal Techniques 20
THIRD APPLICATION OF DFS:
BICONNECTIVITY
21. Not biconnected – node G is an articulation point
(single point of failure)
If there was one more edge, say between E and L,
then G would not be an articulation point.
Algorithms Graph Traversal Techniques 21
EXAMPLE
22. Observation: A non-root node x is an articulation
point if and only if x has a subtree from which no
backward edge originates and ends at a proper
ancestor of x.
Each node i will have two new labels: DFN[i] and L[i].
DFN[i] ::= sequence in which i is visited. Thus, the first node
visited (i.e., the root) has its DFN = 1. The second node visited
has a DFN = 2, and so on.
L[i] ::= Lowest DFN number of node which can be reached from
node i using zero or more tree edges, and then a single back
edge; or DFN[i], whichever is lower
Algorithms Graph Traversal Techniques 22
BICONNECTIVITY (CONT.)
23. Computing DFN[i] and L[i]
The DFNs are easy to compute using a simple counter.
We note that
L[x]=min{
DFN[x],
{DFN[y] | (x,y) is a back edge},
{L[w] | for each child w of x}
}
Algorithms Graph Traversal Techniques 23
BICONNECTIVITY (CONT.)
24. Algorithms Graph Traversal Techniques 24
Procedure DFS(input: graph G,)
Stack T; Integer num = 1; Integer DFN[1:n], L[1:n], Parent[1:n]
Node s := an unvisited node
visit(s), T.push(s), DFN[s] := num++, L[s] := DFN[s]
While (T is not empty) do
Node x := top(T)
if (x has an unvisited neighbor y) then
visit(y), T.push(y), DFN[y] := num++, Parent[y] := x, L[y] :=
DFN[y]
Else
pop(T)
for (every neighbor y of x) do
if (y != parent[x] and DFN[y] < DFN[x]) then
/* y is an ancestor of x, and (x,y) is a back edge*/
L[x] := min(L[x],DFN[y])
Else
if (x = Parent[y]) then
L[x] : min(L[x],L[y])
if (L[y] >= DFN[x] and x is not root) then x is an
articulation point
if (s has more than one child) then s is an articulation point
DFSBICONNECTIVITY
25. Wikipedia link on DFS
Algorithms Graph Traversal Techniques 25
READING / EXTERNAL LINKS