Title: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001
1 UMass Lowell Computer Science 91.404 Analysis
of Algorithms Prof. Karen Daniels Spring, 2001
- Makeup Lecture
- Chapter 23 Graph Algorithms
- Depth-First Search Breadth-First Search
- Topological Sort
- Tuesday, 5/8/01
- Source Cormen et al. textbook except where
noted
2Depth-First Search (DFS) Breadth-First
Search (BFS)
- Running Time Analysis
- Vertex Color Changes
- Edge Classification
- Using the Results of DFS BFS Examples
3Running Time Analysis
- Key ideas in the analysis are similar for DFS and
BFS. In both cases we assume an Adjacency List
representation. Lets examine DFS. - Let t be number of DFS trees generated by DFS
search - Outer loop in DFS(G) executes t times
- each execution contains call DFS_Visit(G,u)
- each such call constructs a DFS tree by visiting
(recursively) every node reachable from vertex u - Time
- Now, let ri be the number of vertices in DFS tree
i - Time to construct DFS tree i
continued on next slide...
4Running Time Analysis
- Total DFS time
- Now, consider this expression for the extreme
values of t - if t1, all edges are in one DFS tree and the
expression simplifies to O(E) - if tV, each vertex is its own (degenerate) DFS
tree with no edges so the expression simplifies
to O(V) - O(VE) is therefore an upper bound on the time
for the extreme cases - For values of t in between 1 and V we have
these contributions to running time - 1 for each vertex that is its own (degenerate)
DFS tree with no edges - upper bound on this total is O(V)
- AdjListu for each vertex u that is the root
of a non-degenerate DFS tree - upper bound on this total is O(E)
- Total time for values of t in between 1 and V
is therefore also O(VE)
Note that for an Adjacency Matrix representation,
we would need to scan an entire matrix row
(containing V entries) each time we examined
the vertices adjacent to a vertex. This would
make the running time O(V2) instead of O(VE).
5Vertex Color Changes
- Vertex is WHITE if it has not yet been
encountered during the search. - Vertex is GRAY if it has been encountered but has
not yet been fully explored. - Vertex is BLACK if it has been fully explored.
6Edge Classification
- Each edge of the original graph G is classified
during the search - produces information needed to
- build DFS or BFS spanning forest of trees
- detect cycles (DFS) or find shortest paths (BFS)
- When vertex u is being explored, edge e (u,v)
is classified based on the color of v when the
edge is first explored - e is a tree edge if v is WHITE for DFS and BFS
- e is a back edge if v is GRAY for DFS only
- for DFS this means v is an ancestor of u in the
DFS tree - e is a forward edge if v is BLACK and for DFS
only v is a descendent of u in the DFS tree - e is a cross edge if v is BLACK and for DFS
only there is no ancestor or descendent
relationship between u and v in the DFS tree - Note that
- For BFS well only consider tree edges.
- For DFS we consider all 4 edge types.
- In DFS of an undirected graph, every edge is
either a tree edge or a back edge.
7Using the Results of DFS BFS
- Using DFS to Detect Cycles
- Using BFS for Shortest Paths
A directed graph G is acyclic if and only if a
Depth-First Search of G yields no back edges.
see p. 486 of text for proof Note DFS can also
be used to detect cycles in undirected graphs if
notion of cycle is refined appropriately.
A Breadth-First Search of G yields shortest path
information For each Breadth-First Search
tree, the path from its root u to a vertex v
yields the shortest path from u to v in G.
see p. 472-475 of text for proof
8Example DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
Edge Classification Legend T tree edge B
back edge F forward edge C cross edge
Source Graph is from Computer Algorithms
Introduction to Design and Analysis by Baase and
Gelder.
9Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
B
G
C
E
F
10Example (continued)DFS of Directed Graph
A
D
T
B
G
C
E
F
11Example (continued)DFS of Directed Graph
A
D
T
B
G
C
E
F
12Example (continued)DFS of Directed Graph
A
D
T
B
G
T
C
E
F
13Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
B
G
T
C
E
F
14Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
B
G
T
C
E
F
15Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
C
E
F
16Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
C
E
F
17Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
G
T
C
E
F
18Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
G
C
T
C
E
F
19Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
G
C
T
C
E
F
20Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
G
C
T
C
E
F
21Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
G
F
C
T
C
E
F
22Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
G
F
C
T
C
E
F
23Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
G
F
C
T
C
E
F
24Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
C
E
F
25Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
C
E
F
C
26Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
C
E
F
C
27Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
C
E
F
C
28Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
C
E
F
C
29Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
C
E
F
C
C
30Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
T
C
E
F
C
C
31Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
B
T
B
G
F
C
T
T
C
E
F
C
C
32Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
C
B
T
B
G
F
C
T
T
C
E
F
C
C
33Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
C
B
T
B
G
F
C
T
T
B
C
E
F
C
C
34Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
C
B
T
B
G
F
C
T
T
B
C
E
F
C
C
35Example (continued)DFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
B
A
D
T
T
C
B
T
B
G
F
C
T
T
B
C
E
F
C
C
36Example (continued)DFS of Directed Graph
A
T
T
E
B
A
D
B
F
T
B
T
T
C
B
F
B
B
T
B
G
G
F
C
T
T
T
C
C
T
B
D
C
E
C
F
C
C
C
C
DFS Tree 2
DFS Tree 1
37Example DFS of Undirected Graph
38Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
G
C
E
F
39Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
G
C
E
F
40Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
G
C
E
F
41Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
G
T
C
E
F
42Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
G
T
C
E
F
43Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
G
T
B
C
E
F
44Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
G
T
B
C
E
F
45Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
G
T
B
C
E
F
46Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
G
T
B
C
E
F
47Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
B
T
G
T
B
C
E
F
48Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
C
E
F
49Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
C
E
F
50Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
C
E
F
51Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
C
E
F
52Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
B
C
E
F
53Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
B
C
E
F
54Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
B
C
E
F
55Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
B
C
E
F
56Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
B
T
C
E
F
57Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
G
T
B
T
B
T
C
E
F
58Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
B
G
T
B
T
B
T
C
E
F
59Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
B
G
T
B
T
B
T
C
E
F
60Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
B
G
T
B
T
B
T
C
E
F
61Example DFS of Undirected Graph
B
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
T
T
B
T
B
G
T
B
T
B
T
C
E
F
62Example DFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
63Example (continued)DFS of Undirected Graph
A
T
B
B
B
T
B
C
B
T
T
D
F
T
B
G
T
E
DFS Tree
64Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
Edge Classification Legend T tree edge
only tree edges are used
Source Graph is from Computer Algorithms
Introduction to Design and Analysis by Baase and
Gelder.
65Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
B
G
E
C
F
Queue A
66Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
B
G
E
C
F
Queue AB
67Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
B
G
T
E
C
F
Queue ABC
68Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
B
G
T
T
E
C
F
Queue ABCF
69Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
B
G
T
T
E
C
F
Queue BCF
70Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
E
C
F
Queue BCFD
71Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
E
C
F
Queue CFD
72Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
E
C
F
Queue FD
73Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
E
C
F
Queue D
74Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
E
C
F
Queue -
75Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
E
C
F
Queue E
76Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
T
E
C
F
Queue EG
77Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
T
E
C
F
Queue G
78Example BFS of Directed Graph
Adjacency List A B,C,F B C,D C - D A,C E
C,G F A,C G D,E
A
D
T
T
B
G
T
T
T
E
C
F
Queue -
79Example (continued)BFS of Directed Graph
Shortest path distance from E to G 1
Shortest path distance from A to B 1
A to C 1 A to F 1 A to D 2
BFS Tree 2
BFS Tree 1
80Example BFS of Undirected Graph
Edge Classification Legend T tree edge
only tree edges are used
81Example BFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
B
G
C
E
F
Queue A
82Example BFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
G
C
E
F
Queue AB
83Example BFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
G
C
E
F
Queue ABC
84Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
G
C
E
F
Queue ABCD
85Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
T
G
C
E
F
Queue ABCDF
86Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
T
G
C
E
F
Queue BCDF
87Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
T
G
C
E
F
Queue CDF
88Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
T
G
C
E
F
T
Queue CDFE
89Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
B
T
T
G
C
E
F
T
Queue DFE
90Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
T
B
T
T
G
C
E
F
T
Queue DFEG
91Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
T
B
T
T
G
C
E
F
T
Queue FEG
92Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
T
B
T
T
G
C
E
F
T
Queue EG
93Example BFS of Undirected Graph
T
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
A
D
T
T
B
T
T
G
C
E
F
T
Queue G
94Example BFS of Undirected Graph
Adjacency List A B,C,D,F B A,C,D C
A,B,D,E,F D A,B,C,G E C,G F A,C G D,E
Queue -
95Example (continued)BFS of Undirected Graph
Shortest path distance from A to B 1 A to
E 2 A to C 1 A to G 2 A to D 1
A to F 1
BFS Tree
96Topological Sort
- Source Previous 91.404 instructors
97Definition DAG
- A Directed Acyclic Graph is often abbreviated by
DAG - As noted on slide 7, if DFS of a directed graph
yields no back edges, then the graph contains no
cycles this is Lemma 23.10 in text
This graph has more than one cycle. Can you find
them all?
This graph has no cycles, so it is a DAG.
98Definition Topological Sort
- A topological sort of a dag G (V, E) is a
linear ordering of all its vertices such that if
G contains an edge (u, v), then u appears before
v in the ordering. - If the graph is not acyclic, then no linear
ordering is possible. - A topological sort of a graph can be viewed as an
ordering of its vertices along a horizontal line
so that all directed edges go from left to right.
- Topological sorting is thus different from the
usual kind of "sorting" .
99Definition Topological Sort
- Directed acyclic graphs are used in many
applications to indicate precedences among
events. - Figure 23.7 gives an example that arises when
Professor Bumstead gets dressed in the morning.
The professor must don certain garments before
others (e.g., socks before shoes). Other items
may be put on in any order (e.g., socks and
pants). A directed edge (u,v) in the dag of
Figure 23.7(a) indicates that garment u must be
donned before garment v. A topological sort of
this dag therefore gives an order for getting
dressed. Figure 23.7(b) shows the topologically
sorted dag as an ordering of vertices along a
horizontal line such that all directed edges go
from left to right.
100Definition Topological Sort
- The following simple algorithm topologically
sorts a dag. - TOPOLOGICAL-SORT(G)
- call DFS(G) to compute finishing times fv for
each vertex v (this is equal to the order in
which vertices change color from gray to black) - as each vertex is finished (turns black), insert
it onto the front of a linked list - return the linked list of vertices
101Definition Topological Sort
- Figure 23.7(b) shows how the topologically sorted
vertices appear in reverse order of their
finishing times. - We can perform a topological sort in time Q(V
E), since depth-first search takes Q(V E) time
and it takes 0(1) time to insert each of the V
vertices onto the front of the linked list.
102Definition Topological Sort
- Theorem 23.11 TOPOLOGICAL-SORT(G) produces a
topological sort of a directed acyclic graph G. - Proof Suppose that DFS is run on a given dag G
(V, E) to determine finishing times for its
vertices. It suffices to show that for any pair
of distinct vertices u,v Î V, if there is an edge
in G from u to v, then fv lt fu. Consider any
edge (u,v) explored by DFS(G). When this edge is
explored, v cannot be gray, since then v would be
an ancestor of u and (u,v) would be a back edge,
contradicting Lemma 23.10. Therefore, v must be
either white or black. If v is white, it becomes
a descendant of u, and so fv lt fu. If v is
black, then fv lt fu as well. Thus, for any
edge (u,v) in the dag, we have fv lt fu,
proving the theorem.
103Example
- For the DAG of slide 97
- DFS produces this result
- this contains 2 DFS trees
- Vertices are blackened in the following order
- C, B, F, A, D, E , G
104Example
- Vertices are added to front of a linked list in
the blackening order. - Final result is shown below
- Note that all tree edges and non-tree edges point
to the right
C
T
C
T
T
T
C
B
C
G
A
D
E
F
T
C
F