Minimum cost for colouring the house? - algorithm

I came across below problem
There are N houses in a row. Each house can be painted in either Red,
Green or Blue color. The cost of colouring each house in each of the
colours is different. Find the colour of each house such that no two
adjacent houses have the same colour and the total cost of colouring
all the houses is minimum.
Here is the complete question
The problem to me looks bit confusing, as objective is to minmize the cost and ensure that no adjacent house have same colour. In that case should not i select
the just two colours out of three whose cost is minimum.
Say here is the cost of colours
Red $100
Green $200
Blue $300
I have 5 row houses to paint
Here will be my Algo
Select Red and Green colour as their cost is minimum
calculate 5%2.
If 5%2 == 1 then start from last and select the colour of last house as Red($100). Now choose the alternate colour
If 5%2 == 0 then start from beginning and choose the alternate colour
I see Is "house coloring with three colors" NP? suggested the dynamic programming But i am not sure what is wrong with my approach and why dynamic programming is required here ?

This is (presumably) a homework problem. I think the intention is to teach you to implement a greedy algorithm:
Paint the first house using the cheapest color ("red").
Paint the next house using the cheapest color available.
The second will alternate between "red" and "green", but you don't have to choose the colors in advance.
If I were assigning this problem, the next problem would be "And solve this assuming you have only enough paint for one red house".

Related

algorithm to find closest object

I need to map blue objects to red ones by distance. The center of each object is known. The yellow and green objects, if they are shown, are hints. They help to decide which red object is more important.
For example, in the situation shown in image below:
The bottom blue object should be mapped to the bottom most right red object since both green and yellow objects are very close to that red object.
The right blue object should be mapped to top-right red object since it's closer to it.
I have a naive solution, but I'm not quite sure what to do instead of "????" below
Do you have any suggestions?
My naive solution in sort of pseudo-code:
for each BLUE:
find group P=(YELLOW_BLUE, GREEN_BLUE and RED_BLUE) when each object in P is the closest to BLUE
vector<RED> redCandidates
for each O in P:
if O is YELLOW OR O is GREEN
find closest RED to O
insert RED to redCandidates
if size of redCandidates is 0 -> return RED_BLUE
else if size of redCandidates is 1 -> return redCandidates[0] since hint has more weight to the decision
else if size of redCandidates is > 1 -> ????
UPDATE1
After looking into Minimum Cost Flow problem suggested by #ldog, I decided to use Hungarian Algorithm. I created bipartite graph where each blue node is connected to each red node and the weights on the edges are the distance between blue and red.
Now, before I solve the graph, I need to apply rewards on the edges where yellow/green are close to red. I don't quite understand how to do that.
Let's say distance between blue 1 and the red 4 is D_1_4 = 10 and the distance between the yellow hint 11 and the red 4 is D_4_11 = 3. So, because D_1_4 > D_4_11, should I just add reward to the edge 1_4? Or, should I add the reward to each edge that enters node 4, meaning edges 1_4, 2_4 and 3_4?
It seems your question is not fully formed and you are looking for a decent formulation of the things that you expressed in words.
Here are some suggestions:
Use intersection over union for dealing with how to assign similarity for overlapping areas.
There are many ways you could try to make what you expressed in words quantified, and therefore able to be optimized. One reasonable way to quantify what you expressed in words is as a minimization problem I will discuss below.
The minimization should assign one blue box to exactly one red box (given what you told me.) The green and yellow boxes are hints and are not included in this constraint, they are simply used to modify which red box is preferential over others. To formalize what you described in words, we have the set of red boxes R and the set of blue boxes B. There are m red boxes and n blue boxes with m >= n. Each pairing of blue box i with red box j has a preference w_{ij} (this preference is pre-calculated accounting for the hint boxes as well as spatial proximity.)
We wish to compute:
max \sum_{i<j} w_{ij}x_{ij}
such that
\sum_{k} x_{ik} = 1, \sum_{l} x_{lj} = 1, x_{ik} \in {0,1}
The variable x_{ij} is 1 if and only if blue box i is assigned to red box j, it is 0 otherwise.
This problem (is Totally Unimodular) and can be solved in polynomial time. In fact, it can be solved as an instance of the common Minimum Cost Flow problem. To do this, define a node in a graph for each blue box i, and a node in a graph for each red box j. Connect each blue node to each red node (directed blue->red) with an edge with weight -w_{ij} and capacity 1. Connect each blue node to the source (directed source -> blue) with an edge of capacity 1 and weight 0. Connect each red node to the sink (directed red->sink) with an edge of capacity 1 and weight 0. Give the source a supply of n and the sink a demand of n. Compute the Minimum Cost Flow on this graph (see for example lemon) and the resulting flow yields the maximum solution (alternatively minimum flow.)
Having described this in detail, I see this is already a common approach [1] to solve exactly problems like yours. Here is an implementation.
YMMV depending on how good you make your weights to be. You may want to try a machine learning approach to determine optimal weights using a ground truth dataset and an iterative refinement. The refinement can be computed for a fixed set of blue and red ground truth boxes by refining the weights w_{ij} until all other possible assignments other than the ground truth have a lower score of the optimization than the ground truth. This can be done iterative using any max-margin learning framework or technique, combined with the method I described above (and apparently is described in [1].)
[1] Zhang, Li, Nevatia: Global data association for multi-object tracking using network flows, CVPR (2008).

Maximization algorithm for ball preferences

I'm trying to devise the most efficient algorithm for a problem, but I'm having some difficulty. If someone could lend a hand, either by proposing an algorithm or classifying the problem so I can do some further research, I would be very appreciative.
The problem is as follows:
There are n (an integer) number of distinct red balls, each of which has its own number, and m number of distinct green balls, each of which has its own corresponding number as well. For example, if n = 3, then there are three red balls named Red Ball 1, Red Ball 2 and Red Ball 3.
There are also two boxes in which the balls can be placed.
Before the balls are placed in the boxes however, x number of people make predictions as to which balls will be placed in which box (either box 1 or box 2). Each person gets one prediction and for each prediction they can guess one ball to be in each box. The only condition is that the ball they guess in box 1 cannot be the same color as the ball they guess to be in box 2. An example prediction would be: "I think that Red Ball 2 will be in box 1 and Green Ball 3 will be in box 2"
After everyone has made their predictions, the balls will be placed in the boxes the maximize the number of predictions that are correct.
The code I must write will be prompted with n, m, and x as well as the predictions and then be asked to return the maximum number of predictions that are correct.
Once again, I am looking for either algorithmic help or help to identify the type of problem this is. I currently have a recursive algorithm running on (n^2), but I need something a little more efficient.
Thanks for your help! Cheers, Mates!

recommended shortest path algorithm from edge to edge

I have a board game for mobile application shown in this link: http://i45.tinypic.com/23u8u1h.png
The starting point can be any cell. the winner is the one who connects his/her vertically (green player) or horizontally (red player).
The nodes are the colored cells. The white cells can be considered weight maybe? I am not sure how to implement it, but when I think of Dijkstra algorithm, I believe that it would take a lot of time to compute till it comes with the correct answer when the board is in this state: http://i50.tinypic.com/35ivofd.png (I have to apply the algorithm on those four green cells)
I want an algorithm that tells me that " http://i48.tinypic.com/28c2ijl.png " black, brown, blue and purple paths are the shortest one within reasonable of time.
Thanks in advance.
I think Dijkstra will work just fine here, especially in conjuction with Union Find algorithm used to connect fields adjacent to each other and filled with color other than white. Edges leading to white fields cost 1, edges leading to own fields cost 0 and edges leading to enemy fields cost infinity (e.g. mn). Then, you aren't going to run Dijkstra on nodes representing individual fields, but on unions of these fields (clusters of non-white colored fields).
Dijkstra will work here in approximately O(mn log^2(mn)), where n and m are dimensions of the board, and Union Find will work in O(log mn) time with simple optimalizations, mentioned at the bottom of the linked article.

Hungarian Rings Puzzle

I'm having a hard time finding an admissible heuristic for the Hungarian Rings puzzle. I'm planing on using IDA* algorithm to solve and am writing the program in Visual Basic. All I am lacking is how to implement the actual solving of the puzzle. I've implemented both the left and right rings into their own arrays and have functions that rotate each ring clockwise and counterclockwise. I'm not asking for code, just somewhere to get started is all.
Here is the 2 ring arrays:
Dim leftRing(19) As Integer
' leftRing(16) is bottom intersection and leftRing(19) is top intersection
Dim rightRing(19) As Integer
' rightRing(4) is top intersection and rightRing(19) is bottom intersection
In the arrays, I store the following as the values for each color:
Red value = 1 Yellow = 2 Blue = 3 and Black = 4
I suggest counting "errors" in each ring separately - how many balls need to be replaced to make the ring solved (1 9-color, 1 10-color, one lone ball from a 9-color). At most two balls can be fixed using a rotation, then another rotation is needed to fix another two. Compute the distance of each ring individually = 2n-1 where n is half the amount of bad positions and take the larger of them. You can iterate over all twenty positions when looking for one that has the least amount of errors, but I suppose there's a better way to compute this metric (apart from simple pruning).
Update:
The discussion with Gareth Reed points to the following heuristic:
For each ring separately, count:
the number of color changes. The target amount is three color changes per ring, and at most four color changes may be eliminated at a time. Credits go to Gareth for this metric.
the count of different colors, neglecting their position. There should be: 10 balls of one 10-color, 9 balls of one 9-color and one ball of the other 9-color. At most 2 colors can be changed at a time.
The second heuristic can be split into three parts:
there should be 10 10-balls and 10 9-balls. Balls over ten need to be replaced.
there should be only one color of 10-balls. Balls of the minor color need to be replaced.
there should be only one ball of a 9-color. Other balls of the color need to be replaced. If all are the same color, and 9-color is not deficient, one additional ball need to be replaced.
Take the larger of both estimates. Note that you will need to alternate the rings, so 2n-1 moves are actually needed for n replacements. If both estimates are equal, or the larger one is for the latest moved ring, add an additional one. One of the rings will not be improved by the first move.
Prune all moves that rotate the same ring twice (assuming a move metric that allows large rotations). These have already been explored.
This should avoid all large local minima.

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