Lect 21
Lect 21
Graph Traversals
James Fogarty
Autumn 2007
Graph Connectivity
Undirected graphs are connected if there is a path between
any two vertices
2
Graph Traversals
Breadth-first search (and depth-first search) work for
arbitrary (directed or undirected) graphs - not just
mazes!
Must mark visited vertices. Why?
So you do not go into an infinite loop! It’s not a tree.
Either can be used to determine connectivity:
Is there a path between two given vertices?
Is the graph (weakly/strongly) connected?
Which one:
Uses a queue?
Uses a stack?
Always finds the shortest path (for unweighted graphs)? 3
The Shortest Path Problem
Given a graph G, edge costs ci,j, and vertices s
and t in G, find the shortest path from s to t.
For a path p = v0 v1 v2 … vk
unweighted length of path p = k (a.k.a. length)
4
Single Source Shortest Paths
(SSSP)
Given a graph G, edge costs ci,j, and vertex
s, find the shortest paths from s to all
vertices in G.
5
All Pairs Shortest Paths (APSP)
Given a graph G and edge costs ci,j, find the
shortest paths between all pairs of vertices
in G.
6
Depth-First Graph Search
Open – Stack
Criteria – Pop
DFS( Start, Goal_test)
push(Start, Open);
repeat
if (empty(Open)) then return fail;
Node := pop(Open);
if (Goal_test(Node)) then return Node;
for each Child of node do
if (Child not already visited) then push(Child, Open);
Mark Node as visited;
end
7
Breadth-First Graph Search
Open – Queue
Criteria – Dequeue (FIFO)
BFS( Start, Goal_test)
enqueue(Start, Open);
repeat
if (empty(Open)) then return fail;
Node := dequeue(Open);
if (Goal_test(Node)) then return Node;
for each Child of node do
if (Child not already visited) then enqueue(Child, Open);
Mark Node as visited;
end
8
Comparison: DFS versus BFS
Depth-first search
Does not always find shortest paths
Must be careful to mark visited vertices, or you
could go into an infinite loop if there is a cycle
Breadth-first search
Always finds shortest paths – optimal solutions
Marking visited nodes can improve efficiency, but
even without doing so search is guaranteed to
terminate
10
BFS Space Requirements
Assume
Distance from start to a goal is d
Highest number of out edges is k BFS
Queue could grow to size kd
For k=10, d=15, size is
1,000,000,000,000,000
11
Conclusion
For large graphs, DFS is hugely more
memory efficient, if we can limit the
maximum path length to some fixed d.
If we knew the distance from the start to the
goal in advance, we can just not add any
children to stack after level d
But what if we don’t know d in advance?
12
Iterative-Deepening DFS (I)
Bounded_DFS(Start, Goal_test, Limit)
Start.dist = 0;
push(Start, Open);
repeat
if (empty(Open)) then return fail;
Node := pop(Open);
if (Goal_test(Node)) then return Node;
if (Node.dist ≥Limit) then return fail;
for each Child of node do
if (Child not already i-visited) then
Child.dist := Node.dist + 1;
push(Child, Open);
Mark Node as i-visited;
end
13
Iterative-Deepening DFS (II)
IDFS_Search(Start, Goal_test)
i := 1;
repeat
answer := Bounded_DFS(Start, Goal_test, i);
if (answer != fail) then return answer;
i := i+1;
end
14
Analysis of IDFS
Work performed with limit < actual
distance to G is wasted – but the
wasted work is usually small compared
to amount of work done during the last
iteration
d
∑k
i =1
i
= O(k )
d Ignore low order
terms!
16
Example
Seattle
San Francisco
Dallas
17
Example (Unweighted Graph)
Seattle
San Francisco
Dallas
18
Example (Unweighted Graph)
Seattle
San Francisco
Dallas
19
Graph Search, Saving Path
Search( Start, Goal_test, Criteria)
insert(Start, Open);
repeat
if (empty(Open)) then return fail;
select Node from Open using Criteria;
if (Goal_test(Node)) then return Node;
for each Child of node do
if (Child not already visited) then
Child.previous := Node;
Insert( Child, Open );
Mark Node as visited;
end
20
Weighted SSSP:
The Quest For Food
Home
Ben & Jerry’s
Cedars
Neelam’s 40
285 U Village
70 Delfino’s
75 365 25
Coke Closet 375
The Ave 350
350 10
25 HUB
35 ALLEN 35
Café Allegro 15
Schultzy’s Vending Machine in EE1
15,356
Parent’s Home
Parent’s Home
23
General Graph Search Algorithm
Open – some data structure (e.g., stack, queue, heap)
Criteria – some method for removing an element from Open
Search( Start, Goal_test, Criteria)
insert(Start, Open);
repeat
if (empty(Open)) then return fail;
select Node from Open using Criteria;
if (Goal_test(Node)) then return Node;
for each Child of node do
if (Child not already visited) then Insert( Child, Open );
Mark Node as visited;
end
24
Shortest Path for Weighted
Graphs
Given a graph G = (V, E) with edge
costs c(e), and a vertex s ∈ V, find the
shortest (lowest cost) path from s to
every vertex in V
25
Dijkstra’s Algorithm for
Single Source Shortest Path
Similar to breadth-first search, but uses a
heap instead of a queue:
Always select (expand) the vertex that has a
lowest-cost path to the start vertex
Correctly handles the case where the
lowest-cost (shortest) path to a vertex is
not the one with fewest edges
26