Week 10 (Graphs and Trees)
Week 10 (Graphs and Trees)
Week 10 (Graphs and Trees)
• Graph Basics
• Graph Searching
• Depth First Search
• Breadth First Search
• Topological Sort
Graphs
Directed Graph: A graph whose edges are ordered pairs of vertices. That is, each
edge can be followed from one vertex to another vertex where edge (u, v) goes
from vertex u to vertex v.
Acyclic Graph: A graph with no path that starts and ends at the same vertex.
1 2 1 2 1 2
3 4 3 4 3 4
A B
F C
E D
d(A)=3, d(B)=3,d(C)=2
Weighted graph: associates weights with either the edges or the vertices
Connected: if every vertex of a graph can reach every other vertex, i.e., every
pair of vertices is connected by a path
Strongly connected: every 2 vertices are reachable from each other (in a
digraph)
Weighted Graph
Graph Applications
2
3
8
1 10
4
5
9
11
6
7
2
4 3
8 8
1 6 10
2 4 5
4 4 3
5
9
11
5 6
6 7
7
2
3
8
1 10
4
5
9
11
6
7
(a) (b)
(a) (b)
(a) (b)
(a) (b)
a. A directed graph G having five vertices and six edges.
b. The adjacency-list representation of G.
Graph Representation
(a) (b)
(a) (b)
Breadth-first search
Depth-first search
Other variants: best-first, iterated deepening search, etc.
Depth-First Search (DFS)
Explore “deeper” in the graph whenever possible
Edges are explored out of the most recently discovered vertex v that still has
unexplored edges (LIFO)
When all of v’s edges have been explored, backtrack to the vertex from which
v was discovered
computes 2 timestamps: d[ ] (discovered) and f[ ] (finished)
builds one or more depth-first tree(s) (depth-first forest)
Discovered,
On Process
Finished, all
adjacent vertices x y z
have been
discovered
Discovered,
On Process
Finished, all
adjacent vertices x y z
have been
discovered
Discovered,
On Process
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Discovered,
On Process
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Discovered,
On Process
4/
4/5 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Discovered,
On Process
4/5
4/ 3/
3/6
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/12
9/
Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge
Forward Edge
Discover time/ Finish time
Cross Edge
DFS Analysis
Idea:
1. Call DFS(G) to compute finishing time f[ ]
2. Insert vertices onto a linked list according to decreasing order of f[ ]
Algorithm
colors each vertex:
WHITE : undiscovered
GRAY: discovered, in process
BLACK: finished, all adjacent vertices have been discovered
BFS (Intuition)
BFS: The Code
BFS(G, s) {
initialize vertices;
Q = {s};
while (Q not empty) {
u = Dequeue(Q);
for each v adjacent to u do {
if (color[v] == WHITE) {
color[v] = GRAY;
d[v] = d[u] + 1;// compute d[]
p[v] = u; // build BFS tree
Enqueue(Q, v);
}
}
color[u] = BLACK;
}
}
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Analysis
initialize : O(n)
Queue operations
each vertex is enqueued/dequeued at most once. Why?
each operation takes O(1) time, hence O(n)
Adjacency checks
adjacency list of each vertex is scanned at most once
sum of lengths of adjacency lists = O(e)
• http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/11-
Graph/dfs.html
• CLRS: 22.1, 22.2, 22.3, 22.4, 22.5
• HSR: 2.2, 2.5