|
1 |
| -package com.thealgorithms.graphs; |
| 1 | +package com.thealgorithms.graph; |
2 | 2 |
|
3 |
| -import java.util.Deque; |
4 | 3 | import java.util.ArrayDeque;
|
5 | 4 | import java.util.Arrays;
|
| 5 | +import java.util.Deque; |
6 | 6 | import java.util.List;
|
7 | 7 |
|
8 | 8 | /**
|
9 |
| - * 0-1 BFS for shortest paths on graphs with 0/1 edge weights. |
| 9 | + * 0-1 BFS for shortest paths on graphs with edges weighted 0 or 1. |
10 | 10 | *
|
11 |
| - * <p>Time: O(V + E). Space: O(V). |
| 11 | + * <p>Time Complexity: O(V + E). Space Complexity: O(V). |
12 | 12 | *
|
13 | 13 | * <p>References:
|
14 | 14 | * <ul>
|
|
18 | 18 | public final class ZeroOneBfs {
|
19 | 19 |
|
20 | 20 | private ZeroOneBfs() {
|
21 |
| - // utility class |
| 21 | + // Utility class; do not instantiate. |
22 | 22 | }
|
23 | 23 |
|
24 | 24 | /**
|
25 |
| - * Computes shortest distances from {@code src} in a graph with edges weighted 0 or 1. |
| 25 | + * Computes shortest distances from {@code src} in a graph whose edges have weight 0 or 1. |
26 | 26 | *
|
27 |
| - * @param n number of vertices labeled [0..n-1] |
28 |
| - * @param edges adjacency list where each entry is (to, weight) with weight ∈ {0,1} |
29 |
| - * @param src source vertex |
30 |
| - * @return distances array; {@code Integer.MAX_VALUE} if unreachable |
| 27 | + * @param n the number of vertices, labeled {@code 0..n-1} |
| 28 | + * @param adj adjacency list; for each vertex u, {@code adj.get(u)} is a list of pairs |
| 29 | + * {@code (v, w)} where {@code v} is a neighbor and {@code w} is 0 or 1 |
| 30 | + * @param src the source vertex |
| 31 | + * @return an array of distances; {@code Integer.MAX_VALUE} denotes unreachable |
| 32 | + * @throws IllegalArgumentException if {@code n < 0}, {@code src} is out of range, |
| 33 | + * or any edge has weight other than 0 or 1 |
31 | 34 | */
|
32 |
| - public static int[] shortestPaths(int n, List<List<int[]>> edges, int src) { |
| 35 | + public static int[] shortestPaths(int n, List<List<int[]>> adj, int src) { |
| 36 | + if (n < 0 || src < 0 || src >= n) { |
| 37 | + throw new IllegalArgumentException("Invalid n or src"); |
| 38 | + } |
33 | 39 | int[] dist = new int[n];
|
34 | 40 | Arrays.fill(dist, Integer.MAX_VALUE);
|
35 | 41 | Deque<Integer> dq = new ArrayDeque<>();
|
| 42 | + |
36 | 43 | dist[src] = 0;
|
37 | 44 | dq.addFirst(src);
|
38 | 45 |
|
39 | 46 | while (!dq.isEmpty()) {
|
40 | 47 | int u = dq.pollFirst();
|
41 |
| - for (int[] e : edges.get(u)) { |
| 48 | + List<int[]> edges = adj.get(u); |
| 49 | + if (edges == null) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + for (int[] e : edges) { |
| 53 | + if (e == null || e.length < 2) { |
| 54 | + continue; |
| 55 | + } |
42 | 56 | int v = e[0];
|
43 | 57 | int w = e[1];
|
| 58 | + if (v < 0 || v >= n) { |
| 59 | + continue; // ignore bad edges |
| 60 | + } |
| 61 | + if (w != 0 && w != 1) { |
| 62 | + throw new IllegalArgumentException("Edge weight must be 0 or 1"); |
| 63 | + } |
44 | 64 | int nd = dist[u] + w;
|
45 | 65 | if (nd < dist[v]) {
|
46 | 66 | dist[v] = nd;
|
|
0 commit comments