How to calculate all possible permutations for a tree diagram - c
I have a problem of tee diagram that represents city roads and need to calculate all possible ways -permutations- to establish these roads.
The role is " you can not start level 2 without finishing level 1" and so on for higher levels
this image illustrates the ideahere
i tried to think of it as an array for every level and then a single column for every branch like this
level1=[1 2]
level2=[3 4
5 6]
and
l
;evel1=[1 2 3]
level2=[4 5 0;
6 7 8;
9 10 0]
level3=[11 12;
13 0;
14 15;
16 17;
0 0;
0 0;
18 19]
But i stopped and have no idea how to complete. so i need to know how to think in this kind of problems.
You should look into the concept of "abstract datatypes" as your current design won't scale. Every time you add a new level, you need to create a new array and then update your code to know about this new array.
And the process of searching one level before moving on to the next is known as "breadth first" searching. If you create a list type, you can add all the nodes from the current level to the list and then as you process the list, you add the child nodes to the end of the list.
But that isn't really applicable to generating all permutations - doing "depth first" is more suitable as you travel down the tree and when you can't go no further, the path you've taken is a permutation.
Related
What is KdTree TreechunkLevel (how to identify different treechunklevels)?
I am working on task related to my work so can't share all the details. But will try to capture the problem in general: Using Kdtree (k-dimensional tree) for storing huge data. As per the requirement, there will be maximum 2 treechunklevels (treechunklevel0 & treechunklevel1). First treechunklevel (treechunklevel0) will be above the level where (index + 1) - (index) > 1. e.g. In below small KdTree example with two treechunk levels. (TC: Treechunk). Index_list (in this seq for the given example): [1 2 3 4 8 5 10 11 6 12 13 7] Index_list[4] - Index_list[3] > 1 (i.e. 4 - 8 > 1) so levelbase will be 3. And after the level base, treechunklevel1 will start. I am trying to understand the Treechunklevel here, treechunklevel0 (2-D tree) can be recognized easily and after that treechunklevel1 (3-D Tree) will start. Though requirement says there will be maximum 2 treechunklevels, but to test or fail the same I need to understand how different treechunk levels can be identified or differentiated. What will be the start conditions/properties of treechunklevel2 (or the 3rd treechunklevel). Didn't find any information on internet. Any leads on this would be helpful. Thanks in advance.
Describing a numbering system that counts from x at the y position in an index
From a theoretical perspective, I'm interested in learning how one would correctly describe a numbering system that starts counting from x at the y position in an index. This came up when our team recently created a task list using a spreadsheet. Items are referred to by their line item; for example, the item on the third line is Item #3. However, the spreadsheet also has a header row, somewhat like this: 1 TASK DESCRIPTION 2 eat cheese gouda is preferable here 3 look around you just... look around you 4 rickroll never gonna give you up 5 spoon you spoony bard! The counting and indexing system has these properties: indexing begins at 1 the first item in the index begins at 2 the nth item is therefore at the n-1 position This is a simple enough system to understand at a practical level; but, how would one model it on an abstract level, and what are the correct terms to do so?
Best approach for finding the maximum array element in a given range
Given a non-negative integer array of length n and m queries consisting of two integers a and b, it is expected to find the maximum in the range of index [a,b] of the array. Note that a can be greater than b, in which case the desired range is from a to n and then from 1 to b. And an input k is also given that signifies that the length of the range to be considered is also constant that is constant Example: INPUT: 6 3 5 ---> n,m,k 7 6 2 6 1 5 ---> integer array 1 5 ---> query 1 2 6 ---> query 2 4 2 ---> query 3 OUTPUT: 7 6 7 I referred this article but am not able to get how to take care of the cases where a>b. Is there any other approach for this problem
Sliding window approach: To solve the problem using approach mentioned i.e. Sliding Window Maximum, Just append the input array to itself like as shown below: 7 6 2 6 1 5 7 6 2 6 1 5 For a<=b case work as normal. For a>bcase: Consider b = a + k. So your new range is [a,a+k] which you can happily solve without any changes to algorithm. To optimize the above approach a bit, you can just append first k elements. If you slide over every time a query arrives, it takes O(n) per query. k being very close or equal to n is the worst case. Alternative Approach: Use the following approach in case of heavy querying and flexible ranges. You are looking for range queries and this is what Segment Trees are popular for. This tutorial finds the minimum in given range. I know you have asked for maximum, which is just a trivial change you have to make in code. For a>b case, query two times once for [1,b] & then for [a,n] and report the maximum out of the two. Preprocessing time: O(n) Extra Space: O(n) This approach is very efficient as it will answer every query in O(logn) which is quite helpful in case you are querying too much. Sliding Window is going to output maximum element in all the ranges, but you need the maximum element only in given range. So instead of going with Sliding Window approach go with Segment Trees or Binary Indexed Trees. You'll feel the fun of truly querying within a range and not sliding over. (Just sliding over every time a query arrives won't scale if the range is flexible.)
I think this could be done by using divide and conquer approach, so let's take a look at the above example. So for the case a>b find max for range (1,b), say max_b = max_in_range(1,b). find max for range (a,n), say max_a = max_in_range(a,n). Now you can easily take up max between two numbers using a in built max method in any language as ans = max(max_a, max_b) But problems like this which involes ranges, you can solve it using segment trees, here is the link to start with - https://en.wikipedia.org/wiki/Segment_tree Hope this helps!
Heuristic for shifting array
Given a goal state int final[3][3]={{1,2,3}, {4,5,6}, {7,8,9}}; and a random initial state, I want to sort my array as final only by shifting rows (right or left) and columns (up and down) of my table 7 8 4 by shifting to the right the first row it will become 4 7 8 2 1 9 2 1 9 6 5 3 6 5 3 So I want to use a* search and I'm trying to find a good heuristic. I've already tried with misplaced array elements. Any suggestions?
I view this as an algebraic problem. You are given a group of permutation which is generated by 6 cycles (3 rows and 3 columns) and you want to find some more moves which help you to get to any permutation. First advice: not all permutations are possible! Since every shift is an even permutation (a 3-cycle is the composition of two transpositions) only even permutations are possible. Hence you will not find any solution to a configuration where all is in place but two swapped numbers as in (2,1,3),(4,5,6),(7,8,9). Second advice. If r is a row shift and c is a coumn shift, compute the action of rcr'c' where r' and c' are the inverse shifts. This "commutator" is again a cycle of 3 elements but this time they are not in a row or column. By choosing different r and c you get a lot of 3-cycles which can be used in the third advice. Third advice. Consider the region of numbers which are already in their final position. Apply 3-cycles to the complement of this set to reduce it, until you get to a solution.
Building an array of numbers no repeating order doesn't matter (Gaming stats)
So I play heroes of newerth. I have the desire to make a statistical program that shows which team of 5 heroes vs another 5 heroes wins the most. Given there are 85 heroes and games are 85 choose 5 vs 80 choose 5, that's a lot of combinations. Essentially I'm going to take the stats data the game servers allow me to get and just put a 1 in an array which has heroes when they get a win [1,2,3,4,5][6,7,8,9,10][W:1][L:0] So after I parse and build the array from the historical game data, I can put in what 5 heroes I want to see, and I can get back all the relevant game data telling me which 5 hero lineup has won/lost the most. What I need help starting is a simple algorithm to write out my array. Here's similar output I need: (I have simplified this to 1-10, where the code I get I can just change 10 to x for how many heroes there are). [1,2,3,4,5][6,7,8,9,10] [1,2,3,4,6][5,7,8,9,10] [1,2,3,4,7][5,6,8,9,10] [1,2,3,4,8][5,6,7,9,10] [1,2,3,4,9][5,6,7,8,10] [1,2,3,4,10][5,6,7,8,9] [1,2,3,5,6][4,7,8,9,10] [1,2,3,5,7][4,6,8,9,10] [1,2,3,5,8][4,6,7,9,10] [1,2,3,5,9][4,6,7,8,10] [1,2,3,5,10][4,6,7,8,9] [1,2,3,6,7][4,5,8,9,10] [1,2,3,6,8][4,5,7,9,10] [1,2,3,6,9][4,5,7,8,10] [1,2,3,6,10][4,5,7,8,9] [1,2,3,7,8][4,5,6,9,10] [1,2,3,7,9][4,5,6,8,10] [1,2,3,7,10][4,5,6,8,9] [1,2,3,8,9][4,5,6,7,10] [1,2,3,8,10][4,5,6,7,9] [1,2,3,9,10][4,5,6,7,8] [1,2,4,5,6][3,7,8,9,10] [1,2,4,5,7][3,6,8,9,10] [1,2,4,5,8][3,6,7,9,10] [1,2,4,5,9][3,6,7,8,10] [1,2,4,5,10][3,6,7,8,9] [1,2,4,6,7][3,5,8,9,10] [1,2,4,6,8]... [1,2,4,6,9] [1,2,4,6,10] [1,2,4,7,8] [1,2,4,7,9] [1,2,4,7,10] [1,2,4,8,9] [1,2,4,8,10] [1,2,4,9,10] ... You get the Idea. No repeating and order doesn't matter. Its essentially cut in half doesn't matter the order of the arrays either. Just need a list of all the combinations of teams that can be played against each other. EDIT: additional thinking... After quite a bit of thinking. I have come up with some ideas. Instead of writting out the entire array of [85*84*83*82*81][80*79*78*77*76*75] possible combinations of characters, which would have to be made larger for the introduction of of new heroes as to keep the array relevant and constantly updating. To instead when reading from the server parse the information and build the array from there. It would be much simpler to just make an element in the array when one is not found, ei the combinations have never been played before. Then parsing the data would be 1 pass, and build your array as it complies along. Yes it might take a while, but the values that are created will be worth the wait. It can be done over time too. Starting with a small test case say 1000 games and working up the the number of matches that have been played. Another Idea would be to start from our current spot in time and build the data base from there. There is no need to go back to the first games ever played based off the amount of changes that have occurred to heroes over that time frame, but say go back 2-3 months to give it some foundation and reliability of data, and with each passing day only getting more accurate. Example parse and build of the array: get match(x) if length < 15/25, x++; //determine what length matches we want and discredit shorter than 15 for sure. if players != 10, x++; //skip the match because it didn't finish with 10 players. if map != normal_mm_map // rule out non mm games, and mid wars if != mm, rule out custom games //and so forth match_psr = match(x).get(average_psr); match_winner = match(x).get(winner); //Hero ids of winners Wh1 = match.(x).get(winner.player1(hero_id))) Wh2 = match.(x).get(winner.player2(hero_id))) Wh3 = match.(x).get(winner.player3(hero_id))) Wh4 = match.(x).get(winner.player4(hero_id))) Wh5 = match.(x).get(winner.player5(hero_id))) //hero ids of losers Lh1 = match.(x).get(loser.player1(hero_id))) Lh2 = match.(x).get(loser.player2(hero_id))) Lh3 = match.(x).get(loser.player3(hero_id))) Lh4 = match.(x).get(loser.player4(hero_id))) Lh5 = match.(x).get(loser.player5(hero_id))) //some sort of sorting algorithim to put the Wh1-5 in order of hero id from smallest to largest //some sort of sorting algorithim to put the Lh1-5 in order of hero id from smallest to largest if(array([Wh1, Wh2, Wh3, Wh4, Wh5],[Lh1,Lh2,Lh3,Lh4,Lh5],[],[],[],[],[],[],[],[],[]) != null) array([Wh1, Wh2, Wh3, Wh4, Wh5],[Lh1,Lh2,Lh3,Lh4,Lh5],[],[],[],[],[],[],[],[],[]) += array([],[],[1],[][][][](something with psr)[][][[]) else(array.add_element([Wh1, Wh2, Wh3, Wh4, Wh5],[Lh1,Lh2,Lh3,Lh4,Lh5],[1],[][][][](something with psr)[][][[]) Any thoughts?
Encode each actor in the game using a simple scheme 0 ... 84 You can maintain a 2D matrix of 85*85 actors in the game. Initialize each entry in this array to zero. Now use just the upper triangular portion of your matrix. So, for any two players P1,P2 you have a unique entry in the array, say array[small(p1,p2)][big(p1,p2)]. array(p1,p2) signifies how much p1 won against p2. You event loop can be like this : For each stat like H=(H1,H2,H3,H4,H5) won against L=(L1,L2,L3,L4,L5) do For each tuple in H*L (h,l) do if h<l increment array[h][l] by one else decrement array[l][h] by one Now, at the end of this loop, you have an aggregate information about players information against each other. Next step is an interesting optimization problem. wrong approach : select 5 fields in this matrix such that no two field's row and column are same and the summation of their absolute values is maximum. I think you can get good optimization algorithms for this problem. Here, we will calculate five tuples (h1,l1), (h2,l2), (h3,l3) ... where h1 wins against l1 is maximized but you still did not see it l1 is good against h2. The easier and correct options is to use brute force on the set of (85*84)C5 tuples.