flowgorithm - Avoid nested for loops - nested-loops

I'm trying to create a Flowgorithm that has a nested loop. The problem is that the number of nested loops (the depth) could vary.
The main idea is if I have n people to assign to k categories. Let's say n = 10 and k = 4. The first category could then have 0 to 10 people, the second 0 to 10 - the number of people in category 1, the third category 0 to 10 minus the number of people in categories 1 and 2, and the last category is then always n - sum of the previous categories.
With Python this would for example look like:
max_val=10
n_cat = 4
for i in range(0,max_val+1):
for j in range(0, max_val+1-i):
for k in range(0, max_val+1-i-j):
print(i, j, k, max_val-i-j-k)
I'm scratching my head on how to avoid the nested loops, but keep hitting a wall.
Any suggestions?

Related

Split sequence of numbers from 1 to n^2 in n subsequences so they all have the same sum

Given the number n and a sequence of numbers from 1 to n^2 how to split it in n subsequences so all of the subsequences have the same sum and length of n ?
For example if n = 3 answer could be:
3 4 8 = 15
2 6 7 = 15
1 5 9 = 15
So I feel this problem can be solved by making few observations to the problem.
For example, let's say we have n=3. Then n^2=9.
Now total sum of all the numbers from 1 to 9 = 9 * (9+1) / 2 = 45.
So, now we can split 45 into three equal groups each having sum = 45/3 = 5.
Similarly:-
n = 4, sum of 1 to 16 numbers = 16 * 17/2 = 136. each group sum = 136/4 = 34.
n = 5, sum of 1 to 25 numbers = 25 * 26/2 = 25*13. each group sum = 25*13/5 = 65.
Now, we know what should be sum of each set of groups in order to split numbers into n sub sequences.
Now Another observation that we make is whether our n is odd or even.
For n being even, the splitting it very easy.
n = 2, so we have numbers 1 to 4.
1 4
2 3.
Let's assume a matrix of n x n , in above case it will be 2 x 2.
Rules for even n:-
1. Keep a counter = 1.
2. Fill the first column (1 to n), incrementing the counter by 1.
3. When we reach at the bottom of the column, for column 2, we do a reverse iteration (n to 1) and fill them with counter by incrementing it by 1.
You can verify this technique will work by taking n=2,4,6 ... and filling the array.
Now let's see how to fill this matrix n x n for n odd.
Rules for odd n:-
1. Keep a counter = 1.
2. Fill the first column (1 to n), incrementing the counter by 1.
3. Now this case is slightly different from even case, from the next column onwards,
we don't reverse our calculation from n to 1 but we keep moving ahead in column.
Let's understand this step by looking at an example.
Let's take n=3.
Our first column will be 1,2,3.
Now for the second column we start at bottom column which is n in our example it's 3.
Fill the n = 3 with value 4. next row value = (n+1)%n = 0, which gets 5, next row = (n+1+1)%n = 1 , which gets value 6. Now all the column 2 values are filled, let's move onto next column i.e third.
We will start at row = 1 , so row 1 column 3 will get 7, then row 2 column 3 will get 8 and then row 0 column 3 will get 9.
Hope this helps!

Algorithm to sort users into teams (based on minimum/maximum number for each team)

I am trying to come up with an algorithm for sorting and assigning teams to a fixed number of users. The majority of algorithms that I've found assume the number of groups to divide by; I would like to create a smart system where the groups are assigned automatically (to the best of its ability) and predicted based on the total user count, and the minimum and maximum number of users per group.
Assume the following criteria for each group:
Minimum of 3 per group
Maximum of 6 per group
Smart grouping based on total number of users
Here are some of the possibilities based on the total user count and the minimum/maximum values for each group:
For 24 members:
4 groups of 5 and 1 group of 4
6 groups of 4
4 groups of 6
For 21 members:
3 groups of 6 and 1 group of 3
3 groups of 5 and 1 group of 6 (better choice)
For 10 members:
2 groups of 5 (better choice)
2 groups of 3 and 1 group of 4
Although not necessary, the "better choice" for groups would be an equal amount of users per group (e.g. 10 members would be 2 groups of 5), unless impossible (e.g. 21 members) whereby 5+5+5+6 is "more equal" in terms of members per group than 6+6+6+3.
With the criterion you've given so far - minimum group size variation - the number of cases is limited.
If the target is a multiple of 3, 4, or 5, split into equal-sized groups. (We never need groups of size 6.)
Otherwise the target is either 1 or 2 (mod 3).
For the mod 1 case use a single group of 4 with all others being 3.
For the mod 2 case use two groups of 4 with all others being 3.
Note for 21 you left out the obvious choice of 7 groups of 3.
With this algorithm:
24 - 8 groups of 3
21 - 7 groups of 3
10 - 2 groups of 3, 1 group of 4 (the 1 (mod 3) case)
14 - 2 groups of 3, 2 groups of 4 (the 2 (mod 3) case)
Maybe you are quietly thinking (but not saying) that you'd also like to minimize the number of groups. If that's true, then you need to indicate the relative importance of small number of groups and group size variation. These two factors require a tradeoff.
Here's a solution in JS for anyone that's interested https://jsfiddle.net/kp8d0w27/6/
The first loop checks whether the total number of attendees can be divided equally. If it can, do that.
The second loop will only run if the total is not equally divisible, and will loop until the remainder (modulo) is greater than the minimum set and less than the maximum.
Finally, I'm using a Lodash function called chunk to split the original array into equal parts.
const minNumber = 3
const maxNumber = 6
let divisible = false
let totalGroups = 0
let perGroup = 0
const attendees = ['chris', 'kevin', 'thomas', 'gio', 'nathan', 'michael', 'elyse', 'sarah', 'jacinthe', 'chloe', 'benoit', 'alex', 'darren', 'john']
const totalAttendees = attendees.length
// Can we divide into equal groups?
for (let divider = maxNumber; minNumber <= divider; divider--) {
totalGroups = totalAttendees / divider
if (Number.isInteger(totalGroups)) {
divisible = true
perGroup = divider
break
}
}
// Nope, so divide teams and make sure their between our min and max
if (!divisible) {
totalGroups = 0;
for (let j = maxNumber, remainder = 0; minNumber <= j; j--) {
remainder = totalAttendees % j
if (remainder >= minNumber && remainder <= maxNumber) {
perGroup = j;
break
}
}
}
console.log(JSON.stringify(_.chunk(attendees, perGroup)))
Not the cleanest (and there's some situations that I did not account for here, like if the number of attendees is less than the minimum), I'm still refactoring, but it works well and solves my original problem.

Dividing a large number by 2

let's say I have the following implementation of a list:
list=^listelement
listelement=record
w:integer;
next:list;
end;
and a list represents a large number written decimally
(a list 1 -> 2 -> 3 represents the number 123).
What I want to do is to transform such a number into binary representation. So, the eastiest way to do this required dividing a number by 2
The problem is I'm having a hard time implementing the division by 2 algorithm. I understand the basic algorithms such as this one
https://www.mathsisfun.com/long_division.html, but I can't think of a way to translate that into code
I would appreciate some help
You will proceed from left to right, dividing the digits by two. Every time a digit is odd, you will propagate a carry (10) to the next digit.
Example: divide 123
1 divided by 2 is 0, carry = 10
2 + 10 divided by 2 is 6, no carry
3 divided by 2 is 1, carry = 10
The last carry can be ignored.
Result: 061.
carry= 0;
element= head;
WHILE element <> NIL DO
BEGIN
element^.w= element^.w + carry;
IF ODD(element^.w) THEN carry= 10 ELSE carry= 0;
element^.w= element^.w DIV 2;
element= element^.next
END.

Cycle sort Algorithm

I was browsing through the internet when i found out that there is an algorithm called cycle sort which makes the least number of memory writes.But i am not able to find the algorithm anywhere.How to detect whether a cycle is there or not in an array?
Can anybody give a complete explanation for this algorithm?
The cycle sort algorithm is motivated by something called a cycle decomposition. Cycle decompositions are best explained by example. Let's suppose that you have this array:
4 3 0 1 2
Let's imagine that we have this sequence in sorted order, as shown here:
0 1 2 3 4
How would we have to shuffle this sorted array to get to the shuffled version? Well, let's place them side-by-side:
0 1 2 3 4
4 3 0 1 2
Let's start from the beginning. Notice that the number 0 got swapped to the position initially held by 2. The number 2, in turn, got swapped to the position initially held by 4. Finally, 4 got swapped to the position initially held by 0. In other words, the elements 0, 2, and 4 all were cycled forward one position. That leaves behind the numbers 1 and 3. Notice that 1 swaps to where 3 is and 3 swaps to where 1 is. In other words, the elements 1 and 3 were cycled forward one position.
As a result of the above observations, we'd say that the sequence 4 3 0 1 2 has cycle decomposition (0 2 4)(1 3). Here, each group of terms in parentheses means "circularly cycle these elements forward." This means to cycle 0 to the spot where 2 is, 2 to the spot where 4 is, and 4 to the spot where 0 was, then to cycle 1 to the spot where 3 was and 3 to the spot where 1 is.
If you have the cycle decomposition for a particular array, you can get it back in sorted order making the fewest number of writes by just cycling everything backward one spot. The idea behind cycle sort is to try to determine what the cycle decomposition of the input array is, then to reverse it to put everything back in its place.
Part of the challenge of this is figuring out where everything initially belongs since a cycle decomposition assumes you know this. Typically, cycle sort works by going to each element and counting up how many elements are smaller than it. This is expensive - it contributes to the Θ(n2) runtime of the sorting algorithm - but doesn't require any writes.
here's a python implementation if anyone needs
def cycleSort(vector):
writes = 0
# Loop through the vector to find cycles to rotate.
for cycleStart, item in enumerate(vector):
# Find where to put the item.
pos = cycleStart
for item2 in vector[cycleStart + 1:]:
if item2 < item:
pos += 1
# If the item is already there, this is not a cycle.
if pos == cycleStart:
continue
# Otherwise, put the item there or right after any duplicates.
while item == vector[pos]:
pos += 1
vector[pos], item = item, vector[pos]
writes += 1
# Rotate the rest of the cycle.
while pos != cycleStart:
# Find where to put the item.
pos = cycleStart
for item2 in vector[cycleStart + 1:]:
if item2 < item:
pos += 1
# Put the item there or right after any duplicates.
while item == vector[pos]:
pos += 1
vector[pos], item = item, vector[pos]
writes += 1
return writes
x = [0, 1, 2, 2, 2, 2, 1, 9, 3.5, 5, 8, 4, 7, 0, 6]
w = cycleSort(x)
print w, x

Dynamic programming: can interval of even 1's and 0's be found in linear time?

Found the following inteview q on the web:
You have an array of
0s and 1s and you want to output all the intervals (i, j) where the
number of 0s and numbers of 1s are equal. Example
pos = 0 1 2 3 4 5 6 7 8
0 1 0 0 1 1 1 1 0
One interval is (0, 1) because there the number
of 0 and 1 are equal. There are many other intervals, find all of them
in linear time.
I think there is no linear time algo, as there may be n^2 such intervals.
Am I right? How can I prove that there are n^2 such ?
This is the fastest way I can think of to do this, and it is linear to the number of intervals there are.
Let L be your original list of numbers and A be a hash of empty arrays where initially A[0] = [0]
sum = 0
for i in 0..n
if L[i] == 0:
sum--
A[sum].push(i)
elif L[i] == 1:
sum++
A[sum].push(i)
Now A is essentially an x y graph of the sum of the sequence (x is the index of the list, y is the sum). Every time there are two x values x1 and x2 to an y value, you have an interval (x1, x2] where the number of 0s and 1s is equal.
There are m(m-1)/2 (arithmetic sum from 1 to m - 1) intervals where the sum is 0 in every array M in A where m = M.length
Using your example to calculate A by hand we use this chart
L # 0 1 0 1 0 0 1 1 1 1 0
A keys 0 -1 0 -1 0 -1 -2 -1 0 1 2 1
L index -1 0 1 2 3 4 5 6 7 8 9 10
(I've added a # to represent the start of the list with an key of -1. Also removed all the numbers that are not 0 or 1 since they're just distractions) A will look like this:
[-2]->[5]
[-1]->[0, 2, 4, 6]
[0]->[-1, 1, 3, 7]
[1]->[8, 10]
[2]->[9]
For any M = [a1, a2, a3, ...], (ai + 1, aj) where j > i will be an interval with the same number of 0s as 1s. For example, in [-1]->[0, 2, 4, 6], the intervals are (1, 2), (1, 4), (1, 6), (3, 4), (3, 6), (5, 6).
Building the array A is O(n), but printing these intervals from A must be done in linear time to the number of intervals. In fact, that could be your proof that it is not quite possible to do this in linear time to n because it's possible to have more intervals than n and you need at least the number of interval iterations to print them all.
Unless of course you consider building A is enough to find all the intervals (since it's obvious from A what the intervals are), then it is linear to n :P
A linear solution is possible (sorry, earlier I argued that this had to be n^2) if you're careful to not actually print the results!
First, let's define a "score" for any set of zeros and ones as the number of ones minus the number of zeroes. So (0,1) has a score of 0, while (0) is -1 and (1,1) is 2.
Now, start from the right. If the right-most digit is a 0 then it can be combined with any group to the left that has a score of 1. So we need to know what groups are available to the left, indexed by score. This suggests a recursive procedure that accumulates groups with scores. The sweep process is O(n) and at each step the process has to check whether it has created a new group and extend the table of known groups. Checking for a new group is constant time (lookup in a hash table). Extending the table of known groups is also constant time (at first I thought it wasn't, but you can maintain a separate offset that avoids updating each entry in the table).
So we have a peculiar situation: each step of the process identifies a set of results of size O(n), but the calculation necessary to do this is constant time (within that step). So the process itself is still O(n) (proportional to the number of steps). Of course, actually printing the results (either during the step, or at the end) makes things O(n^2).
I'll write some Python code to test/demonstrate.
Here we go:
SCORE = [-1,1]
class Accumulator:
def __init__(self):
self.offset = 0
self.groups_to_right = {} # map from score to start index
self.even_groups = []
self.index = 0
def append(self, digit):
score = SCORE[digit]
# want existing groups at -score, to sum to zero
# but there's an offset to correct for, so we really want
# groups at -(score+offset)
corrected = -(score + self.offset)
if corrected in self.groups_to_right:
# if this were a linked list we could save a reference
# to the current value. it's not, so we need to filter
# on printing (see below)
self.even_groups.append(
(self.index, self.groups_to_right[corrected]))
# this updates all the known groups
self.offset += score
# this adds the new one, which should be at the index so that
# index + offset = score (so index = score - offset)
groups = self.groups_to_right.get(score-self.offset, [])
groups.append(self.index)
self.groups_to_right[score-self.offset] = groups
# and move on
self.index += 1
#print self.offset
#print self.groups_to_right
#print self.even_groups
#print self.index
def dump(self):
# printing the results does take longer, of course...
for (end, starts) in self.even_groups:
for start in starts:
# this discards the extra points that were added
# to the data after we added it to the results
# (avoidable with linked lists)
if start < end:
print (start, end)
#staticmethod
def run(input):
accumulator = Accumulator()
print input
for digit in input:
accumulator.append(digit)
accumulator.dump()
print
Accumulator.run([0,1,0,0,1,1,1,1,0])
And the output:
dynamic: python dynamic.py
[0, 1, 0, 0, 1, 1, 1, 1, 0]
(0, 1)
(1, 2)
(1, 4)
(3, 4)
(0, 5)
(2, 5)
(7, 8)
You might be worried that some additional processing (the filtering for start < end) is done in the dump routine that displays the results. But that's because I am working around Python's lack of linked lists (I want to both extend a list and save the previous value in constant time).
It may seem surprising that the result is of size O(n^2) while the process of finding the results is O(n), but it's easy to see how that is possible: at one "step" the process identifies a number of groups (of size O(n)) by associating the current point (self.index in append, or end in dump()) with a list of end points (self.groups_to_right[...] or ends).
Update: One further point. The table of "groups to the right" will have a "typical width" of sqrt(n) entries (this follows from the central limit theorem - it's basically a random walk in 1D). Since an entry is added at each step, the average length is also sqrt(n) (the n values shared out over sqrt(n) bins). That means that the expected time for this algorithm (ie with random inputs), if you include printing the results, is O(n^3/2) even though worst case is O(n^2)
Answering directly the question:
you have to constructing an example where there are more than O(N) matches:
let N be in the form 2^k, with the following input:
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 (here, N=16)
number of matches (where 0 is the starting character):
length #
2 N/2
4 N/2 - 1
6 N/2 - 2
8 N/2 - 3
..
N 1
The total number of matches (starting with 0) is: (1+N/2) * (N/2) / 2 = N^2/8 + N/4
The matches starting with 1 are almost the same, expect that it is one less for each length.
Total: (N^2/8 + N/4) * 2 - N/2 = N^2/4
Every interval will contain at least one sequence of either (0,1) or (1,0). Therefore, it's simply a matter of finding every occurance of (0,1) or (1,0), then for each seeing if it is adjacent to an existing solution or if the two bookend elements form another solution.
With a bit of storage trickery you will be able to find all solutions in linear time. Enumerating them will be O(N^2), but you should be able to encode them in O(N) space.

Resources