Problem solution · Java

Find Edges in Shortest Paths

Find Edges in Shortest Paths: a Java solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Breadth-first search
Source
walkccc LeetCode Solutions
Length
60 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Find Edges in Shortest Paths, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 60 lines of Java from the credited upstream file 3123.java.
  • The implementation visibly relies on sequence storage, work queue.
  • 4 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind Edges in Shortest Paths · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  // Similar to 2203. Minimum Weighted Subgraph With the Required Paths  public boolean[] findAnswer(int n, int[][] edges) {    boolean[] ans = new boolean[edges.length];    List<Pair<Integer, Integer>>[] graph = new List[n];    Arrays.setAll(graph, i -> new ArrayList<>());     for (int[] edge : edges) {      final int u = edge[0];      final int v = edge[1];      final int w = edge[2];      graph[u].add(new Pair<>(v, w));      graph[v].add(new Pair<>(u, w));    }     int[] from0 = dijkstra(graph, 0);    int[] from1 = dijkstra(graph, n - 1);     for (int i = 0; i < edges.length; ++i) {      final int u = edges[i][0];      final int v = edges[i][1];      final int w = edges[i][2];      ans[i] = from0[u] + w + from1[v] == from0[n - 1] || //               from0[v] + w + from1[u] == from0[n - 1];    }     return ans;  }   private static int MAX = 1_000_000_000;   private int[] dijkstra(List<Pair<Integer, Integer>>[] graph, int src) {    int[] dist = new int[graph.length];    Arrays.fill(dist, MAX);     dist[src] = 0;    Queue<Pair<Integer, Integer>> minHeap =        new PriorityQueue<>(Comparator.comparingInt(Pair::getKey)) {          { offer(new Pair<>(dist[src], src)); } // (d, u)        };     while (!minHeap.isEmpty()) {      final int d = minHeap.peek().getKey();      final int u = minHeap.poll().getValue();      if (d > dist[u])        continue;      for (Pair<Integer, Integer> pair : graph[u]) {        final int v = pair.getKey();        final int w = pair.getValue();        if (d + w < dist[v]) {          dist[v] = d + w;          minHeap.offer(new Pair<>(dist[v], v));        }      }    }     return dist;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗