From c13b6aa6ede1b92ff18980f22836d1947468fdf9 Mon Sep 17 00:00:00 2001 From: sayak119 Date: Thu, 5 Oct 2017 00:41:15 +0530 Subject: [PATCH] Added Minimum cost path algorithm and updated README --- README.md | 3 ++ dp/min_cost_path/README.md | 6 +++ dp/min_cost_path/c++/min_cost_path.c++ | 54 +++++++++++++++++++++++++ dp/min_cost_path/min_cost_path.png | Bin 0 -> 4793 bytes 4 files changed, 63 insertions(+) create mode 100644 dp/min_cost_path/README.md create mode 100644 dp/min_cost_path/c++/min_cost_path.c++ create mode 100644 dp/min_cost_path/min_cost_path.png diff --git a/README.md b/README.md index 9c38e03eba..5efc5324a8 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ Clean examples implementations of data structures and algorithms written in diff * [k-means](cluster_analysis/k-means) * [Dynamic Programming](dp) * [kadane algorithm](dp/kadane-_algorithm) + * [knapsack algorithm](dp/knapsack_problem) + * [levenshtein distance](dp/levenshtein_distance) + * [minimum cost path](dp/min_cost_path) ## Contribution * Contributions are always welcome. Language doesn't matter. Just make sure you're implementing an algorithm. diff --git a/dp/min_cost_path/README.md b/dp/min_cost_path/README.md new file mode 100644 index 0000000000..8c3fda9b3b --- /dev/null +++ b/dp/min_cost_path/README.md @@ -0,0 +1,6 @@ +### Minimum Cost Path algorithm + +Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell (i, j), cells (i+1, j), (i, j+1) and (i+1, j+1) can be traversed. You may assume that all costs are positive integers. + +#### A graphical example of Minimum Cost Path algorithm +![](min_cost_path.png) diff --git a/dp/min_cost_path/c++/min_cost_path.c++ b/dp/min_cost_path/c++/min_cost_path.c++ new file mode 100644 index 0000000000..231d3e58b1 --- /dev/null +++ b/dp/min_cost_path/c++/min_cost_path.c++ @@ -0,0 +1,54 @@ +#include +#include +#define R 3 +#define C 3 + +int min(int x, int y, int z); + +int minCost(int cost[R][C], int m, int n) +{ + int i, j; + + // Instead of following line, we can use int tc[m+1][n+1] or + // dynamically allocate memoery to save space. The following line is + // used to keep te program simple and make it working on all compilers. + int tc[R][C]; + + tc[0][0] = cost[0][0]; + + /* Initialize first column of total cost(tc) array */ + for (i = 1; i <= m; i++) + tc[i][0] = tc[i-1][0] + cost[i][0]; + + /* Initialize first row of tc array */ + for (j = 1; j <= n; j++) + tc[0][j] = tc[0][j-1] + cost[0][j]; + + /* Construct rest of the tc array */ + for (i = 1; i <= m; i++) + for (j = 1; j <= n; j++) + tc[i][j] = min(tc[i-1][j-1], + tc[i-1][j], + tc[i][j-1]) + cost[i][j]; + + return tc[m][n]; +} + +/* A utility function that returns minimum of 3 integers */ +int min(int x, int y, int z) +{ + if (x < y) + return (x < z)? x : z; + else + return (y < z)? y : z; +} + +// Test program +int main() +{ + int cost[R][C] = { {1, 2, 3}, + {4, 8, 2}, + {1, 5, 3} }; + printf(" %d ", minCost(cost, 2, 2)); + return 0; +} diff --git a/dp/min_cost_path/min_cost_path.png b/dp/min_cost_path/min_cost_path.png new file mode 100644 index 0000000000000000000000000000000000000000..d1044e0036fc3c05db26d2cf5ef3cdf938aec277 GIT binary patch literal 4793 zcmb7|XEa>VyT)h6kkLmQy^Ya_2qK7#I$E?*hbTh?5uIq!MIF5*h%O;|PxKxwgd{|i zNJQ@hK}hcTuY14UweDK?!zp{6_w2Qw{o(gK`;9Zu)1ancqX2Zq)%3=J$h^GHP7kJ3M+#Up? z*V9r{HuksPwYqJ=CkJo&Ez}+zmdx;+;rZ7gwRAlP70qFR7cTTLPZ{B<9g-kO5Cj7` z*fYUDo+oQ>Uzw@AE3&!o@-EQ$%A{L6IgO>OtBd9U4CeVB-lNa;7{>$yg;Rj*U?CuA z5FLmZM5h+a148s(3k$mrA*5c9D5r(?AkRSX11t!AgoU4kgYZyp5Tpx6rK@KZ(_iGS z2vn5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