Restricted combinations (algorithm) - algorithm

Consider the following example:
I have a list of 5 items, each with their occurrence with either 1 or 0:
{a, b, c, d, e}
The restricted combinations are as follows:
the occurrence of a, c, and e cannot be 1 at any given time.
the occurrence of b, d, and e cannot be 1 at any given time.
basically, if found in database that occurrence of a and c is already 1, and if a given input is e (giving e an occurrence of 1) is not allowed (clause 1) or vice versa.
another example, d and e has an occurrence of 1 respectively in the database, a new input of b will not be allowed (following clause 2).
An even more solid example:
LETTER | COUNT(OCCURRENCE)
------------------------------
a | 1
b | 1
c | 1
d | 0
e | 0
Therefore, a new input of e would be rejected because of the violation of clause 1.
What is the best algorithm/practice for this solution?
I thought of having many if-else statements, but that doesn't seem efficient enough. What if I had a dynamic list of elements instead? Or at least have a better extensibility to this piece of program.

As mentioned by BKassem(I think) in the comments(removed for whatever reason).
The algorithm for this scenario:
(count(a) * count(c) * count(e)) == 0 //proceed to further actions
Worked flawlessly!

Related

Cleaner way to represent languages accepted by DFAs?

I am given 2 DFAs. * denotes final states and -> denotes the initial state, defined over the alphabet {a, b}.
1) ->A with a goes to A. -> A with b goes to *B. *B with a goes to *B. *B with b goes to ->A.
The regular expression for this is clearly:
E = a* b(a* + (a* ba* ba*)*)
And the language that it accepts is L1= {w over {a,b} | w is b preceeded by any number of a's followed by any number of a's or w is b preceeded by any number of a's followed by any number of bb with any number of a's in middle of(middle of bb), end or beginning.}
2) ->* A with b goes to ->* A. ->*A with a goes to *B. B with b goes to -> A. *B with a goes to C. C with a goes to C. C with b goes to C.
Note: A is both final and initial state. B is final state.
Now the regular expression that I get for this is:
E = b* ((ab) * + a(b b* a)*)
Finally the language that this DFA accepts is:
L2 = {w over {a, b} | w is n 1's followed by either k 01's or a followed by m 11^r0' s where n,km,r >= 0}
Now the question is, is there a cleaner way to represent the languages L1 and L2 because it does seem ugly. Thanks in advance.
E = a* b(a* + (a* ba* ba*)*)
= a*ba* + a*b(a* ba* ba*)*
= a*ba* + a*b(a*ba*ba*)*a*
= a*b(a*ba*ba*)*a*
= a*b(a*ba*b)*a*
This is the language of all strings of a and b containing an odd number of bs. This might be most compactly denoted symbolically as {w in {a,b}* | #b(w) = 1 (mod 2)}.
For the second one: the only way to get to state B is to see an a in A, and the only way to get to C from outside C is to see an a in B. C is a dead state and the only way to get to it is to see aa starting in A. That is: if you ever see two as in a row, the string is not in the language; the language is the set of all strings over a and b not containing the substring aa. This might be most compactly denoted symbolically as {(a+b)*aa(a+b)*}^c where ^c means "complement".

dequeuemin in Fibonacci heaps

I wanted to ask about Fibonacci heaps.
If I have this scenario:
A
|
B
Then, we add two more nodes C and D:
A
|\
B C
|
D
Now we delete B:
A
|
C
|
D
Now we add E and F.
I saw it creates a tree like that:
E
|\
F A
|
C
|
D
But I don't understand why E and F are connected with the tree. From what I read, we connect trees with the same rank (for example, a tree of one node with another tree of one node), am I wrong?
Thank you very much.
If you add the nodes C and D into the first Fibonacci heap, you would not end up with the tree you drew. Instead, you'd have this heap:
A C D
|
B
Remember that Fibonacci heaps lazily add each newly added value to the top-level list of trees and only coalesce things on a deletion. If you were to delete B, you'd promote it to the top level, like this:
A B C D
You'd then remove B:
A C D
Now, you'd scan the root list and coalesce the trees of the same order together. Let's suppose you scan the nodes in the order A, C, D. First, you'll coalesce A and C together, like this:
A D
|
C
At this point, no further coalesces will happen, since there's exactly one tree of each order.
If you then add in E and F, you'd put them at the top level, like this:
A D E F
|
C
So yes, you're right, those nodes shouldn't get added into the tree.

Construction from many sets

I have four sets:
A={a,b,c}, B={d,e}, C={c,d}, D={a,b,c,e}
I want to search the sequence of sets that give me: a b c d
Example: the sequence A A A C can give me a b c d because "a" is an element of A, "b" is an element of A, "c" is an element of A and "d" is an element of C.
The same thing for : D A C B, etc.
I want an algorithm to enumerate all sequences possibles or a mathematical method to find the sequences.
You should really come up with some code of your own and then ask specific questions about problems with it. But it's interesting, so I'll share some thoughts.
You want a b c d.
a can come from A, D
b can come from A, D
c can come from A, C, D
d can come from B, C
So the problem reduces to finding all of the 2*2*3*2=24 ways to combine those options.
One way is recursion with backtracking. Build it from left to right, output when you have a complete set. Like the 8 queens problem, but much simpler since everything is independent.
Another way is to count the integers and map them into a mixed-base system. First digit base 2, then 2, 3, 2. So 0 becomes AAAB, 1 is AAAC, 2 is AACB, etc. 23 is DDDC and 24 needs five digits so you stop there.

Languages to context free grammars

Provide context free grammars for the following languages.
(a) {a^mb^nc^n | m ≥ 0 and n ≥ 0 }
(b) {a^nb^nc^m | m ≥ 0 and n ≥ 0 }
If there were any other rules involved such as m = n or anything like that, I could get it, but the general m greater than or equal to zero? I'm pretty confused. and also I don't understand how a and b would be any different.
Here was my shot at making a grammar out of this:
S1 --> S2 | e
S2 --> aS2bS2c | S3
S3 --> aS3 | S4
S4 --> bS4 | S5
S5 --> cS5 | c
Is a^mb^n m repetitions of a followed by n repetitions of b? It looks like you copy-pasted an assignment and neglected even to format it to be readable on this site.
Assuming I'm reading that correctly, moving on…
The key is that (in the first language) b and c are repeated an equal number of times. When you match a b you must simultaneously match a c. A production matching this subsequence would be
S1 => e
S1 => b S1 c
Note that there are two languages there so you need two answers. You aren't being asked for one grammar that handles both languages. (The main problem with that would be ambiguity in the case that n = m).

Algorithm to find minimal elements needed to uniquely identify a collection of those elements

Say I have 5 collections that contain a bunch of strings (hundreds of lines).
Now I want to extract the minimum nr of lines from each of these collections to uniquely identify that 1 collection.
So if I have
Collection 1:
A
B
C
Collection 2:
B
B
C
Collection 3:
C
C
C
Then collection 1 would be identified by A.
Collection 2 would be identified by BC or BB.
Collection 3 would be identified by CC.
Is there any algorithm already out there that does this kind of thing? Name?
Thanks,
Wesley
If the order is not important, I would sort all Lists (Collections).
Then you could look whether all 5 start with the same element. You would group them by the first element:
Start - Character instead of Strings/Lines.:
T A L U D
N I O S A D
R A B E
T A U C
D A N E B
Sorted internally:
A D U L T
A D O N I S
A B E R
A C U T
A B E N D
Sorted:
A B E N D
A B E R
A C U T
A D U L T
A D O N I S
Grouped (2):
(A B) E N D
(A B) E R
(A C) U T # identified by 2 elements
(A D) U L T
(A D) O N I S
Rest grouped by 3 elements:
(A C) U T # identified by 2 elements
(A B E) N D
(A B E) R
(A D U) L T # only ADU...
(A D O) N I S # only ADO...
Rest grouped by 4 elements:
(A C) U T # AC..
(A D U) L T # ADU...
(A D O) N I S # ADO...
(A B E N) D
(A B E R)
This is an easy problem to solve. You have one multiset (collection 1) (it is a "multiset" because the same element can occur multiple times), and then a number of other multisets (collections 2..N), and you want to find a minimum-size subset of collection 1 that does not occur in any of the other collections (2..N).
It is an easy problem to solve because it can be solved by simple set theory. I'll explain this first without multisets, i.e. assuming that every line can occur only once in any given set, and then explain how it works with multiset.
Let's call your collection 1 set S and the other collections sets X1 .. XN. Now keeping in mind that for now the sets do not have multiple instances of any item, it is obvious that any singleton set { a } such that a ∉ Xi distinguishes S from Xi, and so it is enough to calculate the set differences A - X1, ..., A - XN and then pick up a minimum-size set R such that R shares an element with all these difference sets. This is then the SET COVER combinatorial optimization problem that is NP-complete but for your small problem (5 collections) can be handled easily by brute force.
Now then when the sets are actually multisets this only changes so that the distinguishing "singleton" sets are actually multisets containing 1 or more copies of the same element and thus they have different costs. You can still calculate the set differences as above (you subtract element counts), but now your SET COVER combinatorial optimization part has take into account the fact that the distinguishing elements can be multisets and not singletons. Here's an illustration how it works for your problem when we solve for collection 3:
S = {{ c, c, c }}
X1 = {{ a, b, c }}
X2 = {{ b, b, c }}
S - X1 distinguishers: {{ c, c }}
S - X2 distinguishers: {{ c, c }}
Minimum multiset covering a distinguisher for every set: {{ c, c }}
And here how it works for calculating for collection 1:
S = {{ a, b, c }}
X1 = {{ b, b, c }}
X2 = {{ c, c, c }}
S - X1 distinguishers: {{ a }}
S - X2 distinguishers: {{ a }}, {{ b }}
Minimum multiset covering a distinguisher for every set: {{ a }}

Resources