dynamic

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 5 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 Abbreviation

func Abbreviation(a string, b string) bool

Returns true if it is possible to make a equals b (if b is an abbreviation of a), returns false otherwise

func Bin2

func Bin2(n int, k int) int

Bin2 function

func CoinChange

func CoinChange(coins []int32, amount int32) int32

CoinChange finds the number of possible combinations of coins of different values which can get to the target amount.

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

func LongestIncreasingSubsequenceGreedy(nums []int) int

LongestIncreasingSubsequenceGreedy is a function to find the longest increasing subsequence in a given array using a greedy approach. The dynamic programming approach is implemented alongside this one. Worst Case Time Complexity: O(nlogn) Auxiliary Space: O(n), where n is the length of the array(slice). Reference: https://www.geeksforgeeks.org/construction-of-longest-monotonically-increasing-subsequence-n-log-n/

func LpsDp

func LpsDp(word string) int

LpsDp function

func LpsRec

func LpsRec(word string) 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