- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program that takes in a list of weights and values for items and a maximum weight capacity for knapsack
In this Go language article, we will write programs that take in list of weights and values for items and a maximum weight capacity for knapsack. Knapsack problem is an optimization problem that uses dynamic programming. Here, the purpose is to find out the set of items that can be included in the knapsack without exceeding its weight capacity or maximum weight.
Dynamic programming involves solving of problems by breaking them down into smaller subproblems and them combining them to get an optimal solution.
Syntax
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.
func len(v Type) int
The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.
Algorithm
This program imports the necessary packages fmt and main in the program.
Create a struct type called Item which contains two fields item with its weight and value both of type int.
Create a knapsack function that takes an array of items and the capacity as input parameters.
This function returns the maximum value that can be achieved within the given capacity.
In this step, create a 2D slice named dp to store the dynamic programming table using the make function.
This table has len(items)+1 rows and capacity+1 columns.
In this step, initialize the first row and column of the table with zeros since they represent the case when no items are chosen or the capacity is zero.
Iterate over the items and capacity using two nested loops which check if the current item's weight is less than or equal to the current capacity then calculate the maximum value: (items[i-1].value + dp[i-1][j-items[i-1].weight], dp[i-1][j]).
But, if the current item's weight is greater than the current capacity, it cannot be included. In this case, the maximum value remains the same as the previous row.
After the loops, the maximum value and capacity will be stored in dp[len(items)][capacity].
Return the value.
In this step, implement the max function that takes two integers and returns the maximum of the two
Create a main function.
In the main, initialize the weights, values and the capacity of the items.
In this step, use make function to create an item slice equal to the length of the weights.
Use a for loop to fill the slice based on the weights and the values provided there.
In this step, call the knapsack function with the items and capacity and assign the output to the variable named maxValue.
Finally, print the maximum value on the console using the Println function from the fmt package where ln means new line.
Example
In this example, we will write a Golang program that takes in a list of weights and values for items and maximum weight capacity for knapsack based on the dynamic programming approach.
package main import ( "fmt" ) type Item struct { weight int value int } func knapsack(items []Item, capacity int) int { dp := make([][]int, len(items)+1) for i := 0; i <= len(items); i++ { dp[i] = make([]int, capacity+1) } for i := 1; i <= len(items); i++ { for j := 1; j <= capacity; j++ { if items[i-1].weight <= j { dp[i][j] = max(items[i-1].value+dp[i-1][j-items[i-1].weight], dp[i-1][j]) } else { dp[i][j] = dp[i-1][j] } } } return dp[len(items)][capacity] } func max(a, b int) int { if a > b { return a } return b } func main() { weights := []int{2, 3, 4, 5} values := []int{3, 4, 5, 6} capacity := 5 items := make([]Item, len(weights)) for i := 0; i < len(weights); i++ { items[i] = Item{weight: weights[i], value: values[i]} } maxValue := knapsack(items, capacity) fmt.Println("Maximum value:", maxValue) }
Output
Maximum value : 7
Conclusion
We compiled and executed the Golang program that takes in a list of weights and values for items and a maximum weight capacity for knapsack using dynamic programming approach.