Average case of Linear search [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array of elements A1,A2,...,An.
The probability of a user searching for each element are P1,P2,...,Pn.
If the elements are rearranged, will the average case of the algorithm change?
Edit : I have posted the question, which appeared in my exam.

The expected number of comparisons is sum_{i=1...n}(i * p_i).
Re-ordering the elements in descending order reduces the expectation. That's intuitive since by looking at the most probable choices first will reduce, on average, the number of elements looked at before you find a particular choice.
As an example, suppose there's three items k1, k2, k3 with match probabilities 10%, 30% and 60%.
Then in the order k1, k2, k3, the expected number of comparisons is 1*0.1 + 2*0.3 + 3*0.6 = 2.5
With the most likely first: k3, k2, k1, the expected number of comparisons is 1*0.6 + 2*0.3 + 3*0.1 = 1.5

No, because it takes O(1) time to access an element in array and it does not depend on a position of this element in array. So arr[0] and arr[10000] should take the same amount of time.
If you will have something like a linked list or a binary tree, then it makes sense to put elements that are accessed with higher probability closer to the beginning.

Related

Task about pairs of points and segments [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can you help me with problem? Given N <= 10^5 pairs of points, suppose they are written in
array A, A[i][0] <= A[i][1]. Also, given M <= 10^5 pairs of segments, i-th pair given in form L_1[i], R_1[i], L_2[i], R_2[i]. For each pair of segments I need to find number of pairs from array A, such that for each pair (A[z][0], A[z][1]) it should be L_1[i] <= A[z][0] <= R_1[i] <= L_2[i] <= A[z][1] <= R_2[i].
I think here we can use scan line algorithm, but I don't know how to fit in time and memory. My idea works in N * M * log(N).
If you map A[i] to a point (A[i][0], A[i][1]) on 2d-plane, then for each segment, basically you're just counting the number of points inside the rectangle whose left-bottom corner is (L_1[i], L_2[i]) and right-top corner is (R_1[i], R_2[i]). Counting the points on 2d-plane is a classic question which could be solved in O(n logn). Here are some possible implementations:
Notice that number of points in a rectangle P(l,b,r,t) could be interpreted as P(0,0,r,t)-P(0,0,l-1,t)-P(0,0,r,b-1)+P(0,0,l-1,b-1), so the problem can be simplified to calculating P(0,0,?,?). This could be done easily if we maintain a fenwick tree during the process which basically resembles scan line algorithm.
Build a persistent segment tree for each x-coordinate (in time O(n logn)) and calculate the answers for segments (in time O(m logn)).
Build a kd-tree and answer each query in O(sqrt(n)) time. This is not efficient but could be useful when you want to insert points and count points online.
Sorry for my poor English. Feel free to point out my typos and mistakes.

Possible Array combinations based on constraints [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.

What's the minimal column sums difference? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Imagine you are given a matrix of positive integer numbers (maximum 25*15, value of number does not exceed 3000000). When you do column sums and pick the smallest and the largest one, the difference between them must be the smallest possible.
You can swap numbers in every row (permute rows), not in column, how many times you want.
How would you solve this task?
I'm not asking for your code but your ideas.
Thanks in advance
I would make an attempt to solve the problem using Simulated Annealing. Here is a sketch of the plan:
Let the distance to optimize the difference between the max and min column sums.
Set the goal to be 0 (i.e., try to reach as close as possible to a matrix with no difference between sums)
Initialize the problem by calculating the array of sums of all columns to their current value.
Let a neighbor of the current matrix be the matrix that results from swapping two entries in the same row of the matrix.
Represent neighbors by their row index and two swapping column indexes.
When accepting a neighbor, do not compute all sums again. Just adjust the array of sums in the columns that have been swapped and by the difference of the swap (which you can deduce from the swapped row index)
Step 6 is essential for the sake of performance (large matrices).
The bad news is that this problem without the limits is NP-hard, and exact dynamic programming at scale seems out of the question. I think that my first approach would be large-neighborhood local search: repeatedly choose a random submatrix (rows and columns) small enough to be amenable to brute force and choose the optimal permutations while leaving the rest of the matrix undisturbed.

Powers of a half that sum to one [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!

Proposing an O(logm) algorithm for the following [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to propose an algorithm for the following: let us assume that we have an array consisting of zeros and ones. The array is filled with zeros from the beginning of the array to the index m, and all remaining indexes are filled with ones. I need to find this index m in O(logm) time. Here is what i thought: I think this is like binary search, first i look at the middle element of the array, if that is zero, then i forget about the left part of the array and do the same for the right part, and continue like this until i encounter a one. If the middle element is one, then i forget about the right part and do the same for left part of the array. Is this a correct O(logm) solution? Thanks
It is not "like" a binary search - it is a binary search. Unfortunately, it is O(logN), not O(logM).
To find the borderline in O(logM), start from the other end: try positions {1, 2, 4, 8, 16, ... 2^i} and so on, until you hit a 1. Then do a binary search on the interval between 2^i and 2^(i+1), where 2^i+1 is the first position where you discovered a 1.
Finding the first 1 takes O(logM), because the index is doubled on each iteration. After that, the binary search takes another O(logM), because the length of the interval 2^i..2^(i+1) is less than M as well.

Resources