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

Path Sum: Three Ways

NOTE: This problem is a more challenging version of Problem 81.

The minimal path sum in the 5 by 5 matrix below, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right, is indicated in red and bold; the sum is equal to 994.

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

Find the minimal path sum from the left column to the right column 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>
#include <climits>
int path_sum_three_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(80, std::vector<int>(80, INT_MAX));
for (int i = 0; i < 80; ++i) {
dp[i][0] = matrix[i][0];
}
for (int j = 1; j < 80; ++j) {
// from left
for (int i = 0; i < 80; ++i) {
dp[i][j] = dp[i][j - 1] + matrix[i][j];
}
// from up and down
for (int i = 1; i < 80; ++i) {
dp[i][j] = std::min(dp[i][j], dp[i - 1][j] + matrix[i][j]);
}
for (int i = 78; i >= 0; --i) {
dp[i][j] = std::min(dp[i][j], dp[i + 1][j] + matrix[i][j]);
}
}
int min_sum = INT_MAX;
for (int i = 0; i < 80; ++i) {
min_sum = std::min(min_sum, dp[i][79]);
}
return min_sum;
}

Solution Notes

Mathematical Background

This extends Problem 81 by allowing movement in three directions (up, down, right) and requiring paths to start anywhere in the left column and end anywhere in the right column. It requires finding the minimum path sum across all possible start and end positions.

Algorithm Analysis

The dynamic programming approach processes each column sequentially. For each column, it first computes paths coming from the left, then relaxes the values by considering paths coming from above and below within the same column. This ensures all possible movements are considered.

Time complexity: O(N³) where N=80, due to the nested loops and relaxation sweeps per column. Space complexity: O(N²) for the DP table.

Alternative approaches like Dijkstra’s algorithm (as in Python) work but are slower for this problem size.

Performance

Execution times vary by implementation: fastest in Rust (~5ms), moderate in C++/Go/Java (~10ms), slower in JavaScript (~50ms) and Python (~100ms with Dijkstra).

Key Insights

  • The relaxation technique allows efficient handling of vertical movements within each column.
  • Starting from left-to-right column processing ensures dependencies are resolved.
  • Multiple algorithms can solve this - DP is optimal for grid constraints.

Educational Value

Demonstrates advanced DP techniques for multi-directional grid problems, including relaxation methods for constraint satisfaction.

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