242-535 ADA: 9. Graph Search 1
• Objectiveo describe and compare depth-first and breadth-
first graph searching, and look at the creation of spanning trees
Algorithm Design and Analysis
(ADA)242-535, Semester 1 2014-2015
9. Graph Search
242-535 ADA: 9. Graph Search 2
1. Graph Searching2. Depth First Search (DFS)3. Uses of DFS
o cycle detection, reachability, topological sort
4. Breadth-first Search (BFS)5. DFS vs. BFS6. IP Multicasting
Overview
242-535 ADA: 9. Graph Search 3
• Given: a graph G = (V, E), directed or undirected
• Goal: visit every vertex
• Often the end result is a tree built over the grapho called a spanning tree
• it visits every vertex, but not necessarily every edge
o Pick any vertex as the rooto Choose certain edges to produce a treeo Note: we might build a forest if the graph is not
connected
1. Graph Searching
242-535 ADA: 9. Graph Search 4
Example
search then build a spanning tree(or trees)
242-535 ADA: 9. Graph Search 5
• DFS is “depth first” because it always fully explores down a path away from a vertex v before it looks at other paths leaving v.
• Crucial DFS properties:• uses recursion: essential for graph structures• choice: at a vertex there may be a choice of several
edges to follow to the next vertex• backtracking: "return to where you came from"• avoid cycles by grouping vertices into visited and
unvisited
2. Depth First Search (DFS)
242-535 ADA: 9. Graph Search 6
Directed Graph Examplea
d
fec
b
Graph G
DFS works with directedand undirected graphs.
242-535 ADA: 9. Graph Search 7
• enum MARKTYPE {VISITED, UNVISITED};
struct cell { /* adj. list */ NODE nodeName; struct cell *next;};typedef struct cell *LIST;
struct graph { enum MARKTYPE mark; LIST successors;};typedef struct graph GRAPH[NUMNODES];
Data Structures
242-535 ADA: 9. Graph Search 8
void dfs(NODE u, GRAPH G)// recursively search G, starting from u{ LIST p; // runs down adj. list of u NODE v; // node in cell that p points at
G[u].mark = VISITED; // visited u p = G[u].successors; while (p != NULL) { // visit u’s succ’s v = p->nodeName; if (G[v].mark == UNVISITED) dfs(v, G); // visit v p = p->next; }}
The dfs() Function
242-535 ADA: 9. Graph Search 9
• Call Visitedd(a) {a}d(a)-d(b) {a,b}d(a)-d(b)-d(c) {a,b,c}
Skip b, return to d(b)d(a)-d(b)-d(d) {a,b,c,d}
Skip cd(a)-d(b)-d(d)-d(e) {a,b,c,d,e}
Skip c, return to d(d)
Calling dfs(a,G)
continued
call it d(a)for short
242-535 ADA: 9. Graph Search 10
d(a)-d(b)-d(d)-d(f){a,b,c,d,e,f}Skip c, return to
d(d)
d(a)-d(b)-d(d) {a,b,c,d,e,f}Return to d(b)
d(a)-d(b) {a,b,c,d,e,f}Return to d(a)
d(a) {a,b,c,d,e,f}Skip d, return
242-535 ADA: 9. Graph Search 11
• Since nodes are marked, the graph is searched as if it were a tree:
DFS Spanning Tree
d/4
f/6e/5c
b/2
a/1
c/3
A spanning tree is a subgraph of a graph G which contains all the verticies of G.
242-535 ADA: 9. Graph Search 12
Example 2
a b
dc
e f
hg
a b
dc
e f
hg
the tree generated by DFS is drawn with thick lines
DFS
242-535 ADA: 9. Graph Search 13
• The time taken to search from a node is proportional to the no. of successors of that node.
• Total search time for all nodes = O(|V|)Total search time for all successors = time to search all edges = O(|E|)
• Total running time is O(V + E)
dfs() Running Time
continued
242-535 ADA: 9. Graph Search 14
• If the graph is dense, E >> V (E approaches V2) then the O(V) term can be ignoredo in that case, the total running time = O(E) or O(V2)
242-535 ADA: 9. Graph Search 15
• Finding cycles in a grapho e.g. for finding recursion in a call graph
• Searching complex locations, such as mazes
• Reachability detectiono i.e. can a vertex v be reached from vertex u?o useful for e-mail routing; path finding
• Strong connectivity
• Topological sorting
3. Uses of DFS
continued
242-535 ADA: 9. Graph Search 16
• The DFS algorithm is similar to a classic strategy for exploring a mazeo mark each intersection,
corner and dead end (vertex) as visited
o mark each corridor (edge ) traversed
o keep track of the path back to the previous branch points
Maze Traversal
Graphs 16
Reachability• DFS tree rooted at v: what are the vertices reachable from v
via directed paths?
A
C
E
B
D
F
A
C
E D
A
C
E
B
D
F
start at C
start at B
Strong Connectivity• Each vertex can reach all other vertices
Graphs 18
a
d
c
b
e
f
g
Strong Connectivity Algorithm
• Pick a vertex v in G.• Perform a DFS from v in G.
o If there’s a vertex not visited, print “no”.
• Let G’ be G with edges reversed.• Perform a DFS from v in G’.
o If there’s a vertex not visited, print “no”
• If the algorithm gets here, print “yes”.
• Running time: O(V+E).19
G:
G’:
a
d
c
b
e
f
g
a
d
c
b
e
f
g
Strongly Connected Components
• List all the subgraphs where each vertex can reach all the other vertices in that subgraph.
• Can also be done in O(V+E) time using DFS.
20
{ a , c , g }
{ f , d , e , b }
a
d
c
b
e
f
g
242-535 ADA: 9. Graph Search 21
• Topological sort of a directed acyclic graph (DAG):o linearly order all the vertices in a graph G such that
vertex u comes before vertex v if edge (u, v) Go a DAG is a directed graph with no directed cycles
Topological Sort
242-535 ADA: 9. Graph Search 22
Example: Getting Dressed
Underwear Socks
ShoesTrousers
Belt
Shirt
Watch
Tie
Jacket
Socks Underwear Trousers Shoes Watch Shirt Belt Tie Jacket
one topological sort(not unique)
242-535 ADA: 9. Graph Search 23
Topological-Sort(){ Run DFS;
When a vertex is finished, output it;Vertices are output in reverse topological order;
}
• Time: O(V+E)
Topological Sort Algorithm
242-535 ADA: 9. Graph Search 24
• Process all the verticies at a given level before moving to the next level.
• Example graph G (again):
4. Breadth-first Search (BFS)
a b
dc
e f
hg
242-535 ADA: 9. Graph Search 25
• 1) Put the verticies into an orderingo e.g. {a, b, c, d, e, f, g, h}
• 2) Select a vertex, add it to the spanning tree T: e.g. a
• 3) Add to T all edges (a,X) and X verticies that do not create a cycle in To i.e. (a,b), (a,c), (a,g)
T = {a, b, c, g}
Informal Algorithm
continued
a
b c g
242-535 ADA: 9. Graph Search 26
• Repeat step 3 on the verticies just added, these are on level 1o i.e. b: add (b,d)
c: add (c,e) g: nothing
T = {a,b,c,d,e}
• Repeat step 3 on the verticies just added, these are on level 2o i.e. d: add (d,f)
e: nothingT = {a,b,c,d,e,f}
a
b c g
d e
a
b c g
d e
fcontinued
level 1
level 2
242-535 ADA: 9. Graph Search 27
• Repeat step 3 on the verticies just added, these are on level 3o i.e. f: add (f,h)
T = {a,b,c,d,e,f,h}
• Repeat step 3 on the verticies just added, these are on level 4o i.e. h: nothing, so stop
a
b c g
d e
f
h
continued
level 3
242-535 ADA: 9. Graph Search 28
• Resulting spanning tree:
a b
dc
e f
hg
a differentspanning treefrom the earliersolution
242-535 ADA: 9. Graph Search 29
Example 2
242-535 ADA: 9. Graph Search 30
Algorithm Graphicallystart node
pre-builtadjency list
242-535 ADA: 9. Graph Search 31
242-535 ADA: 9. Graph Search 32
242-535 ADA: 9. Graph Search 33
boolean marked[]; // visited this vertex?int edgeTo[]; // vertex number going to this vertex
void bfs(Graph graph, int start){ Queue q = new Queue(); marked[start] = true; q.add(start); // add to end of queue while (!q.isEmpty()) { int v = q.remove(); // get from start of queue for (int w : graph.adjacentTo(v)) // v --> w if (!marked[w]) { edgeTo[w] = v; // save last edge on a shortest path marked[w] = true; q.add(w); // add to end of queue } }} // end of bfs()
BFS Code
242-535 ADA: 9. Graph Search 34
5. DFS vs. BFS
Applications DFS BFSSpanning forest, connected components, paths, cycles
Shortest paths
Biconnected components
see part 11
242-535 ADA: 9. Graph Search 35
• DFS is like one person exploring a mazeo do down a path to the end, get to a dead-end,
backtrack, and try a different path
• BFS is like a group of searchers fanning out in all directions, each unrolling a ball of string. o at a branch point, the searchers split up to explore all
the branches at once o if two groups meet up, they join forces (using the ball of
string of the group that got there first)o the group that gets to the exit first has found the
shortest path
DFS and BFS as Maze Explorers
242-535 ADA: 9. Graph Search 36
BFS Maze Graphically
Also called flood filling; used in paintsoftware.
242-535 ADA: 9. Graph Search 37
• The BFS "fanning out" algorithm is best implemented in a parallel language, where each "group of explorers" is a separate thread of execution.o e.g. use fork and join in Java
• The earlier implementation uses a queue to implement the fanning out as a sequential algorithm.
• DFS is inherently a sequential algorithm.
Sequential / Parallel
242-535 ADA: 9. Graph Search 38
• A network of computers and routers:
6. IP Multicasting
sourcecomputer
router
continued
242-535 ADA: 9. Graph Search 39
• How can a packet (message) be sent from the source computer to every other computer?
• The inefficient way is to use broadcastingo send a copy along every link, and have each router
do the sameo each router and computer will receive many copies
of the same packeto loops may mean the packet never disappears!
continued
242-535 ADA: 9. Graph Search 40
• IP multicasting is an efficient solutiono send a single packet to one routero have the router send it to 1 or more routers in such a
way that a computer never receives the packet more than once
• This behaviour can be represented by a spanning tree.
• Can use either BFS or DFS, but BFS will usually produce shorter pathso i.e. BFS is a better choice
continued
242-535 ADA: 9. Graph Search 41
• One spanning tree for the network:
sourcecomputer
router
the tree isdrawn withthick lines