In this post i will explain an interesting algorithm commonly called as House Robber Algorthim.Suppose a robber is planning to steal money from a group of houses.Amount of money each house is having is painted
on each house.But condition is he should not steal from adjacent house means , if he steals from nth house, he should
not steal from (n-1)th and (n+1)th house.How can he steal the maximum amount following the above condition.
We can solve this easily by using dynamic programming with recurssion.
Suppose we have houses in this order : House= 1, 2 , 3, 4 …. n
and A(House) is the amount in the House.
If we select a house with highest amount and start from there max(A(1),A(2),A(3)….) ,chances are we end with the less amount.
Hence for finding the best solution , we maintain 2 meta data at each house .Including(I) and Excluding(E)
Assume n=1,2,3… amount at each house,
I=n+ E(n-1)
E=max[I(n-1),E(n-1)]
After doing a recursive calculation at each house. Then the house with max(I,E) is the house to start the robbery.
Let me explain it more clearly by giving an example.