15-111 Lecture 32 (Friday, April 10, 2009)

Topological Sort

A topological sort is an ordering of vertices in a directed, acyclic (no cycles) graph which travels in one direction only. In order to get a college degree, you must complete a topological sort (ordering) of the required courses, taking prerequisites before you take the coures which build on those prerequisites. Think of the core (or part of the core) curriculum for Information Science. Which vertices have an indegree of 0? The freshman-level courses. Topological sort puts all vertices with an indegree of 0 into a queue.

```36-201,	15-1xx,	21-121
```

While the queue is not empty, dequeue.

Dequeue 36-201 and remove it. What happens to the indegree of 36-202? It becomes 0, so enqueue it.

```15-1xx,	21-121,	36-202
```

Dequeue 15-1xx and remove it. Now the indegree of 15-200 is 0, so enqueue it.

```21-121,	36-202, 15-200
```

Dequeue 21-121 and remove it. Now the indegree of 21-122 is 0, so enqueue it.

```36-202, 15-200, 21-121
```

Dequeue 36-202 and remove it. Now the indegree of 36-203 is 0, so enqueue it.

```15-200, 21-121, 36-203
```

Dequeue 15-200 and remove it. This does not reduce the indegree of any vertex to 0.

```21-121, 36-203
```

Dequeue 21-121 and remove it. Now the indegree of 66-270 is 0, so enqueue it.

```36-203, 66-270
```

Dequeue 36-203. This does not reduce the indegree of any vertex to 0.

```66-270
```

Dequeue 66-270 and remove it. Now the indegree of 66-271 is 0, so enqueue it.

```66-271
```

Dequeue 66-271 and remove it. Now the indegree of 66-272 is 0, so enqueue it.

```66-272
```

Dequeue 66-272 and remove it. Now the indegree of 66-273 is 0, so enqueue it.

```66-273
```

How would you implement a topological sort of a graph? You'll need a queue. You'll also need to keep track of the indegree of each vertex in the graph so that you can enqueue vertices of indegree 0. You'll start your algorithm by enqueing every vertex in the graph with an indegree of 0, and your algorithm will terminate when the queue is empty.

Assuming that the indegree of each vertex is stored and that the graph has been read into an adjacency list, we can apply the following (pseudocode) algorithm to generate a topological ordering. Let's assume that the graph is directed and has no cycles.

``````
void topologicalSort()
{
Queue q; //keeps track of what vertex is next (e.g., what course to take next)
Vertex v, w; //references to Vertex objects
int counter=0; //keeps track of the topological ordering of the dequeued vertices (e.g., order of courses taken)

q=new Queue();

//enqueue all vertices with an indegree of 0 (e.g., courses with no unfulfilled prerequisites
for each vertex v
if (v.indegree == 0)
q.enqueue(v);

//dequeue vertices one at a time, reducing the indegree of their children as you remove them
while(!q.isEmpty())
{
v = q.dequeue();
v.topNum == ++counter; //give the vertex you're dequeing its order in the topological sort

for each w adjacent to v
if(--w.indegree == 0)
q.enqueue(w);
}
}
```
```