[Math] Algorithm for finding pairs in a list.

algorithms

I have a list of numbers let's say $\{1,2,3,…10\}$ and I have a list for each number denoting with what numbers that number can pair with.
Example:
$1:2,3$
$2:3,4$
$3:4,5$
$4:5,6$

$8:9,10$
$9:10,1$
$10:1,2$
I now need an algorithm (meaning a well defined step by step solution and not python code) that finds a way for each number having a partner. In this case there would be 2 solutions one being:
$1,2$
$2,3$
$3,4$
$4,5$
$5,6$

$10,1$

EDIT:
The statement $a:b$ and $b:c$ is just a shortcut in my notation for $a:b$ and $b:a,c$ because otherwise this would be a contradiction.

Best Answer

With this information you could build up a bipartite graph $G=(V\cup W,E)$ with the partition calsses $V$ and $W$, each of one containing all the numbers of your list. Then you introduce an edge for each pair, e.g. for $1 : 2,3$ you would introduce an edge between $v_1\in V$ and $w_2\in W$, and between $v_1\in V$ and $w_3\in W$. Now you can use one of several matching finding algorithms known to graph theory, e.g. Hopcroft–Karp algorithm. In case there is no perfect matching, most of the algorithms would give you at least the cardinality maximal matching.

Related Question