Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.

Slides:



Advertisements
Similar presentations
Chapter 5: Tree Constructions
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
DIJKSTRA’s Algorithm. Definition fwd search Find the shortest paths from a given SOURCE node to ALL other nodes, by developing the paths in order of increasing.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Discussion #35 Chapter 7, Section 5.5 1/15 Discussion #35 Dijkstra’s Algorithm.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
Dijkstra’s Shortest Paths CS 312 Lecture 4. Announcements Project 1 comes out Friday –Min spanning trees and scheduling –due 2 weeks from Friday (1 week.
The Shortest Path Problem
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Data Structures Using C++ 2E
Copyright © Cengage Learning. All rights reserved.
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Minimal Spanning Tree Problems in What is a minimal spanning tree An MST is a tree (set of edges) that connects all nodes in a graph, using.
1 Network Models Transportation Problem (TP) Distributing any commodity from any group of supply centers, called sources, to any group of receiving.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Chapter 9 Finding the Optimum 9.1 Finding the Best Tree.
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 Upon completion you will be able to:
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
1 1 Slide © 2005 Thomson/South-Western Chapter 9 Network Models n Shortest-Route Problem n Minimal Spanning Tree Problem n Maximal Flow Problem.
Shortest Path Problems
Redraw these graphs so that none of the line intersect except at the vertices B C D E F G H.
Shortest Path from G to C Using Dijkstra’s Algorithm
Network Flow Problems – Shortest Path Problem
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Shortest Path Problems
Shortest Path Graph represents highway system Edges have weights
Spanning Trees.
Visualizing Prim’s MST Algorithm Used to Trace the Algorithm in Class
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Shortest Path.
Chapter 11 Graphs.
Shortest Path Problems
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Networks Kruskal’s Algorithm
Minimum Spanning Tree.
Chapter 6 Network Flow Models.
離散數學 DISCRETE and COMBINATORIAL MATHEMATICS
Weighted Graphs & Shortest Paths
Graphs.
CSE 417: Algorithms and Computational Complexity
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Prim’s algorithm for minimum spanning trees
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms

Section 6.3Shortest Path and Minimal Spanning Tree1 Shortest Path Problem Assume that we have a simple, weighted, connected graph, where the weights are positive. Then a path exists between any two nodes x and y. How do we find a path with minimum weight? For example, cities connected by roads, with the weight being the distance between them. The shortest-path algorithm is known as Dijkstra’s algorithm.

Section 6.3Shortest Path and Minimal Spanning Tree2 Shortest-Path Algorithm To compute the shortest path from x to y using Dijkstra’s algorithm, we build a set (called IN ) that initially contains only x but grows as the algorithm proceeds. IN contains every node whose shortest path from x, using only nodes in IN, has so far been determined. For every node z outside IN, we keep track of the shortest distance d[z] from x to that node, using a path whose only non-IN node is z. We also keep track of the node adjacent to z on this path, s[z].

Section 6.3Shortest Path and Minimal Spanning Tree3 Shortest-Path Algorithm Pick the non-IN node p with the smallest distance d. Add p to IN, then recompute d for all the remaining non-IN nodes, because there may be a shorter path from x going through p than there was before p belonged to IN. If there is a shorter path, update s[z] so that p is now shown to be the node adjacent to z on the current shortest path. As soon as y is moved into IN, IN stops growing. The current value of d[y] is the distance for the shortest path, and its nodes are found by looking at y, s[y], s[s[y]], and so forth, until x is reached.

Section 6.3Shortest Path and Minimal Spanning Tree4 Shortest-Path Algorithm ALGORITHM ShortestPath ShortestPath (n  n matrix A; nodes x, y) //Dijkstra’s algorithm. A is a modified adjacency matrix for a //simple, connected graph with positive weights; x and y are //nodes in the graph; writes out nodes in the shortest path from x // to y, and the distance for that path Local variables: set of nodes IN //set of nodes whose shortest path from x //is known nodes z, p //temporary nodes array of integers d //for each node, the distance from x using //nodes in IN array of nodes s //for each node, the previous node in the //shortest path integer OldDistance //distance to compare against //initialize set IN and arrays d and s IN = {x} d[x] = 0

Section 6.3Shortest Path and Minimal Spanning Tree5 Shortest-Path Algorithm for all nodes z not in IN do d[z] = A[x, z] s [z] = x end for//process nodes into IN while y not in IN do //add minimum-distance node not in IN p = node z not in IN with minimum d[z] IN = IN  {p} //recompute d for non-IN nodes, adjust s if necessary for all nodes z not in IN do OldDistance = d[z] d[z] = min(d[z], d[ p] + A[ p, z]) if d[z] != OldDistance then s [z] = p end if end for end while

Section 6.3Shortest Path and Minimal Spanning Tree6 Shortest-Path Algorithm //write out path nodes write(“In reverse order, the path is”) write (y) z = y repeat write (s [z]) z = s [z] until z = x // write out path distance write(“The path distance is,” d[y]) end ShortestPath

Section 6.3Shortest Path and Minimal Spanning Tree7 Shortest-Path Algorithm Example Trace the algorithm using the following graph and adjacency matrix: At the end of the initialization phase:

Section 6.3Shortest Path and Minimal Spanning Tree8 Shortest-Path Algorithm Example The circled nodes are those in set IN. heavy lines show the current shortest paths, and the d-value for each node is written along with the node label.

Section 6.3Shortest Path and Minimal Spanning Tree9 Shortest-Path Algorithm Example We now enter the while loop and search through the d-values for the node of minimum distance that is not in IN. Node 1 is found, with d[1] = 3. Node 1 is added to IN. We recompute all the d-values for the remaining nodes, 2, 3, 4, and y. p = 1 IN = {x,1} d[2] = min(8, 3 + A[1, 2]) = min(8,  ) = 8 d[3] = min(4, 3 + A[1, 3]) = min(4, 9) = 4 d[4] = min( , 3 + A[1, 4]) = min( ,  ) =  d[y] = min(10, 3 + A[1, y]) = min(10,  ) = 10 This process is repeated until y is added to IN.

Section 6.3Shortest Path and Minimal Spanning Tree10 Shortest-Path Algorithm Example The second pass through the while loop: p = 3 (3 has the smallest d-value, namely 4, of 2, 3, 4, or y) IN = {x, 1, 3} d[2] = min(8, 4 + A[3, 2]) = min(8, 4 +  ) = 8 d[4] = min( , 4 + A[3, 4]) = min( , 4 + 1) = 5 (a change, so update s[4] to 3) d[y] = min(10, 4 + A[3, y]) = min(10, 4 + 3) = 7 (a change, so update s[y] to 3)

Section 6.3Shortest Path and Minimal Spanning Tree11 Shortest-Path Algorithm Example The third pass through the while loop: p = 4 (d-value 5) IN = {x, 1, 3, 4} d[2] = min(8, 5 + 7) = 8 d[y] = min(7, 5 + 1) = 6 (a change, update s[y])

Section 6.3Shortest Path and Minimal Spanning Tree12 Shortest-Path Algorithm Example The third pass through the while loop: p = y IN = {x, 1, 3, 4, y} d[2] = min(8, 6 ) = 8 y is now part of IN, so the while loop terminates. The (reversed) path goes through y, s[y]= 4, s[4]= 3, and s[3] = x.

Section 6.3Shortest Path and Minimal Spanning Tree13 Shortest-Path Algorithm Analysis ShortestPath is a greedy algorithm - it does what seems best based on its limited immediate knowledge. The for loop requires Θ(n) operations. The while loop takes Θ(n) operations. In the worst case, y is the last node brought into IN, and the while loop will be executed n - 1 times. The total number of operations involved in the while loop is Θ(n(n - 1)) = Θ(n2 ). Initialization and writing the output together take Θ(n) operations. So the algorithm requires Θ(n + n2) = Θ(n2 ) operations in the worst case.

Section 6.3Shortest Path and Minimal Spanning Tree14 Minimal Spanning Tree Problem DEFINITION: SPANNING TREE A spanning tree for a connected graph is a nonrooted tree whose set of nodes coincides with the set of nodes for the graph and whose arcs are (some of) the arcs of the graph. A spanning tree connects all the nodes of a graph with no excess arcs (no cycles). There are algorithms for constructing a minimal spanning tree, a spanning tree with minimal weight, for a given simple, weighted, connected graph. Prim’s Algorithm proceeds very much like the shortest- path algorithm, resulting in a minimal spanning tree.

Section 6.3Shortest Path and Minimal Spanning Tree15 Prim’s Algorithm There is a set IN, which initially contains one arbitrary node. For every node z not in IN, we keep track of the shortest distance d[z] between z and any node in IN. We successively add nodes to IN, where the next node added is one that is not in IN and whose distance d[z] is minimal. The arc having this minimal distance is then made part of the spanning tree. the minimal spanning tree of a graph may not be unique. The algorithm terminates when all nodes of the graph are in IN. The difference between Prim’s and Dijkstra’s algorithm is how d[z] (new distances) are calculated. Dijkstra’s: d[z] = min(d[z], d[p] + A[ p, z]) Prim’s: d[z] = min(d[z], A[ p, z]))