Section 08: Solutions

Similar documents
SPANNING TREES. Lecture 21 CS2110 Spring 2016

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

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

CS 310 Advanced Data Structures and Algorithms

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

TIE Graph algorithms

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

Figure 1: A directed graph.

CMPUT 396 Sliding Tile Puzzle

TIE Graph algorithms

CSE 373: Data Structures and Algorithms

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.

Chapter 9 Graph Algorithms

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

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

(Re)Introduction to Graphs and Some Algorithms

Algorithms: Lecture 10. Chalmers University of Technology

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Copyright 2000, Kevin Wayne 1

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

Chapter 9 Graph Algorithms

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

Graphs. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. What is a Graph? b d f h j

CSE 373 Final Exam 3/14/06 Sample Solution

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Chapter 9 Graph Algorithms

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

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Homework Assignment #3 Graph

Lecture 9 Graph Traversal

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

Solutions to relevant spring 2000 exam problems

CS170 Discussion Section 4: 9/18

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

CMPSCI 250: Introduction to Computation. Lecture #24: General Search, DFS, and BFS David Mix Barrington 24 March 2014

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

INTRODUCTION TO HEURISTIC SEARCH

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

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

Outline. Graphs. Divide and Conquer.

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Graphs & Digraphs Tuesday, November 06, 2007

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

CSE 100: GRAPH ALGORITHMS

CS61BL. Lecture 5: Graphs Sorting

CSE 373: Data Structures and Algorithms

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Breadth First Search. cse2011 section 13.3 of textbook

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CMU-Q Lecture 2: Search problems Uninformed search. Teacher: Gianni A. Di Caro

CS Data Structures and Algorithm Analysis

Spanning Trees. Lecture 20 CS2110 Spring 2015

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

CSE 100: GRAPH ALGORITHMS

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Basic Graph Definitions

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

W4231: Analysis of Algorithms

Algorithm Design and Analysis

Question 2 (Strongly Connected Components, 15 points). What are the strongly connected components of the graph below?

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

More Graph Algorithms

Data Structures Brett Bernstein

CPSC 436D Video Game Programming

Chapter 6. The Traveling-Salesman Problem. Section 1. Hamilton circuits and Hamilton paths.

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Unweighted Graphs & Algorithms

Introduction to Graphs. CS2110, Spring 2011 Cornell University

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Last week: Breadth-First Search

Graphs. The ultimate data structure. graphs 1

Dynamic Programming II

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Unit 2: Algorithmic Graph Theory

3.1 Basic Definitions and Applications

Lecture 3 - States and Searching

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS


Graphs. Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. What is a Graph? b d f h j

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

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

(Refer Slide Time: 05:25)

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun

CPS 170: Artificial Intelligence Search

Understand graph terminology Implement graphs using

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

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Graphs. The ultimate data structure. graphs 1

Module 6 NP-Complete Problems and Heuristics

Game AI: The set of algorithms, representations, tools, and tricks that support the creation and management of real-time digital experiences

CPSC W1: Midterm 1 Sample Solution

Spanning trees. Suppose you have a connected undirected graph

Transcription:

Section 08: Solutions 1. Limitations and properties of shortest path algorithms (a) Draw an example of a directed graph where (a) there exists a path between two vertices s and t but (b) there is no shortest path between s and t. If the graph has a negative-cost cycle, there s always a way to get from s to t, but there s no shortest way: we can basically loop through the negative cycle an infinite number of times to bring the cost down as much as we want. s z t (b) Draw an example of a directed graph where there does exist a shortest path between two vertices s and t but if we try running Dijkstra s algorithm on s, it fails to find that shortest path and returns an incorrect result. Dijkstra s algorithm is not guaranteed to work correctly if there are negative cost edges. For example, in this problem, if we start on s, Dijkstra s algorithm will initially assign t and z a cost of 1 and 100 respectively. Dijkstra s will then pick the t node next remember, Dijkstra s algorithm is a greedy algorithm and will always pick the pending node that has the smallest cost. This turned out to be a poor decision though, because if we were more patient and tried exploring z first, we would have discovered a more cost-efficient route. 1 s t 10-0 z (c) Given some arbitrary graph, how would you determine if it contained a cycle? Run any graph-traversal algorithm (e.g. BFS or DFS) and see if we visit a node we were already on. The main edge case we must handle is if the graph contains unconnected components: after running the traversal algorithm, we basically need to check if there are any nodes we haven t visited yet, and try running the traversal algorithm again. 1

. Simulating Dijkstra s (a) Consider the following graph: 5 t x 6 - s 8-4 -3 y 9 z Suppose we run Dijkstra s algorithm on this graph starting with vertex s. (Note: we will very likely get meaningless results due to the negative edges, but this is still a good way to practice running the algorithm.) What are the final costs of each vertex and the shortest paths from s to each vertex? 5 t: 6 x: 4 6 - s: 0 8-4 -3 y: z: 9 (b) Here is another graph. What are the final costs and shortest paths if we run Dijkstra s starting on node A? B 3 A C D -4 1 1 E G - F

B: 3 A: 0 C: D: 4-4 1 1 E: 9 G: 8 F: 6-3. Practicing topological sort Find a topological sort of the following graph: A B C J D I E F G K H L One possible solution: E, F, G, H, L, K, A, C, D, I, J, B 4. Graph representations (a) Suppose we have a directed graph represented in adjacency list form. Design an algorithm that produces a new graph (also in adjacency list form) where each edge is reversed. Here is one possible implementation: def reverse(graph): output = new empty graph for ((v, neighbors) : graph): for (u : neighbors): if (!output.containskey(u)): output.put(u, new set or list) output.get(u).add(v) return output 3

(b) Implement an algorithm that does the same thing, except that we now accept and return a graph in adjacency matrix form. Here is one possible implementation: def reverse(graph): output = new d array for (i : 0 to graph.numrows()): for (j : 0 to graph.numcols()): output[j][i] = graph[i][j] return output 5. Designing algorithms: Applying algorithms (a) Explain how you would implement DFS recursively, rather then iteratively. How much memory does the recursive algorithm use vs the iterative one? Here is one way we could implement this algorithm recursively: def dfs(vertex v, Set<Vertex> visited): if (v not in visited): visited.add(v) for (u : v.neighbors()): dfs(u, visited) Note that both the iterative and recursive versions use the same amount of memory. With the iterative one, we store pending nodes on an explicit stack data structure. With the recursive one, we store them implicitly on the function call stack. (b) Suppose you want to model how a tweet gets re-tweeted by followers on Twitter. You have data on who the users of Twitter are and all the followers of each user. Given a source, a tweet can be re-tweeted by any follower of that source. Suppose a particular user makes a tweet. Design an algorithm that designs which users could have seen this tweet, assuming the tweet was re-tweeted at most k times. Run BFS or DFS to a depth of k + 1 (the extra iteration is to account for the people on the final layer who may have seen the tweet but don t retweet it). (c) Suppose we have graph where the edges are unweighted, but the vertices have numerical weights. Explain how you would modify one of the algorithms we ve studied in class to find the shortest path between two vertices. Here, we can run Dijkstra s algorithm almost unmodified. The only change we d need to make is that instead of updating adjacent nodes using the sum of the current vertex cost and the edge weight, we just add the current vertex cost to the other vertex s existing cost. 4

(d) Challenge: Suppose you are trying to find the most strenuous hiking trail between two points: the path with the most elevation gain or loss per mile. We model this problem as a connected undirected graph G = (V, E). Each vertex is associated with some height h(v) and each edge is associated with some positive distance d(e). Let a and b be our starting and ending points. We assume h(a) = h(b) = 0. Now, we define an up-down path as a path where there exists some vertex t such that all vertices we visit from a to t have increasing heights, and all vertices we visit from t to b have decreasing heights. Metaphorically, an up-down path represents a hill, where t is the very top of the hill. Next, we define the difficulty of the up-down path has: difficulty = h(t) sum of the lengths of the edges in path That is, the difficulty is the height of the hill divided by the total horizontal length we end up walking. Design an algorithm that finds an up-down walk with the maximum possible difficulty between two nodes a and b. If there does not exist an up-down path, throw an exception. Construct a directed graph G = (V, E ) where for each e E where e = (u, v), there is a directed edge e = (u, v) in E only if h(u) < h(v). Run Dijkstra s algorithm starting on a within graph G. Let d 1 and π 1 be the shortest path distances and predecessors. Run Dijkstra s algorithm starting on b within graph G. Let d and π be the shortest path distances and predecessors. If d 1 (v) = or d (v) = for all v V (ignoring a and b, throw an exception Otherwise, let t be whatever node that maximizes the quantity h(t)d 1 (h) + d (v). Construct the shortest paths p 1 and p using π 1 and π respectively. Return p 1 concatenated with the reverse of p. 5

6. Designing algorithms: Pathfinding in mazes (a) Suppose we are trying to design a maze within a d top-down video-game. The world is represented as a grid, where each tile is either an impassable wall, an open space a player can pass through, or a wormhole. On each turn, the player may move one space on the grid to any adjacent open tile. If the player is standing on a wormhole, they can instead use their turn to teleport themselves to the other end of the wormhole, which is located somewhere else on the map. Now, suppose the there are several coins scattered throughout the map. Your goal is to design an algorithm that finds a path between the player and some coin in the fewest number of turns possible. Describe how you would represent this scenario as a graph (what are the vertices and edges? Is this a weighted or unwighted graph? Directed or undirected?). Then, describe how you would implement an algorithm to complete this task. We can represent this as an undirected, unweighted graph where each tile is a vertex. Edges connect tiles we can travel between. When we have a wormhole, we add an extra edge connecting that wormhole tile to the corresponding end of the wormhole. Because it takes only one turn to travel to each adjacent tile, there is actually no need to store edge weights: it costs an equal amount to move to the next vertex. All paths are bidirectional, so we can also use an undirected graph. (If there are paths or wormholes that are one-way, we can switch to using a directed graph). To find the shortest path, we can run BFS starting with the player and stop the moment we hit a coin. (We can use other algorithms like DFS or Dijkstra s algorithm if we re careful, but those would be less efficient.) (b) Challenge: Suppose the map now stuffed with a huge number of players. We now want an algorithm that finds the shortest path between every single player and the closest coin. A naive way of doing this would be to run the same algorithm from the previous part on every single player. However, this would be prohibitively expensive. Design a more efficient algorithm that does the same thing. When designing your algorithm, you may assume that the map is relatively small/that there are only a few coins, relative to the number of players. One idea is to start at each coin and run BFS or DFS radiating outwards. Every time we visit a tile, save the distance from it to the coin along with a backpointer that leads towards the coin. If we visit a tile that already has a distance associated with it, see if that distance is higher or lower then the current one. If it s higher, replace it with the shorter distance/the new backpointer. Otherwise, leave it alone. Now, once we ve computed a distance and backpointer for every tile, we can simply just loop through each player, check the backpointer for the tile they re standing on, and move them in that direction. We now need to run BFS/DFS just once over the entire map, rather then per each player. Once a player find a coin, we can re-run BFS/DFS again and update the backpointers. 6

(c) Challenge: Now, suppose our map contains just a single player again. The player now wants to collect all the coins in the fewest number of turns possible. Design an algorithm to do this efficiently. This is basically the traveling salesman problem, which is NP-COMPLETE. We ll discuss what this means in the last week of class, but basically, if you can find an efficient algorithm to solve this problem which doesn t resort to brute force, you should basically be teaching this class/will be instantly rich and famous/will have your name immortalized in history books forever... Good luck lol. Designing algorithms: Planning course prerequisites (a) Suppose you just got into the department of your choice and are trying to plan out the courses you need to take in order to graduate. You have a list of all available courses, their prerequisites, and whether or not that course is optional. Now suppose you want to all of the available classes. Design an algorithm that prints out one possible order in which you can take these classes. What is the worst-case runtime of your algorithm? Note: if a class has a prerequisite, make sure you take the prerequisites first. For the sake of simplicity, you may assume you will take only one class per quarter. Run toposort this will take O ( V + E ) time. However, we also know our schedule will contain no duplicate edges or self-loops. This means our graph is actually a simple graph so E O ( V ). This means we can express our runtime as O ( V + V ) = O ( V ). That said, in practice, each course is likely to have only a small, constant number of pre-reqs. This means our graph is sparse it s highly likely that E O ( V ). Therefore, our runtime is (most likely) O ( V ) assuming the department s course listing isn t insane. (b) What if you only want to take some of the classes? Modify your algorithm so that it accepts a list of all classes you want to take and prints out a schedule where you take only those courses (or prerequisites to those courses). What is the worst-case runtime of your algorithm? There are several possible solutions. One possible solution is to scan through the graph and retain only the courses we want to take (and their pre-reqs). One way we can do this is to start at each mandatory node and run DFS, following the edges backwards. We then save each node and edge we visit into our new graph. We then run toposort on this graph. We visit each node and edge a constant number of times to do the initial filtering, then once again to run toposort. So the final runtime is the same as before.

(c) Challenge: What if you can take up to k classes per quarter? Adjust your algorithm so it prints out a schedule that lets you graduate as soon as possible. What is the runtime of your algorithm? It turns out this is also an example of an NP-COMPLETE problem when k 3! This means that the best we can do boils down to brute force, if you can figure out a non-brute-force algorithm you deserve fame and glory, etc. That said, there are also many different heuristics for solving this problem many algorithms that attempt to approximate a reasonable schedule. It turns out this problem is a fairly important one, and shows up in many different places in computer science. For example, suppose we are trying to schedule tasks on a computer that has 4 cores, where each task may depend on the completion of other pending tasks. In what order should we assign tasks to which processor? Ideally, we d want to make use of all four processors as frequently as possible help make our computer speedy. 8