dynamic

package
v0.0.3-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package dynamic is a package of certain implementations of dynamically run algorithms.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidPosition = fmt.Errorf("invalid position in subset")
View Source
var ErrNegativeSum = fmt.Errorf("negative sum is not allowed")

Functions

func Bin2

func Bin2(n int, k int) int

Bin2 function

func CutRodDp

func CutRodDp(price []int, length int) int

CutRodDp solve the same problem using dynamic programming

func CutRodRec

func CutRodRec(price []int, length int) int

CutRodRec solve the problem recursively: initial approach

func EditDistanceDP

func EditDistanceDP(first string, second string) int

EditDistanceDP is an optimised implementation which builds on the ideas of the recursive implementation. We use dynamic programming to compute the DP table where dp[i][j] denotes the edit distance value of first[0..i-1] and second[0..j-1]. Time complexity is O(m * n) where m and n are lengths of the strings, first and second respectively.

func EditDistanceRecursive

func EditDistanceRecursive(first string, second string, pointerFirst int, pointerSecond int) int

EditDistanceRecursive is a naive implementation with exponential time complexity.

func IsSubsetSum

func IsSubsetSum(array []int, sum int) (bool, error)

func Knapsack

func Knapsack(maxWeight int, weights, values []int) int

Knapsack solves knapsack problem return maxProfit

Example
package main

import (
	"fmt"

	"github.com/TheAlgorithms/Go/dynamic"
)

func main() {
	fmt.Print(dynamic.Knapsack(10, []int{4, 5, 8}, []int{50, 15, 60}))
}
Output:

65

func LongestCommonSubsequence

func LongestCommonSubsequence(a string, b string, m int, n int) int

LongestCommonSubsequence function

func LongestIncreasingSubsequence

func LongestIncreasingSubsequence(elements []int) int

LongestIncreasingSubsequence returns the longest increasing subsequence where all elements of the subsequence are sorted in increasing order

func LpsDp

func LpsDp(word string) int

LpsDp function

func LpsRec

func LpsRec(word string, i, j int) int

LpsRec function

func MatrixChainDp

func MatrixChainDp(D []int) int

MatrixChainDp function

func MatrixChainRec

func MatrixChainRec(D []int, i, j int) int

MatrixChainRec function

func Max

func Max(a, b int) int

Max function - possible duplicate

func NthCatalanNumber

func NthCatalanNumber(n int) (int64, error)

NthCatalan returns the n-th Catalan Number Complexity: O(n²)

func NthFibonacci

func NthFibonacci(n uint) uint

NthFibonacci returns the nth Fibonacci Number

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL