Nested tree element number - math

What I want to ask is mathematical/logical situation
I have nested tree which looks similar to
And I need the indexing number of each items order, ex:
Tube - 1
LCD -2
Plasma - 3
MP3 Player - 1
CD Player - 2
2 Way Radios -3
(1,2,3 ... values are not necessary to be exactly these)
This will be required for ordering the items by this value. After the order I need to get all first items, than all second items... and so on
It is easy to set an order number of each item but what I am looking for is a value calculated from the left-right values of each item and its parent

You can compute the index as one plus the index of the left sibling, or as one if there is no left sibling. If you can't control the order in which indices get computed, you might have to iterate over all left siblings, counting them, to compute the index.
There is no direct way. One way to see this is imagining a node being inserted into the tree with a pretty low index. Nodes other than its immediate siblings will have no way of knowing about this insertion, just by looking at parent and direct sibling pointers. But their indices will have to change nonetheless. Which means that these pointers can not be enough to compute indices directly.

To order them, you can use DFS (Depth First Search algorithm). Below is a possible Pseudo-code,
DFS(node)
{
index=1
for all child v of node
assign v the value of index
DFS (v)
increment index by 1
end for
}
assign the root the index 1
DFS (root)

Related

Can SPARK be used to prove that Quicksort actually sorts?

I'm not a user of SPARK. I'm just trying to understand the capabilities of the language.
Can SPARK be used to prove, for example, that Quicksort actually sorts the array given to it?
(Would love to see an example, assuming this is simple)
Yes, it can, though I'm not particularly good at SPARK-proving (yet). Here's how quick-sort works:
We note that the idea behind quicksort is partitioning.
A 'pivot' is selected and this is used to partition the collection into three groups: equal-to, less-than, and greater-than. (This ordering impacts the procedure below; I'm using this because it's different than the in-order version to illustrate that it is primarily about grouping, not ordering.)
If the collection is 0 or 1 in length, then you are sorted; if 2 then check and possibly-correct the ordering and they are sorted; otherwise continue on.
Move the pivot to the first position.
Scan from the second position position to the last position, depending on the value under consideration:
Less – Swap with the first item in the Greater partition.
Greater – Null-op.
Equal — Swap with the first item of Less, the swap with the first item of Greater.
Recursively call on the Less & Greater partitions.
If a function return Less & Equal & Greater, if a procedure re-arrange the in out input to that ordering.
Here's how you would go about doing things:
Prove/assert the 0 and 1 cases as true,
Prove your handling of 2 items,
Prove that given an input-collection and pivot there are a set of three values (L,E,G) which are the count of the elements less-than/equal-to/greater-than the pivot [this is probably a ghost-subprogram],
Prove that L+E+G equals the length of your collection,
Prove [in the post-condition] that given the pivot and (L,E,G) tuple, the output conforms to L items less-than the pivot followed by E items which are equal, and then G items that are greater.
And that should do it. [IIUC]

Existence of a random integer in a sorted circularlly linked list

This is my task:
There exists a circular and sorted linked list with n integers. Every element has a successor pointer to the next bigger item. The biggest item in the list points back to the smallest item. Determine whether a passed in target item is a member of the list. You can access an item in the list in two ways: You can follow the next pointer from an item that has already been accessed, or you can use a function "RAND" that returns a pointer to a uniformly random item in the list. Create a randomized algorithm that finds the target item and makes at most O(√n) passes in expectation and always returns the right answer.
I'm unsure about how to construct an algorithm in the required time complexity. I think it has something to do with calculating and storing some set of sums in the list but can't figure that step out.
The solution is based on the Dave's comment:
Use RAND sqrt(n) times, storing the largest element < target. Show that this is expected to be within sqrt(n) of the target.
Denote by i index of biggest element in the list which less to the target element.
Now lets compute expectation of hitting element in a range (i - sqrt(n), i] in sqrt(n) trials. On every trial the probability of hitting the range is range length divided by the list length, which is sqrt(n)/n = 1/sqrt(n), so
E = 1/sqrt(n) * sqrt(n) = 1.
So we expect to check presence of the target element by choosing the biggest element which is smaller than the target in our sqrt(n) trials and then advance linearly for sqrt(n) items.

Depth First Search

Perform Depth-first Search on the graph shown starting with vertex a. When you traverse the neighbours, process them in alphabetical order.
The question is to find the DFI, Level and the Parent of each vertex.
Here is a picture of it:
I'm unsure of how to get going with this, it is a practice question for an upcoming exam. I know for depth first search, it uses a stack and it will start at vertex a and go in alphabetical order in the stack but i'm not sure how I would get the values for each of the columns. Can someone explain further or help me with this?
So you start at 'a' and must traverse the nodes in alphabetical order so from a you either have the option of going to b or g so you choose b because it is first alphabetically. from b your only choice is g and so on....
now for your values. the parent of a is null since you have no previous nodes the parent of b is a and the parent of g is b and so on.
the dfs level is the level that it would end up on a tree. so imagine that you do your traversal then erase all lines that weren't part of the traversal. and then you take your root and 'shake it out' what i mean is you rearrange it so that it looks like a tree. (this particular graph is very uninteresting) and then you assign levels based on that tree.
And the dfs index is simply the order in which you touched the nodes.
The folowing are for your graph but using g as a starting point....I think it makes it slightly more intersting
the numbers are the order in which the edges were taken.
Here is what i was talking about when i said 'shake it out' this is what your tree looks like and in blue i show the level of each node(0 based). I hope the images make it a little more understandable.
the one i drew( the terrible free hand one) was formed by deleting all of the edges that weren't used and then rearranging them to look like a tree.
You can think of the depth as how many steps did i have to take from the root to get to the current node. so from g to b is 1 step so depth of 1 from g to i 3 because we go from g->c->d->i 3 steps. after you have made your traversal you ignore the fact that you can in fact get from g to i in two steps(g->h->i) because it wasnt part of the traversal
The index is simply the number in order that the node is visited. a is first, write 1 there. Knowing depth first search as you do, you should know what the second node is; so write 2 under that. Depth is how high a node is; every time you deepen the depth, it increases, and whenever you go shallower, it's less. So a is on depth 1; the next node and its sister will be on depth 2, etc. The parent is the letter identifying the node that you just came from; so a has no parent, and the node with index 2 will have a as parent.
If your class uses a zero-based numbering system, replace 2 in the above paragraph with 1, and 1 with 0. If you have no idea what "zero-based numbering system" is, ignore this paragraph.

Number of movements in a dynamic array

A dynamic array is an array that doubles its size, when an element is added to an already full array, copying the existing elements to a new place more details here. It is clear that there will be ceil(log(n)) of bulk copy operations.
In a textbook I have seen the number of movements M as being computed this way:
M=sum for {i=1} to {ceil(log(n))} of i*n/{2^i} with the argument that "half the elements move once, a quarter of the elements twice"...
But I thought that for each bulk copy operation the number of copied/moved elements is actually n/2^i, as every bulk operation is triggered by reaching and exceeding the 2^i th element, so that the number of movements is
M=sum for {i=1} to {ceil(log(n))} of n/{2^i} (for n=8 it seems to be the correct formula).
Who is right and what is wrong in the another argument?
Both versions are O(n), so there is no big difference.
The textbook version counts the initial write of each element as a move operation but doesn't consider the very first element, which will move ceil(log(n)) times. Other than that they are equivalent, i.e.
(sum for {i=1} to {ceil(log(n))} of i*n/{2^i}) - (n - 1) + ceil(log(n))
== sum for {i=1} to {ceil(log(n))} of n/{2^i}
when n is a power of 2. Both are off by different amounts when n is not a power of 2.

Dynamic Programming Recursion and a sprinkle of Memoization

I have this massive array of ints from 0-4 in this triangle. I am trying to learn dynamic programming with Ruby and would like some assistance in calculating the number of paths in the triangle that meet three criterion:
You must start at one of the zero points in the row with 70 elements.
Your path can be directly above you one row (if there is a number directly above) or one row up heading diagonal to the left. One of these options is always available
The sum of the path you take to get to the zero on the first row must add up to 140.
Example, start at the second zero in the bottom row. You can move directly up to the one or diagonal left to the 4. In either case, the number you arrive at must be added to the running count of all the numbers you have visited. From the 1 you can travel to a 2 (running sum = 3) directly above or to the 0 (running sum = 1) diagonal to the left.
0
41
302
2413
13024
024130
4130241
30241302
241302413
1302413024
02413024130
413024130241
3024130241302
24130241302413
130241302413024
0241302413024130
41302413024130241
302413024130241302
2413024130241302413
13024130241302413024
024130241302413024130
4130241302413024130241
30241302413024130241302
241302413024130241302413
1302413024130241302413024
02413024130241302413024130
413024130241302413024130241
3024130241302413024130241302
24130241302413024130241302413
130241302413024130241302413024
0241302413024130241302413024130
41302413024130241302413024130241
302413024130241302413024130241302
2413024130241302413024130241302413
13024130241302413024130241302413024
024130241302413024130241302413024130
4130241302413024130241302413024130241
30241302413024130241302413024130241302
241302413024130241302413024130241302413
1302413024130241302413024130241302413024
02413024130241302413024130241302413024130
413024130241302413024130241302413024130241
3024130241302413024130241302413024130241302
24130241302413024130241302413024130241302413
130241302413024130241302413024130241302413024
0241302413024130241302413024130241302413024130
41302413024130241302413024130241302413024130241
302413024130241302413024130241302413024130241302
2413024130241302413024130241302413024130241302413
13024130241302413024130241302413024130241302413024
024130241302413024130241302413024130241302413024130
4130241302413024130241302413024130241302413024130241
30241302413024130241302413024130241302413024130241302
241302413024130241302413024130241302413024130241302413
1302413024130241302413024130241302413024130241302413024
02413024130241302413024130241302413024130241302413024130
413024130241302413024130241302413024130241302413024130241
3024130241302413024130241302413024130241302413024130241302
24130241302413024130241302413024130241302413024130241302413
130241302413024130241302413024130241302413024130241302413024
0241302413024130241302413024130241302413024130241302413024130
41302413024130241302413024130241302413024130241302413024130241
302413024130241302413024130241302413024130241302413024130241302
2413024130241302413024130241302413024130241302413024130241302413
13024130241302413024130241302413024130241302413024130241302413024
024130241302413024130241302413024130241302413024130241302413024130
4130241302413024130241302413024130241302413024130241302413024130241
30241302413024130241302413024130241302413024130241302413024130241302
241302413024130241302413024130241302413024130241302413024130241302413
1302413024130241302413024130241302413024130241302413024130241302413024
02413024130241302413024130241302413024130241302413024130241302413024130
But I like homework :)
I find it easier to reason about the 'paths' problem when starting from the top, and following the rules the other way around.
This means:
a partial path can be the top zero, or an extended partial path
the extensions of a partial path Pr,c are, unless r is the last row, in which they're complete, the union of
the extensions of Pr,c + P(r+1),c
the extensions of Pr,c + P(r+1),c+1
The 'sum' rule just selects certain of all complete paths.

Resources