02 CME4422 SearchAndSortAlgorithms
02 CME4422 SearchAndSortAlgorithms
◘ Breadth-first Search
◘ Depth-first Search
◘ Topological Sorting
Graph-Searching Algorithms
◘ Searching a graph:
– Systematically follow the edges of a graph to visit the
vertices of the graph.
◘ Used to discover the structure of a graph.
◘ Standard graph-searching algorithms.
– Breadth-First Search (BFS).
– Depth-First Search (DFS).
– Topological Ordering (Sorting)
Breadth-First Search
Definitions:
Path between vertices u and v: Sequence of vertices (v1, v2, …, vk)
such that u=v1 and v =vk, and (vi,vi+1) E, for all 1 i k-1.
Length of the path: Number of edges in the path.
Path is simple if no vertex is repeated.
Breadth-first Search
breadth-first-search
mark starting vertex as visited; put on queue
while the queue is not empty
dequeue the next node
for all unvisited vertices adjacent to this one
mark vertex as visited
add vertex to queue
BFS for Shortest Paths
2 3 3
2
2 3
1 S
S 2 S
1 1
2
2 3 3
Q: a queue of discovered v
white: undiscovered
color[v]: color of v
gray: discovered
d[v]: distance from s to v
black: finished
[v]: predecessor of v
Example (BFS)
r s t u
0
v w x y
Q: s
0
Example (BFS)
r s t u
1 0
1
v w x y
Q: w r
1 1
Example (BFS)
r s t u
1 0 2
1 2
v w x y
Q: r t x
1 2 2
Example (BFS)
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
2 2 2
Example (BFS)
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
2 2 3
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
2 3 3
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
3 3
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
3
Example (BFS)
r s t u
1 0 2 3
2 1 2 3
v w x y
Q:
Example (BFS)
s
0
r w
BF Tree 1
1
v t x
2 2 2
u y
3 3
Analysis of BFS
Computational Complexity
Initialization takes O(V).
Traversal Loop
• Each vertex is enqueued and dequeued at most once, and each
operation takes O(1). So, total time for queuing is O(V).
• Adjacency list representation
– The adjacency list of each vertex is scanned at most once.
– The sum of lengths of all adjacency lists is O(E).
– Summing up over all vertices: O(V+E) linear in the size of the graph.
• Adjacency matrix representation
– Have to check all entries in matrix: O(n2)
Space Complexity
Queue to handle unexplored nodes
• Worst case: all nodes put on queue (if all are adjacent to first node)
• O(n)
Breadth-First Search (BFS)
Applications of BFS
A
A B
B
C D
I D C
E L I
J E
K H L K
F F
H J
Theorem:
In DFS of an undirected graph, we get only tree and back edges.
No forward or cross edges.
Example (DFS)
u v w
1/
x y z
Example (DFS)
u v w
1/ 2/
x y z
Example (DFS)
u v w
1/ 2/
3/
x y z
Example (DFS)
u v w
1/ 2/
4/ 3/
x y z
Example (DFS)
u v w
1/ 2/
4/ 3/
x y z
Example (DFS)
u v w
1/ 2/
4/5 3/
x y z
Example (DFS)
u v w
1/ 2/
4/5 3/6
x y z
Example (DFS)
u v w
1/ 2/7
4/5 3/6
x y z
Example (DFS)
u v w
1/ 2/7
F B
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7
F B
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7 9/
F B
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7 9/
F B C
4/5 3/6
x y z
Example (DFS)
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/12
F B C
◘ A digraph is a graph
whose edges are all E
directed
D
– Short for “directed graph”
◘ Applications C
– one-way streets
– flights B
– task scheduling
A
Digraph Application
4 5
nap more c.s.
7
play
8
write c.s. program 6
9 work out
bake cookies
10
sleep 11
dream about graphs
Topological Ordering
source sink
◘ A directed acyclic graph (DAG)
is a digraph that has no directed D E
cycles.
◘ Every DAG has at least one B
source and at least one sink. DAG G
C
◘ A topological ordering of a
digraph is a numbering A
v1 , …, vn v4 v5
of the vertices such that for D E
every edge (vi , vj), we have i < j v2
B
Theorem: v3
v1 C
A digraph admits a topological Topological
ordering if and only if it is a DAG A
ordering of G
Running Time
O(n + m)
Topological Sorting Example
A
B
C
D
E F
H
G
I
Topological Sorting Example
0
0
A
B
1
C 3
D
2
E F
2
1
H
G
B
1
A I 3
ready
indegree ordering
Topological Sorting Example
0
1
A
B
1
C 2
D
1
E F
2
1
B H
G
1
A I 3
ready
Topological Sorting Example
2
1
A
B
0
C 1
D
1
E F
2
1
A H
G
1
C I 3
ready
Topological Sorting Example
2
1
A
B
3
C 0
D
1
E F
2
0
C H
G G
E
0
D I 3
ready
Topological Sorting Example
2
1
A
B
3
C 0
D
1
E F
2
0
G H
G
E
4
D I 2
ready
Topological Sorting Example
2
1
A
B
3
C 0
D
1
E F
1
5
E H
G
4
D I 2
ready
Topological Sorting Example
2
1
A
B
3
C 6
D
0
E F
1
5
D H
G
4
F I 2
ready
Topological Sorting Example
2
1
A
B
3
C 6
D
7
E F
0
5
F H
G
4
H I 1
ready
Topological Sorting Example
2
1
A
B
3
C 6
D
7
E F
8
5
H H
G
4
I I 0
ready
Topological Sorting Example
2
1
A
B
3
C 6
D
7
E F
8
5
I H
G
4
I 9
ready
Topological Sorting Example
2
1
3
6
5 7
8
4
9
Applications of TS
◘ Dependency resolution
– Find compilation order of dependent classes
– Project management
– Instruction scheduling
–…
Questions?