07d-topologicalsort

Upload: frankjamison

Post on 04-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 07d-TopologicalSort

    1/6

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 1

    Graphs

    TopologicalSort

  • 8/13/2019 07d-TopologicalSort

    2/6

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 2

    Topological Sorting

    When a set of tasks has to be performed, you may need to order thetasks based on priority or dependency

    For example, a student cannot take CSII unless he/she takes CSI

    First.

    Or for example in a program, procedure 2 cannot be executed

    unless procedure 1 is executed; however, order of procedure 3 and

    procedure 4 may not be important

    A topological sort creates a linear structure of a diagram; that is it

    labels all its vertices with number 1, 2, , |V| so that i

  • 8/13/2019 07d-TopologicalSort

    3/6

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 3

    The diagram must not include cycle; otherwise, the topologicalsort is impossible

    The algorithm for topological sort is To find a vertex vwith no outgoing edge called a sinkor aminimal vertexand then disregard all edges leading from anyvertex to v

    To summarize the above:TopologicalSort (diagraph)for i=1 to |V|{

    find a minimal vertex v;num(v) = i;

    }remove from diagraph vertex v and all edges incident with v

    The next example shows a graph that goes through a sequence ofdeletion to find the topological sort of the vertices

  • 8/13/2019 07d-TopologicalSort

    4/6

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 4

    c

    a

    f

    d

    b

    g

    e c

    a

    f

    d

    b

    e

    First minimalvertex is g

    c

    a

    f

    d

    b

    c

    a

    f

    dc

    a

    d

    c

    a

    aNULL

    nextoneis e

    next one is bnext one is f

    nextone

    is d

    next one is c next one is a

  • 8/13/2019 07d-TopologicalSort

    5/6

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 5

    TS(v)

    num(v) = i++;

    for all vertices u adjacent to v

    if num(u) = 0TS(u)

    else if TSNum(u) = 0 // cycle is detected

    error;

    TSNum(v) =j++

    topologicalSorting(digraph)

    for all vertices v

    num(v) = TSNum(v) = 0

    i = j = 1;while there is a vertex v such that num(v) = 0

    TS(v)

    Output vertices according to their TSNums

    This is a differentalgorithm thattravels through the

    vertices and marksthe vertices basedon their dependencyto find a topologicalsort of vertices

    This algorithm doesnot remove anyvertex or edge from

    the graph

  • 8/13/2019 07d-TopologicalSort

    6/6

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 6

    c

    a

    f

    d

    b

    g

    e c(2)

    a(1)

    d(3)

    d

    f

    f(7)b(4) e

    e(5)

    g(6)

    The vertices are numbered as they are ordered

    The topological sort of the vertices can be found if youprint this tree in post order traversal

    g e b f d c a

    TSNum(g) = 1

    TSNum(b) = 3

    TSNum(e) = 2

    TSNum(f) = 4

    TSNum(d) = 5

    TSNum(c) = 6

    TSNum(a) = 7