CS Algorithms and Complexity

Similar documents
CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

CS 380 ALGORITHM DESIGN AND ANALYSIS

Dynamic Programming 1

Dynamic Programming. Lecture Overview Introduction

CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

CS 170 DISCUSSION 8 DYNAMIC PROGRAMMING. Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17

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

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Algorithms. Ch.15 Dynamic Programming

Subset sum problem and dynamic programming

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Unit-5 Dynamic Programming 2016

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.)

CS261: Problem Set #1

So far... Finished looking at lower bounds and linear sorts.

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

CS171 Final Practice Exam

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

CS Algorithms and Complexity

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

Dynamic Programming CPE 349. Theresa Migler-VonDollen

- Main approach is recursive, but holds answers to subproblems in a table so that can be used again without re-computing

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

(Refer Slide Time: 05:25)

CSE 417: Algorithms and Computational Complexity

4 Dynamic Programming

Last week: Breadth-First Search

CS171 Final Practice Exam

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

Graph Algorithms (part 3 of CSC 282),

Chapter 15-1 : Dynamic Programming I

CMSC 451: Dynamic Programming

Graph Algorithms (part 3 of CSC 282),

DESIGN AND ANALYSIS OF ALGORITHMS

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

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

1. Basic idea: Use smaller instances of the problem to find the solution of larger instances

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Dynamic programming 4/9/18

Tutorial 7: Advanced Algorithms. (e) Give an example in which fixed-parameter tractability is useful.

Info 2950, Lecture 16

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

Lecture 3: Graphs and flows

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

Introduction to Algorithms

ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

CS/COE

Solving NP-hard Problems on Special Instances

Homework3: Dynamic Programming - Answers

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Chapter 3 Dynamic programming

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.

Algorithmics. Some information. Programming details: Ruby desuka?

Lecture 10 Graph algorithms: testing graph properties

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Algorithms and Complexity

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

1 More on the Bellman-Ford Algorithm

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Exact Algorithms for NP-hard problems

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

15.Dynamic Programming

CS 491 CAP Intermediate Dynamic Programming

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

Dynamic Programming CS 445. Example: Floyd Warshll Algorithm: Computing all pairs shortest paths

Dynamic Programming. An Introduction to DP

Final Review Document Solution

1 Non greedy algorithms (which we should have covered

CS 310 Advanced Data Structures and Algorithms

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

CSE 421: Introduction to Algorithms

A loose end: binary search

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Algorithms Assignment 3 Solutions

CSE 101 Homework 5. Winter 2015

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CSE Winter 2015 Quiz 2 Solutions

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2011

Algorithm Design and Analysis

L.J. Institute of Engineering & Technology Semester: VIII (2016)

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

CSE 417 Branch & Bound (pt 4) Branch & Bound

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Dynamic Programming Matrix-chain Multiplication

CS 231: Algorithmic Problem Solving

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

Lecture 8. Dynamic Programming

CS521 \ Notes for the Final Exam

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Transcription:

CS 350 - Algorithms and Complexity Dynamic Programming Sean Anderson 2/20/18 Portland State University

Table of contents 1. Homework 3 Solutions 2. Dynamic Programming 3. Problem of the Day 4. Application to Hard Problems 1

Homework 3 Solutions

Problem 1 - Single cycle detection Easiest way: BFS or DFS tracking visited vertices. If a vertex is revisited, there is a cycle. Time Complexity: if there is no cycle, we will visit every vertex and expand every edge. An acyclic connected graph has V + 1 edges. If there is, in the worst case, we discover it at the very end after visiting every vertex, and stop exploring then. So we will never expand more than V + 1 vertices. Either way, O( V ) time. Space Complexity: tracking visited vertices is O( V ) 2

Problem 2 - Bipartite Graphs Part 1: a tree is acyclic, so we can color it by assigning a color to one node, the other color to its neighbors, and so on - no neighboring nodes share a neighbor Part 2: in a rooted tree, all nodes of even depth will share a color, and nodes of odd depth the other color 3

Dynamic Programming

Recursive Fibonacci Fibonacci sequence is recursively defined: F i = F i 1 + F i 2 We can compute it recursively: F i = F i 1 + F i 2 = F i 2 + F i 3 + F i 2 = Pseudocode: Fibonacci(i): if i 2: return 1 return Fibonacci(i 1) + Fibonacci(i 2) 4

Recursive Fibonacci F i F i 1 F i 2 F i 2 F i 3 F i 3 F i 4 F i 3 F i 4 F i 4 F i 4 5

Recursive Fibonacci F i F i 1 F i 2 F i 2 F i 3 F i 3 F i 4 F i 3 F i 4 F i 4 F i 4 6

Recursive Fibonacci Complexity This is exponential growth! But we don t need to compute subproblems repeatedly. If we compute and store F 1, F 2, F i 1, we have everything we need to compute F i That s O(n) time and space complexity! If we discard values more than two below current, that can be constant space complexity 7

Memoization and Tabulation Dynamic Programming is analogous to Divide-and-Conquer, but with overlapping subproblems. Building a table of computed subproblems recursively is called Memoization Starting at the bottom and building up a table of computed subproblems is called Tabulation 8

Is There a Difference? The key difference is that tabulation will typically compute all subproblems, whether or not they would come up in a recursive memoization algorithm. On the other hand, depending on the problem, either top-down or bottom-up techniques could be more intuitive or perform differently. 9

Coin-Path Problem Suppose we have an m n grid with coins scattered throughout it. Each tile (i, j) has c ij coins. We want to send a robot from (1, 1) to (m, n) along a shortest path that also allows it to collect the maximum number of coins. Notes: Assuming (1, 1) is the top-left corner, we re trying to reach the bottom-right, so we will always move either down or right Any path will have m 1 right moves and n 1 down moves 10

Pictures 11

Recursive Pseudocode CoinPath(i, j): if i < m: down CoinPath(i, j + 1) if j < n: right CoinPath(i + 1, j) if j = i = 0: return 0 return c ij + max(down, right) 12

Memoization Pseudocode T m n array of NULL CoinPath(i, j): if i < m: if T[i, j + 1] = NULL: T[i, j + 1] CoinPath(i, j + 1) if j < n: dn T[i, j + 1] if T[i + 1, j] = NULL: T[i + 1, j] CoinPath(i + 1, j) rt T[i + 1, j] else: return 0 return c ij + max(dn, rt) 13

Tabulation Pseudocode CoinPath(): T m n array for i m..1: return T[1][1] for j n..1: T[i][j] c ij +max(t[i + 1][j], T[i][j + 1]) 14

Pictures 15

Analysis The recursive version acts like the tabulation when the recursion is unrolled. Either way, O(mn) time and space. 16

String Edit Distance Measure of how different two strings are. Minimum path from string A to B via one of three operations: Insertion - bother brother by add r Deletion - brother bother by add r Modification - sitting fitting by s f 17

Wagner-Fischer Algorithm for String Edit Distance For two strings A and B, let d ij be the distance between their i and j length suffixes So, for kitten and sitting, d 67 is the distance between kitten and sitting d 56 is the distance between itten and itting 18

Wagner-Fischer Recurrence The we can define our distances: d i,0 = i d 0,j = j d i,j = d i 1,j 1 else d i,j = min of: for 0 i A for 0 i B if a i = b i d i 1,j + 1 (delete) d i,j 1 + 1 (add) d i 1,j 1 + 1 (modify) 19

Analysis Let m = A, n = B Normal recursive approach has runtime O(3 MIN(m,n) ) on some inputs (imagine aaaaaaaaa and bbbbbbbb. With dynamic programming, O(mn time and space 20

20

Problem of the Day

Plank Cutting Suppose we have a plank of wood n units long, and an array of prices p i for boards each integer length 0 i n. Give an algorithm to find the optimal combination of boards to cut our plank into. 21

Application to Hard Problems

{0,1}-Knapsack Recursive algorithm idea for knapsack: for each item i, recurse on subproblem with items < i and with weight capacity W w i This gives us an n W table 22

Polynomial and Pseudopolynomial We ve said a lot about problems being hard, but not necessarily what that means A polynomial time algorithm has runtime in O(n c ) for some c We make a distinction between that and exponential, factorial, etc. If our knapsack dynamic programming is O(Wn c ), that s not quite polynomial because W = 2 W - but it s a bit different from O(2 n ). It s called pseudopolynomial. 23

References i