Algorithm to shuffle an Array randomly based on different weights

后端 未结 6 721
天涯浪人
天涯浪人 2021-01-19 14:19

I have a collection of elements that I want to shuffle randomly, but every element has a different priority or weight. So the element with bigger weight has to have more pro

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 14:46

    I can think of two approaches to solve it, though my gut tells me there should be modification for Fisher-Yates to achieve it even better:

    O(n*W) solution: (simple to program)

    First approach, create duplicates according to the weight (same as your approach), and populate a new list. Now run a standard shuffle (fisher-yates) on this list. Iterate the list and discard all duplicates, and keep only the first occurance of each element. This runs in O(n*W), where n is the number of elements in the list, and W is the average weight (pseudo-polynomial solution).


    O(nlogn) solution: (significantly harder to program)

    Second approach would be to create a list of sum of weights of the elements:

    sum[i] = weight[0] + ... + weight[i]
    

    Now, draw a number, between 0 to sum[n], and chose the first element whose sum is greater/equals this random number.
    This will be the next element, discard the element, recreate the list, and repeat.

    This runs in O(n^2*logn)

    It can be further enhanced by creating a binary tree rather than a list, where each node also stores the value of weights of the entire subtree.
    Now, after choosing an element, find the matching element (whose sum up to him is the first one higher than the random selected number), delete the node, and recalculate the weights on the path to the route.
    This will take O(n) to create the tree, O(logn) to find the node at each step, and O(logn) to recalculate the sum. Repeat it until the tree is exhausted, and you get O(nlogn) solution.
    The idea of this approach is very similar to Order Statistics Trees, but using the sum of weights rather than the number of descendants. The search and balancing after deletion will be done simiarly to order statistics tree.


    Explanation to constructing and using the binary tree.

    Assume you have elements=[a,b,c,d,e,f,g,h,i,j,k,l,m] with weights=[1,2,3,1,2,3,1,2,3,1,2,3,1]

    First construct an almost full binary tree, and populate the elements in it. Note that the tree is NOT Binary search tree, just a regular tree, so order of elements does not matter - and we won't need to maintain it later on.

    You will get something like the following tree:

    enter image description here

    Legend: w - weight of that node, sw - sum of weight for the entire subtree.

    Next, calculate sum of weights for each subtree. Start from the leaves, and calculate s.w = w. For every other node calculate s.w = left->s.w + right->s.w, filling the tree from the bottom up (post order traversal).

    enter image description here

    Building the tree, populating it, and calculating s.w. for each node is done in O(n).

    Now, you iteratively need to chose a random number between 0 to sum of weights (the s.w. value of the root, in our case 25). Let that number be r, and find for each such number the matching node.
    Finding the matching node is done recursively

    if `r< root.left.sw`:
       go to left son, and repeat. 
    else if `r

    Example, chosing r=10:

    Is r

    This is done in O(h) = O(logn) per iteration.

    Now, you need to delete that node, and reset the weights of the tree.
    One approach to deleting that ensures the tree is in logarithmic weight is smilar to binary heap: Replace the chosen node with the bottom rightest node, remove the new rightest bottom node, and rebalance the two branches going from the two involved nodes to the tree.

    First switch:

    enter image description here

    Then recalculate:

    enter image description here

    Note that recalculation is needed only to two paths, each of depth at most O(logn) (the nodes colored orange in the pic), so deletion and recalculation is also O(logn).

    Now, you got yourself a new binary tree, with the modified weights, and you are ready to choose the next candidate, until the tree is exhausted.

提交回复
热议问题