Title: Depth First Search
1Depth First Search
2Graph Traversal
- Depth First Search
- Explore first unexplored node adjacent to the one
where you are. - Go as deep as possible as fast as possible.
- Each node is visited twice, when first explored
and next time when finished. After that is is
never revisited. - Stack based
- Breath First Search
- Explore first unexplored node adjacent to node n.
- After that explore next one adjacent to n and so
on. - When all adjacent nodes are explored explore
their adjacent nodes. - Each node is only visited once.
- Queue based
3DFS
- Imagine that nodes in a graph are islands and
edges are bridges that are one way roads. You may
do exploration in the direction of the road, and
you may backup in the opposite direction. - Islands are ordered. For each one there is
ordered list of adjacent islands. One is selected
for the start of the exploration.
4DFS strategy
- Strategy of the exploration
- Islands will have three possible states based on
state of exploration - Unexplored (never visited -cyan color)
- Visited or explored ( yellow color)
- Finished (purple color)
- Initially all islands are unexplored.
5DFS
- From one node always proceed to the first
unexplored island on the list of adjacent
islands. As soon as you get there that island is
visited. The first number associated with each
node is point in time when an island is visited
first. If there are no more unexplored islands on
the adjacency list, mark the island as finished
(purple color) assign second number as point in
time when you leave that island forever. Backup
to the island from which you got here.
6DSF
- When you return to the island where you started
- All islands may be finished (This is the END)
- or
- Some are finished and other are unexplored
because you could not reach them from where you
started. Continue DSF from the first unexplored
island using the same approach.
7Example
8Start from A. Next explore B.
9From B we explore C
10From C there is Nowhere to GoC is finished. We
Back up to B.
11From B there are no Unexplored Nodes. We backup
to A. B is finished.
12From A there are no Unexplored Nodes. No Backup.
A is finished.
13Next What?
- We arrived back to the origin of our trip.
- Are there additional unexplored islands?
- If no, we are done.
- Otherwise, continue exploration from the first
unexplored one. - In our case it is island D.
14From D we explore E
15From E there is Nowhere to Go.E is finished.
Backup to D
16We got back to D. D is finished. Are there Other
Unexplored? No. Traversal is Completed.
17DFS Traversal
- DFS Traversal creates a list of trees (forest).
- In our example A ? B? C is one tree and D ? E
is another. - Different start node would produce different
result. - The order of nodes is also important. Different
order would produce different result.
18Edges
- Graph edges can be one of the following four
types - Tree edge From explored to unexplored node.
- Back edge From explored node w to the
predecessor node v such that there is tree path
from v to w.
19Edge Types
- Forward edge From explored node v to successor
node w, such that there is tree path from v to
w. - Cross edge connecting either two nodes in
different trees of the forest or connecting two
nodes from the same tree such that none is
predecessor of the other in the tree.
20Type of Edges
21DFS Algorithm
- Let graph G be G(V,E) where V1,2,n and
adjVertices be list of adjacency lists. - INPUT Array adjVertices of adjacency lists
- n-number of vertices
- ALGORITHM Visits every node in the graph G using
depth first search.
22Code Outline
- int dfsSweep(IntList adjVertices, int n)
- Initialize color array to white (unexplored)
- for each vertex v of G
- if (color(v) white)
- dsf( adjVertices, color, v)
-
23- dsf (IntList adjVertices, int color, int v)
- colorv gray // v is visited (explored)
- remAdj adjVerticesv
- while(remAdj ! null)
- w first(remAdj)
- if (colorw white)
- dsf(adjVertices, color, w)
-
- // postorder processing display v
- colorv black // v is finished
-
24Analysis of DFS
- Let G(V,E) and let Vn Em
- The number of instructions done by dfsSweep,
excluding calls to dfs is linear in n. - In dfs the number of instructions is proportional
to the number of elements in adjacency list that
is traversed. - Adjacency lists are traversed only once.
- The complexity of dfs is in ?(nm).
25DFS Applications
- Call structure for recursive calls
- Eight queens
- Finding connected components
- Finding cycles
- Topological Sorting
26Fibonacci Numbers
- Let graph nodes represent method invovations.
- Let edge xy represent the fact that method x
invokes method y - Fig 7.13 page 337
- Call structure for recursive calls is given as
DFS traversal (preorder) of the tree.
27Eight Queens on a Chessboard
- Problem Place 8 queens on a chessboard so that
none is under attack by any other. (None can
reach another by moving along one row, column or
diagonal.) - Algorithm
- Place first queen in row 1 as far left as
possible. That is (1,1). - Place second queen in row 2 as left as possible.
That is (2,3) since (2,1) and (2,2) are attacked
by first queen. - Place third queen in row 3 as left as possible.
That is (3,5). - Place fourth queen in row 4 as left as possible.
That is (4,2) - Place fifth queen in row 5 as left as possible.
That is (5,4). - Place sixth queen in row 6 as left as possible.
All positions are under attack. Back up to
previous row. Move the queen in the previous row
to the right (minimal number of positions).
Continue as before. -
28DFS- Eight Queens
- Each vertex except the root is labeled by
position on the board. (see Fig 7.14 page 339) - The vertices at level k are labeled by board
positions in row k. - The children of a vertex v at level k are all
board positions in row k1 that are not under
attack by any queen on the path from the root to
vertex v. - Traversal stops when solution is obtained. So,
only part of the tree is traversed in backtrack
search.
29Connected Components with DFS
- A connected component of undirected graph G(V,
E) is maximal connected subgraph that is not
contained in any larger connected subgraph. - Each edge is represented as two directed arcs.
- To find connected components start with any
vertex, do a DFS to find all other vertices (and
edges) in the same component. If there are some
remaining vertices repeat the same process.
30Directed Acyclic Graphs
- Directed Acyclic Graphs (DAGs) are graphs with no
cycles. - DAGs are important for
- Scheduling problems where some tasks should be
done before some others (cycle would be deadlock) - Problems on general dircetd graphs are easier to
solve on DAGs
31DAG Partial Order
- DAG corresponds mathematically to a partial order
on its vertices. - An edge uw can be interpreted as the relationship
vRw. - Partial order relation should have no cycles.
- Relation R is partial order if it is reflexive
(aRa), antisymmetric (aRb and bRagtab) and
transitive(aRb and bRc gtaRc).
32DAG
- Apply DFS to create a forest of trees.
- Mark all tree edges (T).
- Specify the type of other edges (Back, Forward,
and Cross). - Graph G is DAG if it has no back edges.
- Obviously, the presence of a back edge makes a
cycle.
33Topological Order
- Topological ordering of vertices in a DAG is an
arrangement of vertices where all edges are
pointing from left to right. - Topological order for G(V,E) where Vn, is an
assignment of distinct integers 1,2,,n to the
vertices of V, called their topological numbers,
such that if vw is an edge then topo(v) lt topo(w).
34Reverse Topological Order
- Reverse Topological order for G(V,E) where
Vn, is an assignment of distinct integers
1,2,,n to the vertices of V, such that if vw is
an edge in the graph G then topo(v) gt topo(w). - LEMMA If a directed graph G has a cycle, then G
has no topological order. - LEMMA Every DAG has at least one topological
order.
35Scheduling with Task Dependencies
- Assume that tasks cannot be performed before the
tasks that they depend on are completed. - EXAMPLE Get out in the morning.
- You shower after you wake up, you dress after you
take shower e.t.c. The table gives list of tasks
and the tasks that should be done before.
36(No Transcript)
37Dependency Graph
- In scheduling problems we draw edges to point
backward in time. - If task u requires task v to be done before it,
we draw an arc uv from u to v. - The list of dependencies in the table is
interpreted as adjacency list for the graph
38Reverse Topological Ordering
- Modify DFS by adding global counter initialized
to zero and global array topo. - At postorder processing increment counter and
assign the value of counter to current vertex v.
That is - topovcounter
- Vertices are ordered by their finishing times in
DFS. The last vertex to finish has highest
topological number.
39Dependency Graph