Dynamic programming problem for minimum cost [closed] - algorithm

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a cell tower question. There are n towns. We want to build cell tower in some of the towns. Each cell tower can cover itself and its neighbor. Each town has a cost to build cell tower. We want to find out the minimum cost to build the cell tower to cover all towns.
For example,
(1)
TOWN 1 2 3
COST 5 1 2
We select to build cell tower in town-2. The cost is 1.
(2)
TOWN 1 2 3 4
COST 5 1 2 3
We select to build cell tower in town-2/3. The cost is 1+2=3.
(3)
TOWN 1 2 3 4
COST 5 1 3 2
We select to build cell tower in town-2/4. The cost is 1+2=3.
It's a dynamic programming algorithm. How can I solve it?
Thanks
Ling

I'd go with something among the following lines:
f(0,_) = 0
f(1,true) = 0
f(1,false) = cost[1]
f(x,true) = min{ f(x-1,true) + cost[x], f(x-1,false) }
f(x,false) = min { f(x-1,true) + cost[x], f(x-2,true) + cost[x-1]}
The idea is:
x is the current number of city we are looking at, and the boolean is true if this city is already covered (by the city from the left).
f(0,_) is an empty base clause - it is free to cover nothing.
f(1,false) is base where city 1 is not covered, so you must put a tower there, and f(1,true) is a base where city 1 is covered, so you don't need a tower and you are done.
f(x,true) is the case that the city x is already covered, so you can put there a tower, and continue to the next city [which is also covered - f(x-1,true)], or don't put a tower there, and the next city is not covered [f(x-1,false)]
f(x,false) is the case where city x is not covered, so you basically have two choices, or put a tower in there, and then continue to f(x-1,true). Or put a tower in the next city (in x-1) and then continue to f(x-2,true)
Start with f(x,false), where x is the last city, and you'll get the minimal solution.
It is easy to see that this recursive formula can easily be modified to DP.

Related

Locker Room Algorithm [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
Note: Just to give a heads up, this is not an assignment for my school since I myself don't even know which school wrote this question. Hopefully no misunderstanding occurred.
I found this interesting questions about locker room:
The locker room of the Rec has N lockers that are labeled 1, 2, . . ., N .
Each locker is locked, but can be opened using its unique key.
Copies of the key to each locker are in its adjacent lockers; i.e. a
copy of the key to locker i is placed in locker i + 1 and i − 1 (the
key to locker 1 is only in locker 2 and the key to locker N is only in
locker N − 1).
T tennis balls are inside T distinct lockers (and you
know which of the lockers they are in). You are given keys to M of
the lockers and your goal is to collect all of the tennis balls by
opening the least number of lockers.
For pictures you can see it directly to the file here.
I have to give this questions to some freshman but I wanted to make sure first that I myself already have the correct answer beforehand.
What I'm thinking is that:
Need to check the ball one by one. So for each ball (ignore the other balls), each key have to visit by traversing to the designated ball. For each key, calculate the steps it takes to visit the ball. The smallest result is stored in an variable called "total steps".
Do this exact thing to the next ball and when I get the minimum steps for this current ball. I add this value to "total steps".
Special condition applied if there is a ball above the key, then key start traversing from i + 1 and i - 1.
My question is: am I right? I don't want to share the wrong algorithm to others since it's unprofessional. Looking forward to any comments, suggestions and inputs.
Your algorithm will not result in minimum number of steps. You can not consider balls independently. Lets consider the following case: You have only one key for locker number 1 and balls are in lockers 12, 10, 8, 6, 4, 2. If You consider the balls in the order I have given you will end up with total steps equal to 11 + 9 + 7 + 5 + 3 + 1, while you can solve the problem in a single pass of 11 steps. You should not ignore the boxes visited on the previous steps.
Here is a technique you can use:
Consider each locker (in which there is a ball, of which you have a key and which have none) to be a node in the graph. The nodes will be of two types:
A) Lockers having balls
B) Lockers of which you have keys.
C) Lockers which have none
Consider all lockers of type-A to be closed and type-B to be open.
From all open lockers, find paths to all closed lockers (from locker type-B) and open the locker in A with min path length. Also, open all lockers of type C which fall in this min-path and move them from category-C to B.
Repeat above step till all lockers in A are opened. Sum of all the min-path lengths that you have come across will be your answer.

Minimum numbers of attacks needed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We are given 2 Dimensional grid of cells.Each cell may or may not contain a monster.
We are given a list of cells that contain monsters.
In a single attack we can kill all the monsters standing in a row or in a column. We
need to tell the minimum number of attacks that will be require to destroy all the monsters.
Constraints:
1 ≤ N ≤ 1000
1 ≤ X, Y ≤ 10^9
Example:
Input:
3
0 0
1 0
0 1
Output:
2
How to approach this problem..??
This can be modelled as a graph problem.
Create a graph node for each row and column where there's a monster.
Connect the nodes if a monster is on that row and that column.
This is a bipartite graph, and you want to do minimum vertex cover. König's theorem shows that for bipartite graphs the problem is equivalnt with the maximum matching problem which can be solved in polinomial time:
http://en.wikipedia.org/wiki/Maximum_matching#Maximum_matchings_in_bipartite_graphs
This reminds me the Set Cover Problem:
You have a set U that stores the N monsters: U = {1, 2, ..., N}, a set S of possible attacks. Each attack is as well a set of the indexes of the monster that the attack kills. In your example, S is S = { {1}, {2}, {}, {1}, {2} }. You have to find the smallest set C in S whose union is U.

Which algorithm is fastest in walking in binary tree to any given node at a any level of tree [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
for example:-there are computers connected in a binary tree structure. There are 20 rooms consider each room as a level in the binary tree starting from room 0 (level 0) in which the main computer is placed and all its children nodes (computers) are in room 1 (level 1) and so on. Each computer is numbered according to its place in the room like computer1 , computer 2, computer3 and computer4 (maximum number of computers in room2 is equals to the 2 raised to the power 2)in room 1 (level 1).Now starting from the root if I want to get to the computer756 in room 12 what is the fastest algorithm or method.
considering the above image as an example level 4 has total 16 nodes (1 to 16 say)that is 2 raised to the power 4. If the tree is huge (say tree with 50 levels) which is the fastest algorithm to access the node numbered 1,099,511,628,800 at level 50
If I understand your question correctly, given N and L you want to find a path from the root to the Nth node in the Lth level. I'll assume that the nodes in each level are numbered left-to-right, and that N and L are zero-based.
This is simple if you use the binary representation of N: Represent N as a binary number of L bits. Now go through these bits from most significant to least significant. A 0 means that you need to go to the left child, and a 1 means that you need to go the the right child.
For example, to find node #3 in level #3: The node number in binary is 011. So from the root, you go left, right, right.

States graph for the '3 jugs of water' [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Regarding the 3 jugs of water problem:
we have 3 water jugs that the capacity of first jug is 12 and capacity of second jug is 8 and capacity of third jug is 3.
the initial state is: (0,0,0)
the successor function is:
Add: completely fill a jag
Pour to another one: Pour the content of one jug two a second one (until the first is empty or second is completely full)
Empty is: empty a jar from all its content
The goal state is: (1,1,1)
i want to draw its state tree. i did it by myself but i'm not really sure that it is right or not?
(0,0,0)
/ | \
/ | \
/ | \
(12,0,0) (0,8,0) (0,0,3)
the child node for(12,0,0) is: (12,0,0),(12,8,0),(12,8,3),(0,8,3),(0,0,3),(0,0,0),(9,8,3),(12,8,0),(4,8,3),(12,0,3),(12,5,3),(12,5,3),(12,8,0)
which (12,0,0),(0,0,0)==>because it is in root,(12,8,0)==>
are fail node and we don't expand them.
i think if i expand (0,0,3), i will reach to my goal state:
the child for the node (0,0,3): (3,0,0),(0,3,0),(0,0,3),(1,1,1) (1,1,1) is the goal state am i right?
Question: Am I understanding it correctly? Are these the states and generated tree?
The graph is correct for the first step, however - you expand the siblings (12,0,0), (0,8,0) and (0,0,3) wrong.
You should do a single step, not multiple in each iteration, and not try to do many steps.
Thus:
successors((12,0,0)) = { (12,0,3), (12,8,0), (0,0,0), (9,0,3), (4,8,0) }
successors((0,8,0)) = { (12,8,0), (0,8,3), (8,0,0), (0,5,3), (0,0,0) }
successors((0,0,3)) = { (12,0,3), (0,8,3), (3,0,0), (0,3,0), (0,0,0) }
(From each state, you can only do 1 allowed operation, not more - to get the successors/following states).
By keep expanding these, you will get eventually all possibilities.
FYI, this problem is sometimes known as The Die Hard Problem, and is a classic example of problem solving using reductions to graph algorithms by building the states graph and running a pathfinding algorithm, such as A* or BFS.

find number of tennis matches required [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Hi I came across this question from my friend.
Give me a generalised formula to find how many tennis matches (singles) are required for n players ?
Example : if the number of players are 16 then
first : we need 8 mataches (for 16 players) , here 8 players will be eliminated and 8 players will be there
secode : we need 4 matches (for 8 players) , here again 4 players will be eliminated and 4 will be remaining
third :
we need 2 matches (for 4 players) , here again 2 players will be eliminated and 2
will be remaining
Final :
we need 1 macth to decide a winner among the 2 players
so totally 15 matches are required.
I need a generalised formula to find , such that if I give the value n I should get the number of matches required to find the winner
n may be odd or even
For elimination game, the number of matches is always n-1, because one player will be eliminated after one game and n-1 players have to be eliminated in total.
As eventually every player but 1 (the champion) has to lost his match (and every player can lost only in 1 match) then the number of matches required is n-1
n-1, because there is one player to leave after each game. and the champion remains

Resources