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

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Chapter 3 The Greedy Method 3.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
1 Spanning Trees Lecture 20 CS2110 – Spring
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
SPANNING TREES Lecture 21 CS2110 – Spring
Minimum Spanning Trees
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be a directed graph with |V| = n. 1.{ v 1, v 2,.... v.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Minimum Spanning Trees Prof. Sin-Min Lee Dept. of Computer Science, San Jose State University.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Fall 2008NOEA – Computer Science1 Session 11 Revisiting Graph Algorithms Algorithm Patterns Algorithm Efficiency Difficult and Unsolvable Problems.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Fundamentals, Terminology, Traversal, Algorithms
Minimum Spanning Trees and Shortest Paths
Short paths and spanning trees
Graph Algorithm.
Chapter 11 Graphs.
CSCI2100 Data Structures Tutorial
CSE 373: Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

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

Fall 2008 NOEA - Computer Science2 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

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

Fall 2008 NOEA - Computer Science4 Topological Order – planning the project of boiling an egg (activity network) Activity network: Two different topological orders

Fall 2008 NOEA - Computer Science5 Algorithm (using bsf)

Fall 2008 NOEA - Computer Science6 Algorithm 2 (dsf)

Fall 2008 NOEA - Computer Science7 for (all vertices v in theGraph){ if (v has no predecessors){

Fall 2008 NOEA - Computer Science8 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

Fall 2008 NOEA - Computer Science9 Example – Spanning Tree

Fall 2008 NOEA - Computer Science10 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

Fall 2008 NOEA - Computer Science11 DFS Spanning Tree

Fall 2008 NOEA - Computer Science12 BFS Spanning Tree

Fall 2008 NOEA - Computer Science13 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.

Fall 2008 NOEA - Computer Science14 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.

Fall 2008 NOEA - Computer Science15 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?

Fall 2008 NOEA - Computer Science16 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

Fall 2008 NOEA - Computer Science17 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.

Fall 2008 NOEA - Computer Science18 Exercise: Use Prim’s Algorithm to find a minimum spanning tree for this graph

Fall 2008 NOEA - Computer Science19 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

Fall 2008 NOEA - Computer Science20 Kruskal's algorithm Precise Formulation Sort the edges by weight (…,e k,…) E T  Ø k  0 for i  0 to |V|-1 do –k++ –if (E T  {e k } is acyclic) then add e k to E T – all vertices must included in the spanning tree the spanning tree is empty initialy

Fall 2008 NOEA - Computer Science21 Example Exercise: Use Kruskal’s Algorithm to find a minimum spanning tree for the graph on slide 18

Fall 2008 NOEA - Computer Science22 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:

Fall 2008 NOEA - Computer Science23 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#

Fall 2008 NOEA - Computer Science24 Dijkstra En af de helt store: –Korteste vej –Semaforer –Multiprogrammering –“separation of concerns” –korrekthed edu/users/EWD/ edu/users/EWD/ Edsger W. Dijkstra EWD717.PDF

Fall 2008 NOEA - Computer Science25 Shortest Path in an Weighted Graph Dijkstra’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.

Fall 2008 NOEA - Computer Science26 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))

Fall 2008 NOEA - Computer Science27 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

Fall 2008 NOEA - Computer Science28

Fall 2008 NOEA - Computer Science29 Dijkstra's Algorithm is very heavily used: –routing in networks –Navigation systems – –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.

Fall 2008 NOEA - Computer Science30 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