Graph Topological Sort Using Depth-First Search

Graph Topological Sort Using Depth-First Search

How to Perform a Topological Sort on a Directed Graph

Introduction to Topological Sorting

  • The tutorial introduces topological sorting of directed graphs using depth-first search (DFS). Familiarity with DFS is assumed.
  • In the context of project management, vertices represent tasks and edges denote dependencies. For example, an edge from A to B indicates that task B cannot start until task A is completed.

Understanding Dependencies

  • Two valid sequences for tasks are presented: ABCDE and ACBDE. Both maintain the dependency that A precedes B, but their order can vary as long as dependencies are respected.
  • Topological sorting arranges vertices based on interconnecting edges rather than numerical values, making it a non-numerical sort.

Assigning Topological Numbers

  • Vertices are assigned numbers from 0 to n-1 (where n is the number of vertices), creating what are known as topological numbers.
  • If there’s a cycle in the graph (e.g., A depends on B and vice versa), topological sorting is impossible since neither can proceed without the other.

Conditions for Topological Sorting

  • Only directed acyclic graphs (DAGs) can be topologically sorted. The goal is to develop an algorithm that assigns topological numbers effectively.
  • Both DFS and breadth-first search (BFS) can assist in this process; however, DFS will be explored first.

Implementing Depth First Search for Topology

  • The challenge arises when trying to assign increasing numbers during vertex visits. An alternative approach involves assigning numbers upon backing up from a vertex in DFS.
  • By following DFS from vertex A through its neighbors and assigning numbers only when backing up, we ensure correct ordering based on dependencies.

Example Walkthrough of DFS Process

  • Starting at vertex E after visiting all paths leads us to assign it the highest number (4), then backtrack through D and B while assigning decreasing values accordingly.
  • Depending on which neighbor we choose first during traversal, different valid sequences may emerge—demonstrating flexibility in achieving topological order.

Code Implementation Overview

  • The video transitions into coding by modifying existing recursive DFS methods within a graph class structure for implementing topological sorting.
  • Key modifications include identifying where to assign topological numbers right after completing recursive calls for all neighboring vertices.

Topological Sorting and Recursive Calls

Understanding Recursive Calls in Topological Sorting

  • The method for topological sorting can be optimized by directly returning n - 1 after the recursive call, rather than first recommending then returning.
  • The return value of n may not always be simply one less than the input; an example is provided to clarify this concept.

Writing Out Topological Sequences

  • To write out the topological sequence, we need to identify vertices based on their top numbers, starting with the vertex corresponding to top number 0.
  • Searching for each top number can become computationally expensive, potentially leading to O(N²) time complexity if they appear in reverse order.

Optimizing Topological Sequence Retrieval

  • A more efficient approach involves using top numbers as array indices and vertex numbers as entries, allowing linear time retrieval of the sequence.
  • This requires modifying code so that n serves as the index while v becomes the array entry.

Handling Traversal from Different Starting Points

  • If traversal starts at a different vertex (e.g., C), only certain vertices will be reached. The algorithm must restart from unvisited vertices when necessary.
  • A driver method is used to initiate recursive DFS calls for each new starting vertex, ensuring all vertices are eventually reached.

Verifying Topological Numbers Across Different Starts

Video description

In this video tutorial, you will learn how to do a topological sort on a directed acyclic graph (DAG), i.e. arrange vertices in a sequence according to dependency constraints shown by edges. The sort is done by using the depth-first search algorithm as the basis, and modifying it to label vertices with sequence numbers. The complete code is available at https://www.dropbox.com/s/71fe4ttpj27jxa8/Graph.java?dl=0 and the examples graphs illustrated in the video are at https://www.dropbox.com/s/6s8i9ji1wla28we/topsort_graph1.txt?dl=0 and https://www.dropbox.com/s/tasix4uihrs1n5p/topsort_graph2.txt?dl=0 Error: At around 6:24, the graph example used has a cycle including X, C, D, P. The edge from X to C should go in the opposite direction, from C to X. Everything else works as it should.