FLIPKART Coding Question – Solved

12 Live
Undirected Coloured Graph Shortest Path You are given an undirected weighted graph G(V, E) where each vertex is assigned a colour. Vertices form pairs of the same colour. Thus, total pairs = V/2 (if V is even) or (V/2)+1 (if V is odd). Each pair is assigned a unique colour. A query Q specifies a colour, and your task is to find the shortest path (in terms of weight) between the two vertices sharing this colour. Input Format: - The first line contains two integers V and E separated by a space (number of vertices and edges). - The next E lines each contain three integers u, v, and w: an edge between vertex u and vertex v with weight w. - The next V/2 (or V/2+1 if V is odd) lines specify the vertex pairs for each colour (each line has two vertex indices). If V is odd, the last line contains a single vertex with no pair. - The final line contains a single integer Q, representing the colour number to query. Output Format: - Print the shortest path (by weight) between the vertex pair corresponding to colour Q. - Each vertex in the path should be printed in order, separated by a single space. Constraints: - 2 ≤ V ≤ 100 - 1 ≤ E ≤ 100 Sample Input 1: 7 8 0 6 10 0 2 11 2 4 9 6 4 2 5 2 7 5 1 8 1 4 4 3 4 3 0 1 2 3 5 6 4 3 Sample Output 1: 5 1 4 6 Explanation: There are 7 vertices and 8 edges. Colour 1 → nodes 0, 1 Colour 2 → nodes 2, 3 Colour 3 → nodes 5, 6 Colour 4 → node 4 (no pair) Query = 3 → Find shortest path between 5 and 6. Shortest weighted path: 5 → 1 → 4 → 6

Asked in: FLIPKART

Image of the Question

Question Image Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <bits/stdc++.h>
using namespace std;

int n, m;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, first clearly understand the task: Given an undirected weighted graph where vertices are paired by colours, find the shortest path between the two vertices that share the queried colour. The path must be the minimum total weight path connecting the two vertices.

Step 1: Understand the Graph Representation
You have V vertices and E edges, with weights on each edge. The graph is undirected, so each edge connects two vertices in both directions with the same weight. This implies you can travel back and forth along any edge.

Step 2: Understand the Colour Pairing
Each colour corresponds to either exactly two vertices (forming a pair) or, if the number of vertices is odd, one colour may correspond to a single vertex. The input lists these pairs by colour, so you know which two vertices correspond to each colour. Your goal is to find the shortest path between the two vertices that share the queried colour Q.

Step 3: Identify the Core Task
The core computational task is to find the shortest path between two given vertices in a weighted undirected graph. This is a classic shortest path problem. Since the graph is small (V ≤ 100, E ≤ 100), algorithms with O(V^2) or O(E log V) complexity are feasible.

Step 4: Choose an Appropriate Shortest Path Algorithm
Given the constraints, Dijkstra’s algorithm is a natural choice. It efficiently finds shortest paths from a single source to all other vertices in graphs with non-negative edge weights.

Key points to consider:
- Run Dijkstra starting from one of the two vertices in the pair.
- Keep track of the shortest distances to all vertices.
- Also maintain a predecessor array or map to reconstruct the shortest path once the target vertex is reached.

Alternatively, if the graph is very small, Floyd-Warshall algorithm (all pairs shortest path) is possible but might be unnecessary given only one query.

Step 5: Handling the Query
Once you identify the pair of vertices corresponding to the query colour Q, run the shortest path algorithm from the first vertex to find the shortest distance and path to the second vertex.

Step 6: Reconstruct the Shortest Path
Dijkstra’s algorithm typically maintains predecessors for each vertex: the previous vertex along the shortest path from the source. After completing the algorithm, use the predecessor chain from the target vertex back to the source to reconstruct the path in reverse order. Then reverse it to get the path from source to target.

Step 7: Edge Cases and Validation
- If the colour corresponds to a single vertex (only one vertex in the pair), the shortest path is just that vertex.
- Ensure that all edges are correctly handled as undirected: each edge implies bidirectional travel with the same weight.
- Verify that vertices are zero-indexed or one-indexed consistently as per input to avoid off-by-one errors.
- Check that the graph is connected at least between the vertices of the queried pair; if no path exists, clarify expected output (the problem may assume the graph is connected or path always exists).

Step 8: Output
Once the shortest path is reconstructed, print the vertices in order separated by spaces. This path is guaranteed to be the minimal weight path connecting the two vertices sharing the queried colour.

Step 9: Summary of the Approach
- Parse the graph edges and build an adjacency list or matrix with weights.
- Parse the colour pairs to map colours to their corresponding vertex pairs.
- Identify the start and end vertices for the queried colour.
- Run Dijkstra’s algorithm from the start vertex to compute shortest paths.
- Reconstruct the shortest path to the target vertex using predecessors.
- Print the path vertices in order.

This structured approach leverages well-known graph algorithms to solve the problem efficiently within given constraints.
```


Related Questions