notes68

# notes68 - April 24th Topological Sort April 26th Breadth...

This preview shows pages 1–4. Sign up to view the full content.

April 24th: Topological Sort April 26th: Breadth First Search / Minimum Spanning Tree April 28th: Implementation of minimum spanning tree in Java / Review for final April 24th Topological Sort It is a sorting of the verttices such that if there is an edge i,j (i -> j) the i should appear in the sorting before j. Topological Sort: A B C D E F or A C B D E F Not a Topological Sort: C E A B D F why? there is an edge (A -> C), but C appears before A. A topological sort is useful for scheduling of events. A topological sort that depend on each other would be a possible schedule of the execution building of a house: Topological Sort: 1. Foundation, 2. Grass, 3. Walls, 4. Electric, 5. Piping, 6. Windows. Algorithm: Topological Sorting

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

View Full Document
Input: A digraph G with n vertices. Output: An array "sorted" with the vertices in topological order. //we use a stack to store the vertices with no dependencies. (We could use a queue or other container). stack{}; //Compute indegree of vertices for each edge(v,w){ // v->w indegree[w]++; } //Put vertices without incident edges (no dependencies) in stack for all u in G do if indegree[v] == 0 push v in stack; end end i=0; //index of "sorted" while stack is not empty u <- stack.pop() //u has no dependencies: add to "sorted" sorted[i] = u; i++; for all outgoing edges (u,w) of u do{ //remove dependency of u on w indegree[w]--; if indegree[w] == 0){ push w into stack; end end end if ( i < n ) { //not all vertices are sorted return null //topological sort not possible } return sorted Graphs with cycles do NOT have topological sort:
Sort: A B C -> Not a topological sort because there is an edge C->A but C does not appear before A. In the algorithm, if the graph has cycle, the indegree of some of the vertices will never be 0. Implementation of Topological Sort class Graph{ . . . int [] topoSort(){

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

View Full Document
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.

### Page1 / 12

notes68 - April 24th Topological Sort April 26th Breadth...

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

View Full Document
Ask a homework question - tutors are online