Assigning the level of the node - dataframe

I have a game tree (economics) structured in a dataframe like this:
Node - Parent Node
b - a
c - a
d - b
e - b
f - b
g - c
h - d
i.e. the uppermost node in the tree is a which leads to b and c. b in tun leads to d, e, f and c leads to g. and finally node d leads to h. I want to create an additional column which tells me the level at which the node occurs, i.e., I want an output which is something like this:
Node - Parent Node - Level
b - a - 1
c - a - 1
d - b - 2
e - b - 2
f - b - 2
g - c - 2
h - d - 3
How do I do this?
Moreover, if the data is not organised and is random that is the rows are not ordered the way I have shown (but it always has information on what the parent node is of a specific node), is their a way of solving the same problem and assigning the level of the node?
I know this might be super simple but I am new to Python and I didn't know how to search for this specifically.
Thanks in advance!

If you always start at your root, and ordered, you just travel down your tree and add a level when you visit a new child node.
If its not ordered i guess you could travel backwards and count the steps until you hit the root.
You'll probably need some recursive funtion to traverse around the tree.

Related

Randomisation - Partial Incomplete Block Design

I'm looking to replicate this randomisation in R studio.
Key features:
A and B are the primary comparison and must have 2 by 2 cross over design (i.e., occur togeather in each sequence)
The incomplete block design should include C D E and F - comparisons of interest are C vs D and E vs F. These comparisons need to occur the same number of times within the whole design, and one comparison must occur in each sequence
C D E F need to be balanced so that they occur the same number of times in a sequence
C D E F need to be balanced so that occurr the same number of times across periods
Any help would be greatly appreciated.
Many thanks.
The code I tried below is just for the incomplete block design C,D,E,F but I can't get it to balance across periods.
library(crossdes)
out=find.BIB(4,20,3, iter=1)#each dosed 6 times achieving first order balance
out
isGYD(out)
I had then planned to join on the A and B rand.

Reordering rows in sql database - idea

I was thinking about simple reordering rows in relational database's table.
I would like to avoid method described here:
How can I reorder rows in sql database
My simple idea was to use as ListOrder column of type double-precision 64-bit IEEE 754 floating point.
At inserting a row between two existing rows we calculate listOrder value as average of these sibling elements.
Example:
1. Starting state:
value, listOrder
a 1
b 2
c 3
d 4
e 5
f 6
2. Moving "e" two rows up
One simple sql update on e-row: update mytable set listorder=2.5 where value='e'
value, listOrder
a 1
b 2
e 2.5
c 3
d 4
f 6
3. Moving "a" one position down
value, listOrder
b 2
a 2.25
e 2.5
c 3
d 4
f 6
I have a question. How many insertions can I perform (in the edge situation) to have properly ordered list.
For the 64 bit integer there is less than 64 insertions in the same place.
Is floating point types allows to more insertions?
There are other problems with described approach?
Do you see any patches/adjustments to make this idea safe and usable in applications?
This is similar to a lexical order, which can also be done with varchar columns:
A
B
C
D
E
F
becomes
A
B
BM
C
D
F
becomes
B
BF
BM
C
D
F
I prefer the two step process, where you update every row in the table after the one you move to be one larger. Sql is efficient about this, where updating the rows following a change is not as bad as it seems. You preserve something that's more human readable, the storage size for your ordinal value scales in a linear rather with your data size, and you don't risk coming to a point where you don't have enough precision to put an item in between two values

What is the structure of a node for this B-Tree specification?

I am trying to create a B-tree with the following properties:
Every node x contains following attributes:
x.n is the number of keys present in node x
x.key1,x.key2,.....x.keyx.n are the keys present in the node
x.c1,x.c2,.........x.cx.n,x.cx.n+1 are the pointers to the child nodes
x.leaf is a boolean variable that shows whether the node is a leaf node or not
Based on this specification, how would I implement the structure for a node:
struct Node{
...?
}
The notional structure when drawn is something like this.
a b c d
/ | | | \
la bab bbc bcd gd
la = less than a
bab = between a and b
bbc = between b and c
bcd = between c and d
gd = greater than d
Where there are more pointers than elements.
So a b-tree of order N has at most N children. So using BTREE_ORDER as this value, and ensuring BTREE_ORDER is greater than 1.
The structure is most efficiently done as
struct Node{
size_t numNodes;
KEY_TYPE Key[BTREE_ORDER -1];
struct Node * Children[BTREE_ORDER];
}
So it has space for BTREE_ORDER-1 keys and BTREE_ORDER child nodes. The arangement is up to the code, and is
Children[0] Key[0] Children[1] Key[1] .... Key[numNodes - 2] Children[ numNodes - 1]

XSLT - Fill in expected but missing values in an XML file

I have the following XML file that I am parsing with xsltproc:
Fig1 sheet1.xml
<data>1</data>
<data>2</data>
<data>3</data>
<data>4</data>
At certain times I will be receiving data like this
Fig2 sheet2.xml
<data>1</data>
<data>3</data>
<data>4</data>
Currently the data is displayed like this:
a - 1
b - 2
c - 3
d - 4
But with figure 2's data i would get this:
a - 1
b - 2
c - 3
d -
And I want to output data like this:
a - 1
b - NOT EXISTING
c - 3
d - 4
I hope my description of the problem is clear. If you need me to provide more information on my issue - let me know. Thanks for any help you can provide. I am a beginner so please provide solutions that you think I could implement without too much complication based on the description of my problem.
I've decided to use a recursive template to solve this.

Algorithm - combine multiple lists, resulting in unique list and retaining order

I want to combine multiple lists of items into a single list, retaining the overall order requirements. i.e.:
1: A C E
2: D E
3: B A D
result: B A C D E
above, starting with list 1, we have ACE, we then know that D must come before E, and from list 3, we know that B must come before A, and D must come after B and A.
If there are conflicting orderings, the first ordering should be used. i.e.
1: A C E
2: B D E
3: F D B
result: A C F B D E
3 conflicts with 2 (B D vs D B), therefore requirements for 2 will be used.
If ordering requirements mean an item must come before or after another, it doesn't matter if it comes immediately before or after, or at the start or end of the list, as long as overall ordering is maintained.
This is being developed using VB.Net, so a LINQy solution (or any .Net solution) would be nice - otherwise pointers for an approach would be good.
Edit: Edited to make example 2 make sense (a last minute change had made it invalid)
The keyword you are probably interested in is "Topological sorting". The solution based on that would look as follows:
Create an empty directed graph.
Process sequences in order, for each two consecutive elements X,Y in a sequence add an edge X->Y to the graph, unless this would form a cycle.
Perform a topological sort on the vertices of the graph. The resulting sequence should satisfy your requirements.