Estimating a probability - probability

For the following question I wanted to know how to estimate the probability that I have selected from bin A.
There are 10 bins, 4 are labelled A, 6 labelled B. Each bin has balls with two colors (Red/ blue). The distribution of red and blue balls in bin A is (0.3 to 0.7) The distribution of red and blue balls in bin B is (0.7 to 0.3). If you randomly draw two balls with replacement and they turn out to be red and blue what is the probability we selected from A?
I am getting (Probability selecting red from any Bin that is A/ Total probability selecting red from all bins)X (Probability selecting blue from any bin that is A/ Total probability of selecting a blue from all bins) =13.7%

Related

How many ways are there to color uncolored edges in a circle

given n points on a circle and all edges (C(2,n)) are drawn. Some of these edges are already colored in blue or red. You should find out how many ways are possible to color rested edges in order to have final picture with conditions below:
all edges are colored.
all triangles have 0 or 2 edges in red.
here are some examples:
example 1
input: n = 3 and 0 number of edges are already colored.
output = 4 : because we can color all edges in blue or only one one of them in blue and rest of them in red.
example 2
input n = 4 and 4 number of edges are already colored
1 2 blue
2 3 blue
3 4 red
4 1 red
output = 1 : because the only way to color rested edges is like below:
1 3 blue
2 4 red
constraints:
3 <= n <= 100,000
time limit : 1 second
memory limit: 256 MB
actually I have no idea about ideal data structure for such question, and I need your help for some clues
Here's a linear-time algorithm.
First, observe that every cycle of a valid coloring contains an even number of red edges (I'll leave this as an exercise). Given the colors of a spanning tree, there exists exactly one valid completion. Uniqueness is easy to prove because the color of each edge not in the tree is determined by the parity of the colors of the tree edges with which it forms a cycle. I'll leave validity as another exercise (pressed for time, sorry).
The algorithm is, use depth-first search to find a spanning forest of the given edges, storing the parity of the edge colors between each node and the root of its tree. Given this data, we can verify the given color of every edge not in the forest. If any is wrong, then there are 0 colorings. Otherwise, there are 2^(number of trees minus one) colorings.

Finding enclosed sections of a graph algorithm

Given a 30 x 30 image of red and green pixels, stored as an array of 0s and 1s with 1 being red and 0 being green.
The image starts out as green and random patterns of red gets drawn on top.
All of the outer most pixels of the image are also painted red.
The question is how to fill every single pockets of green that's not connected to the biggest pocket of green with red?
Get/write a flood-fill algorithm that, starting at some pixel, fills all connected pixels with a different value and, while doing that, counts the pixels.
Have an int biggestSize = 0 and a Point biggestStartPoint = null variable with the given initial values.
Scan the image.
When you get at a green pixel, flood-fill with blue.
If the count of that flood-fill is bigger than biggestSize, flood-fill the old biggest area (from biggestStartPoint) with red. Store the new count and start pixel in biggestSize and biggestStartPoint.
If the count isn't bigger, flood-fill the (now blue) region with red, and leave the variables unchanged.
Finally, flood-fill the biggest area with green.

Three type of Balls from Bag

I have a problem which sounds like this:
We have a bag with balls in. There are R red balls, B blue balls and G green balls.
I need to find the minimum number of extractions from the bag such that i am sure that i will have at least K balls of same colour.
Anyone can help with any idea? Or tips, etc?
If K>max(R,G,B) then the problem has no solution. So let's assume K <= max(R,G,B).
If you don't have any control over which ball gets extracted, you'll need at most (i.e. this is a lower bound) min(R, (K-1))+min(G, (K-1))+min(B, (K-1))+1 extractions for obvious reasons: extract K-1 red balls (or all red balls if R<K), then extract K-1 green balls (or all green balls if G<K), and finally extract K-1 blue balls (or all blue balls if B<K). Now there is at least one ball remaining---because max(R,G,B)>=K by assumption---and when we extract it we have K balls of the same color. (This is clearly the worst case.)
You obviously need at least K extractions (this is the best case).
Since you tagged the question with probability, maybe you can only extract a ball chosen uniformly at random. In that case we can talk of the expected number of extractions needed before we end up with K balls of the same color. This is an interesting question.
You need to make sure the upper bond working... When I test the upper bond, it blows off the int range (I using java)

sorting of different color balls in minimum passes

There are 10,000 balls and may be 500 different colors of ball
Example: There are:
4 - red balls
5900 - Blue balls
3700 - Green balls
396 - mintcream balls
Or there may be 10,000 red balls.
Or all balls are has same range, i.e. 500 red, 500 blue, etc.
We don’t know the range of any ball and number of color balls, but the minimum is 1 color and maximum is 500 different colors, and we have auxiliary array of size 500. how to arrange all same color ball together in minimum passes over balls and in minimum swap? is it possible to do in less than two passes ??
You need one pass (in pseudocode with OO syntax):
for (ball in balls) {
array[array.getIndexByName(ball.color)].add(ball);
}
where getIndexByName returns the slot assigned to a certain color.
If no slot has been assigned to the color in question, a new slot is assigned.
Assuming a naive implementation of getIndexByName the complexity is O(number of balls * number of colors).
First you go over all the balls and count per color the number of occurrences, you can do this using the auxiliary array you have.
Then you know how much space you must reserve per color. So when you go over all balls again you have a pointer to the location they should be going to in the output array since you know how many balls of every color you expect. Don't forget to update the counter of the color after you processed a ball.
By swapping the balls in the input array with the balls at the position they should go to you can get an algorithm of O(n) swaps.
An example:
The first part is simple, say you have 10 balls, 3 red, 4 blue, 2 green and 1 yellow ball.
The second part takes your input array: red,blue,green,yellow
For simplicity say we order them in the order:
You start with the first ball. If it has color blue we have to swap it with the ball at location 4 in the array (since all balls before that have to be red. The new location we have to swap a blue ball to has to be updated in the auxilary array and should be 5 now. Then if the ball we swapped with is green we swap it with the ball at location 7. Now if the ball is red after the swap we can continue with the next ball. You can of course skip the balls you have swapped before. It is important to note that you swap a ball at most two times using this technique and you swap them to a known location, hence you don't need an additional pass.
you could do it in less than two passes, but it would be more complicated.
Here is how i would do it-
pass #1- determine number of balls for each color and number of colors
pass #2- for every 3 colors, (example 1, 4, 5, balls respectively=put a marker in 10th spot) and use the dutch color flag problem here

Is this problem NP-hard?

I'm trying to come up with a reasonable algorithm for this problem:
Let's say you have a bunch of balls. Each ball has at least one color, but can also be multicolored. Each ball also has a number on it. There are also a bunch of boxes which are each only one color. The goal is to maximize the sum of the numbers on the balls in the boxes, and the only rules are:
in order to place a ball in a box, it
has to at least have the box's color
on it
you can only put one ball in each
box.
For example, you can put a blue and green ball into a blue box or a green box, but not into a red box.
I've come up with a few optimizations that help a lot in terms of running time. For example, you can sort the balls in descending order of point value. Then as you go from highest number to lowest, if the ball only has one color, and there are no other higher-point balls that contain that color, you can put it in that box (and thus remove that box and that ball from the remaining combinations).
I'm just curious is there's some kind of dynamic algorithm for this type of problem, or if it's just the traveling salesman problem in disguise. Would it help if I knew there were at most X colors? Any help is greatly appreciated. Thanks!
Edit - here's a simple example:
Balls:
1 red ball - 5 points
1 blue ball - 3 points
1 green/red ball - 2 points
1 green/blue ball - 4 points
1 red/blue ball - 1 point
Boxes:
1 red
1 blue
1 green
Optimal Solution:
red ball in red box
blue ball in blue box
green/blue ball in green box
Total value: 12 points (5 + 3 + 4)
This is a special case of the maximum weight matching problem on a weighted bipartite graph. Construct a graph whose left vertices correspond to balls, whose right vertices correspond to boxes and with the edge joining a ball and a box having weight V where V is the number on the ball if the ball can be placed in the box, and 0 otherwise. Add extra boxes or balls joined to the other side with edges of weight zero until you have the same number of vertices on each side. The assignment you're looking for is determined by the set of edges of nonzero weight in the maximum (total) weight matching in the resulting graph.
The assignment algorithm can be solved in O(n^3) time, where n is here the maximum of the number of balls or boxes, using the Hungarian algorithm. (BTW, I should make the disclaimer that I only mention the Hungarian algorithm because it is the theoretical result I happen to be familiar with and it presumably answers the question in the title of whether the original problem is NP-hard. I have no idea whether it is the best algorithm to use in practice.)
Have you tried a greedy alg?
Sort by points/value and place in box if possible.
If there are any exceptions im missing id like to see them.

Resources