da12

Upload: sasisanjeev

Post on 07-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 DA12

    1/6

    B.B. Karki, LSU1CSC 3102

    Topological Sorting

  • 8/4/2019 DA12

    2/6

    B.B. Karki, LSU2CSC 3102

    Digraph

    A directed graph or digraph is a graph withdirections 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.

    c

    a

    d

    b

    e

    a

    b

    c

    d

    e

  • 8/4/2019 DA12

    3/6

    B.B. Karki, LSU3CSC 3102

    Digraph - Example

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

    {C1, C2, C3, C4, C5}, only one course per term, inany order as long as the following courseprerequisites are met:

    C1 and C2 have no prerequisites

    C3 requires C1 and C2

    C4 requires C3

    C5 requires C3 and 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

    in such an order that, for every edge (u,v) in thegraph, the vertex (u) where the edge starts is listedbefore 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 anecessary and sufficient condition for topologicalsorting to be possible.

    C2

    C1

    C3

    C5

    C4

    Digraph representingthe prerequisite structure

    of five courses

  • 8/4/2019 DA12

    4/6

    B.B. Karki, LSU4CSC 3102

    Solving Topological Sorting Problem

    Solution: Verify whether a given digraph is a dag and, if it is, producean 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 edgehas been encountered during the traversal.

    Source removal algorithm

    Identify a source, which is a vertex with no incoming edges and delete italong 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.

  • 8/4/2019 DA12

    5/6

    B.B. Karki, LSU5CSC 3102

    DFS-Based Topological Sorting

    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 processingthe entire digraph if a back edge is encountered.

    C2

    C1

    C3

    C5

    C4 The popping-off order:C5, C4, C3, C1, C2

    The topologically sorted list:C2 C1 C3 C4 C5

    C51

    C42

    C33

    C14

    C25

  • 8/4/2019 DA12

    6/6

    B.B. Karki, LSU6CSC 3102

    Source Removal Topological Sorting

    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.

    C2

    C1

    C3

    C5

    C4

    C2

    C3

    C5

    C4

    C3

    C5

    C4

    C5

    C4

    C5

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

    Delete C1 Delete C2

    Delete C3 Delete C4 Delete C5