Advertisement

Find Edges in Shortest Paths - LeetCode 3123 Solution

Find Edges in Shortest Paths - Complete Solution Guide

Find Edges in Shortest Paths is LeetCode problem 3123, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an undirected weighted graph of n nodes numbered from 0 to n - 1 . The graph consists of m edges represented by a 2D array edges , where edges[i] = [a i , b i , w i ] indicates that there is an edge between nodes a i and b i with weight w i . Consider all the shortest paths from node 0 to node n - 1 in the graph. You need to find a boolean array answer where answer[i] is true if the edge edges[i] is part of at least one shortest path. Otherwise, answer[i] is false . Return the arra

Detailed Explanation

The problem asks us to find all edges that lie on at least one shortest path between node 0 and node n-1 in a given undirected weighted graph. The input is the number of nodes 'n' and a list of edges, where each edge is represented by a tuple (u, v, w), denoting an edge between nodes u and v with weight w. The output is a boolean array 'answer' of the same length as the 'edges' array. answer[i] is true if edge edges[i] belongs to at least one shortest path from node 0 to n-1, and false otherwise. The graph may not be connected, and there are no repeated edges.

Solution Approach

The solution uses Dijkstra's algorithm to find the shortest distances from node 0 to all other nodes (dist_from_0) and from node n-1 to all other nodes (dist_from_n_1). Then, it iterates through each edge and checks if that edge can be part of a shortest path between node 0 and node n-1. The core idea is that an edge (u, v, w) is on a shortest path from 0 to n-1 if and only if the shortest distance from 0 to u, plus the weight of the edge (u, v), plus the shortest distance from v to n-1 equals the shortest distance from 0 to n-1 (and similarly for the path from 0 to v, through edge (v, u) to n-1).

Step-by-Step Algorithm

  1. Step 1: Build an adjacency list representation of the graph from the given edge list.
  2. Step 2: Use Dijkstra's algorithm to compute the shortest distances from node 0 to all other nodes. Store the result in dist_from_0.
  3. Step 3: Use Dijkstra's algorithm to compute the shortest distances from node n-1 to all other nodes. Store the result in dist_from_n_1.
  4. Step 4: Calculate the shortest distance between node 0 and node n-1, using the result from dist_from_0.
  5. Step 5: Create a boolean array 'answer' to store the results. Initialize all elements to false.
  6. Step 6: Iterate through each edge (u, v, w) in the 'edges' list. For each edge, check if dist_from_0[u] + w + dist_from_n_1[v] == shortest_path_len OR dist_from_0[v] + w + dist_from_n_1[u] == shortest_path_len. If either condition is true, then the edge is part of at least one shortest path, so set answer[i] to true.
  7. Step 7: Return the 'answer' array.

Key Insights

  • Insight 1: We need to find the shortest distance between node 0 and node n-1. Dijkstra's algorithm is the standard approach for finding shortest paths in a weighted graph.
  • Insight 2: Once the shortest distance between node 0 and node n-1 is known, we can determine if an edge (u, v, w) lies on any shortest path by checking if dist(0, u) + w + dist(v, n-1) == shortest_distance or dist(0, v) + w + dist(u, n-1) == shortest_distance. This leverages the optimal substructure property of shortest paths.
  • Insight 3: The graph may be disconnected. Thus the shortest distance between 0 and n-1 might be infinite. This must be checked.

Complexity Analysis

Time Complexity: O(m + n + m*log(n))

Space Complexity: O(n + m)

Topics

This problem involves: Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path.

Companies

Asked at: WeRide.