Find the number of cycles and isolated vertices

1.2k Views Asked by At

I want the algorithm that allow me to find the number of cycles and isolated vertices from undirected graph.

The isolated vertices means the vertices that are not connected to the root vertex, for example vertex 1:

Example:

enter image description here

The number of cycles=2

The number of isolated vertices= 6 (4-9-10-6-7-11) I want only the number 6.

1

There are 1 best solutions below

0
On BEST ANSWER

For the first question, breadth-first search will find the connected component of the given vertex, and then every other vertex is not connected to it.

For the second, you can find all (simple) cycles containing a given vertex in the process of enumerating self-avoiding walks starting at that vertex. That is, if $x_0, x_1, \ldots, x_k$ is a self-avoiding walk, i.e. a sequence of distinct vertices with $(x_i, x_{i+1})$ an edge of the graph for all $i$, consider the neighbours $t$ of $x_k$ other than $x_{k-1}$. If $t = x_0$ you have a cycle containing $x_0$. If $t \ne x_0$ you have a longer self-avoiding walk $x_0, x_1, \ldots, x_k, t$.