Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures.

Similar presentations


Presentation on theme: "ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures."— Presentation transcript:

1 ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks The shortest-path algorithms are all single-source algorithms, which begin at some starting point and compute the shortest paths from it to all vertices. Weiss Chapter 14 WeissNO CYCLES (acyclic, cannot contain cycles) EDGE COSTS UNRESTRICTED (edge costs can be negative)

2 topological sorting (Weiss) A topological sort orders vertices in a directed graph such that if there is a path from u to v, then v appears after u in the ordering....a topological sort is not possible if a graph has a cycle... – Which node comes first? We cannot say. A graph may have several topological orders, and in most cases, any legal ordering will do. Every acyclic graph has a topological order. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2

3 topological sort example http://en.wikipedia.org/wiki/Topological_sort 8/11/09 http://en.wikipedia.org/wiki/Topological_sort 7, 5, 3, 11, 8, 2, 9, 10 3, 5, 7, 8, 11, 2, 9, 10 3, 7, 8, 5, 11, 10, 2, 9 5, 7, 3, 8, 11, 10, 9, 2 etc. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 valid topological sorts... if there is a path from u to v, then v appears after u in the ordering.

4 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 Topological Sort http://www.itl.nist.gov/div897/sqg/dads/HTML/topologicalSort.html Definition: To arrange items when some pairs of items have no comparison, that is, according to a partial order.partial order Some pairs of items have no comparison. You need to put your underwear on before you put your jeans on. You need to put your socks on before you put your shoes on. You need to put your jeans on before you put your shoes on. You need to put a shirt on before you put your jacket on. Do you put your socks or underwear on first?

5 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5 http://mathworld.wolfram.com/TopologicalSort.html Only directed acyclic graphs can be topologically sorted.

6 Features of algorithm a by Weiss. The indegree of every vertex (the number of incoming edges) is computed. The node with no incoming edges is logically removed. (Remember, no cycles.) – In practice, logically remove means that we lower the count of incoming edges for each vertex adjacent to v. – If there were several vertices of indegree 0, we could choose any one of them. The process of removing the node with the smallest indegree repeats until all nodes have been removed. – This process yields a list of nodes in a topological order. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6

7 Figure 14.30 A topological sort. Weiss ©Addison-Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7 V2, V0, V1,... V2 has indegree 0, so it is first in topological order.

8 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 V2, V0, V1, V3, V4, V6, V5 Figure 14.30 A topological sort. Weiss ©Addison-Wesley

9 Features of algorithm a by Weiss. When a vertex has its indegree lowered to zero, it is placed on the queue. To find the next vertex in the toplogical order, we merely get and remove the front item from the queue. If the queue empties before all the vertices have been topologically sorted, the graph has a cycle. When vertex v is visited (removed from the queue of vertices in a topological order), we are guaranteed that D v can no longer be lowered; by the topological ordering rule, it has no incoming edges emanating from unvisited nodes. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9

10 Features of algorithm a by Weiss. The implementation combines a topological sort calculation and a shortest-path calculation. The indegree information is stored in the scratch data member. The result is a linear-time algorithm even with negative edge weights. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10

11 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 public void acyclic( String startName ) { Vertex start = vertexMap.get( startName ); get start vertex from the map if( start == null ) throw new NoSuchElementException( "Start vertex not found" ); clearAll( ); call to the reset method for each vertex Queue q = new LinkedList ( ); make queue start.dist = 0; start to itself has distance zero // Compute the indegrees Collection vertexSet = vertexMap.values( ); Collection view of values for( Vertex v : vertexSet ) for( Edge e : v.adj ) e.dest.scratch++; scratch is the indegree (number of incoming edges) // Enqueue vertices of indegree zero possibly more than one for( Vertex v : vertexSet ) if( v.scratch == 0 ) q.add( v );

12 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 int iterations; for( iterations = 0; !q.isEmpty( ); iterations++ ) { Vertex v = q.remove( ); for( Edge e : v.adj ) { Vertex w = e.dest; double cvw = e.cost; if( --w.scratch == 0 ) lower the indegree of the destination node q.add( w ); add to queue if indegree == 0 if( v.dist == INFINITY ) continue; no need to attempt the distance calculation if( w.dist > v.dist + cvw ) { if a better path has been found w.dist = v.dist + cvw; w.prev = v; note of previous vertext (v) on the current shortest path } if( iterations != vertexMap.size( ) ) throw new GraphException( "Graph has a cycle!" ); }

13 application: critical path analysis http://www.gamedev.net/reference/business/features/criticalpath/IMAGE008.JPG 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 The critical path (longest path, usually in terms of time) is: activity A before activity D before activity E before activity F before activity K 14 + 10 +31 + 7 + 21 = 83 What is the latest start time for activity G?

14 Figure 14.38 Worse-case running times of various graph algorithms. Weiss © Addison-Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 Graph.java: the user specifies which algorithm should be executed to find the shortest path: – u unweighted (breadth-first search algorithm) – d only positive weights on edges (Dijkstra´s algorithm) – n negative weights present (Bellman-Ford algorithm) – a acyclic with neg. weights (topological sort algorithm) You need to know what kind of graph you have.


Download ppt "ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures."

Similar presentations


Ads by Google