Problem
You are given an undirected weighted connected graph containing n nodes labeled from 0 to n - 1, and an integer array edges where edges[i] = [ai, bi, wi] indicates that there is an edge between nodes ai and bi with weight wi.
Some edges have a weight of -1 (wi = -1), while others have a positive weight (wi > 0).
Your task is to modify all edges with a weight of -1 by assigning them positive integer values in the range [1, 2 * 109] so that the shortest distance between the nodes source and destination becomes equal to an integer target. If there are multiple modifications that make the shortest distance between source and destination equal to target, any of them will be considered correct.
Return an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from source to destination equal to target, or an empty array if it’s impossible.
Note: You are not allowed to modify the weights of edges with initial positive weights.
Examples
Example 1:
graph TD subgraph Input Graph 0[0] 1[1] 2[2] 3[3] 4[4] 0 ---|"-1"| 3 2 ---|"-1"| 0 4 ---|"-1"| 1 4 ---|"-1"| 3 end subgraph Output Graph A[0] B[1] C[2] D[3] E[4] A ---|"3"| D C ---|"1"| A E ---|"1"| B E ---|"1"| D end style 0 fill:#f96,stroke:#f66,stroke-width:2px; style 1 fill:#9f9,stroke:#0f0,stroke-width:2px; style A fill:#f96,stroke:#f66,stroke-width:2px; style B fill:#9f9,stroke:#0f0,stroke-width:2px;
| |
Example 2:
graph TD
subgraph Input Graph
0[0]:::source
1[1]
2[2]:::destination
0 ---|"-1"| 1
0 ---|"5"| 2
end
subgraph Output Graph
end
style 0 fill:#f96,stroke:#f66,stroke-width:2px
style 2 fill:#9f9,stroke:#0f0,stroke-width:2px
| |
Example 3:
graph TD
subgraph Input Graph
0[0]:::source
1[1]
2[2]:::destination
3[3]
1 ---|"4"| 0
1 ---|"3"| 2
2 ---|"5"| 3
0 ---|"-1"| 3
end
subgraph Output Graph
A[0]:::source
B[1]
C[2]:::destination
D[3]
B ---|"4"| A
B ---|"3"| C
C ---|"5"| D
A ---|"1"| D
end
style 0 fill:#f96,stroke:#f66,stroke-width:2px;
style 2 fill:#9f9,stroke:#0f0,stroke-width:2px;
style A fill:#f96,stroke:#f66,stroke-width:2px;
style C fill:#9f9,stroke:#0f0,stroke-width:2px;
| |
Solution
Method 1 - Dijkstra Algorithm
The code aims to modify edges in an undirected weighted graph such that the shortest path from a given source to a destination equals a specified target distance. It specifically updates edges initially marked with a weight of -1 to achieve this.
Here are the steps:
- Building the Graph:
- Uses
buildGraph(int[][] edges)to create an adjacency list representation of the graph, excluding edges with a weight of-1.
- Uses
- Initial Shortest Path Calculation:
- Runs
runDijkstra()to compute the shortest path distances from the source to all other nodes. - After the calculation,
dist[destination]holds the shortest path distance from the source to the destination.
- Runs
- Evaluating Initial Shortest Path:
- If
dist[destination]is less thantarget, the function returns an empty array, indicating it’s impossible to achieve the target distance. - If
dist[destination]equalstarget, it returns the edges as is.
- If
- Adjusting -1 Edges:
- Iterates over the edges list to adjust edges with an initial weight of
-1. - Sets an initial weight of
1to each edge marked-1, then updates the adjacency list accordingly.
- Iterates over the edges list to adjust edges with an initial weight of
- Recalculate Shortest Path:
- Re-runs
runDijkstra()to recompute the shortest path distances after introducing the adjusted edge. - Checks if the new distance equals the target:
- If it matches, returns edges where any remaining
-1weights are set to an upper bound. - If the new distance is less than the target, increments the edge weight such that the shortest path distance equals the target.
- If it matches, returns edges where any remaining
- Re-runs
- Return Updated Edges:
- If no feasible solution is found, returns an empty array. Otherwise, returns the modified edges.
Code
| |
Complexity
- ⏰ Time complexity:
O(E.(E+V) log V)whereEis number of edges andVis number of vertices- Building graph takes O(E) time
- Running Dijkstra’s algorithm initially (in the
runDijkstramethod) takes (O((V + E) \log V)), - Then for each edge
ewith -1 weight, we run dijkstra’s algorithm again, and in worst case it will be E such edges, hence it isO(E.( E+V) log V
- 🧺 Space complexity:
O(E+V)- For storing graph it takes
O(E+V) - Distance array takes
O(V)
- For storing graph it takes