A and B wants to reach some destination in an undirected graph using the least number of unique edges. Find the count of unique edges.
Let's say the two paths come out to be this.. A-x-y-D B-y-D
Output : 4
Explanation : A-x, x-y, y-D and B-y
Follow up: What if there are multiple sources?
Can someone help me to solve this optimally?
Auto comment: topic has been updated by jankit366 (previous revision, new revision, compare).
Do a BFS from nodes A, B and D, now you know the distance from A, B and D to every node. There are two cases:
1) the paths A — D and B — D don't intersect
2) the paths meet at some node X(A — X — D and B — X — D)
the first case is just $$$d_{DA} + d_{DB}$$$
in the second case to find the node X we can just try for every node $$$d_{AX} + d_{BX} + d_{DX}$$$
Got it. But what about the case where we have multiple sources?
How many sources are we talking about? Also what are the other constraints?
There can be more nodes other than A and B
Assume:
Nodes: A, B, C, D, E, F, G, H
A, B, C, D are the source.
E is the destination.
I mean how many sources and/or nodes can there be? 10? 100? $$$10^5$$$?
Considering number of nodes = 4*10^3.
What's the source of the problem?
No Source as such, got it from leetcode discussion.
Please share it to make sure you're not cheating
https://leetcode.com/discuss/interview-question/5748871/