Graph Algorithms

Topological Sort

Order vertices in a directed acyclical graph such that if there is a path from Vi to Vj then Vj appears after Vi in the path.

An Example - Figure out the sequence of Courses you need to take to get to CS3240

Graph - directed graph representing courses and pre-requisite information.

Vertices - classes

Edges - directed edge represent for the course at the starting vertex an immediate pre-requisite course which is represented by the ending vertex of the edge.

 

Algorithm - Topological Sort

Order vertices in a directed acyclical graph such that if there is a path from Vi to Vj then Vj appears after Vi in the path.

Simple algorithm -
1) Find any vertex with no incomming edges. Put it on the order list and remove it and its edges from the Graph.

2) Repeat step on until there are no more vertices.

Result on our example:

CS1020, CS1160, CS2340, CS2360, CS3240

(is there another possible order??)

Example Pseudocode

 

 

© Lynne Grewe