The Challenge
Your program should take 3 inputs:
- A positive integer which is the number of variables,
- A set of unordered pairs of nonnegative integers, where each pair represents an equality between variables, and
- A positive integer which represents the starting variable,
It should return a set of nonnegative integers which represent all variables which can be shown to be transitively equal to the starting variable (including the starting variable itself).
In other words, given inputs N
, E
, and S
, return a set Q
, such that:
S ∈ Q
.- If
Z ∈ Q
and(Y = Z) ∈ E
, thenY ∈ Q
. - If
Z ∈ Q
and(Z = Y) ∈ E
, thenY ∈ Q
.
This can also be expressed as a graph-theory problem:
Given an undirected graph and a vertex in the graph, list the vertices in its connected component.
Specifications
- You can choose to use 0-based or 1-based indexing.
- The first input counts the number of variables that are present, where variables are given as numbers. Alternatively, you can not take this input, in which case this is assumed to be equal to either the highest variable index present, or one more than this, depending on your indexing scheme.
- You can assume the input is well formed: you will not be given variables outside of the range specified by the first input. For example,
3, [1 = 2, 2 = 0], 1
is a valid input, while4, [1 = 719, 1 = 2, 3 = 2], -3
is not. - You cannot assume that any variable will have any equalities associated with with it. If given a third input that is "lonely" (has no equalities), the correct output is a singleton set containing only that input (since it is equal to itself).
- You can assume that the equalities will not contain an equality from a variable to itself, and that the same equality will not be given multiple times (this includes things like
1 = 2
and2 = 1
). - You can assume that all integers given will be within your language's representable range.
- You can take the second input in any reasonable format.
Here are some reasonable formats:
0 = 2
0 = 3
1 = 0
{(0, 2), (0, 3), (1, 0)}
[0, 2, 0, 3, 1, 0]
0 2 0 3 1 0
Graph[{{0, 2}, {0, 3}, {1, 0}}]
[0 = 2, 0 = 3, 1 = 0]
- You can output in any reasonable format (i.e. set, list, etc.). Order is irrelevant.
Scoring
This is code-golf, so the shortest valid program (in bytes) wins.
Test Cases (0-indexed)
3, [1 = 2, 2 = 0], 1 -> {0, 1, 2}
5, [0 = 2, 0 = 3, 1 = 2], 3 -> {0, 1, 2, 3}
6, [0 = 3, 1 = 3, 2 = 4, 5 = 1], 4 -> {2, 4}
6, [0 = 3, 1 = 3, 2 = 4, 5 = 1], 5 -> {0, 1, 3, 5}
5, [0 = 1, 2 = 0, 0 = 3, 4 = 0], 2 -> {0, 1, 2, 3, 4}
6, [0 = 1, 1 = 2, 2 = 3, 3 = 4, 4 = 5], 3 -> {0, 1, 2, 3, 4, 5}
4, [0 = 1, 1 = 2, 2 = 0], 3 -> {3}
5, [0 = 2, 2 = 4], 2 -> {0, 2, 4}
8, [], 7 -> {7}
Test Cases (1-indexed)
3, [2 = 3, 3 = 1], 2 -> {1, 2, 3}
5, [1 = 3, 1 = 4, 2 = 3], 4 -> {1, 2, 3, 4}
6, [1 = 4, 2 = 4, 3 = 5, 6 = 2], 5 -> {3, 5}
6, [1 = 4, 2 = 4, 3 = 5, 6 = 2], 6 -> {1, 2, 4, 6}
5, [1 = 2, 3 = 1, 1 = 4, 5 = 1], 3 -> {1, 2, 3, 4, 5}
6, [1 = 2, 2 = 3, 3 = 4, 4 = 5, 5 = 6], 4 -> {1, 2, 3, 4, 5, 6}
4, [1 = 2, 2 = 3, 3 = 1], 4 -> {4}
5, [1 = 3, 3 = 5], 3 -> {1, 3, 5}
8, [], 8 -> {8}