Dynamic Programming

All Pair shortest path

The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph. The Graph is represented as Adjancency Matrix, and the Matrix denotes the weight of the edegs (if it exists) else INF.

Input:
The first line contains an integer V denoting the size of the adjacency matrix(Starting with 0 as the initial name of the Vertex). The next V lines contain V space separated values of the matrix (graph). All input will be integer type.

Assembly line Scheduling

A car factory has two assembly lines, each with n stations. A station is denoted by Si,j where i is either 1 or 2 and indicates the assembly line the station is on, and j indicates the number of the station. The time taken per station is denoted by ai,j. Each station is dedicated to some sort of work like engine fitting, body fitting, painting and so on. So, a car chassis must pass through each of the n stations in order before exiting the factory. The parallel stations of the two assembly lines perform the same task. After it passes through station Si,j, it will continue to station Si,j+1 unless it decides to transfer to the other line. Continuing on the same line incurs no extra cost, but transferring from line i at station j – 1 to station j on the other line takes time ti,j. Each assembly line takes an entry time ei and exit time xi which may be different for the two lines.

Input:
Enter the number of assembly lines (n) first line. Array e and array x in next line the initial input and output. Then next two lines with the time taken in production with n space integer followed by two lines for transfer the input form one assembly line to other with n spaced integer.

Matrix Chain Multiplication

Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. There are many options to multiply a chain of matrices because matrix multiplication is associative i.e. no matter how one parenthesize the product, the result will be the same.

Input:
The first line contains an integer N, denoting the number of elements in the array. Then next line contains N space separated integers denoting the values of the element in the array.

Longest Comman Subsequence

Given two sequences, find the length of longest subsequence present in both of them. Both the strings are of uppercase.

Input:
Enter 2 space separated integers A and B denoting the size of string str1 and str2 respectively The next two lines contains the 2 string str1 and str2 .

0/1 knapsack

You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).

Input:
Enter 2 numbers first the number of items and second the maximum capacity of the knapsack. In the next line are N space separated positive integers denoting the values of the N items, and in the next line are N space separated positive integers denoting the weights of the corresponding items.