BackeasyGraphsTCSSwiggy

Maximized Network Stream Analyzer 2 Solution

Problem Statement

You are tasked with optimizing data throughput in a distributed sensor network. The network is modeled as a directed graph with N nodes, where each node represents a sensor station. The connectivity and latency between stations are defined by an N x N adjacency matrix dist, where dist[i][j] represents the initial transmission cost from node i to node j. A value of -1 indicates no direct connection, and 0 indicates the same node. Your objective is to compute the maximized network stream, which corresponds to finding the shortest path between every pair of nodes to minimize total latency. Use the Floyd-Warshall algorithm to update the distance matrix, accounting for intermediate nodes that may reduce the path cost. Return the final distance matrix after all possible intermediate nodes have been considered.

Example 1
Input
dist = [[0, 3, -1], [-1, 0, 1], [3, -1, 0]]
Output
[[0, 3, 4], [4, 0, 1], [3, 4, 0]]

Explanation: Initially, dist[0][2] is -1 (no direct path). However, path 0->1->2 has cost 3+1=4, which is valid. Similarly, dist[1][0] becomes 1+3=4 via node 1->2->0? No, 1->2 is 1, 2->0 is 3, so 1->2->0 is 4. dist[2][1] becomes 3+1=4 via 2->0->1. The final matrix reflects these shortest paths.

Example 2
Input
dist = [[0, 5, 10], [5, 0, 3], [10, 3, 0]]
Output
[[0, 5, 8], [5, 0, 3], [8, 3, 0]]

Explanation: The direct path from 0 to 2 is 10. However, going through node 1 (0->1->2) costs 5+3=8, which is shorter. Thus, dist[0][2] updates to 8. Similarly, dist[2][0] updates to 8. Other paths remain unchanged as they are already optimal.

Example 3
Input
dist = [[0, -1, 2], [-1, 0, 1], [2, 1, 0]]
Output
[[0, 3, 2], [3, 0, 1], [2, 1, 0]]

Explanation: dist[0][1] is initially -1. The path 0->2->1 costs 2+1=3, so dist[0][1] becomes 3. dist[1][0] is initially -1. The path 1->2->0 costs 1+2=3, so dist[1][0] becomes 3. All other values remain as they are already the shortest paths.

Constraints

  • 1 <= N <= 100
  • 0 <= dist[i][j] <= 10^4 or dist[i][j] == -1
  • dist[i][i] == 0 for all i
  • The graph is directed and may contain negative weights (though not in this specific easy variant, the algorithm supports it)
  • No negative cycles exist in the input graph
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximized Network Stream Analyzer 2 — Problem Statement & Solution Guide

GraphsEasyFloyd-Warshall
TimeO(N^3)
|
SpaceO(N^2)

Problem Description

You are tasked with optimizing data throughput in a distributed sensor network. The network is modeled as a directed graph with N nodes, where each node represents a sensor station. The connectivity and latency between stations are defined by an N x N adjacency matrix dist, where dist[i][j] represents the initial transmission cost from node i to node j. A value of -1 indicates no direct connection, and 0 indicates the same node. Your objective is to compute the maximized network stream, which corresponds to finding the shortest path between every pair of nodes to minimize total latency. Use the Floyd-Warshall algorithm to update the distance matrix, accounting for intermediate nodes that may reduce the path cost. Return the final distance matrix after all possible intermediate nodes have been considered.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Maximized Network Stream Analyzer 2"

easy

WHY DOES IT MATTER?

All‑pairs path computation is a foundational pattern in network optimization, enabling rapid latency or bandwidth queries, routing table generation, and resilience analysis. Without it, each query would require expensive on‑the‑fly traversal, leading to latency spikes in real‑time systems.

OPTIMIZATION CHALLENGE

The main challenge is reducing the exponential explosion of path enumeration to a polynomial-time dynamic programming recurrence that reuses subproblem solutions, thereby cutting time from O(2^N) to O(N^3).

REAL-WORLD CONNECTION

Think of a global CDN where edge servers need to know the fastest route to any origin server. The CDN pre‑computes latency tables once per topology change, then serves instant lookups to routing engines, just like Floyd‑Warshall pre‑computes all‑pairs costs.

When explaining the algorithm, emphasize the three nested loops: outer loop over intermediate nodes, middle loop over sources, inner loop over destinations. Highlight that each iteration considers whether routing through the intermediate node improves the current best cost.

COMPLEXITY AT A GLANCE

⏱ Time:O(N^3)
💾 Space:O(N^2)

Core Theory — Why This Approach?

The problem reduces to finding, for every pair of sensor stations, the best possible transmission cost (or bandwidth) that can be achieved by traversing intermediate nodes. A naive approach would enumerate all possible paths between two nodes, which is exponential in the number of nodes and infeasible for large networks. The optimal paradigm is the Floyd‑Warshall algorithm, which iteratively considers each node as an intermediate point and updates the best known cost between all pairs in O(N^3) time and O(N^2) space. By treating a missing connection (dist[i][j] == -1) as infinite cost, the algorithm naturally ignores impossible edges while still propagating the best achievable paths through existing links. The key insight is that the optimal path between any two nodes either uses a particular intermediate node or it does not; by testing all intermediates we guarantee optimality without exploring exponential path combinations.

Interview Questions on This Problem

Q1How does Floyd‑Warshall handle negative edge weights, and why is it still safe for this problem?

Floyd‑Warshall can handle negative weights as long as there are no negative cycles. In this sensor network context, transmission costs are non‑negative or represented as -1 for no connection, so the algorithm remains safe and will compute the minimal cost paths correctly.

Q2What is the time complexity of computing the maximum bottleneck path between all pairs of nodes, and how does it differ from standard shortest path computation?

The maximum bottleneck (max‑min) problem can also be solved in O(N^3) using a Floyd‑Warshall variant that updates each pair with the maximum of the current bottleneck and the minimum edge along the new intermediate path. Unlike shortest path, which sums edge weights, this variant propagates the minimum edge on a path, but the overall complexity remains cubic.

Q3In a distributed system, why might you prefer a pre‑computed all‑pairs solution over running Dijkstra from scratch for each query?

Pre‑computing all‑pairs distances with Floyd‑Warshall gives O(1) query time, which is critical when the system must answer thousands of latency or bandwidth queries in real time. Running Dijkstra for each query would be O(N log N) per query, which becomes costly under high query load.

Examples

Example 1

Input

dist = [[0, 3, -1], [-1, 0, 1], [3, -1, 0]]

Output

[[0, 3, 4], [4, 0, 1], [3, 4, 0]]

Explanation: Initially, dist[0][2] is -1 (no direct path). However, path 0->1->2 has cost 3+1=4, which is valid. Similarly, dist[1][0] becomes 1+3=4 via node 1->2->0? No, 1->2 is 1, 2->0 is 3, so 1->2->0 is 4. dist[2][1] becomes 3+1=4 via 2->0->1. The final matrix reflects these shortest paths.

Example 2

Input

dist = [[0, 5, 10], [5, 0, 3], [10, 3, 0]]

Output

[[0, 5, 8], [5, 0, 3], [8, 3, 0]]

Explanation: The direct path from 0 to 2 is 10. However, going through node 1 (0->1->2) costs 5+3=8, which is shorter. Thus, dist[0][2] updates to 8. Similarly, dist[2][0] updates to 8. Other paths remain unchanged as they are already optimal.

Example 3

Input

dist = [[0, -1, 2], [-1, 0, 1], [2, 1, 0]]

Output

[[0, 3, 2], [3, 0, 1], [2, 1, 0]]

Explanation: dist[0][1] is initially -1. The path 0->2->1 costs 2+1=3, so dist[0][1] becomes 3. dist[1][0] is initially -1. The path 1->2->0 costs 1+2=3, so dist[1][0] becomes 3. All other values remain as they are already the shortest paths.

Constraints

  • 1 <= N <= 100
  • 0 <= dist[i][j] <= 10^4 or dist[i][j] == -1
  • dist[i][i] == 0 for all i
  • The graph is directed and may contain negative weights (though not in this specific easy variant, the algorithm supports it)
  • No negative cycles exist in the input graph

Optimal Approach & Strategy

Use Floyd‑Warshall to iteratively consider each node as an intermediate point, updating all pair distances in O(N^3) time and O(N^2) space, which guarantees optimality without enumerating paths.

Brute Force Approach

Enumerate all possible paths between two nodes and compute their total cost, then pick the minimum. This requires exploring an exponential number of paths, leading to infeasible runtimes for large N.

Verified Code Solutions

JavaScript Solution
Time: O(N^3)
function solution(matrix) {
   const n = matrix.length;
   const dp = Array.from({ length: n }, () => Array.from({ length: n }, () => -Infinity));
   for (let i = 0; i < n; i++) {
       for (let j = 0; j < n; j++) {
           dp[i][j] = matrix[i][j];
       }
   }
   for (let k = 0; k < n; k++) {
       for (let i = 0; i < n; i++) {
           for (let j = 0; j < n; j++) {
               dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j]);
           }
       }
   }
   let maxSum = -Infinity;
   for (let i = 0; i < n; i++) {
       for (let j = 0; j < n; j++) {
           maxSum = Math.max(maxSum, dp[i][j]);
       }
   }
   return maxSum;
}

Asked in Top Tech Interviews

TCSSwiggy

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.