ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort...

14
ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/2009 1 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 ...to all vertic …as implemented in Graph.java by Weiss Chapter 14 Weiss NO CYCLES (acyclic, cannot contain cycles) EDGE COSTS UNRESTRICTED (edge costs can be negative)

Transcript of ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort...

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

1

ALG0183 Algorithms & Data Structures

Lecture 23a acyclic with neg. weights (topological sort algorithm)

8/25/2009

“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

...to all vertices.

…as implemented in Graph.java by Weiss

Chapter 14 Weiss NO CYCLES (acyclic, cannot contain cycles)EDGE COSTS UNRESTRICTED (edge costs can be negative)

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

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

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

3

topological sort examplehttp://en.wikipedia.org/wiki/Topological_sort 8/11/09

• 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

valid topological sorts

... if there is a path from u to v, then v appears after u in the ordering.

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

48/25/2009

Topological Sorthttp://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.

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?

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

58/25/2009

http://mathworld.wolfram.com/TopologicalSort.html

Only directed acyclic graphs can be topologically sorted.

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

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

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

7

Figure 14.30 A topological sort.Weiss ©Addison-Wesley

8/25/2009V2, V0, V1, ...

V2 has indegree 0, so it is first in topological order.

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

88/25/2009

V2, V0, V1, V3, V4, V6, V5

Figure 14.30 A topological sort.Weiss ©Addison-Wesley

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

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 Dv can no longer be lowered; by the topological ordering rule, it has no incoming edges emanating from unvisited nodes.”

8/25/2009

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

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

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

118/25/2009

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<Vertex> q = new LinkedList<Vertex>( ); make queue start.dist = 0; start to itself has distance zero // Compute the indegrees Collection<Vertex> 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 );

startName from user

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

128/25/2009

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!" );}

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

13

application: critical path analysishttp://www.gamedev.net/reference/business/features/criticalpath/IMAGE008.JPG

8/25/2009

The critical path (longest path, usually in terms of time) is:activity A before activity D before activity E before activity F before activity K14 + 10 +31 + 7 + 21 = 83What is the latest start time for activity G?

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

ALG0183 Algorithms & Data Structures by Dr Andy Brooks

14

Figure 14.38 Worse-case running times of various graph algorithms.Weiss © Addison-Wesley

8/25/2009

• 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.