Suppose that there exists a magic square in which Sherlock Holmes copied the output of the reverse alphabetical string in a sequential manner. Later, he has given this magic square as an input to a parallel program running on a general-purpose parallel computer such that one of the characters from the first three rows gets erased due to some bug. With the help of underlying parallel capability-based hardware, the (parallel) program again arranges the modified output into the reverse alphabetical list (with the respect to the remaining 15 characters) such that the erased character is being pushed onto the last box of the magic square.
(a) Which is the methodology used by the parallel program.
(b) Explain with the help of an example using the given reverse alphabetical string.
A story about a pure-[SERIAL] Sherlock HOLMESequipped with a magic-[PARALLEL], yet ( just a bit ) "defective" WATSON ...
The initial state:
anOrderedListOfCHARs "abcdefghijklmnop"
(indices) : ////////////////
0123456789ABCDEF
0 1 2 3
\ \ \ \
aMagicSQUARE: [_] [_] [_] [_]
[_] [_] [_] [_]
[_] [_] [_] [_]
[_] [_] [_] [_]
\ \ \ \
C D E F
Next ?
Sir Holmes, a "detective", has
used [SERIAL]-process,
putting one CHAR
after another,
in a reversed order,
to produce this:
aMagicSQUARE (indices) [F] [E] [D] [C]
reverse'd
[B] [A] [9] [8]
[7] [6] [5] [4]
[3] [2] [1] [0]
Next ?
A [PARALLEL]-machine a "defective" one
has damaged the
state, somewhere
in row 1, 2 or 3
we do not know where
we do yet know it was just one and only a one CHAR
aMagicSQUARE (indices)
one & only one [?] [?] [?] [?]
cell in R1:R3
has a single [?] [?] [?] [?]
defect (EMPTY)
[?] [?] [?] [?]
[d] [c] [b] [a]
Next ?
A [PARALLEL]-machine can first ___________________________________
[PARALLEL]-map cells / / / / /
back into [F] [E] [D] [C] /
a vector-like ______________________________ /
list / / / / / /
[B] [A] [9] [8] / /
_________________________ / /
/ / / / / / /
[7] [6] [5] [4] / / /
____________________ / / /
/ / / / / / / /
[3] [2] [1] [0] / / / /
/ / / /
0123__________/ / / /
////4567________/ / /
////////89AB______/ /
////////////CDEF____/
////////////////
"abcdefghijklmnop"
////////////////
0123456789ABCDEF
And now ?
Given the "defective" step,
we expect not more
than a one defect,
so now we get this:
"????????????mnop"
////////////////
0123456789ABCDEF
So ?
Next step is a [PARALLEL]-test
for a single (empty)
a one & only one
index we can STORE
it is a collision-free
_aDefectCellINDEX_: 3
|
"abc^efghijklmnop"
////////////////
0123456789ABCDEF
Next ?
Next step is a [PARALLEL]-shuffle
for all cells
since a single found defect
it has to be orchestrated
as a [PARALLEL]-READ,
but a block/stride-safe
ordered (as destructive) ________________
[PARALLEL]-WRITE / /
for all cells "after" 3 /
_aDefectCellINDEX_ ^456789ABCDEF /
and placing (EMPTY) ^//////////// /
"defect" to the last "abc^efghijklmnop" /
position in the list //////////// /
"abcefghijklmnop^"/
/_/
Depending on the actual HARDWARE,
the [PARALLEL]-machine
may prefer to BLOCK-READ
to BLOCK-ERASE
and BLOCK-SHIFT/WRITE
not to handle a single exception
(a defect) alone in a "divergent"-code-path
Elementary, dear Watson... Wasn't it?
Related
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
What postfix expression does the following infix expression translate to?
2 + 3 - 4 / 3 * 3 + 4
I'm really confused on how does it translate to 2 3 4 3 / 3 * - + 4 +?.
Can somebody explain it, please?
Thanks in advance
Put parens around the infix to show order of operations honoring precedence and associativity:
((2 + 3) - ((4 / 3) * 3)) + 4
Now you can use this to draw a syntax tree:
+
_________/ \_
| |
- 4
_/ \____
| |
+ *
/ \ _/ \_
2 3 | |
'/' 3
/ \
4 3
Now you can get postfix by traversing the tree in post order:
2 3 + 4 3 / 3 * - 4 +
There are other post orders that give the right answer. For example, you can get more by choosing to evaluate either the left or the right subtree first for each commutative operator. Equivalently, you can reverse the left and right hand subtrees for each commutative operator and always use a standard, left child first post order search.
You can check the order by executing it with a stack machine:
Stack
read 2, push 2 [2
read 3, push 3 [2 3
read +, pop 3, pop 2, push 5 (i.e. 2 + 3) [5
read 4, push 4 [5 4
read 3, push 3 [5 4 3
read /, pop 3, pop 4, push 1.33... (i.e. 4/3) [5 1.33...
read 3, push 3 [5 1.33... 3
read *, pop 3, pop 1.33..., push 4 (i.e. 1.33... * 3)[5 4
read -, pop 4, pop 5, push 1 (i.e. 5 - 4) [1
read 4, push 4 [1 4
read +, pop 4, pop 1, push 5 (i.e. 1 + 4) [5
So 5 is the answer, which agrees with the infix evaluation.
hey i have a questions on my homework and i am being able to solve it i just want someone to see if i am doing right or wrong...
A b-tree with minimum branching factor of t=3
[D][G][K][N][V]
/ / / | \ \
/ / / | \ \
/ / / | \ \
AC EF HI LM OPRST WX
Now when i insert J in above tree this is the output i am getting....
[K]
/ \
/ \
/ \
[D][G] [N][V]
/ / / / \ \
/ / / / \ \
/ / / / \ \
AC EF HIJ LM OPRST WX
After Inserting Q in above tree this is the Final tree i am getting.
[K]
/ \
/ \
/ \
[D][G] [N][Q][V]
/ / / / / \ \
/ / / / / \ \
/ / / / / \ \
AC EF HIJ LM OP RST WX
Is this the Final Tree Correct?
No, the final B tree is not correct. The intermediate one is though. The last one should be like this
[K]
/ \
/ \
/ \
[D][G] [N][R][V]
/ / / / / \ \
/ / / / / \ \
/ / / / / \ \
AC EF HIJ LM OPQ ST WX
You missed something very important. In a B-tree, insertions are only done in the leaf node and every full node on the way is split. You inserted Q in a level 2 node in your final tree.
Edit: I think you are confused about the insertion algorithm. Insertions only take place in the leaf node. In the downward path from root to leaf, if any full node is encountered it is split first. If the leaf node is full, it will be split first and then the key will be inserted. In your case the leaf node OPRST will be split when it is encountered because it has 5 nodes and is full. Thus R will be moved up and and a new leaf node containing keys ST will be created. The older leaf node now will only have OP keys. Q is then compared with R and search moves leftward to OP node where Q finally gets inserted.
If the branching factor is 3, doesn't that mean the minimum number of keys in non-root node? How can the initial tree be correct?
Initial state would be:
└── E, I, N, S
├── A, C, D
├── F, G, H
├── K, L, M
├── O, P, R
└── T, V, W, X
I am given 2 y 5 1 4 3 - * - * +, and am asked to evaluate it, and then draw the equivalent expression tree. I haven't done any work with this before, can someone show the steps you would take to solve this type of question?
I have looked at: Post order traversal of a formula
and am confused as to how to come to that answer.
What you are given is a postfix expression. It is well-known that these things are evaluated with stacks according to the following rule:
Working left to right, when you encounter a value, push it. When you encounter an operator, pop the top two values, apply the operation, and push the result back.
So your expression evaluation proceeds like this
2 (push 2)
2 y (push y)
2 y 5 (push 5)
2 y 5 1 (push 1)
2 y 5 1 4 (push 4)
2 y 5 1 4 3 (push 3)
2 y 5 1 1 (pop 3, pop 4, push 4-3)
2 y 5 1 (pop 1, pop 1, push 1*1)
2 y 4 (pop 1, pop 5, push 5-1)
2 4y (pop 4, pop y, push y*4)
2+4y (pop 4y, pop 2, push 2+4y)
Your answer is the value left on the stack.
Now, you asked about producing a tree also. To produce a tree, rather than evaluating the expression when you find an operator, you "apply" the operator by building a tree fragment with the operator as the root, and the popped tree fragments as children.
So after pushing
2 y 5 1 4 3
you see a -, so you pop the 4 and 3 and you push back this structure
-
/ \
4 3
Next you see the * so you pop the top tree fragment and the one below it, which is actually a tree fragment consisting of the single node
1
So it will look like
*
/ \
1 -
/ \
4 3
You should be able to take it from here.
The answer at Post order traversal of a formula says find the first operator. In your case it is '-'. The second step he describes is put it between the previous two operands.
In your case these two operands are 4 and 3 (they are directly before the '-'). So the formula after this step becomes:
2 y 5 1 (4-3) * - * +
Remember that the expression (4-3) is now one operand.
We apply the steps again to this formula. We see that the first operator now is ''.
The two operands before the '' are 1 and (4-3). The formula becomes:
2 y 5 (1*(4-3)) - * +
Now you can apply this untill all operators are gone.
I will not continue giving more steps because probably this is a homework question. However I think it is clear?
As stated by novalis from the question you linked, scan for the first operator and previous 2 operands and then replace that group with a more familiar expression in parentheses, ie.
if you have:
op1 op2 operator
4 3 -
this becomes:
(op1 operator op2)
(4 - 3 )
so, proceeding...
2 y 5 1 4 3 - * - * +
2 y 5 1 (4 - 3) * - * +
2 y 5 (1 * (4 - 3)) - * +
Proceed in a similar fashion to build the tree. Scan for the first operator and create a tiny tree:
-
/ \
4 3
Then, each new operand is the top node of your new tree:
*
/ \
1 -
/ \
4 3
and then:
-
/ \
5 *
/ \
1 -
/ \
4 3
Suppose my characters and their frequencies are as follows:
Char Freq.
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
When constructing a tree, at step 2 we have this:
[3] [3] [4] [5] [6] [7] [8]
/ \ c d e f g h
/ \
[1] [2]
a b
Now, since we have two 3s, how can we determine the priority of them?
In the Huffman Coding this is considered as:
[3] [3] [4] [5] [6] [7] [8]
c / \ d e f g h
/ \
[1] [2]
a b
Why?
What's the difference? Ignoring d through h for the moment, in the first case you'd get
a = 00
b = 01
c = 1
and in the second case,
a = 10
b = 11
c = 0
As long as c is at the same height in the final tree, its code will have the same length.
I would take c with bigger priority (shorter code). This would be in-line with the basic principle of Huffman trees: priority/shorter code for immediate results and lower priority for more parsing.
Your case is not interesting. The assignments of 0's and 1's to the branches is arbitrary, so the choice you outline results in the same code, i.e. the same code lengths, either way.
There are however interesting cases where you face a choice of three or more groups with the same total frequency and different shapes. Any choice will result in the same overall optimality, i.e. exactly the same number of total bits to encode the provided symbols at the provided frequencies. However the choices can result in different shape trees with different combinations of bit lengths. Then such a choice can be made to arrive at deeper or shallower trees, depending on what is desired.