Dynamic Programming in Data Structures and Algorithms - Lecture Slides | CS 245, Study notes of Data Structures and Algorithms

Material Type: Notes; Professor: Galles; Class: Data Struct & Algorithms; Subject: Computer Science; University: University of San Francisco (CA); Term: Intersession 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-ur5
koofers-user-ur5 🇺🇸

10 documents

1 / 149

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
CS245-2009S-22
Dynamic Programming
David Galles
Department of Computer Science
University of San Francisco
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Dynamic Programming in Data Structures and Algorithms - Lecture Slides | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

CS245-2009S-22Dynamic Programming^ David GallesDepartment of Computer ScienceUniversity of San Francisco

22-0:^ Dynamic Programming^ Simple, recursive solution to a problem^ Naive solution recalculates same value many times^ Leads to exponential running time

22-2:^ Fibonacci Numbers int Fibonacci(int n) {if (n == 0)return 1;if (n == 1)return 1;return Fibonacci(n-1) + Fibonacci(n-2);}

22-3:^ Fibonacci Numbers^ Why is this solution bad?^ Recalculate values many times^ Many, many, times

22-5:^ Fibonacci Numbers^ Fib(3)Fib(2)Fib(1) Fib(1)^ Fib(0)^ Fib(1)^

Fib(5) Fib(4)^ Fib(3)Fib(2)Fib(1) Fib(1)^ Fib(0)Fib(2)Fib(0)^ Fib(3)Fib(2) Fib(1)^ Fib(0) Fib(6)^ Fib(1)^ Fib(4)Fib(2)Fib(1)^ Fib(1)^ Fib(0)

Fib(4) Fib(3)Fib(2)Fib(1)Fib(2)Fib(0)^ Fib(1)^ Fib(0)^ Fib(1) Fib(5)^ Fib(3)Fib(2)Fib(1)^ Fib(0) Fib(7)

22-6:^ Fibonacci Numbers^ Fib(4)^ Fib(3)Fib(2)Fib(1)Fib(2) Fib(1)^ Fib(0)^ Fib(1)^ Fib(0)

Fib(5)^ Fib(3)Fib(2)Fib(1) Fib(1)^ Fib(0)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0) Fib(6)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0)^ Fib(1)^ Fib(4)Fib(2)Fib(1)^ Fib(0) Fib(7)^ Fib(5)^ Fib(4)Fib(2)^ Fib(0)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0)^ Fib(5)^ Fib(4)^ Fib(3)^ Fib(3)Fib(2)Fib(1)Fib(2)Fib(1)^ Fib(1)^ Fib(0)Fib(2) Fib(1)^ Fib(0)^ Fib(1)^ Fib(0)^ Fib(3)Fib(2)Fib(1)^ Fib(1)^ Fib(0)

Fib(8)^ Fib(6)^ Fib(4)Fib(2)^ Fib(1)^ Fib(0)

22-8:^ How Bad is Recalculation?^ Assume 2 GHz machine^ Add every cycle^ No time spent on recursive call overhead^ Lower bound on time required^ Fibonacci(100) will take:^ 9087 Years

22-9:^ How Bad is Recalculation?^ Assume 2 GHz machine^ Add every cycle^ No time spent on recursive call overhead^ Lower bound on time required^ Fibonacci(100) will take:^ 9087 Years^ Fibonacci(200) will take:

22-11:^ Dynamic Programming^ Recalculating values can lead to unacceptable runtimes^ Even if the total number of values that needs tobe calculated is small^ Solution: Don’t recalculate values^ Calculate each value once^ Store results in a table^ Use the table to calculate larger results

22-12:^ Dynamic Programming^ To calculate Fibonacci(100), only need to calculate101 values^ Fibonacci(n) can be calculated in time

O(1)

Assuming we have values for Fibonacci(n-1)and Fibonacci(n-2)

22-14:^ Faster Fibonacci int Fibonacci(int n) {int[] FIB = new int[n+1];FIB[0] = 1;FIB[1] = 1;for (i=2; i<=n; i++)FIB[i] = FIB[i-1] + FIB[i-2];return FIB[n];}

22-15:^ Dynamic Programming^ To create a dynamic programming solution to aproblem:^ Create a simple recursive solution (that mayrequire a large number of repeat calculations^ Design a table to hold partial results^ Fill the table such that whenever a partial resultis needed, it is already in the table

22-17:^ World Series^ Calculate the probability that

Twill win the series,^1

given^ Tneeds to win^1

x^ more games, and

T^2

needs to win

y^ more games PT1win(x,y)

The probability that

Pwill win a best-of-seven^1

series is then PT1win(4,4) The probability that

Pwill win a best-of-seven^1

series, if^ P^1

has already one 2 games, and

Phas^2

won 1 game is then PT1win(2,3)

22-18:^ World Series^ Base cases:^ What is PT1win(0,x)?