Best way to find if a subgraph has a cycle

58 Views Asked by At

I am implementing Kruskal's algorithm to find a minimal spanning tree of a connected graph $G$. If $H$ is a subtree of $G$, does anyone know a smart way of checking if $H+e$, where $e$ is an edge of $G$ not in $H$, has a cycle, i.e. is not anymore a tree?

EDIT: $H$ is not necessarily connected.

1

There are 1 best solutions below

0
On BEST ANSWER

Ok, so the edge:

$e = (a, b)$

and the subtree:

$H= {T_0, T_1, ..., T_n}$

so let $a_T$ be the component of $H$ containing $a$ and define $b_T$ similarly. We have:

  1. $a_T = b_T\Longrightarrow e$ is a cycle edge of the shared component.
  2. $a_T \neq b_T\Longrightarrow e$ connects those components.
  3. One of $a$ of $b$ is not in H $\Longrightarrow e$ extends the component ($T$).
  4. Neither $a$ nor $b$ are in $H$ $\Longrightarrow e$ is a new $T$ in $H$.

Some of that notation might be a bit unclear, but I think that covers it.