Você está na página 1de 6

Topological Sorting

CSC 3102

B.B. Karki, LSU

Digraph
A directed graph or digraph is a graph with

a c

directions specified for all its edges


Resulting forest are complex: DFS forest exhibits all four types of edges possible: Tree edges: ab, bc, de Back edges: ba Forward edges: ac Cross edges: dc Directed acyclic graph (dag): has no back edges. Directed cycle: A back edge in DFS forest can connect a vertex to its parent. The presence of a back edge indicates that diagraph has a directed cycle.
CSC 3102
2

d e

a b c

d e

B.B. Karki, LSU

Digraph - Example
A part-time student needs to take a set of five courses

{C1, C2, C3, C4, C5}, only one course per term, in any order as long as the following course prerequisites are met:
C1 and C2 have no prerequisites C3 requires C1 and C2 C4 requires C3 C5 requires C3 and C4.

C1 C3 C2

C4

The situation can be modeled by a diagraph: Vertices represent courses. Directed edges indicate prerequisite requirements. Topological sorting problem: How to list the vertices

C5

in such an order that, for every edge (u,v) in the graph, the vertex (u) where the edge starts is listed before the vertex (v) where the edge ends.
Find an ordering of digraphs vertices from left to right. Solution is possible only if digraph is a dag. This is a

Digraph representing the prerequisite structure of five courses

necessary and sufficient condition for topological sorting to be possible.


CSC 3102
3

B.B. Karki, LSU

Solving Topological Sorting Problem


Solution: Verify whether a given digraph is a dag and, if it is, produce

an ordering of vertices.
Two algorithms for solving the problem. They may give different

(alternative) solutions. DFS-based algorithm Perform DFS traversal and note the order in which vertices become dead ends (that is, are popped of the traversal stack). Reversing this order yields the desired solution, provided that no back edge has been encountered during the traversal. Source removal algorithm Identify a source, which is a vertex with no incoming edges and delete it along with all edges outgoing from it. There must be at least one source to have the problem solved. Repeat this process in a remaining diagraph. The order in which the vertices are deleted yields the desired solution.
CSC 3102
4

B.B. Karki, LSU

DFS-Based Topological Sorting


C1 C3 C2 C5 C4 C51 C42 C33 C14 The popping-off order: C5, C4, C3, C1, C2 The topologically sorted list: C2 C1 C3 C4

C25

C5

DFS traversal stack with the subscript numbers indicating the popping off order. All edges in the sorted list point from left to right. Time efficiency is in O(|V2|) for the adjacency matrix representation and O(|V |+|E |) for the adjacency linked list representation. Since the reversing requires only (|V |) and it can stop before processing the entire digraph if a back edge is encountered.
5

CSC 3102

B.B. Karki, LSU

Source Removal Topological Sorting


C1 C3 C2 C5 C4 Delete C1 C3 C2 C5 C4 Delete C2 C3 C5 C4

C4 Delete C3 Delete C4 Delete C5

C5

C5

The solution obtained is C1, C2, C3, C4, C5



CSC 3102

If there are more than one sources (C1 and C2), break the tie arbitrarily. On each iteration, a vertex with no incoming edges is deleted from the digraph.
6

B.B. Karki, LSU

Você também pode gostar