algorithm,np-complete,subset-sum , Reduce Subset Sum to Polyomino Packing

Reduce Subset Sum to Polyomino Packing


Tag: algorithm,np-complete,subset-sum

This is a homework assignment, so any help is appreciated.

I should prove that the following problem is NP-complete. The hint says that you should reduce the subset sum problem to this problem.

Given a set of shapes, like the below, and an m-by-n board, decide whether is it possible to cover the board fully with all the shapes. Note that the shapes may not rotate.

For example, for a 3-by-5 board and the following pieces, the board can be covered like this:


covered board

Now the important thing to note is that the subset sum problem we are trying to reduce should be given input length polynomial in terms of m and n.

Any ideas for using another NP-complete problem are appreciated.


I would recommend doing this reduction in two steps.

First, reduce subset-sum to the partition problem. This problem is related to subset-sum, except that instead of being given a target k, the goal is to split the set into two exactly equal halves. This isn't too hard (I'll leave it as an exercise) and makes the next part a lot easier.

Next, reduce the partition problem to the tiling problem. Intuitively, the idea is the following. If the input numbers in the partition problem are n1, n2, ..., nk, create tiles whose sizes are 3n1 × 1, 3n2 × 1, ..., and 3nk × 1, where w is some number we'll choose later. Then, if the sum of the numbers in the input set is 2N (meaning that the goal is to split the set into two subsets that sum up to N each), create an 3N × 2 board.

The intuition is that if we can split the set into two equal-sum subsets, then we can tile this board by taking all the numbers from the first half, choosing those tiles, then laying them end-to-end to form an 3N × 1 rectangle. We can then take the numbers from the remaining half and use them to form an 3N × 1 rectangle as well, so putting the first line of tiles in the top row and the second line of tiles in the second row completely tiles the board.

To show that the other direction works, first notice that the tiles all have size at least 3 × 1, so they have to be oriented from the left to the right. Therefore, if we can tile the board, we've found a way to split the tiles into two groups whose widths are equal, so there's some way to split the set into two equal-sum pieces.

Overall, the size of the input only grows by a polynomial at most (we triple the size of each number, which adds at most two bits to each input number length), and the size of the board is polynomially as large as the input. Therefore, overall, the reduction takes only polynomial time to evaluate.

Hope this helps!


Should checking loop conditions be counted towards total number of comparisons?

I have implemented three different sorting algorithms and now I want to confirm that my approach of counting the total number of comparisons is correct. In my mind, the number of comparisons shouldn't be tied to the conditional branches because if the condition isn't met, the comparison was still made...

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

I tried to find all pairs in the array with sum equal to k, the solution to that takes O(n*log(n)) time. Here is the code snippet - map<int,int> mymap; map<int,int>::iterator it; cin>>n>>k; for( int i = 0 ; i < n ; i++ ){ cin>>a; if( mymap.find(a) != mymap.end() )...

Does there exist an algorithm for iterating through all strings that conform to a particular regex?

I'm making a script to try and hack into an account whose login password is at least 8 characters long and includes at least 1 number, 1 special character and 1 capital letter. I will use brute force. Is there a compact, elegant and efficient way to iterate through every...

How can I declare a counter inside of my recursive function? (Additive Persistence: Coderbyte)

While working through some Coderbyte challenges, I was able to solve the following problem recursively, but was hoping to get some feedback on how I can improve it. Have the function AdditivePersistence(num) take the num parameter being passed which will always be a positive integer and return its additive persistence...

Update minimum spanning tree if edge is added

I have a possible solution for the following question, but not sure if correct: Assume we have already found a minimum spanning tree T for a weighted, undirected graph G = (V,E). We would like to be able to efficiently update T should G be altered slightly. An edge is...

Identify that a string could be a datetime object

If I knew the format in which a string represents date-time information, then I can easily use datetime.datetime.strptime(s, fmt). However, without knowing the format of the string beforehand, would it be possible to determine whether a given string contains something that could be parsed as a datetime object with the...

Binary Search - Best and worst case

In an exam I see a question: Which one of the following is true? For a binary search, the best-case occurs when the target item is in the beginning of the search list. For a binary search, the best-case occurs when the target is at the end of the search...

Vector Merge Algorithm C++

Having a hard time designing an efficient algorithm that accomplishes the following. If I start with a 2d vector A, A = [1 2 3; 2 3 4; 5 6] I want to take the rows that contain common elements and combine them (removing duplicates) resulting in 2d vector B:...

Hungarian algorithm in PHP with multiple assignments

In the following we are facing the problem of multiple assignments of the hungarian algorithm. Scenario: We have 100 students and 5 courses, which the students can vote with priority. So every student will get assigned for one specific cours. Priority from 1 to 5. 1 lowest 5 highest The...

Removing a prior sample while using Welford's method for computing single pass variance

I'm successfully using Welford's method to compute running variance and standard deviation as described many times on Stack Overflow and John D Cook's excellent blog post. However in the stream of samples, sometimes I encounter a "rollback", or "remove sample" order, meaning that a previous sample is no longer valid...

Update minimum spanning tree if edge is removed

I am having trouble with the following question: Assume we have already found a minimum spanning tree T for a weighted, undirected graph G = (V,E). We would like to be able to efficiently update T should G be altered slightly. An edge is removed from G to produce a...

How to apply ordering to a Scala Priority Queue?

I've defined a priority queue like so import scala.collection.mutable.PriorityQueue ... val queue = new PriorityQueue[(Int,Int)]() I want to use this ordering: If we are comparing two items A and B in the queue, A is bigger than B if the first element of its (Int,Int) tuple is larger than B's....

How to check whether the letters of a string exist in the given order within another string?

I'm trying to make a program to check whether the word "nadia" is present with this sequence in a string. if you can delete zero or more characters to get this word then you should print "YES" else print "NO". Can anyone help me find what are the test cases...

algorithm to get all combinations of splitting up N items into K bins

Assuming I have a list of elements [1,2,3,4,] and a number of bins (let's assume 2 bins), I want to come up with a list of all combinations of splitting up items 1-4 into the 2 bins. Solution should look something like this [{{1}, {2,3,4}}, {{2}, {1,3,4}}, {{3}, {1,2,4}}, {{4},...

Looking for a particular algorithm for numerical integration

Consider the following differential equation f(x) = g'(x) I have a build a code that spits out values of the function f(x) for the variable x, where x goes from 0 to very large. Now, I'm looking for a scheme that will analyse these values of f(x) in order to...

Disconnect all vertices in a graph - Algorithm

I am looking for an algorithm that finds minimal subset of vertices such that by removing this subset (and edges connecting these vertices) from graph all other vertices become unconnected (i.e. the graph won't have any edges). Is there such algorithm? If not: Could you recommend some kind of heuristics...

What is this algorithm mapping coordinates to numbers called?

I'm writing a program for visualizing crystals. As a part of the program, I have to generate all different basic points in a lattice structure. For those that aren't familiar with crystallography, you can find the most general cases of these structures here: The problem was that I wanted...

Knapsack with unbounded items

I am familiar with the 0-1 knapsack problem and when you are given a certain number of copies from each item but I can figure out how to solve it when you are given infinite copies of each item using dynamic programming. I am trying to solve it by hand...

create specific permutation of a list in python [closed]

I'm trying to write a function that would input a list (of pre-determined length) and output the permutation of that list according to my choice of re-ordering. For example, suppose I have the list [1,'a',12,'b','poppy'] and I have the permutation (3,4,5,2,1) Then the function output would be a list with...

How to generate all partitions of a set

For a set of the form A = {1, 2, 3, ..., n}. It is called partition of the set A, a set of k<=n elements which respect the following theorems: a) the union of all the partitions of A is A b) the intersection of 2 partitions of A...

Calculating completion time of scheduled dependent tasks

I am trying to calculate least completion time of a project schedule, which has n number of tasks and each task can have a dependency task. example Task ID Duration Days Dependent ID 1 10 0 2 3 0 3 6 1 4 5 2 5 10 1 Task with...

Use Recursion to get Subsets of an array. C++ and Java give me different results

For example, given a set like below - S=[1,3] we want to get the list of list with following values: [[],[1],[3],[1,3]] I used C++ with the following code and it worked perfect for me. However, after I changed it to Java, the code didn't give me right results. Any help?...

strcmp performance in C

int myStrCmp (const char *s1, const char *s2) { const unsigned char *p1 = (const unsigned char *)s1; const unsigned char *p2 = (const unsigned char *)s2; while (*p1 != '\0') { if (*p2 == '\0') return 1; if (*p2 > *p1) return -1; if (*p1 > *p2) return 1;...

Given length L find the shortest string formed only of as & bs >= L such that adding some character (Either a or b) doesn't produce a new palindrome

Given length L find the shortest string >= L formed only of as & bs such that adding some character (Either a or b) doesn't produce a new palindrome substring (never seen before palindrome) For example for L = 1 there is the string aabbaba, adding "a" to it to...

Algorithm for [inclusive/exclusive]_scan in parallel proposal N3554

Proposal N3554 (A Parallel Algorithms Library) for C++14, proposes (among other things), what seem to be parallel versions of the current std::partial_sum, e.g.: template< class ExecutionPolicy, class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator inclusive_scan( ExecutionPolicy &&exec, InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); With the explanation Effects: For each...

How to give mathemarical proof or support my answer through reasoning as a general case?

You are managing a software project that involves building a computer-assisted instrument for medical surgery. The exact placement of the surgical knife is dependent on a number of different parameters, usually at least 25, sometimes more. Your programmer has developed two algorithms for positioning the cutting tool, and is seeking...

Recursive algorithm that returns a bool when checking if array[i] == i (must be O(log n))

I'm trying to write a recursive algorithm that returns true if at least one array[i] == i. And false if there is no array[i] = i. Also, the parameters needed are (int * arr, int start, int end). So i'll be traversing the array with a pointer. For example: int...

How to reduce time to find the n-th place from consecutive digits number for less than 1 second

I'm following the programming test, and there are questions like this From this consecutive digits: 123456789101112131415161718192021.... For example The 10th digit is 1 The 11th digit is 0 and so on What is the 1,000,000th digit? What is the 1,000,000,000th digit? What is the 1,000,000,000,000th digit? Your solution should run...

Determining if a graph has a cycle without using DFS

I came around one of those questions in my exams: Topologocial sorting using Kahn's Algorithm requires the graph to be DAG (Directed Acyclic Graph). How can we determine if a graph contains no cycles without using DFS/BFS first? I am trying to answer that for too long now and I...

How to print the right hemisphere of a square matrix

I am trying to print the right hemisphere of a matrix. If we draw the main and the secondary diagonals in a matrix we see that we got 4 equal parts, the right part is called (in my algorithms textbook) the right hemisphere of a square matrix. For example, in...

Project Euler # 5; this solution works - but why?

The project Euler problem 5 is stated as : "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" Here's...

Genetic Algorithm - convergence

I have a few questions about my genetic algorithm and GAs overall. I have created a GA that when given points to a curve it tries to figure out what function produced this curve. An example is the following Points {{-2, 4},{-1, 1},{0, 0},{1, 1},{2, 4}} Function x^2 Sometimes I...

How to place 100 different files on 20 file systems?

I have 20 file systems of: /fs01 /fs02 /fs03 ... /fs20 in my Unix environment. Each of these file systems, contains some free space, which I can use for saving files. These file systems are independent of each other, and each contain a different amount of free space. I have...

Getting Wrong Answer in “Longest non regular parentheses sub-sequence ”codechef june cook off

I attended a programming competition(it has ended now). I don't know why my solution is giving WA, I read the editorial, saw other people solution but unable to find a flaw in my solution. Obviously I am missing somewhere. Please help! Question Link My Solution My approach If the pattern...

Longest Increasing Subsequence code in O(N)?

Someone asked me a question Find the longest alphabetically increasing or equal string composed of those letters. Note that you are allowed to drop unused characters. So ghaaawxyzijbbbklccc returns aaabbbccc. Is an O(n) solution possible? and I implemented it code [in python] s = 'ghaaawxyzijbbbklccc' lst = [[] for i...

Javascript: Detailed differences between !variable and variable == false?

I accidentally got undefined == true and undefined == false that both of them returns false. But !undefined returns true. And this is the question: What's the algorithmic difference(s) between !someVariable and someVariable == false? If i want to explain it more, type undefined == false ? 't' : 'f'...

Algorithmic big o order of growth code

I'm doing an online course and i'm stuck on this question. I know there are similar questions but they don't help me. What is the order of growth of the worst case running time of the following code fragment as a function of N? int sum = 0; for (int...

Reverse ^ operator for decryption

I'm trying to reverse the following code in order to provide a function which takes the buffer and decrypts it. void crypt_buffer(unsigned char *buffer, size_t size, char *key) { size_t i; int j; j = 0; for(i = 0; i < size; i++) { if(j >= KEY_SIZE) j = 0;...

Dynamic programming: how to design algorithm for when there are two factors to consider?

I have the following problem and I only have a slight idea about it: Consider a tape storage problem. Given n files of length l1,...,ln and frequencies with which they are accessed f1,...,fn, where sum of all frequencies is 1 and 0<fi<1. "Optimal" means to minimize the average retrieval time...

3 X 3 magic square recursively

I'm trying to find all possible solutions to the 3X3 magic square. There should be exactly 8 solutions. My code gets them all but there are a lot of repeats. I'm having a hard time tracking the recursive steps to see why I'm getting all the repeats. // This program...

Lengths of cycles in random sequence

The following LINQPad code generates random sequence of unique integers from 0 to N and calculates the length of cycle for every integer starting from 0. In order to calculate cycle length for a given integer, it reads value from boxes array at the index equal to that integer, than...

Best way to extract ODD digits from a binary number

Given a 64 bit number, I need to extract every other bit from it, and convert it into a number: decimal: 357 binary: 0000 0001 0110 0101 odd bits: 0 0 0 1 1 0 1 1 decimal: 27 Any idea of a good algorithmic way to do it? And...

Understanding Big-Ω (Big-Omega) notation

I was doing some reading on logarithms and the rate of growth of the running time of algorithms. I have, however, a problem understanding the Big-Ω (Big-Omega) notation. I know that we use it for 'asymptotic lower bounds', and that we can express the idea that an algorithm takes at...

Ranking with time weighting

I am looking for a basic algorithm that gives more weigh to the recent reviews. So, the output value of the algorithm is mutable. For example, two reviews with exactly the same score, will have a different ranking based on the timestamp of the creation. Review_1 Score 10 creation 10/5/2014...

Remove smallest non-unique value from vector

I have an unsorted vector of doubles (Actually objects with a double member which is used in this case). From this vector I need to remove the smallest non-unique value. However, it is not guaranteed that a non-unique value exists. It is allowed to sort the range. As always I...

Find the shortest path sum in a matrix. Is Dijkstra not optimal for this case?

I am trying to solve the following problem from project euler (please take a look at description and the example in the link, but here is the short explanation). in the matrix, find the minimal path sum from the top left to the bottom right, by moving left, right, up,...

Travels using graph

Someone can help me to think in the better way to adapt Dijkstra's Algorithm in these conditions? All I thought didn't was good. Example of input: GP4578 MADRID 01:00 PORTO 02:00 IK6587 PORTO 03:00 VALENCIA 05:00 05:30 TENERIFE 08:00 AB5874 VALENCIA 05:40 BERLIM 10:00 "VALENCIA 05:00 05:30" This is a...

Recursive solution doesn't iterate correctly

I'm working through a toy problem in Ruby: how to produce all possible 10-digit phone numbers where each successive number is adjacent to the last on the keypad. I've represented the adjacent relationships between numbers, and have a recursive function, but my method isn't iterating through the whole solution space....

Explain the time complexity of these grouping functions

Here I have 800 derived classes of Base and a list of 8000000 objects of these types, which can be of any order. The goal is to separate the list into the 800 types as efficiently as possible. Here I have written two functions to do that. The first is...

C: sorted input serials

I try find out what is wrong with the program that takes n - size of serials and this same number of elements numbers. With n = 4 and numbers 1, 2, 3, 4 I get output: 1, 0, 2293428, 1990567906. Somewhere in the code something is not okey, but...