

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


12/3/2007 ITK 279 1
12/3/2007 ITK 279 2
If the graph is not acyclic, then there is no
topological sort for the graph.
Not an acyclic Graph
12/3/2007 ITK 279 3
12/3/2007 ITK 279 4
12/3/2007 ITK 279 5
12/3/2007 ITK 279 6
12/3/2007 ITK 279 7
Topological Sort in C++
// return an array where the first entry is the size of g // or 0 if g is not acyclic; int * graph::topsort(const Graph & g) { int *topo; Graph tempG = g; topo = new int[g.size()+1]; topo[0] = g.size(); for (int i=1; i<=topo[0]; i++) { int v = findVertexwithZeroIn(tempG); if (v == -1) { // g is not acyclic delete [] topo; topo = new int[1]; topo[0] = 0; return topo; } topo[i] = v; remove_v(tempG, v); } return topo; }
12/3/2007 ITK 279 8
12/3/2007 ITK 279 9
Common Algorithm Techniques
12/3/2007 ITK 279 10
Greedy method Greedy method Dynamic method
12/3/2007 ITK 279 11
Starting Vertex
w Final Vertex
Starting Vertex
w (^) Final Vertex
12/3/2007 ITK 279 12
Starting Vertex
Final Vertex
Starting Vertex
n Final Vertex