N = 4, redEdges = [[0, 1], [2, 3]], blueEdges = [[1, 2], [1, 3]]
The shortest paths for each node from node ‘0’ are:
1: 0->1 Length: 1
2: 0->1->2 Length: 2
3: 0->1->3 Length: 2
So, the ‘answer’ array will be: [0, 1, 2, 2].
1. The given graph could be a disconnected graph.
2. Any two nodes ‘i’ and ‘j’ can have at most one red edge from ‘i’ to ‘j’ and at most one blue edge from ‘i’ to ‘j’.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains three space-separated integers ‘N’, ‘rlen’, and ‘blen’ denoting the number of nodes in the graph, the size of array ‘redEdges’, and the size of array ‘blueEdges’.
The next 'rlen' lines of each test case contain two space-separated integers, ‘i’ and ‘j’ denoting a ‘red’ edge from node ‘i’ to node ‘j’.
The next 'blen' lines of each test case contain two space-separated integers, ‘i’ and ‘j’ denoting a ‘blue’ edge from node ‘i’ to node ‘j’.
For every test case, print a single line containing N space-separated integers denoting array ‘answer’, where ‘answer[i]’ contains the valid shortest path length from node ‘0’ to node ‘i’.
The output for each test case will be printed in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 200
0 <= rlen + blen <= 1000
Where ‘T’ is the number of test cases, ‘N’ is the number of nodes in the graph, ‘rlen’ is the size of array ‘redEdges’, and ‘blen’ is the size of array ‘blueEdges’.
Time limit: 1 second
Dijkstra’s algorithm is used for finding the shortest path between nodes in a weighted graph. As the given graph is unweighted, the path length is equal to the number of edges, so consider each edge’s weight to be ‘1’. The maximum shortest path length will be ‘2*n - 3’, which will be achieved when all edges from node ‘i’ to ‘j’ (where ‘i != j’) have the same color ‘red’ and each node has a self-loop of ‘blue’ color or vice-versa.
For any node ‘x’ a valid path can be of two forms:
Create two graphs ‘graph[2][n]’, the first ‘graph[0]’ containing red edges and the second ‘graph[1]’ containing blue edges. Here, ‘graph[i][j][k]’ means an edge of color type ‘i’ (‘0’ is red and ‘1’ is blue) from node ‘j’ to node ‘k’. A node can be represented in the form ‘[x, c]’ where ‘x’ is the node’s label and ‘c’ is the color of the incoming edge. Use a 2-d array ‘dist[n][2]’, where ‘dist[i][0]’ will store the length of the shortest path to reach node ‘i’ from a red edge and ‘dist[i][1]’ is the length with a blue edge. Use a 2-d array ‘sptSet[n][2]’ to check if we have found the shortest path for a node of form ‘[x, c]’.
Initialize the array ‘dist’ with value ‘2*n’ (i.e., unreachable from node ‘0’). Set ‘dist[0][0] = dist[0][1] = 0’ i.e., node ‘0’ is reachable from both blue and red edges with a path of zero length. Set ‘sptSet[0][0] = sptSet[0][1] = 0’, which means the length of shortest path to reach node ‘[0, 0]’ and ‘[0, 1]’ is zero.
Now, use Dijkstra’s shortest path algorithm to find the shortest path length for each such node ‘[x, c]’ from node ‘0’ (i.e., from ‘[0, 0]’ and ‘[0, 1]’). Let ‘minDist’ be the set of nodes (with their path length) that are reachable from the nodes in ‘sptSet’. Initially, both ‘sptSet’ and ‘minDist’ contain two nodes, ‘[0, 0]’ and ‘[0, 1]’, the source nodes.
According to Dijkstra’s algorithm:
In each iteration, remove the node (let’s say ‘[x, c]’) having the smallest path length in ‘minDist’ (let’s say ‘len’). Then ‘len’ is the shortest path length for node ‘[x, c]’ from node ‘0’. Set ‘sptSet[x][c] = true’ and ‘dist[x][c] = len’ as we have found it’s shortest path length. For all the nodes that are reachable from ‘[x, c]’, if they have ‘sptSet’ as false (i.e., their shortest path is not known) and their ‘dist’ value is larger than ‘dist[x][c] + 1’ then, add them to the set ‘minDist’ and update their ‘dist’ value. Stop the iteration when the set ‘minDist’ becomes empty, i.e., when we have found the shortest path for all nodes reachable from node ‘0’ (the source node).
Finally, for each node ‘i’ we have ‘answer[i] = min(dist[i][0], dist[i][1])’. If ‘answer[i]’ is still ‘2*n’ then no valid path exists to reach node ‘i’, set ‘answer[i] = -1’. Use a Priority Queue to implement ‘minDist’, so that we can extract the smallest element efficiently (in ‘O(log(n))’). Below is an implementation of Dijkstra’s algorithm using priority queue:
Since the given graph is unweighted, we can find the shortest path for each node in ‘O(E + V)’ = ‘O(rlen+blen+ n)’ time using a modified Breadth-first search algorithm. Similar to the previous approach, we create two separate graphs for ‘redEdges’ and ‘blueEdges’. Consider ‘2 * n’ nodes of the form ‘[x, c]’ where ‘x’ is the node label, and ‘c’ is the color of the incoming edge.
Create an array ‘dist[n][2]’, where ‘dist[x][c]’ stores the length of shortest path for node ‘[x, c]’ where ‘c = 0’ means red edge and ‘c = 1’ means blue edge. Except for node ‘0’ set ‘dist[i][j]’ as ‘2 * n’ (as maximum possible length of valid path is ‘2 * n - 3’). We know the distance of nodes ‘[0, 0]’ and ‘[0, 1]’ is equal to zero, the base condition for source node ‘0’. Initially, the BFS queue contains these two nodes. Run a loop until the BFS queue is not empty. In each iteration, the current node is popped from the front of the queue. As we reach the current node(let’s say ‘[x, c]’) from an edge of color ‘c’, the next reachable node must be from an opposite edge color (‘1 - c’). For each node that is reachable from ‘[x, c]’, if its distance in ‘dist’ is greater than of ‘dist[x][c] + 1’ then, update its distance value in ‘dist’ and push this node at the end of the BFS queue.
Proof of correctness: As we move level-wise in BFS, if a child node is not visited, it’s not reachable from any previous levels. Its shortest path will be equal to (parent node’s level + 1). Even if it’s reachable from any other node from the next levels, the corresponding path length will always be greater.
Following is a BFS implementation to solve this problem:
Valid Arrangement of Pairs
Valid Arrangement of Pairs
Valid Arrangement of Pairs
Left Right Print
COUNT ISLANDS
The Summit
Distance to a Cycle in Undirected Graph