7
votes

Happy easters, everyone.

I am currently learning topological sort and having a question about what topological sort tries to really sort.

The Algorithm Design Manual describes topological sort in this way:

Topological sorting is the most important operation on directed acyclic graphs (DAGs). It orders the vertices on a line such that all directed edges go from left to right.

This bold part confuses me. So does the topological sorting sort vertices or all directed edges?

Let's take an example which is also in the book.

A DAG

So for the above DAG, we can get a topological sort (G, A, B, C, F, E, D).

I can understand this sort. Not only the vertices are sorted, but the edges are also sorted, i.e., G->A->B->C->F->E->D, this matches the above ADM book description: all directed edges go from left to right

But what if I remove the edge of B->C? The resulting graph is still a DAG, but will the topological sort is still (G, A, B, C, F, E, D)?

If it is Yes, then I think the edges are not sorted, as A->B->C does not exist any more, instead, it is A->B and A->C. So, it this case still a valid topological sort? Can we still think (G, A, B, C, F, E, D) is a valid sort even if A is the parent of B and C?

Thanks

1

1 Answers

9
votes

You can think of it as orderring of elements.

let v1,v2,...,vn be elements. and let an edge (vi,vj) denote that vi<vj. topological sort guarantees that after the sorting: for every vi, and for every vj such that i < j, vj is not greater then vi

Or in other notations: assume (vi,vj) indicate that vj is dependent on vi, topological sorts guarantees that each element "does not depend" on any elements that appears after it in the sort.

So does the topological sorting sort vertices or all directed edges?

topological sort sorts vertices, not edges. It uses edges as constraints for sorting the vertices.

But what if I remove the edge of B->C?

yes, every edge you add, just add a constraint. Note that there could be more then one feasible solution for topological sort [for example, for a graph without edges, any permutation is a feasible solution]. removing a constraint, makes any previous solution, which "solves a harder problem" still feasible.

Can we still think (G, A, B, C, F, E, D) is a valid sort even if A is the parent of B and C?

There is no problem with that! A appears before B,C in the topological sort, so there is no problem it is their father.

Hope that makes it a bit more clear.