6.4 KiB
6.4 KiB
💻 Assignment 04 - Problem Solving Methods
Requirements
- You will have to solve two problem statements from the list below, one using the backtracking programming method and one using the dynamic programming method.
- For the backtracking problem, implement both an iterative as well as a recursive algorithm (deadline is week 5).
- For the dynamic programming problem, implement both the naive, non-optimized version as well as the dynamic programming version (deadline is week 6).
- For the dynamic programming implementation, display the data structure used to memorize the intermediate results and be able to explain how it works.
- For all implementations understand and be able to explain the computational complexity with regards to runtime.
Problem Statements
Backtracking
- A number of
ncoins are given, with values of a1, ..., an and a values. Display all payment modalities for the sums. If no payment modality exists print a message. - Consider a positive number
n. Determine all its decompositions as sums of prime numbers. - The sequence a = a1, ..., an with integer elements is given. Determine all strictly increasing subsequences of sequence
a(conserve the order of elements in the original sequence). - A player at
PRONOSPORTwants to choose score options for four games. The options may be1,X,2. Generate all possible alternatives, knowing that:
- The last score option may not be
X - There should be no more than two score options of
1
- The sequence a = a1, ..., an with distinct integer numbers is given. Determine all subsets of elements having the sum divisible by a given
n. - Generate all sequences of
nparentheses that close correctly. Example: forn=4there are two solutions:(())and()() - Generate all subsequences of length
2n+1, formed only by0,-1or1, such that a1 = 0, ..., a2n+1= 0 and |ai+1 - ai| = 1 or 2, for any 1 ≤ i ≤ 2n. - Consider
npoints in a plane, given by their coordinates. Determine all subsets with at least three elements formed by collinear points. If the problem has no solution, give a message. - The sequence a = a1, ..., an with distinct integer elements is given. Determine all subsets of at least two elements with the property:
- The elements in the subset are in increasing order
- Any two consecutive elements in the subsequence have at least one common digit
- A group of
n(n<=10) persons, numbered from1tonare placed on a row of chairs, but between every two neighbor persons (e.g. persons 3 and 4, or persons 7 and 8) some conflicts appeared. Display all the possible modalities to replace the persons, such that between any two persons in conflict stay one or at most two other persons. - Two natural numbers
mandnare given. Display in all possible modalities the numbers from1ton, such that between any two numbers on consecutive positions, the difference in absolute value is at leastm. If there is no solution, display a message. - Consider the natural number
n(n<=10) and the natural numbers a1, ..., an. Determine all the possibilities to insert between all numbers a1, ..., an the operators+and–such that by evaluating the expression the result is positive. - The sequence a1, ..., an of distinct integer numbers is given. Display all subsets with a mountain aspect. A set has a mountain aspect if the elements increase up to a point and then they decrease. E.g.
10, 16, 27, 18, 14, 7. - Generate all numbers of
ndigits with the property that no number has two identical neighboring subsequences. For example, forn=6,121312is correct, and121313and132132are not correct.
Dynamic Programming
- Determine the longest common subsequence of two given sequences. Subsequence elements are not required to occupy consecutive positions. For example, if
X = "MNPNQMN"andY = "NQPMNM", the longest common subsequence has length4, and can be one of"NQMN","NPMN"or"NPNM". Determine and display both the length of the longest common subsequence as well as at least one such subsequence. - Given the set of positive integers
Sand the natural numberk, display one of the subsets ofSwhich sum tok. For example, ifS = { 2, 3, 5, 7, 8 }andk = 14, subset{ 2, 5, 7 }sums to14. - Given the set of positive integers
S, partition this set into two subsetsS1andS2so that the difference between the sum of the elements inS1andS2is minimal. For example, for setS = { 1, 2, 3, 4, 5 }, the two subsets could beS1 = { 1, 2, 4 }andS2 = { 3, 5 }. Display at least one of the solutions. - Given an
n * nsquare matrix with integer values, find the maximum length of a snake sequence. A snake sequence begins on the matrix's top row (coordinate(0, i), 0 <= i < n). Each element of the sequence, except the first one, must have a value±1from the previous one and be located directly below, or directly to the right of the previous element. For example, element(i, j)can be succeded by one of the(i, j + 1)or(i + 1, j)elements. Display the length as well as the sequence of coordinates for one sequence of maximum length. - Maximize the profit when selling a rod of length
n. The rod can be cut into pieces of integer lengths and pieces can be sold individually. The prices are known for each possible length. For example, if rod lengthn = 7, and the price array isprice = [1, 5, 8, 9, 10, 17, 17](the price of a piece of length3is8), the maximum profit is18, and is obtained by cutting the rod into 3 pieces, two of length two and one of length 3. Display the profit and the length of rod sections sold to obtain it. - Given an array of integers
A, maximize the value of the expressionA[m] - A[n] + A[p] - A[q], wherem, n, p, qare array indices withm > n > p > q. ForA = [30, 5, 15, 18, 30, 40], the maximum value is32, obtained as40 - 18 + 15 - 5. Display both the maximum value as well as the expression used to calculate it. - Given a set of integers
A, determine if it can be partitioned into two subsets with equal sum. For example, setA = { 1, 1, 1, 1, 2, 3, 5 }can be partitioned into setsA1 = { 1, 1, 2, 3 }andA2 = { 1, 1, 5 }, each of them having sum7. Display one such possibility.