Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simplifying Dynamic Programming Jamil Saquer & Lloyd Smith Computer Science Department Missouri State University Springfield, MO USA.

Similar presentations


Presentation on theme: "Simplifying Dynamic Programming Jamil Saquer & Lloyd Smith Computer Science Department Missouri State University Springfield, MO USA."— Presentation transcript:

1 Simplifying Dynamic Programming Jamil Saquer & Lloyd Smith Computer Science Department Missouri State University Springfield, MO USA

2 Motivation Dynamic programming (DP) is a fundamental topic in an algorithms course Many students find DP challenging This presentation is work in progress for making DP easier for students 2

3 Idea Introduce subject early in the curriculum –CS2 –start with simple and interesting problems This should make DP easier for students when they encounter it later 3

4 Where to start? Fibonacci sequence –F 1 = F 2 = 1 –F n = F n-1 + F n-2, if n >2 It can be introduced when students study recursion or big-O Shows how using a cache table improves performance 4

5 What else with Fibonacci? Also using the Fibonacci sequence to show –overlapping sub-problems property –optimal substructure property: solution to original problem contains solutions to sub- problems 5

6 What else with Fibonacci? (cont.) top down approach –when a value is needed, first look for it in table –only if not there, call function, store value in table, then return it bottom up approach: –a table is built starting with basic case(s) –then, rest of table is filled in a bottom up fashion to reach the solution 6

7 Where to go from here Use simple and interesting problems Bottom up approach is easier and better to stick with initially 7

8 Examples of Problems The jump it game Number of ways to climb n steps Robot in a grid Maximum number of golden coins Minimum cost path 8

9 The Jump It Game Given a board with n cells Each cell, except the first, contains a positive integer representing the cost to visit that cells First cell contains 0 Player starts at first cell Player has two types of moves –either move to the adjacent cell –or jump over adjacent cell Cost of game is the sum of costs of visited cells Goal: reach last cell with the cheapest cost Task: find this cheapest cost 9 057574311

10 Number of Ways to climb Stairs Given a stairway consisting of n steps A child stands at the bottom of the stairway and wants to reach the top most step The child can take 1, 2 or 3 steps at a time Goal: find the number of possible ways to climb the stairs 10

11 Robot in a Grid Imagine a robot at the top left corner in a two dimensional grid The robot can only move right or bottom to an adjacent cell Find the number of possible ways for the robot to reach the bottom right cell 11 finish start

12 Maximum Number of Golden Coins Given an m by n table Each cell contains a number of golden coins One starts at the top-left corner and wants to reach the bottom-right corner At each step one can move one cell right or down Goal: find the maximum number of golden coins you can collect 12 539 722 231 5817 121419 141720

13 Minimum Cost Path Given a 2D grid where each cell contains a cost One can move one cell right, down or diagonal Find the minimum cost to reach the bottom-right cell starting from the top-left cell Cost of a path is the sum of the costs of all cells on that path 13

14 What have we done? Collected data in the algorithms course Introduced DP in CS2 this semester This group will take algorithms next semester We will collect data next semester Compare performance of two groups 14

15 Questions 15


Download ppt "Simplifying Dynamic Programming Jamil Saquer & Lloyd Smith Computer Science Department Missouri State University Springfield, MO USA."

Similar presentations


Ads by Google