Topological sort

  • It is an ordering of vertices in a directed acyclic graph in which each node comes before all the nodes to which it has outgoing edges

  • Topological sort for this graph will be : A, C, E, B, D

  • We first find a vertex which has no incoming edge (Indegree = 0)

  • Remove A, reduce the indegree of all its neighbors

  • The next vertex in this sort the one with indegree 0

Indegree

  • Number of inward directed graph edges for a given vertex

  • If there were no vertices with 0 indegree, then there would have been no topological sort

    • A = 0

    • B = 2

    • C = 1

    • E = 1

    • D = 2

Complexity

  • Running time for topological sort is O(V + E)

  • Each edge and every vertex visited once

Implementation

Indegree of adjacency list

Student schedule

  • We have 2 lists

    • List of courses

    • List od pre-reqs for each course

  • Pre-reqs are edges from one course to another - it is a relationship

  • All nodes with indegree = 0 are potential courses the student could start with

    • There are many schedule possible

Last updated