<<

Topological Sorting A topological sort is the process of sorting items over which a partial order is defined. A partial order is an ordering given over some pairs of items but not among all of them. A partial order can be defined as a , such that if a exists from v to w , then w appears after v in the ordering. That is, a directed edge (v,w) indicates that w follows v . Example (DAG describing a )

The problem that topological sorting attempts to solve is to transform the partial order in the DAG into a linear order. Graphically, this implies the arrangement of vertices in a row, such that all arrows point to the right. Example (Topological sort showing the linear arrangement)

The topologically sorted order is not necessarily unique. That is there may be other valid orderings that are also partial orders that describe the ordering in a DAG. Example

1 7 2 9 4 10 6 3 5 8

Data structure for dynamic linked adjacency list implementation struct Leader; struct Follower;

struct Follower { Leader *adjacentLeader; Follower *nextFollower; };

struct Leader { Object element; int inDegree; Leader *nextLeader; Follower *firstFollower; };

Leader *head, *tail, *p, *q, *r; Follower *f Object x, y; int elementCount;

Linked list implementation TopologicalSort (input)

head = new Leader node tail = head elementCount = 0 x = Read (input) while x != END_OF_INPUT y = Read (input) p = BuildList (x, head, tail, elementCount) q = BuildList (y, head, tail, elementCount) f = new Follower node f -> adjacentLeader = q f -> nextFollower = p -> firstFollower p -> firstFollower = f q -> inDegree++ x = Read(input)

p = head head = NULL r = tail i = 0 while p != r q = p p = p -> nextLeader if q -> inDegree == 0 q -> nextLeader = head head = q i++ if i == 1 tail = q

q = head r = tail while q != NULL Print (q -> element) elementCount -- f = q -> firstFollower q = q -> nextLeader while f != NULL p = f-> adjacentLeader p -> inDegree -- if p -> inDegree == 0 p -> nextLeader = NULL r -> nextLeader = p r = p if q == NULL q = r f = f -> nextFollower if elementCount != 0 Print “ERROR - SET NOT PARTIALLY ORDERED” BuildList (w, head, tail, i)

h = head tail -> element = w while h -> element != w h = h -> nextLeader if h == tail tail = new Leader node i++ h -> inDegree = 0 h -> firstFollower = NULL h -> nextLeader = tail return h Basic Idea Behind Output Phase 1. Select a vertex with inDegree = 0 2. Print it out 3. Decrement the inDegree of all adjacent vertices 4. Goto step 1 Partial Order

0 1 1 2

1 2 1 3 4 5

Not a Partial Order

A graph has a topological ordering if and only if it is acyclic. 2 The input phase of TopologicalSort has O(V + E ). For each vertex, we need to check whether it is already in the leader list. An like

1 n n n2 this does approximately 2 ( −1) work (i.e. O( )).

Each edge must be added to the structure. The output phase has O(V + E ).

Each vertex is printed once. There are V vertices.

Each time a vertex is printed, all adjacent vertices are decremented by one. This requires E

edge traversals.