# Finding twin vertices in graphs

Let $G=(V,E)$ be a graph. For a vertex $x\in V$, define $N(x)$ to be the (open) neighbourhood of $x$ in $G$. That is, $N(x)=\{y\in V \,\vert\, \{x,y\}\in E\}$. Define two vertices $u,v$ in $G$ to be twins if $u$ and $v$ have the same set of neighbours, that is, if $N(u)=N(v)$.

Given a graph $G$ on $n$ vertices and $m$ edges as input, how fast can we find a pair of twins in $G$, if such a pair exists?

We can check whether two given vertices are twins in $O(n)$ time, by comparing their neighbourhoods. A straightforward algorithm is to find twins is thus to check, for each pair of vertices, whether they are twins. This takes $O(n^{3})$ time (and also finds all pairs of twins). Is there is significantly faster way to find (if there exists) a pair of twins in the graph? Is there known work in the literature that addresses this problem?

• You could iterate thru neighborhoods and add them to a hashtable. Related: cstheory.stackexchange.com/q/3390/236 – Radu GRIGore Jan 10 '11 at 12:50
• This is exercise 2.17 here books.google.co.uk/… – Radu GRIGore Jan 10 '11 at 13:36
• Someone with edit powers should fix the definition of twins. (See the comments on TheMachineCharmer's answer, or the the definition in the book I linked.) – Radu GRIGore Jan 10 '11 at 14:09

Twins in a graph are just modules of size 2. The modular decomposition of a graph can be found in $O(n+m)$ time. The modular decomposition tree implicitly represents all modules of the graph and consists of three types of internal nodes: series, parallel and prime nodes, and the leaves consists of the individual nodes. A set of at least two vertices $S \subset V$ is a module if and only if it is some node in the tree or the union of some set of children of a series or a parallel node.

So to find a pair of twin nodes, if they exist, we can construct the modular decomposition tree in $O(n+m)$ time. Then look at the leaves, if the parent of any leaf is a series or parallel node then that node must have at least two children which form a twin pair. So the total running time is linear.

http://en.wikipedia.org/wiki/Modular_decomposition

• Thank you, also for introducing me to Modular Decomposition! – gphilip Jan 11 '11 at 10:34

The problem is equivalent to determining is there two equal rows in graph matrix. We can construct trie on rows of graph matrix. Time compleixty will be O(n^2)

• The same idea on adjacency lists gives $O(m+n)$. – Radu GRIGore Jan 10 '11 at 14:08
• Now I'm nuking a fly ;) – Hsien-Chih Chang 張顯之 Jan 10 '11 at 14:15
• This can be generalised somewhat. If we rephrase the problem as "Given $f : X -> Y$ (where here $f(x) := N(x)$) find distinct $x_1$, $x_2$ such that $f(x_1) = f(x_2)$" then for totally ordered $Y$ one approach is to evaluate $f(x)$ for each $x \in X$, sort them, and check the sorted list for duplicates. The trie is effectively radix sort. – Peter Taylor Jan 11 '11 at 18:35

EDIT: the solutions by @MikleB and @Travis are much clever. Sorry for the overkilling answer.

It seems that the problem can be reduced to the matrix multiplication problem on the adjacency matrix $A$ of the graph, by replacing multiplication with EQU (that is, NXOR) and addition with AND. So if there is a pair of twins in the graph, then the resulting matrix $AA^T$ will not be the identity matrix, and the indices $(i,j)$ where the value $a_{i,j}$ is not zero are exactly the twin pair nodes.

To my best knowledge the matrix multiplication problem can be solved in $O(n^\alpha)$ time with $\alpha \approx 2.376$ by the Coppersmith–Winograd algorithm. If practical solutions are needed, any matrix multiplication algorithms works well in practice are nice.

• Awesome this works! :D I think it will suffice to evaluate only upper half of the $A^2$. what do you think? – Pratik Deoghare Jan 10 '11 at 12:14
• @TheMachineCharmer: Thank you :) Yes, if the graph is undirected. – Hsien-Chih Chang 張顯之 Jan 10 '11 at 12:16
• Yes. Exactly! :) – Pratik Deoghare Jan 10 '11 at 12:17

Because of the crazy system on this site I can't comment directly, but I have a couple of observations on existing answers.

I'm pretty sure Hsien-Chih Chang's solution needs to correct $A^2$ to $AA^T$.

TheMachineCharmer's observation 4 is back to front (counter-example: [0,0,1],[0,1,0],[0,1,1] has determinant 0 but no twins). If twins exist then the determinant is zero.

• I see no problem with $A^2$. Any examples? btw, system on this site is NOT crazy! :) – Pratik Deoghare Jan 10 '11 at 13:26
• $A^2$ would work for undirected graphs (for which $A$ == $A^T$) but not, in general, for directed graphs. The AND over XNOR needs to compare two rows of A, and matrix multiplication operates on a row from the first matrix with a column from the second. – Peter Taylor Jan 10 '11 at 13:50
• System may not be crazy, but perhaps counterintuitive to first time posters. You can answer but not comment... but your comments were nice enough IMHO to justify posting. Once you've built up more reputation I think you'll find the system pretty addictive. – hardmath Jan 10 '11 at 15:16
• Being able to answer but not comment is crazy. It forces new users to choose between not being helpful or answering in the wrong place. – Peter Taylor Jan 11 '11 at 18:31

This thread is quite old; however, nobody seems to have hit on the most elegant and simple approach. Lexicographically sort the adjacency-list in O(n+m) time then check for duplicates (see Aho, Hopcroft, Ullman, 74'). You can use modular decomposition, but this is total overkill.

• How do you sort the nodes by adjacency list in O(n+m)? I think radix sort on the sorted adjacency lists gives something like O(n * D) with D the maximal degree. – Labo Apr 7 at 18:55

This thread is old and OP's question has been answered but I'd like to add another algorithm to find all such pairs in linear time. Nobody mentioned Partition Refinement!

This algorithm finds the equivalence classes of false twins. The algorithm relies on an efficient procedure that refines a partition. Given a set S and a partition P = {X1, ..., Xn}. refine(P, S) = {X1 ^ S, X1 - S, X2 ^ S, X2 - S, ..., Xn ^ S, Xn - S}. ^ denotes set intersection and - set difference. A partition is stable if it cannot be further refined. This procedure takes time O(|S|) (see Wikipedia's article on partition refinement), so it's fast.

Algorithm:

P = {V} // initial partition consists of the vertex set
for every vertex v:
P = refine(P, N(v)) // refine with the open neighborhood of v


Total time is O(|V|+|E|). This is simple to program as well.

• This answer should have much more upvotes – Labo Apr 7 at 18:32

Some observations that might help

1. For $a,b \in V$ if $a$ is not twin of $b$ then $c$ and $d$ can't be twins where $c \in N(a)$ and $d \in N(b)$.

2. $|N(a)| \neq |N(b)|$ then $a$ and $b$ can't be twins.

3. If $b \in N(a)$ then $a$ and $b$ can't be twins. This works only if you are looking for non-adjacent twins.

4. If twins exist then determinant of the adjacency matrix is zero.

Fancy idea:

1. Build a complete binary tree with height = |V|.
• The 3rd point is true only if we look for non-adjacent twins. In the usual notion of twins, $a$ and $b$ are allowed to be adjacent. – Mathieu Chapelle Jan 10 '11 at 13:05
• Peter Taylor, Mathieu Chapelle: Thanks! I have edited the answer to reflect the changes. @Serge Gaspers: I guess in that case the condition must be $N(a)-{b} = N(b) -{a}$. – Pratik Deoghare Jan 10 '11 at 13:21