Problem
A city is represented as a bi-directional connected graph with n vertices where each vertex is labeled from 1 to n (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. The time taken to traverse any edge is time minutes.
Each vertex has a traffic signal which changes its color from green to red and vice versa every change minutes. All signals change at the same time. You can enter a vertex at any time, but can leave a vertex only when the signal is green. You cannot wait at a vertex if the signal is green.
The second minimum value is defined as the smallest value strictly larger than the minimum value.
- For example the second minimum value of
[2, 3, 4]is3, and the second minimum value of[2, 2, 4]is4.
Given n, edges, time, and change, return the second minimum time it will take to go from vertex 1 to vertex n.
Notes:
- You can go through any vertex any number of times, including
1andn. - You can assume that when the journey starts, all signals have just turned green.
Examples
Example 1:
graph TD 1 --- 2 1 --- 3 1 --- 4 3 --- 4 4 --- 5
graph TD 1 --- 2 1 -->|1| 3 1 -->|1| 4 3 -->|2| 4 4 -->|2| 5 4 -->|3| 5 linkStyle 2 stroke:blue, stroke-width:4px; linkStyle 4 stroke:blue, stroke-width:4px; linkStyle 1 stroke:gold, stroke-width:4px; linkStyle 3 stroke:gold, stroke-width:4px; linkStyle 5 stroke:gold, stroke-width:4px;
| |
Example 2:
graph LR; 1 --- 2
| |
Solution
To find the second minimum time required to travel from vertex 1 to vertex n in a graph with traffic signals, we need to account for both path traversal and the waiting times caused by the traffic signals’ cycle. The key insights are:
- Each edge traversal takes exactly
timeminutes. - Each signal alternates every
changeminutes between green and red. - You can only leave a vertex when the signal is green.
Method 1 - Dijkstra Algorithm
We can solve this problem using a modified Dijkstra’s or shortest path algorithm. This will help us account for the different paths’ travel times and keep track of the second minimum time.
Here are the steps we can take:
- Build the graph as adjacency list to representation.
- Setup the dijkstra algorithm
- We use Priority Queue to store states represented as
(current_time, current_vertex), and it is a min heap based on time. - Add node 1 with time 0
- Time tracking: We use
distance[n + 1][2]matrix to not just minimum time to reach each neighboring vertex, but also the second minimum time as well. Fordist[1][0]minimum time will be 0. We usedistance[n + 1]as nodes start with 1, not 0.
- We use Priority Queue to store states represented as
- Start Dijkstra Algorithm
- Poll out the node from priority queue based on time
- Check if it’s a red light when we are at current node, if it is, calculate the waiting time until the next green light. Our
currentTimeincreases because of that - Now, we start processing all the neighbours and update the minimum and second minimum times to reach each neighboring vertex.
- If
currentTimeis less thandistance[v][0], setdistance[v][1]asdistance[v][0]anddistance[v][0]ascurrentTime - If
currentTimeis less thandistance[v][1], setdistance[v][1]ascurrentTime
- If
- Return the result as
distance[n][1]
Code
| |
Complexity
- ⏰ Time complexity:
O(E log V), whereEis number of edges andVis number of vertices- Building the graph: (O(E))
- Priority queue operations: (O(E \log V))
- Dijkstra’s algorithm-like edge relaxations: (O(E \log V))
- 🧺 Space complexity:
O(V + E)- Graph representation: (O(V + E))
- Distance arrays: (O(V))
- Priority queue operations: (O(E))