notes42 - Topological Sort of a Directed Graph - Sorting of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Topological Sort of a Directed Graph - Sorting of the vertex such that for every edge (u, v), the vertex u appears before vertex v. Example: The following are not topological sorts of this graph: - A, D, B, C, E because B has to appear before D - A, B, C, E, D because D has to appear before E *Topological sort may not be unique. Topological sort is useful for scheduling events: 1. Building 2. “make” program a.o: a.c gcc -c a.c b.o: b.c gcc -c b.c c: a.o b.o gcc -o c a.o b.o Topological Sorting Algorithm Input: a directed graph G with n vertices Output: a topological ordering v 1 , v 2 , v 3 , …, v n of G. S: an initially empty stack //Compute the indegree (number of incoming edges) of every vertex:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
for each edge (v, w) indegree[w]++ end loop for each vertex u in G if indegree[u] == 0 S.push(u) end if end loop i = 0 while S is not empty u = S.pop() sorted[i] = u //u is the i th vertex i++ //recomputed indegree for all outgoing edges (u, w)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University.

Page1 / 5

notes42 - Topological Sort of a Directed Graph - Sorting of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online