Before solving the exercise, we can ask few questions to specify few aspects that might not be clear:
It is clear that finding the minimum iteratively for each house can return a local minimal solution but we are interested in the global minima.
import math
N = 6
K = 4
cost_matrix = [
[1 , 20, 34, 20],
[40, 10, 15, 30],
[50, 1, 20, 40],
[70, 80, 30, 1 ],
[1 , 60, 30, 40],
[70, 50, 1 , 20]
]
def find_house(row, col, tot, min_tot):
if row < N:
tot += cost_matrix[row][col]
for c in range(K):
if c != col:
min_tot = find_house(row+1, c, tot, min_tot)
else:
if min_tot > tot:
min_tot = tot
return min_tot
def main():
print ("Minimum value found:", find_house(0,0,0, math.inf))
#include<stdio.h>
#include<limits.h>
#define K 4
#define N 6
int cost_matrix[N][K] = {
{1 , 20, 34, 20},
{40, 10, 15, 30},
{50, 1, 20, 40},
{70, 80, 30, 1 },
{1 , 60, 30, 40},
{70, 50, 1 , 20}
};
int find_house(int row, int col, int tot, int min_tot)
{
if (row < N)
{
tot += cost_matrix[row][col];
for (int c=0; c<K; c++)
{
if (c != col)
min_tot = find_house(row+1,c, tot, min_tot);
}
}
else
{
if (min_tot > tot)
min_tot = tot;
}
return min_tot;
}
int main()
{
printf("Minimal cost: %d\n", find_house(0, 0, 0, INT_MAX));
return 0;
}
public class BuildHouse {
static int N=6;
static int K=4;
static int cost_matrix[][] = {
{1 , 20, 34, 20},
{40, 10, 15, 30},
{50, 1, 20, 40},
{70, 80, 30, 1 },
{1 , 60, 30, 40},
{70, 50, 1 , 20}
};
public static void main(String[] args)
{
System.out.println("Minimum value found: "+ findHouse(0,0,0,Integer.MAX_VALUE));
}
static int findHouse(int row, int col, int tot, int min_tot)
{
if (row < N)
{
tot += cost_matrix[row][col];
for (int c=0; c<K; c++)
{
if (c != col)
min_tot = findHouse(row+1, c, tot, min_tot);
}
}
else
{
if (min_tot > tot)
min_tot = tot;
}
return min_tot;
}
}
The recursion stops when all the nodes have been explored.