30
Fall 2008 NOEA - Computer Science 1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Embed Size (px)

DESCRIPTION

Fall 2008 NOEA - Computer Science3 Topological Order Consider a DAG: Directed Acyclic Graph The topological order is an order of visiting all vertices Is used in, for instance –Project planning –Compilers (code optimisation) –Dead-lock detection

Citation preview

Page 1: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 1

Session 10

Graph Algorithms:Topological Order

Minimum Spanning Trees (Prim)Shortest Path (Dijkstra)

Page 2: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 2

Læringsmål

• Kunne forklare følgende algoritmer (formål og virkemåde):– Minimum udspændende træer (Prim og/eller

Kruskal)– Kunne forklare Dijkstra’s algoritme.

• Have kørende implementation af en graf:– Kunne forklare, anvende og modificere denne

implementation

Page 3: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 3

Topological Order

• Consider a DAG: Directed Acyclic Graph

• The topological order is an order of visiting all vertices

• Is used in, for instance – Project planning– Compilers (code optimisation)– Dead-lock detection

Page 4: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 4

Topological Order– planning the project of boiling an egg (activity network)

Activity network:

Two different topological orders

Page 5: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 5

Algorithm (using bsf)

Page 6: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 6

Algorithm 2 (dsf)

Page 7: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 7

for (all vertices v in theGraph){ if (v has no predecessors){

Page 8: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 8

Spanning Trees(on connected graphs)

• Remember: a tree is an undirected, connected and acyclic graph

• A spanning tree for a graph contains– Every vertex of the graph– Enough edges to be a tree (how many is that?)

• Usually a graph will have several spanning trees

Page 9: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 9

Example – Spanning Tree

Page 10: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 10

Finding a Spanning Tree

• Remove edges from the graph (without disconnecting the graph) until it is acyclic – and a spanning tree emerges

• Finding spanning trees can be done applying DFS or BFS

• In both cases: mark the edges that is followed and the marked edges will constitute a spanning tree

Page 11: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 11

DFS Spanning Tree

Page 12: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 12

BFS Spanning Tree

Page 13: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 13

Minimum Spanning Trees(”Lazy Railroad Constructor’s Problem”)

• Problem:– Connect a number of towns with railroad

lines, telephone or telegraph lines, so the total cost is minimised.

– This is a graph problem: Towns are vertices and railroad lines are edges.

– Solution: The cheapest connection is a minimum spanning tree.

Page 14: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 14

Greedy Algorithms

• General strategy for solving optimisation problems:– The solution is build step by step– In each step the most optimal partial

solution is chosen. – Does not always work– But when it does, it is very nice.

Page 15: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 15

Example

• Controlling Pay-Back from a Vending Machine– For instance x is to be return using the fewest

coins:• 48 cents using 25-, 10-, 5- and 1-cent coins

– The greedy strategy works:– 25, 10, 10, 1, 1, 1

• 11 cents using 7-, 5- and 1-cent coins– The greedy strategy does not work:– 7, 1, 1, 1, 1– but 5, 5, 1 would be better.....

Why not?

Page 16: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 16

Minimum Spanning Tree

• Can be applying using several algorithms. We shall be studying two, both following the greedy pattern:– Prim's Algorithm– Kruskal's Algorithm

• Both algorithms find minimum spanning trees in an undirected, weighted graph, but they are using quite different approaches

Page 17: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 17

Prim’s Algorithm

• Pick an (arbitrary) start vertex• While there more unvisited vertices

– Select the edge with minimum weight from a visited vertex (v) to an unvisited vertex (u)

– Mark u as visited– Add u and the edge (v, u) to the minimum

spanning tree.

Page 18: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 18

Exercise:

Use Prim’s Algorithm to find a minimum spanning tree for this graph

Page 19: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 19

Kruskal's algorithm

• Quite different approach:– Step by step a number sub trees are

constructed and merged into larger trees:– In each step:

• Select an edge with minimum weight• Check if the edge will create a cycle in set of

sub trees, if not add the edge to set of subtrees– Eventually we have a minimum spanning

tree

Page 20: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 20

Kruskal's algorithmPrecise Formulation

• Sort the edges by weight (…,ek ,…)

• ET Ø• k 0• for i0 to |V|-1 do

– k++– if (ET {ek} is acyclic) then

• add ek to ET

– all vertices must included in the spanning tree

the spanning tree is empty initialy

Page 21: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 21

Example

Exercise:Use Kruskal’s Algorithm to find a minimum spanning tree for the graph on slide 18

Page 22: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 22

Shortest Path from one vertex (source) to every other in an Unweighted Graph

• Path length == number of edges on the path• The set of vertices is split in two:

– vertices where the shortest path is found– vertices where the shortest is still to be found

• The algorithm proceeds as follows:– The shortest path to the source is known (0), so the source

is add to the solution– All vertices not in the solution and adjacent to vertices in the

solution are added (path length 1)– All vertices not in the solution and adjacent to vertices in the

solution are added (path length 2)– And so on until all vertices are in the solution– Breath-First Search can be applied:

Page 23: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 23

private void unweighted( int source ) { int v, w; Queue q = new QueueAr( );//for the bfs clearData( ); //initialisation table[ source ].dist = 0; //initialisation q.enqueue( new Integer( startNode ) ); //initialisation

while( !q.isEmpty( ) ) {//bfs:v = ( (Integer) q.dequeue( ) ).intValue( );//adjacency list of v:

ListItr p = new LinkedListItr( table[ v ].adj ); for( ; p.isInList( ); p.advance( ) ){ w = ( (Edge) p.retrieve( ) ).dest; if( table[ w ].dist == INFINITY ) {//unvisited table[ w ].dist = table[ v ].dist + 1; table[ w ].prev = v; q.enqueue( new Integer( w ) );//don’t forget the adjacencies to w }//endif }//endfor }//endwhile }

Exercise: Implement this in C#

Page 24: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 24

Dijkstra• En af de helt store:

– Korteste vej– Semaforer– Multiprogrammering– “separation of

concerns”– korrekthed

• http://www.cs.utexas.edu/users/EWD/

Edsger W. DijkstraEWD717.PDF

Page 25: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 25

Shortest Path in an Weighted GraphDijkstra’s Algorithm

• Weighted, undirected graph; weights >0• Problem:

– finding the shortest (lowest weight) path from a given vertex to another

– Dijkstra’s Algorithm finds the shortest path from a given vertex (source) to every other.

Page 26: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 26

• The algorithm works as follows:– A set of selected vertices (S = Solution) is

constructed:• At any point S contains the vertices to which the

shortest path are known. • Initially S only contains the source• In each step a vertex is added to S• On termination S contains all vertices

– An array (W) of weights of the shortest path through S known to each vertices is maintained:

• Initial W is set to the first row of the adjacency matrix• W is adjusted as new vertices are added to S

– At each step a vertex (u) with minimum weight in W and not in S is selected and added to S:

• For every vertex not in S check, if it is cheaper to get to them through u. If this is the case, adjust W accordingly.

The algorithm can be tuned, if W is implemented as a

minimum heap instead of an array (findMin() in O(logn) in

stead of O(n))

Page 27: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 27

Note:– This version of Dijkstra’s Algorithm finds

the lengths of the shortest paths, not the paths themselves

– This could be achieved using a list of vertices for each vertex

– These lists are to be adjusted when W is adjusted

Page 28: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 28

Page 29: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 29

• Dijkstra's Algorithm is very heavily used:– routing in networks– Navigation systems

• www.krak.dk – www.maps.google.com– And much more

• Exercise:– Use Dijkstra’s Algorithm to find the length

shortest paths from vertex a to every other vertex in the graph on slide 18.

Page 30: Fall 2008 NOEA - Computer Science1 Session 10 Graph Algorithms: Topological Order Minimum Spanning Trees (Prim) Shortest Path (Dijkstra)

Fall 2008 NOEA - Computer Science 30

Læringsmål

• Kunne forklare følgende algoritmer (formål og virkemåde):– Minimum udspændende træer (Prim og/eller

Kruskal)– Kunne forklare Dijkstra’s algoritme.

• Have kørende implementation af en graf:– Kunne forklare, anvende og modificere denne

implementation