Can the Message Get Through? — Shortest Paths & MST
Determine whether a message can travel between two machines in a small office network, possibly relaying through others.
Shortest Paths & MST · Easy
The problem
A network has n machines numbered 0 to n - 1 and some two-way cables. Each pair [a, b] in cables joins machine a and machine b in both directions. Work out whether a message can get from machine source to machine destination, passing through any number of other machines.
The story
A small office network has 1 to 8 machines numbered from 0, joined by up to 14 cables. Every cable carries traffic in both directions, and no two cables join the same pair. A technician needs to know whether a message can travel from one given machine to another, hopping across any number of machines on the way. A machine can always reach itself.
What to return
- Return true if a message can travel from source to destination over the cables, otherwise false.
- A machine can reach itself.
Constraints
- 1 <= n <= 8
- 0 <= cables.length <= 14
- each cable is [a, b] with 0 <= a, b < n and a != b
- no pair of machines is joined by more than one cable, in either order
- 0 <= source, destination < n
Example
- Input
n = 4, cables = [[0, 1], [1, 2]], source = 0, destination = 2- Output
true