Building a valid heap - heap

I just need some verification on whether or not I'm doing this correctly. I checked the wiki for a heapsort, but it seems in the animation to build the heap it inserts the numbers into the nodes and orders it as it goes.
The question asks to "Draw a valid heap with these elements.. {7, 12, 1, 3, 22, 5, 11} as a tree"
I've tried it on a few examples, and it seems I should layout the nodes first then reorder the nodes instead of ordering it as I go. Is the way I'm doing it correct?
For example, putting elements into the nodes
7
12 1
3 22 5 11
Ordering begins here: swap 1 and 7
7
12 1
3 22 5 11
Swap 3 and 12
1
12 7
3 22 5 11
Swap 5 and 7
1
3 7
12 22 5 11
done.
1
3 5
12 22 7 11
Actually that's not right.
The answer given is
1
7 3
12 22 5 11
If I begin to re-order the heap from the left side first (beginning with 3) then I get the right answer.

Actually, the heap data structure as only one property, which can be defined as follows: "In a heap T, for every node v other than the root, the key stored at v is greater than or equal to the key stored at v's parent."
So there are many right representations of the heap based on elements (7, 12, 1, 3, 22, 5, 11). With these elements I applied an "insert-and-sort" algorithm and the result gives yet another version:
1
3 5
12 22 7 11

Related

Create the Mapping and Display Runs For Each Over and I Also Add First Over Runs in the Sum of Second Over Runs and Same for Third Over

Source table Cricket_Score:
Overs
Balls
Runs
1
1
1
1
2
2
1
3
4
1
4
0
1
5
1
1
6
2
2
1
3
2
2
1
2
3
1
2
4
4
2
5
6
2
6
0
3
1
2
3
2
1
3
3
1
3
4
6
3
5
0
3
6
4
I Want to an output like this:
Overs
Total_Runs
1
10
2
25
3
39
Description: - For First Over means First 6 Balls I Want Sum of First 6 Balls that is 10. and For Second 6 Balls I Want Sum of First 6 Balls [Over] + Second 6 Balls That is 25 [10 + 15 = 25]. and For Third 6 Balls I Want Sum of First 6 Balls [Over] + Second 6 Balls + Third ^ Balls That is 39 [10 + 15 + 14 = 39].
Note: - 6 balls means one over.
How to create a mapping in for this scenario in Informatica / which logic should I use?
i will assume your data is EXACTLY like you have shown in your question. If its not like this in source then it will be a major issue. If its a table where data is not sorted, it will be an issue.
Solution -
Create an expression transformation with below ports - in below order. in - input port, v_variable port, out_* output port
in_balls
in_runs
in_overs
v_cumulative_runs= in_runs+ iif(isnull(v_cumulative_run),0,v_cumulative_run)
out_total_runs=v_cumulative_runs
out_overs=in_overs
Use an aggregator -
in_total_runs
in_out_overs -- group by this port
out_total_runs = max(in_total_runs)
Attach in_out_overs and out_total_runs links to target.

how to create combinatorial combination of two files

I did some research but i have difficulties finding an answer.
I am using python 2.7 and pandas so far but i am still learning.
I have two CSVs, let say it's the alphabet A-Z in one and digits in the second one, 0-100.
I want to merge the two files to have A0 to A100 up through Z.
For information the two files have DNA sequence so i believe they are strings.
I tried to create arrays with numpy and create a matrix but to no available..
here is a preview of the files:
barcode
0 GGAAGAA
1 CCAAGAA
2 GAGAGAA
3 AGGAGAA
4 TCGAGAA
5 CTGAGAA
6 CACAGAA
7 TGCAGAA
8 ACCAGAA
9 GTCAGAA
10 CGTAGAA
11 GCTAGAA
12 GAAGGAA
13 AGAGGAA
14 TCAGGAA
659
barcode
0 CGGAAGAA
1 GCGAAGAA
2 GGCAAGAA
3 GGAGAGAA
4 CCAGAGAA
5 GAGGAGAA
6 ACGGAGAA
7 CTGGAGAA
8 CACGAGAA
9 AGCGAGAA
10 TCCGAGAA
11 GTCGAGAA
12 CGTGAGAA
13 GCTGAGAA
14 CGACAGAA
1995
I am putting here the way i found to do it, there might be a sexier way:
index = pd.MultiIndex.from_product([df8.barcode, df7.barcode], names = ["df8", "df7"])
df = pd.DataFrame(index = index).reset_index()
def concat_BC(x):#concatenate the two sequences into one new column
return str(x["df8"]) + str(x["df7"])
df["BC"] = df.apply(concat_BC, axis=1)
– Stephane Chiron

remove/filter data from power bi modeling tab

I have a very large data set and would like to remove n rows from the bottom up.
For example, if I had 15 numbers: 1 1 2 3 4 5 5 6 7 7 7 8 8 9 9, and I want to remove 5 entries from the bottom, being 7 8 8 9 9.
How can I do this in dax, remove/filter out n rows from the bottom of my data?
Thanks

GLPK Mathprog group of sets

I'm trying to code a model that can solve the Multiple Choice Knapsack Problem (MCKP) as described in Knapsack Problems involving dimensions, demands and multiple
choice constraints: generalization and transformations between
formulations (Found here, see figures 8 an 9). You can find an example GMPL model of the basic knapsack problem here. For anyone looking for a quick explanation of the knapsack problem read the following illustration:
You are an adventurer and have stumbled upon a treasure trove. There are hundreds of wonderful items 'i' that each have a weight 'w' and a profit 'p'. Say you have a knapsack with weight capacity as 'c' and you want to make the most profit without overfilling your knapsack. What is the best combination of items such that you make the most profit?
In code:
maximize obj :
sum{(i,w,p) in I} p*x[i];
Where 'I' is the basket of items, and x[i] is the binary variable (0 = not chosen, 1 = chosen)
The problem that I am having trouble with is the addition of multiple groups. MCKP requires exactly one item to be selected from each group. So, for example, lets say we have three groups from which to choose. They could be represented as follows (ignore actual values):
# Items: index, weight, profit
set ONE :=
1 10 10
2 10 10
3 15 15
4 20 20
5 20 20
6 24 24
7 24 24
8 50 50;
# Items: index, weight, profit
set TWO :=
1 10 10
2 10 10
3 15 15
4 20 20
5 20 20
6 24 24
7 24 24
8 50 50;
# Items: index, weight, profit
set THREE :=
1 10 10
2 10 10
3 15 15
4 20 20
5 20 20
6 24 24
7 24 24
8 50 50;
I am confused on how I can iterate over each group and how I would define the variable x. I assume it would look something like:
var x{i,j} binary;
Where i is the index of items in j of groups. This assumes I define a set of sets:
set Groups{ONE,TWO,THREE}
Then I'd iterate over the groups of items:
sum{j in Groups, (i,w,p) in Groups[j]} p*x[i,j];
But I am concerned because I believe GMPL does not support ordered sets. I have seen this related question where the answer suggests defining a set within a set. However, I am not sure how it would apply in this particular scenario.
My main question, to be clear: In GMPL, how can I iterate over sets of sets (in this case a set of groups where each group has a set of items)?
Unlike AMPL, GMPL doesn't support sets of sets. Here's how to do it in AMPL:
set Groups;
set Items{Groups} dimen 3;
# define x and additional constraints
# ...
maximize obj: sum{g in Groups, (i,w,p) in Items[g]} p*x[i];
data;
set Groups := ONE TWO THREE;
# Items: index, weight, profit
set Items[ONE] :=
1 10 10
2 10 10
3 15 15
4 20 20
5 20 20
6 24 24
7 24 24
8 50 50;
# Items: index, weight, profit
set Items[TWO] :=
1 10 10
2 10 10
3 15 15
4 20 20
5 20 20
6 24 24
7 24 24
8 50 50;
# Items: index, weight, profit
set Items[THREE] :=
1 10 10
2 10 10
3 15 15
4 20 20
5 20 20
6 24 24
7 24 24
8 50 50;
If you have no more than 300 variables, you can use a free student version of AMPL and solvers (e.g. CPLEX or Gurobi).
Based on this gnu mailing list thread, I believe GMPL/MathProg has support for what you want to do. Here's their example:
set WORKERS;
param number_of_shifts, integer, >= 1;
set WORKER_CLIQUE{1..number_of_shifts}, within WORKERS;
data;
set WORKERS := Jack Kate Sawyer Sun Juliet Richard Desmond Hugo;
param number_of_shifts := 2;
set WORKER_CLIQUE[1] := Sawyer, Juliet;
set WORKER_CLIQUE[2] := Jack, Kate, Hugo;
In your example, I assume you'd use something like, set Items{1..3}, within Groups; with the data block from #vitaut's answer.

Sort a square matrix

I am solving a puzzle which is as follows.
There is a 5x5 matrix with one element as "-" and all others as whole numbers.
I can swap any element with the "-" straight(not diagonally).
Finally, I have to sort the matrix.
These are the steps I follow:
1) Receive user input for 5x5 matrix
2) Locate the position of "-"
3) Find the eligible candidates to be swapped with "-"
4) Apply some algorithm and find the most eligible candidate
5) Swap the element with "-"
6) Repeat the steps 3-5 until matrix is sorted
I have completed till step 3. However I have no idea what logic to be applied for step 4.
Can someone give some thoughts, how to find the most eligible candidate?
Examples
Input Matrix
17 7 9 18 3
15 11 1 12 14
2 - 4 21 24
5 19 6 18 8
10 13 16 19 20
Eligible candidates to swap with "-" are 11,2,4,19
Sorted matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 -
this isn't the most easiest task. Here are two links:
http://en.wikipedia.org/wiki/N-puzzle#Solvability
http://cseweb.ucsd.edu/~ccalabro/essays/15_puzzle.pdf
It is better to use some AI algorithm like A* with the Manhattan distance heuristic.
For A* algorithm read here
For the Manhattan distance heuristic read here