remove a node in xor linklist - algorithm

I find the below code in C++ that insert and traverse a XOR linklist.
How do we remove a node ? As it seems when we remove a node, all the address of the node in the list need to get updated ?
Or my intuition is not correct this time ?
https://en.wikipedia.org/wiki/XOR_linked_list
https://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-1/
https://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-2/

No, you don't update all of the addresses, merely the adjacent ones. Look at the example list; let's extend it two more nodes:
z A B C D E F
<–> z⊕B <–> A⊕C <-> B⊕D <-> C⊕E <-> D⊕F <->
The only values that use the address of C are those for B and D. If we remove C, we need only alter those values by the next ones moving in:
B.link = B.link ⊕ C ⊕ D
D.link = D.link ⊕ C ⊕ B
This gives us
z A B D E F
<–> z⊕B <–> A⊕D <-> B⊕E <-> D⊕F <->
Do you see how that works? There's very little additional work involved: we have already traversed the list to C to find the item to remove; all we need do is keep the back-pointer as we move along (to operate on B), and then take one more step to access D.

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.

Distance vector routing algorithm advertising

In such a network we assume distance vector routing algorithm used. If the link goes down from A to B, A will change its distance vector B and will advertise its neighbor C about that. The question is that since A uses node B to reach D, will A change its distance vector to D?
Yes. If A lost the link to B, A will set all the paths via B by infinity, and wait to recalculate the shortest paths by receiving new broadcast.
In your case, after A lost the link to B, it will set the distance vector (via B to B) and (via B to D) by infinty, and broadcast this to C. After C broadcasts it has (via D to D) = 4 and (via B to B) = 6 to A, A will recalculate and get the new shortest paths (via C to D) = 6 and (via C to B) = 8.

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