Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #81 medium

Path Sum: Two Ways

In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427.

(131673234103182019634296515063080374642211153769949712195680573252437331)\begin{pmatrix} \color{red}{131} & 673 & 234 & 103 & 18\\\\ \color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & 150\\\\ 630 & 803 & \color{red}{746} & \color{red}{422} & 111\\\\ 537 & 699 & 497 & \color{red}{121} & 956\\\\ 805 & 732 & 524 & \color{red}{37} & \color{red}{331} \end{pmatrix}

Find the minimal path sum from the top left to the bottom right by only moving right and down in matrix.txt (right click and 'Save Link/Target As...), a 31K text file containing an 80 by 80 matrix.

View on Project Euler

Implementations

cpp
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
int path_sum_two_ways() {
std::ifstream file("matrix.txt");
std::vector<std::vector<int>> matrix(80, std::vector<int>(80));
std::string line;
int row = 0;
while (std::getline(file, line) && row < 80) {
std::stringstream ss(line);
std::string token;
int col = 0;
while (std::getline(ss, token, ',') && col < 80) {
matrix[row][col] = std::stoi(token);
++col;
}
++row;
}
std::vector<std::vector<int>> dp = matrix;
for (int j = 1; j < 80; ++j) {
dp[0][j] += dp[0][j - 1];
}
for (int i = 1; i < 80; ++i) {
dp[i][0] += dp[i - 1][0];
for (int j = 1; j < 80; ++j) {
dp[i][j] += std::min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[79][79];
}

Solution Notes

Mathematical Background

This problem involves finding the minimal path sum in an 80x80 grid, where movement is restricted to right and down directions. It’s a classic application of dynamic programming for grid-based path minimization.

Algorithm Analysis

The solution uses dynamic programming with a 2D DP table. We initialize the DP table with the matrix values, then compute the minimum path sum for each cell by taking the minimum of the cell above and the cell to the left, adding the current cell’s value. This ensures each subpath to any cell is optimal.

Time complexity: O(N²) where N=80, as we iterate through all cells once. Space complexity: O(N²) for the DP table, though in-place modification could reduce this.

Performance

The algorithm executes very quickly, typically under 1-10ms depending on the implementation language and file I/O overhead. The DP approach is highly efficient for this problem size.

Key Insights

  • Dynamic programming allows us to break down the problem into smaller subproblems, solving each only once.
  • In-place modification of the matrix can optimize memory usage.
  • File I/O for reading the large matrix is the primary bottleneck in some implementations.

Educational Value

This problem demonstrates the power of dynamic programming for optimization problems on grids. It teaches how to transform a path-finding problem into a systematic computation that guarantees optimality.

Problem #81 is taken from Project Euler and licensed under CC BY-NC-SA 4.0.