CS 3410 Ch 14 Graphs and Paths

Similar documents
The Shortest Path Problem

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Graphs & Digraphs Tuesday, November 06, 2007

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

Algorithm Design and Analysis

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

04/03/03 Lecture 20 1

1 Dijkstra s Algorithm

CS 310 Advanced Data Structures and Algorithms

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CS350: Data Structures Dijkstra s Shortest Path Alg.

CSE 100: GRAPH ALGORITHMS

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Chapter 9 Graph Algorithms

More Graph Algorithms: Topological Sort and Shortest Distance

22 Elementary Graph Algorithms. There are two standard ways to represent a

Chapter 9 Graph Algorithms

Algorithm Design and Analysis

Lecture 28: Graphs: Shortest Paths (Part 1)

Shortest Path Problem

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 The Shortest Path Problem

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CSE 373: Data Structures and Algorithms

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Understand graph terminology Implement graphs using

Figure 1: A directed graph.

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Shortest Paths. Nishant Mehta Lectures 10 and 11

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Shortest Paths. Nishant Mehta Lectures 10 and 11

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

Shortest Paths. CSE 373 Data Structures Lecture 21

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

09 B: Graph Algorithms II

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Algorithm Design (8) Graph Algorithms 1/2

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Last week: Breadth-First Search

CS170 Discussion Section 4: 9/18

Solutions to relevant spring 2000 exam problems

Single Source Shortest Path

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Data Structures and Algorithms

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Chapter 10. Fundamental Network Algorithms. M. E. J. Newman. May 6, M. E. J. Newman Chapter 10 May 6, / 33

Minimum Spanning Trees

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Data Structures in Java. Session 17 Instructor: Bert Huang

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Basic Graph Definitions

Shortest path problems

Week 11: Minimum Spanning trees

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

Lecture 9 Graph Traversal

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Graph. Vertex. edge. Directed Graph. Undirected Graph

CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at:

CHAPTER 13 GRAPH ALGORITHMS

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest

Homework Assignment #3 Graph

CSE 100: GRAPH ALGORITHMS


Lecture 11: Analysis of Algorithms (CS ) 1

Dijkstra s Shortest Path Algorithm

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Breadth First Search. cse2011 section 13.3 of textbook

Graphs Data Structures

Recitation 10. Shortest Paths Announcements. ShortLab has been released, and is due Friday afternoon. It s worth 125 points.

Graph Representation

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Konigsberg Bridge Problem

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

Practice Problems for the Final

Dr. Alexander Souza. Winter term 11/12

COT 6405 Introduction to Theory of Algorithms

TIE Graph algorithms

Source. Sink. Chapter 10: Iterative Programming Maximum Flow Problem. CmSc250 Intro to Algorithms

Graph Algorithms shortest paths, minimum spanning trees, etc.

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Algorithms and Data Structures

Graph Algorithms. Definition

Transcription:

CS 3410 Ch 14 Graphs and Paths Sections 14.1-14.3 Pages 527-552 Exercises 1,2,5,7-9 14.1 Definitions 1. A vertex (node) and an edge are the basic building blocks of a graph. Two vertices, (, ) may be connected by an edge, (, ). 2. A graph is a set of vertices, {, {(,,,,, 2), (,,, and a set of edges, }, 7, 1), (,, 3),, (, that connect the vertices: (, ). For example:, 1)}, 12 3. From the perspective of graph theory a vertex has no attributes; however, in the context of real problems, we may add structure to vertices. For instance, the vertices may represent airports or molecules. 4. If the edge pair is ordered, we say that they are directed edges and the graph is a directed graph. 5. In a directed graph, a vertex is said to be adjacent to vertex if and only if (, ). In other words if there is a directed edge between the two vertices, that begins at and ends at. In the graph above, and are adjacent to. 6. An edge may also contain an edge cost (or weight) that measures the cost of traversing the edge. For example, the cost to travel from to is 10. Thus, we modify the notation for an edge to include this cost, (,, ). 7. A path is a sequence of vertices connected by edges. 8. The path length is the number of edges on the path (which is the number of vertices 1). 9. The weighted path length is the sum of the weights of the edges on the path. 10. A path may exist from a vertex to itself. If this path contains no edges, it has length 0. 11. A simple path is a path where all the vertices are distinct, except possibly the first and last. 12. A cycle in a directed graph is a path that begins and ends at the same vertex and contains at least one edge. 13. A simple cycle is a cycle that is a simple path. 14. A directed acyclic graph (DAG) is a directed graph with no cycles. 1

15. Examples where graphs are useful: a. Airport system: nodes are airports, edges are flights between airports b. Email/Packet routing on the internet: nodes are routers, edges are network links c. Printed circuit board design: Integrated circuits are placed on a board. Traces (edges) connect pins (nodes) which are anchored in the integrated circuits. d. Social networks: nodes are people, edges are friends e. Chemistry-molecules: nodes are atoms, edges are bonds f. Biology (disease spread, breeding patterns, etc.): nodes are regions where organism lives, edges are migration paths 16. We say that S represents the size of the set S, e.g. the number of elements in the set. 17. The maximum number of edges in a directed graph is V, thus, E V. 18. If most of the edges are present, we say the graph is dense. If the number of edges is ( ), then we say the graph is sparse. 14.1.1 Graph Representation 1. We can represent a graph with a two-dimensional array called an adjacency matrix. For a dense graph, this is OK, for a sparse graph, there is a lot of wasted space. 0 1 2 3 4 5 6 0 1 2 4 2 2 3 4 1 3 10 2 5 5 8 1 6 4 6 2. For a sparse graph, a better approach is an adjacency list. For each vertex, we keep a list of all adjacent vertices. Thus, the space requirement is approximately ( ). 2

3. In the figure above, vertices are duplicated (e.g. Vertex 3 is in both Vertex 0 and Vertex 1 s adjacency lists). Of course we wouldn t want to implement the adjacency list in exactly this way; we would want to represent a vertex only once. A reasonable way to do this is to represent a Graph as a list of Vertex objects and each Vertex with a list of Edge objects, where an Edge has a cost/weight and a link to the destination (second) vertex. This is an analysis view of the domain: 4. An efficient way to specify a graph for input is to list the edges: begin vertex, end vertex, and weight. 5. Example: Input: Vertex Begin End D C A B D B A D E D B E C A Graph: Weight 10 12 23 87 43 11 19 Internal Representation: 3

6. As we start to design the Graph class, we see that it will be useful to represent the collection of Vertex objects as a Map using the name of the vertex as the key and each Vertex will have a List of Edge objects. 7. Next, we show the Map and List implementations. Map<String,Vertex> vmap HashMap<String,Vertex>(); List<Edge> adjlist LinkedList<Edge>(); 8. What happens when the Map and List implementations change? Notice that we have programmed to an interface, not an implementation. 4

9. Flesh out the design some more by adding methods and fields. 10. Edge class 11. Vertex class Some of the fields will be used in the shortest path algorithms that we consider later. 5

12. Graph class 6

13. getvertex Method This method gets the Vertex with vertexname. If it doesn t exist, it puts it in the map. This makes the process of creating a Graph simpler, as we will see next. 14. addedge Method As we read the edge list we simply call the addedge method, which in turn calls getvertex for the two vertices that form the Edge. getvertex automatically puts the Vertex in the map if it not already there. 15. clearall Method 7

16. Private printpath Method 17. Public printpath Method 8

18. Sample Driver Reads an edge list from a file specified from the command line. Then, calls processrequest to run one of four shortest path algorithms. 9

19. The processrequest method prompts for the start and end nodes, and the algorithm to use. 10

14.2 Unweighted Shortest-Path Problem 1. Unweighted shortest-path problem Find the shortest path (measured by number of edges) from a designated vertex (the source) to every other vertex. You may only want the shortest path to one particular node (the destination); however, the algorithm we consider automatically finds the shortest path to all nodes. 2. Idea Use a roving eyeball to visit each node. Initially, start the eyeball at. If is the vertex that the eyeball is on then find each vertex that is adjacent to and update its distance to + 1 if its distance has not been updated before. When this is complete, move the eyeball to the next unprocessed vertex. Because the eyeball processes each vertex in order of its distance from the starting vertex and the edge adds exactly 1 to the length of the path to, we are guaranteed that the first time is updated (the only time), it is set to the value of the length of the shortest path to. 3. Example: 11

4. Algorithm: Loop over all vertices, u, starting with S For each neighbor, w of u Update w s distance if it hasn t been updated before Get next u (closest to S and unvisited) 5. Algorithm Let be the length of the shortest path from Initialize Let 0, and all other nodes with, to.. be the eyeball that moves from node to node. Initialize to. Loop until all vertices visited by : For each vertex If Set that is adjacent to to next vertex, Otherwise, set Otherwise done +1 with to next vertex, if possible with + 1 if possible Note: this is a breadth-first search. It starts at a node (the Source) and searches all its neighbors (adjacent nodes). Then, in turn it searches each neighbor s neighbors. Each search advances the frontier until all nodes have been processed (in which case all edges have been traversed/processed exactly once). 6. At each Eyeball position, we have to check all the s that are adjacent to the eyeball. This is easy since all we have to do is iterate over the eyeball s adjacency list. Since each edge is only processed once, this incurs a total cost of ( ). However, we must move the Eyeball to another node. We could scan through the vertex map each time which could take ( ) time and we need to do it times. Thus, the total cost of this step would be ( ), for a total complexity of ( + ) ( ). Fortunately, there is a better technique. 7. When a vertex w has its distance set for the first (and only) time, it becomes a candidate for an Eyeball visitation at some later time. Thus, w just has to wait its turn. We can use a queue to model this situation so that when w s distance is set, we put w at the end of the queue. To select a new vertex for the eyeball, we simply take the next vertex in the queue. Since a vertex is enqueued and dequeued at most once, and queue operations are constant, the cost of choosing the eyeball vertex is O( V ) for the entire algorithm. Thus, the total work for the algorithm is O( V + E ) O( E ) which is dominated by O( E ), the processing of the adjacency lists. We say that this algorithm is linear in the size of the graph. 12

8. Notice that this procedure only gives us the minimum distance to each node. With a small modification, we can obtain the shortest path. We remember that the Vertex class had a prev field. When we update a distance on an adjacent node, we will set prev to the Eyeball. This is shown in the example below. 13

9. Java Implementation of unweighted shortest-path problem: 14

14.3 Positive-Weighted Shortest-Path Problem: Dijkstra s Algorithm 1. Positive-weighted shortest-path problem Find the shortest path (measured by total cost) from a designated vertex to every vertex. All edge costs are nonnegative. 2. Suppose we try the un-weighted algorithm: As we see, we run into a problem. The un-weighted algorithm says that when the eye is at, we should not consider updating the cost to, but clearly we must. However, if we modify the algorithm and allow consideration of, and thus update its cost from 5 to 3, then what happens to the cost at? Its value stays at 6. It seems like this approach would require moving the eye back to vertices already visited. 3. Fortunately, there is a better solution, often called Dijkstra s Algorithm. There are two central ideas which we will consider as modifications to the un-weighted case: a. Allow a node s weight to be updated more than once b. Move the eyeball to the next, unvisited node with the smallest cost. For this, we will use a priority queue. Since a node s weight can be updated, this means that the priority queue must support a decreasekey operation. 15

4. Example - Dijkstra s Algorithm: 10. Let s develop an algorithm. First, let: 1. 2. 3. be the length of the shortest path from to. be the source node. be the eyeball that moves from node to node. Algorithm 1. Initialize 0, and all other nodes with, 2. Put S into priority queue. 3. Loop until all priority queue is empty: a. remove min from priority queue b. For each vertex that is adjacent to new cost If put else if + (, ) in priority queue > update priority of 16.

5. Now, let s look at the complexity. First, each node is added to and removed from the priority queue exactly 1 time. The dominant operation here is remove which is (log ). Thus, step 3a contributes towards the total work ( log ). Next, the updating of a priority (decrease key) is also (log ). An upper bound on the number of times this can be called is ( ). Thus, the total work for step 3b is ( log ). Finally, the total work performed is ( log ). ( log ) + ( log ) which is typically dominated by 6. The author implements Dijkstra s algorithm with a priority queue, but not one that supports descreasekey. Instead, he uses an additional field in the Vertex class, scratch which keeps track of whether a Vertex has been processed or not. He shows that this is also ( log ). 7. Another example: 17

8. The crux of the proof that Dijkstra s Algorithm works is the following hypothesis: A least-cost path from X to Y contains least-cost paths from X to every node on the path to Y. For example, if Let is the least-cost path from is the least-cost path from is the least-cost path from to is the least-cost path from to be the shortest path from to, then: to to, which is composed of two sub-paths: and : Proof by contradiction: Assume hypothesis is false. Thus, given a least-cost path better path from to than, the one in. from to that goes through, then there is a Show a contradiction If were better than, then we could replace the sub-path from to in with this lesser-cost path. Now, since doesn t change, we now have a better path from X to Y, and. But this violates the assumption that is the least-cost path from X to Y. Therefore, the original hypothesis must be true 9. Java Implementation 18

19

14.3 Negative-Weighted Shortest-Path Problem 1. As stated earlier, Dijkstra s algortihm does not work when a graph has negative weights. 2. With negative weights on a graph, we have an additional problem, that of a negative-weight cycle. For example, the path from to can be made arbitrarily short by continuing to cycle through the negative weight cycle,,,. In such cases, we say the shortest-path is undefined. 3. The Bellman-Ford algorithm will solve the case with negative weights. If there is a negative-weight cycle, the algorithm will detect it and terminate. Otherwise, it will find the shortest-path. First, let: 1. 2. be the length of the shortest path from be the source node. Algorithm 1. Initialize to. 0, and all other nodes with, 2. For i1 to 1 a. For each edge (, ) if > + (, ) 3. For each edge (, ) if >. + (, ) + (, ) negative weight cycle exists 4. Now, let s consider the complexity. We can see that step 2 dominates and that the outer loop occurs 1 times and the inner loop (step 2a) occurs times. Thus, the complexity of the algorithm is ( ). Thus, it is at least quadratic in the number of vertices, which is considerably slower than Dijkstra s algorithm. 20

5. Example: Distance table: 0 Summary Type of Graph Weights Cycles Unweighted none Yes Weighted, no negative edges positive Yes Weighted, negative edges any Yes Weighted, acyclic any No Running Time Comments Section ( ) Breadth-first search 14.2 Dijkstra s algorithm 14.3 Bellman-Ford algorithm 14.4 ( ) Topological sort 14.5 ( log ) ( ) 21