Basically I have to create a min loser tree with 11 players but I'm not entirely sure what it should look like. So far my best guess is:
W
|
L4
/ \
L3 L3
/ \ / \
L2 L2 L2 P11
/ \ / \ /\
L1 L1 L1 L1 P9 P10
/\ /\ /\ /\
P1 P2 P3 P4 P5 P6 P7 P8
Sorry for the terrible ASCII representation, but I think you get the general idea. Is this visualization correct? I'm mostly concerned about the placement of the players.
I've searched the internet but all I get are results for bracketing software.
i'm pretty sure that you already found the answer, but maybe someone else directed to this page would be happy to have it right under the question ...
in a loser tree, the parent of two child nodes is the loser of the two, the winner is further compared, so the tree should look like this (if we assume that the player with the min ID wins):
P1=W
|
P5
(compare P1 to P5)
/ \
P3 P7
(compare P1 to P3) (compare P5 to P7)
/ \ / \
P2 P4 P6 P8
/ \ / \ / \ / \
P1 P2 P3 P4 P5 P6 P7 P8
Related
I came up with a problem o detecting "mutations" between two directed trees.
Example:
tree1:
A
/ \
B C - D
/ \ / \ \
G A 2 A 3
| \ |\
1 3 2 3
tree2:
A
/ \
B C - F
/ \ / \
G A 2 3
| \ |\
1 3 2 3
The algorithm should find that there is a mutation with
R
|
C - D
|\ \
X Y Z
Subsituted with
R
|
C - D
| \
X Z
Where R, Y and Z are the respective values
I am looking for any ideas, which might be:
link to algorithm or book with some algorithms
pseaudocode
code in any language (preferably python)
library in any language (preferably Python)
Have you looked at any tree difference problems?
Most tree diff problems produce a list of changes (e.g. insertion, deletion, moving, and relabelling of nodes) rather than a template subtree, but they might give you a starting place.
Consider a min heap containing all the integers from 1 to 1023 exactly once. If root is at depth 0, the maximum depth at which 9 can appear is?
The answer to the question is 8.
But, considering that a min heap is a nearly complete BT with-
1) for d <- 0 to h-1, all levels have 2^d nodes.
2) for d <- h, nodes are filled from left.
Source:http://homepages.math.uic.edu/~leon/cs-mcs401-s08/handouts/nearly_complete.pdf
What mistake is in answer being 4,as the level order traversal would be {1,2 3,4 5 6,7 8 9...}
The min-heap requires to put elements which are greater than their parent node.
Considering the question, one can put 1 as root, then 2 as its left child and any element greater than 9 (say 512) as its right child.For 2, one can continue in this way by putting 3 as left child and, say 513 as its right child. The final min heap obtained will be -
1
/ \
/ \
2 512
/ \ /\
/ \ / \
3 513 514 515
/\ /\ /\ /\
/ \
4 516 . . . . . .
/ / \ /\ /\ /\ /\ /\ /\
5 . . .. .. .. .. .. ...
/ /\ /\ /\/\
6 . . . . ...........................
/
7 .......................................................
/
8 ......................................................
/
9
The dots denote filled levels and can be replaced by elements from [517,758], as the levels must be filled.
The depth of 9 is 8
I am solving some problems from a site called codefights and the last one solved was about a binary tree in which are:
Consider a special family of Engineers and Doctors. This family has
the following rules:
Everybody has two children. The first child of an Engineer is an
Engineer and the second child is a Doctor. The first child of a Doctor
is a Doctor and the second child is an Engineer. All generations of
Doctors and Engineers start with an Engineer.
We can represent the situation using this diagram:
E
/ \
E D
/ \ / \
E D D E
/ \ / \ / \ / \
E D D E D E E D
Given the level and position of a person in the ancestor tree above,
find the profession of the person. Note: in this tree first child is
considered as left child, second - as right.
As there is some space and time restrictions, the solution can not be based on actually constructing the tree until the level required and check which element is in the position asked. So far so good. My proposed solution written in python was:
def findProfession(level, pos):
size = 2**(level-1)
shift = False
while size > 2:
if pos <= size/2:
size /= 2
else:
size /= 2
pos -= size
shift = not shift
if pos == 1 and shift == False:
return 'Engineer'
if pos == 1 and shift == True:
return 'Doctor'
if pos == 2 and shift == False:
return 'Doctor'
if pos == 2 and shift == True:
return 'Engineer'
As it solved the problem, I got access to the solutions of other used and I was astonished by this one:
def findProfession(level, pos):
return ['Engineer', 'Doctor'][bin(pos-1).count("1")%2]
Even more, I did not understand the logic behind it and so we arrived to this question. Someone could explain to me this algorithm?
Let's number the nodes of the tree in the following way:
1) the root has number 1
2) the first child of node x has number 2*x
3) the second child of node x has number 2*x+1
Now, notice that each time you go to the first child, the profession stays the same, and you add a 0 to the binary representation of the node.
And each time you go to the second child, the profession flips and you add a 1 to the binary representation.
Example: Let's find the profession of the 4th node in the 4th level (last level in the diagram you have in the question). First we start at the root with number 1, then we go to the first child with number 2 (10 binary). After that we go to the second child of 2 which is 5 (101 binary). Finally, we go to the second child of 5 which is 11 (1011 binary).
Notice that we started with only one bit equal to 1, then every 1 bit we added to the binary representation flipped the profession. So the number of times we flip a profession is equal to the (number of bits equal to 1) - 1. The parity of this amount decides the profession.
This leads us to the following solution:
X = number of bits equal to 1 in [ 2^(level-1) + pos - 1 ]
Y = (X-1) mod 2
if Y is 0 then the answer is "Engineer"
Otherwise the answer is "Doctor"
since 2^(level-1) is a power of 2, it has exactly one bit equal to 1, therefore you can write:
X = number of bits equal to 1 in [ pos-1 ]
Y = X mod 2
Which is equal to the solution you mentioned in the question.
This type of sequence is known as the Thue-Morse sequence. Using the same tree, here is a demonstration of why it gives the correct answer:
p is the 0-indexed position
b is the binary representation of p
c is the number of 1's in b
p0
E
b0
c0
/ \
p0 p1
E D
b0 b1
c0 c1
/ \ / \
p0 p1 p2 p3
E D D E
b0 b1 b10 b11
c0 c1 c1 c2
/ \ / \ / \ / \
p0 p1 p2 p3 p4 p5 p6 p7
E D D E D E E D
b0 b1 b10 b11 b100 b101 b110 b111
c0 c1 c1 c2 c1 c2 c2 c3
c is always even for Engineer and odd for Doctor. Therefore:
index = bin(pos-1).count('1') % 2
return ['Engineer', 'Doctor'][index]
In a logic circuit, I have an 8-bit data vector that is fed into an ECC IC which I am supposed to develop the logic for and that contains a vector of 5 Parity Bits. My first step to develop the logic (with logic gates, XOR), is to figure out which parity bit is going to check for which Data bits (since they are interlaced). I am using even parity, and following general hamming code rules (a parity bit in every 2^n ), I get the following sequence of output:
P1 P2 D1 P3 D2 D3 D4 P4 D5 D6 D7 D8 P5
Following the General Hamming Algorithm:
For each parity bit, Position 1,2,4,8,16 and so on... (Powers of 2), we skip for the first position n (n-1) and we check 1 bit, then we skip another one, the check another one, etc... we repeat the same process for the other bits, but this time checking/skipping every 2^n, where n is the position they occupy in the output array (P1 P2 D1 P3 D2 D3 D4 P4 D5 D6 D7 D8 P5)
Following that convention, I get:
P1 Checks data bits -> XOR(3 5 7 9 10 12)
P2 Checks data bits -> XOR(3 6 7 10 11)
P3 Checks data bits -> XOR(5 6 10 11 12)
P4 Checks data bits -> XOR(9 10 11)
Am I right? The thing that confuses me is that if I should start checking counting the parity bit as one of the 2^n bits that are supposed to be checked, or 1 bit after that specific parity bit. Pretty much sums up to if it is inclusive or not.
Thank you for your help in advance!
Cheers!
You can follow this sheme. The bits marked in each row must sum up to 0 (mod 2) in other words for the marked positions in each row the number of set bits must be even.
P1 P2 D1 P3 D2 D3 D4 P4 D5 D6 D7 D8
x x x x x x
x x x x x x
x x x x x
x x x x x
I don't understand why you have P5 in the scheme.
I was trying to understand the concept of vEB tree.
In an example:
I assumed a universe set U = {0, 1, 2, 3 ..... 8}. So the size is 9.
Now lets take a subset S = {0, 1, 3, 4, 6, 7}.
For an operation FindSuccessor (3, S); where I need to know the smallest element > 3 in subset S, I need to know the high and low bits of my element i.e. 3.
One explanation says its the first half and second half bits, giving the result 00 and 11 as high and low respectively.
Another says:
high = Floor [element/sqrt(|U|)] = Floor [3/ sqrt (9)] = Floor [1] = 1;
low = element % sqrt(|U|) = 3 % sqrt (9) = 0;
Please explain where am I going wrong?
You're not going wrong—the explanations are for two slightly different data structures that coincide only when |U| is a square power of two. At a high level, we're trying to divide a key k into two halves, each with about √|U| possibilities. The first method achieves this goal directly; the second is an approximation that runs faster on commodity hardware (assuming |U| is a power of two, the worst case is when |U| is not square and the first half has twice as many possibilities as the second). Pick one method and stick with it.
Here's an example of FindSuccessor(3, S). For simplicity, I'm going to bottom out the recursion at three elements.
The tree looks like
min=0| aux
max=7|------->min=0|
/ | \ max=2|
/ | \ /|\
/ | \ 0 1 2
/ | \
v v v
min=0| min=3| min=6|
max=1| max=4| max=7|
/| /| /|
0 1 3 4 6 7
At the root, we split 3 = (1, 0) and check whether the 1th (middle) child has max > 3. It does, so we descend there and use brute force to compute the answer, 4. (Of course, if the tree had more than two levels, we would search recursively.)
A more interesting case is when S = {0, 1, 3, 6, 7}.
min=0| aux
max=7|------->min=0|
/ | \ max=2|
/ | \ /|\
/ | \ 0 1 2
/ | \
v v v
min=0| min=3| min=6|
max=1| max=3| max=7|
/| / /|
0 1 3 6 7
Here, we examine the 1th subtree of the root, {3}, and find that its max is not greater than 3. We find the successor of 1 in the aux data structure, which is 2, and return the min of the 2th subtree, which is 6.