Вы находитесь на странице: 1из 3

Applications of Depth-First Search: Another example of Topological Sort

Topological Sort (same digraph, different order to choosing verticies)

Label the vertices of an acyclic digraph G by 1, 2, ..., n, so that


vw is an edge of G ⇒ label[v] < label[w] . 2
B 3
4 1 D 14 15
13 A 4
B
1 N O
D 5 F
14 15 16
A 2
C
N O 6 Q 17
3 F
16 7 M 9 P
C
5 Q 17 E I
6 8 P 8
M
E I H 10 11
7
G L
H 9 10
12
G L 13 K
11 J
12 K
J

Perform a depth-first search of the digraph, with these additions:


Initialization: k = n;
Postorder processing label[v] = k; Vertices selected in reverse alphabetical order, when an arbitrary
of vertex v : –– k; choice must be made. Thick border indicates a starting vertex in
Back edge processing detect error (graph is not acyclic); depth-first search.
of edge vw :
Applications of Depth-First Search: Adjoin a vertex labeled “done”, with duration 0, and an edge from
Critical Path done to each source in the graph. Then perform a depth-first
search, with these additions:
We have a directed acyclic graph, in which each vertex v repre-
sents a task taking a known amount of time (duration[v]). An edge Initialization: for (each vertex v of graph )
from v to w indicates that task v depends on task w; that is, v eft[v] = 0;
cannot start until w has finished. (Otherwise, tasks may be critDep[v] = null;
performed in parallel.)
Tree edge postorder if ( eft[w] > eft[v] )
Find the earliest possible finish time.
processing and cross edge eft[v] = eft[w];
Find a critical path (a sequence of tasks, each dependent on the processing of edge vw:1 critDep[v] = w;
next, that prevents an earlier finish).
Postorder processing eft[v] = eft[v] + duration[v];
46,D of vertex v:
B 15,N
55,F 9 Back edge processing detect error (graph is not
M 12,O 11,Q edge vw: acyclic);
A 48,C 3
7 * N O
45,D F 1 4 Upon termination, eft[v] is the earliest finish time of task v. In
* 3 7,P particular, eft[done] is the earliest finish time for the entire set of
C
* 8 * 37,E
5,– tasks.
Q
55,A D 19,G 2 *
32,H P The critical path is c0,c1,...,ck, where
* 5 I 5
done E c0 = done,
0 26,I * 4
6 * * ci = critDep[ci–1],
H 15,L * 12,Q
22,S 7 critDep[ck] = null.
G * L
R 3 5
9 3,J
13,J 2,– K
J 1
S
11 2
1
No harm in performing these operations for descendent edges as well.
Applications of Depth-First Search: Strongly Connected Components — Phase 2
Strongly Connected Components — Phase 1
Form the transpose graph (edge directions reversed), retaining the
Perform a depth-first search of the digraph, numbering the vertices vertex numbering of phase 1.
as in topological ordering, except that back edges are not treated as
Perform a depth-first search of the transpose graph. In the outer
an error.
loop (for loop), process the vertices in the order of their numbers
2 (assigned in phase 1).
10
1 F M Each white vertex chosen in the outer loop is the leader of a
6 7
A 3 4 strongly connected component, and the depth-first search from that
N O leader processes the vertices of the component.
B E 8
2,A 10,N
5 P
11 9 1,A F M
D 6,N 7,O
13 Q
C A 3,A 4,A
12 I N O
B E 8,O
H 14 15
P
G L 11,C 5,A 9,O
D
13,H Q
16 C
17 K 12,H I
J H 14,H 15,H
G L

17,H K
J 16,H

Вам также может понравиться