Algorithm to calculate N paths from K set [duplicate] - algorithm

This question already has answers here:
Algorithm to return all combinations of k elements from n
(77 answers)
Closed 7 years ago.
I'm looking for an algorithm that can calculate N paths from K set where N=10 and K=32. This is not a combinatory algo, I don't need every number in every position.
So basically I need to be able to find the distinct paths of up to 10 elements in a collection of 32.
Edit:
If I have a set of numbers
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
I would like to get every unique path of 10. So an example would be
Path 1 {1,2,3,4,5,6,7,8,9,10}
Path 2 {1,2,3,4,5,6,7,8,9,11}
Path 3 {1,2,3,4,5,6,7,8,9,12}
Down the line I would like something like this:
Path N { 3,5,6,8,9,12,14,17,18,19 }
Since I need every possible path consisting of 10 elements from the larger set.

This has been answered here :
Algorithm to return all combinations of k elements from n
Note that you will get 64512240 paths. You may want to think a bit about it before filling your RAM with them.

Related

generate all subsets of set given by binary representation of integer in c++ [duplicate]

This question already has answers here:
How do i generate all possible subsets of an binary string?
(5 answers)
Closed 4 years ago.
In c++ I am searching for an efficient algorithm to generate all integers such that their binary representation is a subset of a set which is given by the binary representation of integer N. By efficient I mean that I don't want to for-loop through all integers smaller than N to check whether they are subsets, mainly because N could be very large.
An idea I had was to generate all possible subsets of an integer corresponding to the Hamming weight of N and then shift them to the correct positions using <<, but I am so far failing to find a good way how to do this.
Example:
For set 110100 given by integer N=52 all possible subsets would be:
{000100,010000,010100,100000,100100,110000}
corresponding to integers {4,16,20,32,36,48}, which is what I want to generate.
Let P be popcount(N), the number of bits that are set. The number of results is then 2P - 2.
Treat N as a boolean array (bits). For each bit which is set, generate two subsets: one with that bit set and one without it. This can be done recursively until no bits are set.
Finally, discard the original N from the results, and also 0 (as per your example).
The time complexity is linear in the size of the output, i.e. O(2P).

Function to generate pseudo ramdom sequence [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
How to generate a random alpha-numeric string
(46 answers)
Closed 6 years ago.
Lets assume an ordered finite sequence of integers A = {0,1,2, ... k}.
Im looking for a (seeded) function f: A -> A where the image of f seems to be ramdom, but every element is hit exactly once (bijective).
It's importatant that someone who has access to some results of f should neither be able to estimate if he's looking at direkt neighbours f(n), f(n+1) nor should he be able to guess the next result.
One idea would be to map a range(1, k) on a shuffle(range(1, k)). But this seems very low in performance (for large k) and does miss the basic idea of a generator. The first element in the sequece should be as expensive to calculate as the n-th element.
Difference to 'normal' randomgenerators
Read carefully before marking as dublicated. I'm not looking for randomly picking from a range where collisions are possible. Also I want to calculate the n-th element without calculating all n-1elements before, like I would have to using the MT or Xorshift.

Determine if two numbers in a set equals x in nlgn [duplicate]

This question already has answers here:
Determine whether or not there exist two elements in Set S whose sum is exactly x - correct solution?
(8 answers)
Closed 7 years ago.
I do not want a solution if my own answer is incorrect because i really want to solve this on my own. What i do want is either a yes this is correct or a no this is not correct, followed by tips or suggestions that may lead to an answer without spoiling it all.
The question:
Describe a theta(nlgn)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. (Intro to Algorithms, 2.3-7)
The attempt:
First the problem doesnt state whether or not the set is sorted. I assumed it was not, and sorted it using merge sort as it is theta(nlgn) under the worst case.
Then i said okay, the only way this will still be theta(nlgn) is if i recursively split the problem into two. My approach was to start at index i=0 of our array, see what value k would i need for x=i+k and then used binary search to split the problem in half until i either found k or not. If i did not find a k such that x=i+k, then i would continue the same process for index i=2 to n-1. This would result in theta(nlgn).
The total time complexity from both sorting the array and finding k would add up to theta(nlgn) + theta(nlgn) = theta(2nlg2n) = theta(nlgn) if you remove constants.
Yes, your solution is correct. Don't forget to handle the case when a found element k has the same index i.

list all the permutation to sum up some numbers to a dest number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Algorithm to find which number in a list sum up to a certain number
question:
there is a list: [1,2,3,4,6,8,10,12], i want to use these numbers to sum up to a new number 16.
rules:
1) don't need to use all of the numbers, 6 + 10 will be ok.
2) a number can use multi times, 12+2+1+1 will be ok.
3) order matters, 12 + 6 and 6 + 12 are two different combinations.
i have see algorithm to sum up a list of numbers for all combinations, but this is not the same.
don't know much about algorithm, if this fit certain algorithm, please let me know, or some python code / pseudo-code will be much appreciated.
First - note that even finding if there is any subset that sums to the desired number is NP-Complete and is known as the subset sum problem, so there is no known polynomial solution for it.
Now, regarding the specific issue, here are some options:
First there is of course the obvious "generate all subsets and check the sum" way. Note that if your elements are all non-negative, you can use branch and bound and terminate a large portion of the possibilities before you actually develop them (If you found a subset X with sum(X) == s, and you are looking for the number n < s - you can be sure any set containing X will NOT find the solution). Something along the lines of:
findSubsets(list,sol,n):
if (list.empty() and n == 0): #found a feasible subset!
print sol
return
else if (n < 0): #bounding non feasible solutions
return
else if (list.empty()): #a solution that sums to a smaller number then n
return
e <- list.removeAndReturnFirst()
sol <- sol.add(e)
findSubsets(list,sol,n-e)
sol <- sol.removeLast()
findSubsets(list,sol,n)
list.addFirst(e) #cleanup, return the list to original state
Invoke with findSubsets(list,[],n) where list is your list of items, n is the desired number and [] is an empty list.
Note that it can be easily parallelized if needed, there is no real syncrhonization needed between two subsets explored.
Another alternative if the list contains only integers is using Dynamic Programming for solving the subset sum problem. Once you have the matrix you can re-create all the elements from the table by going back in the table. This similar question discusses how to get a list from the knapsack DP solution. The principles of the two problems are pretty much the same.

Find the smallest missing number in an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find the Smallest Integer Not in a List
I got asked this question in an interview
Given an unsorted array, find the smallest number that is missing. Assume that all numbers are postive.
input = {4,2,1,3,678,3432}
output = 5
Sorting it was my first approach. Second approach of mine is to have a boolean flag array.The second approach takes up lots of space.
Is there any other better approach than this ?
Suppose the length of the given array is N.
You can go with the boolean-flag approach, but you don't need to take numbers that are larger than N into account, since they're obviously too big to affect the answer. You could also consider a bitmap to save some space.
An alternative solution to unkulunkulu's is the following:
k := 1
Make an array of 2^k booleans, all initially set to false
For every value, V, in the array:
if V < 2^k then:
set the V'th boolean to true
Scan through the booleans, if there are any falses then the lowest one indicates the lowest missing value.
If there were no falses then increment k and go to step 2.
This will run in O(n log s) time and O(s) space, where n is the size of the input array and s is the smallest value not present. It can be made more efficient by not rechecking the lowest values on successive iterations, but that doesn't change the constraints.

Resources