Data Structures and Algorithms in Java
GitHubAuthor
  • Preface
    • Syllabus
    • Schedule
  • 0. Getting Started
    • 0.1. Environment Setup
    • 0.2. Quiz
  • 1. Java Essentials
    • 1.1. Abstraction
    • 1.2. Implementation
    • 1.3. Unit Testing
    • 1.4. Quiz
  • 2. Priority Queues
    • 2.1. Simple Priority Queues
    • 2.2. Binary Heap
    • 2.3. Unit Testing
    • 2.4. Benchmarking
    • 2.5. Quiz
  • 3. Sorting Algorithms
    • 3.1. Abstraction
    • 3.2. Comparison-based Sort
    • 3.3. Divide & Conquer Sort
    • 3.4. Distribution-based Sort
    • 3.5. Quiz
    • 3.6. Homework
  • 4. Binary Search Trees
    • 4.1. Binary Search Trees
    • 4.2. Balanced BST
    • 4.2. AVL Trees
    • 4.3. Red-Black Trees
    • 4.4. Quiz
  • 5. Tries
    • 5.1. Concept
    • 5.2. Implementation
    • 5.3. Quiz
    • 5.4. Homework
  • 6. Disjoint Sets
    • 6.1. Concept
    • 6.2. Implementation
    • 6.3. Quiz
  • 7. Graphs
    • 7.1. Implementation
    • 7.2. Cycle Detection
    • 7.3. Topological Sorting
    • 7.4. Quiz
  • 8. Minimum Spanning Trees
    • 8.1. Abstraction
    • 8.2. Prim's Algorithm
    • 8.3. Kruskal’s Algorithm
    • 8.4. Edmonds' Algorithm
    • 8.5. Quiz
    • 8.6. Homework
  • 9. Network Flow
    • 9.1. Flow Network
    • 9.2. Ford-Fulkerson Algorithm
    • 9.3. Simplex Algorithm
    • 9.3. Quiz
  • 10. Dynamic Programming
    • 10.1. Fibonacci Sequence
    • 10.2. Tower of Hanoi
    • 10.3. Longest Common Subsequence
    • 10.4. Quiz
Powered by GitBook

©2023 Emory University - All rights reserved

On this page
  • By Incoming Scores
  • By Depth-First
  • Implementation

Was this helpful?

Export as PDF
  1. 7. Graphs

7.3. Topological Sorting

This section discusses two algorithms for topological sorting.

Previous7.2. Cycle DetectionNext7.4. Quiz

Last updated 4 years ago

Was this helpful?

The task of topological sorting is to sort vertices in a linear order such that for each vertex, all target vertices associated with its incoming edges must appear prior to it.

By Incoming Scores

The followings demonstrate how to perform a topological sort by incoming scores, where the incoming score of a vertex is define as the sum of all source vertices:

By Depth-First

The followings demonstrate how to perform a topological sort by depth-first traversing:

Implementation

Let us define the topological_sort() method under the Graph class that performs topological sort by either incoming scores or depth-first search:

public List<Integer> topological_sort(boolean depth_first) {
    Deque<Integer> global = getVerticesWithNoIncomingEdges();
    List<Deque<Edge>> outgoingEdgesAll = getOutgoingEdges();
    List<Integer> order = new ArrayList<>();

    while (!global.isEmpty()) {
        Deque<Integer> local = new ArrayDeque<>();
        int vertex = global.poll();
        order.add(vertex);
        Deque<Edge> outgoingEdges = outgoingEdgesAll.get(vertex);
        
        while (!outgoingEdges.isEmpty()) {
            Edge edge = outgoingEdges.poll();

            //Remove edge in all incomingEdges of the target vertex
            List<Edge> incomingEdges = getIncomingEdges(edge.getTarget());
            incomingEdges.remove(edge);

            //If the vertex has no incoming edges, add it to the local queue awaited to be added to the global deque
            if (incomingEdges.isEmpty())
                local.add(edge.getTarget());
        }

        //Transfer all vertices in local to global
        while (!local.isEmpty())
            if (depth_first) global.addFirst(local.removeLast());
            else global.addLast(local.removeFirst());
    }

    if (!hasNoEdge()) throw new IllegalArgumentException("Cyclic graph.");
    return order;
}
  • L2: starts with vertices with no incoming edges.

  • L3: retrieves outgoing edges of each source vertex.

  • L4: consists of the list of vertices in topological order.

  • L6: iterates until no more vertex to visit:

    • L8-9: adds a source vertex to order.

    • L12: iterates through all the outgoing edges of the source vertex:

      • L17: removes the outgoing edge from the graph.

      • L20-21: adds the target vertex if it has no incoming edge left to local.

    • L25-27: adds all vertices in local to global.

What is the worst-case complexity of the topological_sort() method?