Topological Sort - PowerPoint PPT Presentation

Topological sort l.jpg
1 / 12

  • Uploaded on
  • Presentation posted in: General

Topological Sort. Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation of Source Removal Algorithm. Review Questions. Introduction.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Topological Sort

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Topological sort l.jpg

Topological Sort

  • Introduction.

  • Definition of Topological Sort.

  • Topological Sort is Not Unique.

  • Topological Sort Algorithms.

  • An Example.

  • Implementation of Source Removal Algorithm.

  • Review Questions.

Introduction l.jpg


  • There are many problems involving a set of tasks in which some of the tasks must be done before others.

  • For example, consider the problem of taking a course only after taking its prerequisites.

  • Is there any systematic way of linearly arranging the courses in the order that they should be taken?

Yes! - Topological sort.

What is a dag l.jpg

What is a DAG?


  • A directed acyclic graph (DAG) is a directed graph without cycles.

  • DAGs arrise in modeling many problems that involve prerequisite constraints (construction projects, course prerequisites, document version control, compilers, etc.)

  • Some properties of DAGs:

    • Every DAG must have at least one vertex with in-degree zero and at least one vertex with out-degree zero

      • A vertex with in-degree zero is called a source vertex, a vertex with out-degree zero is called a sink vertex.

    • G is a DAG iff each vertex in G is in its own strongly connected component

    • Every edge (v, w) in a DAG G has finishingTime[w] < finishingTime[v] in a DFS traversal of G

Definition of topological sort l.jpg

Definition of Topological Sort

  • Given a directed graph G = (V, E) a topological sort of G is an

    ordering of V such that for any edge (u, v), u comes before v in

    the ordering.

  • Example1:

  • Example2:

Definition of topological sort5 l.jpg

Definition of Topological Sort

  • Example3: The graph in (a) can be topologically sorted as in (b)



Topological sort is not unique l.jpg

s1 = {a, b, c, d, e, f, g, h, i}

s2 = {a, c, b, f, e, d, h, g, i}

s3 = {a, b, d, c, e, g, f, h, i}

s4 = {a, c, f, b, e, h, d, g, i}


Topological Sort is not unique

  • Topological sort is not unique.

  • The following are all topological sort of the graph below:

Topological sort algorithms dfs based algorithm l.jpg

Topological Sort Algorithms: DFS based algorithm

  • Topological-Sort(G)

  • {

    • 1. Call dfsAllVertices on G to compute f[v] for each vertex v

    • 2. If G contains a back edge (v, w) (i.e., if f[w] > f[v]) , report error ;

    • 3. else, as each vertex is finished prepend it to a list;// or push in stack

    • 4. Return the list; // list is a valid topological sort

  • }

  • Running time is O(V+E), which is the running time for DFS.

Topological order: A C D B E H F G

Topological sort algorithms source removal algorithm l.jpg

Topological Sort Algorithms: Source Removal Algorithm

  • The Source Removal Topological sort algorithm is:

    • Pick a source u [vertex with in-degree zero], output it.

    • Remove u and all edges out of u.

    • Repeat until graph is empty.

  • int topologicalOrderTraversal( ){

  • int numVisitedVertices = 0;

  • while(there are more vertices to be visited){

  • if(there is no vertex with in-degree 0)

  • break;

  • else{

    • select a vertex v that has in-degree 0;

    • visit v;

    • numVisitedVertices++;

    • delete v and all its emanating edges;

  • }

  • }

  • return numVisitedVertices;

  • }

  • Topological sort source removal example l.jpg





















    D G A B F H J E I C

    Topological Sort: Source Removal Example

    • The number beside each vertex is the in-degree of the vertex at the start of the algorithm.

    Implementation of topological sort l.jpg

    Implementation of Topological Sort

    • The algorithm is implemented as a traversal method that visits the vertices in a topological sort order.

    • An array of length |V| is used to record the in-degrees of the vertices. Hence no need to remove vertices or edges.

    • A priority queue is used to keep track of vertices with in-degree zero that are not yet visited.

    public int topologicalOrderTraversal(Visitor visitor){

    int numVerticesVisited = 0;

    int[] inDegree = new int[numberOfVertices];

    for(int i = 0; i < numberOfVertices; i++)

    inDegree[i] = 0;

    Iterator p = getEdges();

    while (p.hasNext()) {

    Edge edge = (Edge);

    Vertex to = edge.getToVertex();



    Implementation of topological sort11 l.jpg

    Implementation of Topological Sort

    BinaryHeap queue = new BinaryHeap(numberOfVertices);

    p = getVertices();


    Vertex v = (Vertex);

    if(inDegree[getIndex(v)] == 0)



    while(!queue.isEmpty() && !visitor.isDone()){

    Vertex v = (Vertex)queue.dequeueMin();



    p = v.getSuccessors();

    while (p.hasNext()){

    Vertex to = (Vertex);

    if(--inDegree[getIndex(to)] == 0)




    return numVerticesVisited;


    Review questions l.jpg

    Review Questions

    • List the order in which the nodes of the directed graph GB are visited by

    • topological order traversal that starts from vertex a. Use both DFS-based and Source Removal algorithm

    • 2. What kind of DAG has a unique topological sort?

    • 3. Generate a directed graph using the required courses for your major. Now apply topological sort on the directed graph you obtained.

  • Login