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

Similar documents
4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Shortest path problems

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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.

CS 4349 Lecture October 23rd, 2017

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CS 3410 Ch 14 Graphs and Paths

The Shortest Path Problem

CSE 100: GRAPH ALGORITHMS

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CS 340 Homework Alternative 6. Due 11:59 PM Wednesday December 12

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Single-Source Shortest Paths. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

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

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Total Score /15 /20 /30 /10 /5 /20 Grader

Priority queue ADT part 1

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

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

Total Score /15 /20 /30 /10 /5 /20 Grader

Shortest Path Algorithms

CS350: Data Structures Dijkstra s Shortest Path Alg.

Week 12: Minimum Spanning trees and Shortest Paths

Undirected Graphs. Hwansoo Han

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

CS 561, Lecture 10. Jared Saia University of New Mexico

Minimum Spanning Trees

CHAPTER 13 GRAPH ALGORITHMS

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

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

SPANNING TREES. Lecture 21 CS2110 Spring 2016

CS 561, Lecture 10. Jared Saia University of New Mexico

Minimum Spanning Trees

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

Shortest Path Problem

MST & Shortest Path -Prim s -Djikstra s

CS 61BL Data Structures & Programming Methodology

Data Structures Brett Bernstein

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

09 B: Graph Algorithms II

Algorithm Design (8) Graph Algorithms 1/2

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

Algorithms for Data Science

04/03/03 Lecture 20 1

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

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

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

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

ECE 242 HOMEWORK 5. In the figure below, one can go from node B to A but not from A to B.

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CSE 100: GRAPH ALGORITHMS

CSE 100 Practice Final Exam

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

Introduction to Algorithms. Lecture 11

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

COP 3530, Course Outcomes

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CS171 Final Practice Exam

Priority Queues. 04/10/03 Lecture 22 1

Homework Assignment #3 Graph

Graph Algorithms shortest paths, minimum spanning trees, etc.

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

Algorithm Design and Analysis

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

Lecture Summary CSC 263H. August 5, 2016

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Solutions to relevant spring 2000 exam problems

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

CSE 373: Practice Final

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

6.1 Minimum Spanning Trees

Data Structures and Algorithms. Werner Nutt

Exam 3 Practice Problems

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Spanning Trees. Lecture 20 CS2110 Spring 2015

FINAL EXAM SOLUTIONS

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications

Classical Shortest-Path Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

Data Structures and Algorithms

COMP251: Single source shortest paths

Shortest Paths. CSE 373 Data Structures Lecture 21

Greedy Approach: Intro

Lecture 11: Analysis of Algorithms (CS ) 1

CS4800: Algorithms & Data Jonathan Ullman

COS 226 Algorithms and Data Structures Fall Final

Minimum Spanning Trees My T. UF

Chapter 9 Graph Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Transcription:

The homework assignment is available at: http://www.jennylam.cc/courses/16-s17/homework0.html a c d 1 7 e 7 6 f h i 0. b 10 g (a) Illustrate the execution of breadth-first search on the graph above, treating it as an unweighted graph (all edges have a weight of 1), and with vertex e as the source. Solution. Starting at e, breadth first stores the following contents in its (FIFO) queue and records the following distances step queue a b c d e f g h i init e + + + + 0 + + + + e g g + + + + 0 + 1 + + e d g, d + + + 1 0 + 1 + + e b g, d, b + 1 + 1 0 + 1 + + e c g, d, b, c + 1 1 1 0 + 1 + + g f d, b, c, f + 1 1 1 0 1 + + g h d, b, c, f, h + 1 1 1 0 1 + d i b, c, f, h, i + 1 1 1 0 1 b a c, f, h, i, a 1 1 1 0 1 In all subsequent iterations, each vertex being processed will not result in any new updates to distances, and all have at this point been computed. (b) Illustrate the execution of Dijkstras algorithm on the graph above, together with e as the source. Solution. In the following, we show the contents of the priority queue in no particular order. The priority of the elements stored is specified by the distances listed afterwards. 1

step pqueue a b c d e f g h i init e + + + + 0 + + + + relax e g g + + + + 0 + + + relax e d d, g + + + 1 0 + + + relax e b b, d, g + 7 + 1 0 + + + relax e c b, c, d, g + 7 1 0 + + + relax d c b, c, g + 7 1 0 + + + relax d e b, c, g + 7 1 0 + + + relax d f b, c, f, g + 7 1 0 6 + + relax d h b, c, f, g, h + 7 1 0 6 7 + relax d i b, c, f, g, h, i + 7 1 0 6 7 8 relax c a a, b, f, g, h, i 7 7 1 0 6 7 8 relax c b a, b, f, g, h, i 7 7 1 0 6 7 8 relax c e a, b, f, g, h, i 7 7 1 0 6 7 8 relax c d a, b, f, g, h, i 7 7 1 0 6 7 8 relax g b a, b, f, h, i 7 7 1 0 6 7 8 relax g e a, b, f, h, i 7 7 1 0 6 7 8 relax g f a, b, f, h, i 7 7 1 0 6 7 8 relax g h a, b, f, h, i 7 7 1 0 6 7 8 relax f d a, b, h, i 7 7 1 0 6 7 8 relax f h a, b, h, i 7 7 1 0 6 7 8 relax f g a, b, h, i 7 7 1 0 6 7 8 relax a b b, h, i 7 7 1 0 6 7 8 relax a c b, h, i 7 7 1 0 6 7 8 relax b a h, i 7 7 1 0 6 7 8 relax b c h, i 7 7 1 0 6 7 8 relax b e h, i 7 7 1 0 6 7 8 relax b g h, i 7 7 1 0 6 7 8 relax h d i 7 7 1 0 6 7 8 relax h i i 7 7 1 0 6 7 8 relax h f i 7 7 1 0 6 7 8 relax h g i 7 7 1 0 6 7 8 relax i d 7 7 1 0 6 7 8 relax i h 7 7 1 0 6 7 8 (c) Illustrate the execution of the Prim Dijkstra-Jarnik algorithm on the graph above. Solution. This algorithm can start at any vertex. Let s assume it starts with a (as the only node in the tree). We add edges to this tree in the following order: a c: connects c to tree, b c: connects b to tree (or c e, or c d) c e: connects e to tree d e: connects d to tree d f: connects f to tree (or e g) f g: connects g to tree

f -h: connects h to tree h i: connects i to tree. (d) Illustrate the execution of Kruskal s algorithm on the graph above. Solution. The algorithm starts with a forest of individual vertices, and adds edges in the following order: d e h i (or f g) f g f h (or a c) a c c b (or c d or c -e) c d (or c e) d f (or e g) (e) Implement Dijkstra s algorithm in Java. Solution. The following is a possible implementation of Dijkstra s algorithm in Java. In particular it is the variant that returns the shortest path tree. There are several issues in translating the algorithm discussed into code. The first is how to represent a weighted graph. If we choose an adjacency matrix as a starting point, we can replace the 1 entries with actual weights, and leaving 0 entries to represent the lack of an edge. In the following solution, we chose to start with the adjacency list representation, but replaced the lists of neighbors with maps of neighbors to edge weights. For example, if v has exactly two neighbors, w and x, such that v w has weight a and v x has weight b, this is represented as an entry in the map, where the key is v and the value is the map mapping w to a and x to b. The next issue is how to notify the priority queue that there has been a change to the distance of a vertex, given that it has no decreasekey() method, which is pretty typical of priority queues. The solution (although perhaps less efficient) is to remove the vertex, update its distance, and reinsert it with its new priority (ie distance). Finally, in order to delete the vertex from the priority queue, we need to be able to locate it within the priority queue. Since it is actually being stored as a map entry (which is necessary to have both the vertex, and its priority), we use an auxiliary map data structure, which we will call locator, and which maps vertices to the map entry or key-value pair consisting of the vertex itself and its priority. To get the most out of this solution, it is recommended that you read through the code with the goal to understand all the main ideas, and then to reimplement it entirely yourself without looking. Without further ado, the code.

import java.util.*; public class Dijkstra { // Representing a weighted graph as something similar to an adjacency list, but instead of mapping each vertex // to a list of neighbors, map each vertex v to a *map* of its neighbors w to edge weight on v->w. public static <T> Map<T, T> shortestpaths(map<t, Map<T, Integer>> weightedgraph, T source) { Map<T, Integer> dist = new HashMap<>(); Map<T, T> prev = new HashMap<>(); PriorityQueue<Map.Entry<T, Integer>> priorityqueue = new PriorityQueue<>(Comparator.comparing(Map.Entry::getValue)); Map<T, Map.Entry<T, Integer>> locator = new HashMap<>(); // used to locate priority queue elements by their T-value. Integer infinity = sumofweights(weightedgraph); for (T v : weightedgraph.keyset()) { dist.put(v, infinity); prev.put(v, null); dist.put(source, 0); priorityqueue.addall(dist.entryset()); for (Map.Entry<T, Integer> entry : dist.entryset()) locator.put(entry.getkey(), entry); while (!priorityqueue.isempty()) { Map.Entry<T, Integer> ventry = priorityqueue.peek(); priorityqueue.remove(ventry); T v = ventry.getkey(); for (Map.Entry<T, Integer> edge : weightedgraph.get(v).entryset()) { T w = edge.getkey(); int weight = edge.getvalue(); // weight of edge v -> w if (dist.get(v) + weight < dist.get(w)) { // update dist of w in the dist map and in the priority queue Map.Entry<T, Integer> distwentry = locator.get(w); // get the map entry: (w, dist.get(w)) priorityqueue.remove(distwentry); // (Java has no decreasekey, instead remove w... distwentry.setvalue(dist.get(v) + weight); // equivalent to dist.put(w, dist.get(v) + weight) priorityqueue.add(distwentry); //... after update to dist.get(w), re-add w) // update prev of w to v prev.put(w, v); return prev; private static <T> Integer sumofweights(map<t, Map<T, Integer>> weightedgraph) { Integer sum = 0; for (Map<T, Integer> neighbors : weightedgraph.values()) for (Integer weight : neighbors.values()) sum += weight; return sum; public static void main(string[] args) { Map<Integer, Integer> m0 = new HashMap<>(); m0.put(1, 6);

m0.put(, ); Map<Integer, Integer> m1 = new HashMap<>(); m1.put(0, 6); m1.put(, ); m1.put(, ); m1.put(, 1); Map<Integer, Integer> m = new HashMap<>(); m.put(1, ); m.put(, 6); Map<Integer, Integer> m = new HashMap<>(); m.put(0, ); m.put(1, ); m.put(, 1); Map<Integer, Integer> m = new HashMap<>(); m.put(1, 1); m.put(, 6); m.put(, 1); Map<Integer, Map<Integer, Integer>> graph = new HashMap<>(); graph.put(0, m0); graph.put(1, m1); graph.put(, m); graph.put(, m); graph.put(, m); System.out.println(shortestPaths(graph, 0)); 1. (a) What is the running time of this algorithm, using O-notation, as a function of the number n of vertices in the input graph? Solution. Dijkstra s algorithm runs in O(1 T(make-queue)(n) + n T(extract-min)(n) + m T(decrease-key)(n)). Using an unsorted list, which takes O(n) to make-queue with n items, O(n) time per extract-min operation and constant time per decrease-key operation, that comes out to O(n + m). Using the fact that the number of edges is m = O(n ) in a normal graph (one that does not allow multiple edges going in the same direction between the same two vertices), this simplifies to O(n ). (b) Would this algorithm be a better or worse choice than the more usual form of Dijkstra s algorithm using a binary heap, for this type of graph? Explain your answer. Solution. Better since the running time using a binary heap is O(n log n + m log n), which simplifies to O(m log n), assuming that m n, a reasonable assumption for a graph that is an input into Dijkstra s algorithm. (If there were fewer edges than vertices (technically, m < n 1), the graph would be disconnected, and certain vertices would not be reachable from the source.)

. Give an example of an weighted directed graph, G, with negative-weight edges but no negative-weight cycle, such that Dijkstra s algorithm incorrectly computes the shortest-path distances from some start vertex v. Solution. Consider the following example. s 1 a b c 1-100 In this example, we first pull s out of the priority queue, and relax the three edges (s a, s b and s c) coming out of s in no particular order. This sets dist[a] = 1 dist[b] = dist[c] = Since a is the closest vertex to s by dist-value, it is pulled out of the priority queue. a has no outgoing edges, so no edges are relaxed. Next we pull b out of the priority queue and relax its only outgoing edge b a. This does not produce a better path for a, which remains at a distance of 1 from the source. Finally, we pull c out of the priority queue and relax its edge c b, which resets the distance of b to -97. So the final distances produced by Dijkstra s algorithm are dist[a] = 1 dist[b] = -97 dist[c] = However, the shortest path to a is actually s c b a, which means the distance from s to a is actually 100 + 1 = -96 and not 1. In other words, Dijkstra did not find the correct distance to a. 6