1. Below is a dynamic programming solution for this problem to illustrate how it can be used. There is a very straightforward O(1) time solution. It can be shown that इफ n >= 50 then any solution will include a set of coins that adds to exactly 50 cents.
Hence it can be shown that an optimal solution uses 2 · [n/50 ] quarters along with un optimal solution for making n/50 − bn/50c cents which can be looked up in a table ऑफ़ size 50.
[Read More]
Matrix chain multiplication
Problem
Given a sequence of n matrices _M_1, _M_2, … M__n, and their dimensions _p_0, _p_1, _p_2, …, p__n, where where i = 1, 2, …, n, matrix M__i has dimension p__i − 1 × p__i, determine the order of multiplication that minimizes the the number of scalar multiplications.
We wish to determine the value of the product ∏i = 1 to n Mi, where Mi has ri-1 rows and ri columns.
[Read More]
Dynamic Programming Practise Problem Set 1
Suppose we want to make change for n cents, using the least number of coins of denominations 1, 10, and 25 cents. Describe an O(n) dynamic programming algorithm to find an optimal solution. (There is also an easy O(1) algorithm but the idea here is to illustrate dynamic programming.) Here we look at a problem from computational biology. You can think of a DNAsequence as sequence of the characters “a”,”c”,”g”,”t”.
[Read More]
Prim's Algorithm
This algorithm was first propsed by Jarnik, but typically attributed to Prim. It starts from an arbitrary vertex (root) and at each stage, add a new branch (edge) to the tree already constructed, much like a mould. The algorithm halts when all the vertices in the graph have been reached.
To understand the problem statement refer here.
This is similar to djikstra’s algorithm where we were clear where to begin with as we were given the source vertex, but here we have to choose arbitrarily.
[Read More]
Kruskal's Algorithm
In kruskal’s algorithm the selection function chooses edges in increasing order of length without worrying too much about their connection to previously chosen edges, except that never to form a cycle. The result is a forest of trees that grows until all the trees in a forest (all the components) merge in a single tree. graph
Example
In Kruskal’s algorithm, we sort the edges according to their edge weights and start counting them and including them in MST, if they are not forming any cycle.
[Read More]
Dynamic-Programming Algorithm for the Activity-Selection Problem
Problem: Given a set of activities to among lecture halls. Schedule all the activities using minimal lecture halls.
In order to determine which activity should use which lecture hall, the algorithm uses the GREEDY-ACTIVITY-SELECTOR to calculate the activities in the first lecture hall. If there are some activities yet to be scheduled, a new lecture hall is selected and GREEDY-ACTIVITY-SELECTOR is called again. This continues until all activities have been scheduled.
[Read More]
An Activity Selection Problem ( Greedy Approach)
An activity-selection is the problem of scheduling a resource among several competing activity.
Statement: Given a set S of n activities with and start time, Si and fi, finish time of an ith activity. Find the maximum size set of mutually compatible activities.
Compatible Activities
Activities i and j are compatible if the half-open internal [si, fi) and [sj, fj) do not overlap, that is, i and j are compatible if si ≥ fj and sj ≥ fi
[Read More]
Dijkstra's Algorithm (Shortest Path)
Consider a directed graph G = (V, E).
Problem Determine the length of the shortest path from the source to each of the other nodes of the graph. This problem can be solved by a greedy algorithm often called Dijkstra’s algorithm.
The algorithm maintains two sets of vertices, S and C. At every stage the set S contains those vertices that have already been selected and set C contains all the other vertices.
[Read More]
Dynamic-Programming Solution to the 0-1 Knapsack Problem
Problem Statement 0-1 Knapsack problem
Input :
There are n items, and each item has a value:
value vi (non negative) size or weight wi (non negative and integral ) Capicity W (non negative and integer) Output:
Select a subset S ⊆ {1,2,3,….n}
that maximizes ∑ vi subject to max value&sum wi ≤ W Sometimes you may hear a cheesy problem a thief robbing a store and can carry a maximal weight of W into their knapsack.
[Read More]
Find the nth Fibonacci number
Problem: Write the code for nth Fibonacci number.
Solution: There are basically two approaches to solving the Fibonacci problem. Lets looks at the definition of Fibonacci series first.
The Fibonacci series is defined as follows
F(n) = 0 for n = 0 1 for n = 1 F(n-1) + F(n-2) for n > 1 So, F(n) = F(n-1)+F(n-2) for n≥2and 1 otherwise.
There are couple of approach to solve this.
[Read More]