Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#14 from himanshub16/dp/basic-algos
Browse files Browse the repository at this point in the history
Add dynamic programming algorithms
  • Loading branch information
dynamitechetan authored Oct 11, 2017
2 parents c282e70 + 6670a40 commit 802aeca
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
65 changes: 65 additions & 0 deletions dynamic-programming/longest-palindromic-subsequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// longest palindromic subsequence
// http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/

package longestPalindromicSubsequence

// package main

import "fmt"

func max(a, b int) int {
if a > b {
return a
}
return b
}

func lpsRec(word string, i, j int) int {
if i == j {
return 1
}
if i > j {
return 0
}
if word[i] == word[j] {
return 2 + lpsRec(word, i+1, j-1)
}
return max(lpsRec(word, i, j-1), lpsRec(word, i+1, j))
}

func lpsDp(word string) int {
N := len(word)
dp := make([][]int, N)

for i := 0; i < N; i++ {
dp[i] = make([]int, N)
dp[i][i] = 1
}

for l := 2; l <= N; l++ {
// for length l
for i := 0; i < N-l+1; i++ {
j := i + l - 1
if word[i] == word[j] {
if l == 2 {
dp[i][j] = 2
} else {
dp[i][j] = 2 + dp[i+1][j-1]
}
} else {
dp[i][j] = max(dp[i+1][j], dp[i][j-1])
}
}
}

return dp[1][N-1]
}

/*
func main() {
// word := "aaabbbbababbabbabbabf"
word := "aaaabbbba"
fmt.Printf("%d\n", lpsRec(word, 0, len(word)-1))
fmt.Printf("%d\n", lpsDp(word))
}
*/
61 changes: 61 additions & 0 deletions dynamic-programming/matrix-multiplication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// matrix chain multiplication problem
// https://en.wikipedia.org/wiki/Matrix_chain_multiplication
// www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

// package main
package matrixChainMultiplication

import "fmt"

func min(a, b int) int {
if a > b {
return b
} else {
return a
}
}

func matrixChainRec(D []int, i, j int) int {
// d[i-1] x d[i] : dimension of matrix i
if i == j {
return 0
}
q := 1 << 32
for k := i; k < j; k++ {
prod := matrixChainRec(D, i, k) + matrixChainRec(D, k+1, j) + D[i-1]*D[k]*D[j]
q = min(prod, q)
}
return q
}

func matrixChainDp(D []int) int {
// d[i-1] x d[i] : dimension of matrix i
N := len(D)

dp := make([][]int, N) // dp[i][j] = matrixChainRec(D, i, j)
for i := 0; i < N; i++ {
dp[i] = make([]int, N)
dp[i][i] = 0
}

for l := 2; l < N; l++ {
for i := 1; i < N-l+1; i++ {
j := i + l - 1
dp[i][j] = 1 << 31
for k := i; k < j; k++ {
prod := dp[i][k] + dp[k+1][j] + D[i-1]*D[k]*D[j]
dp[i][j] = min(prod, dp[i][j])
}
}
}

return dp[1][N-1]
}

/*
func main() {
D := []int{2, 2, 2, 2, 2} // 4 matrices
fmt.Print(matrixChainRec(D, 1, 4), "\n")
fmt.Print(matrixChainDp(D), "\n")
}
*/
60 changes: 60 additions & 0 deletions dynamic-programming/rod-cutting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Solution to Rod cutting problem
// https://en.wikipedia.org/wiki/Cutting_stock_problem
// http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/

package dpRodCutting

// package main

import "fmt"

func max(a, b int) int {
if a > b {
return a
} else {
return b
}

}

// solve the problem recursively: initial approach
func cutRodRec(price []int, length int) int {
if length == 0 {
return 0
}

q := -1
for i := 1; i <= length; i++ {
q = max(q, price[i]+cutRodRec(price, length-i))
}
return q
}

// solve the same problem using dynamic programming
func cutRodDp(price []int, length int) int {
r := make([]int, length+1) // a.k.a the memoization array
r[0] = 0 // cost of 0 length rod is 0

for j := 1; j <= length; j++ { // for each length (subproblem)
q := -1
for i := 1; i <= j; i++ {
q = max(q, price[i]+r[j-i]) // avoiding recursive call
}
r[j] = q
}

return r[length]
}

/*
func main() {
length := 10
price := []int{0, 1, 5, 8, 9, 17, 17, 17, 20, 24, 30}
// price := []int{0, 10, 5, 8, 9, 17, 17, 17, 20, 24, 30}
// fmt.Print(price[5]+price[length-5], "\n")
fmt.Print(cutRodRec(price, length), "\n")
fmt.Print(cutRodDp(price, length), "\n")
}
*/

0 comments on commit 802aeca

Please sign in to comment.