Count cycles in undirected graph that does not share a vertex - graph

I am trying to understand an algorithm to count the number of isolated cycles in a graph. By isolated, I mean the cycles that do not share any vertex. Here is an example:
Here, 1-2-4-1 and 3-6-5-3 are the two cycles that do not share any vertex. Hence, the algorithm must return 2. Off course, we can find all possible cycles and then figure out the isolated ones but I was wondering if there could be an alternate method for this special case.

Related

Time complexity for detecting a cycle in a graph

I am trying to understand the time complexity of some efficient methods of detecting cycles in a graph.
Two approaches for doing this are explained here. I would assume that time complexity is provided in terms of worst-case.
The first is union-find, which is said to have a time complexity of O(Vlog E).
The second uses a DFS based approach and is said to have a time complexity of O(V+E). If I am correct this is a more efficient complexity asymptotically than O(Vlog E). It is also convenient that the DFS-based approach can be used for directed and undirected graphs.
My issue is that I fail to see how the second approach can be considered to run in O(V+E) time because DFS runs in O(V+E) time and the algorithm checks the nodes adjacent to any discovered nodes for the starting node. Surely this would mean that the algorithm runs in O(V2) time because up to V-1 adjacent nodes might have to be traversed over for each discovered node? It is obviously impossible for more than one node to require the traversal of n-1 adjacent nodes but from my understanding this would still be the upper bound of the runtime.
Hopefully someone understands why I think this and can help me to understand why the complexity is O(V+E).
The algorithm, based on DFS, typically maintains a "visited" boolean variable for each vertex, which contains one bit of information - this vertex was already visited or not. So, none of vertices can be visited more than once.
If the graph is connected, then starting the DFS from any vertex will give you an answer right away. If the graph is a tree, then all the vertices will be visited in a single call to the DFS. If the graph is not a tree, then a single call to the DFS will find a cycle - and in this case not all the vertices might be visited. In both cases the subgraph, induced by all the already visited vertices, will be a tree at each step of the DFS lookup - so the total number of traversed edges will be O(V). Because of that we can reduce the time complexity estimate O(V+E) of the cycle detection algorithm to O(V).
Starting the DFS from all vertices of the graph is necessary in the case when the graph consists of a number of connected components - the "visited" boolean variable guarantees that the DFS won't traverse the same component again and again.

What is an algorithm to find the circuit with max weight in a directed graph?

First problem was that i couldn't find an algorithm that,given an directed graph as input, gives as output a list of all cycles present in the graph. (This problem should be NP-complete).
After thinking about the problem for a while I realized that what probably I really needed was to find the circuit (it can have duplicate vertex but not duplicate edges) with max weight (sum of the weights of the edges).
It should be a NP-complete problem too, and a way to proceed could be to list all circuits present in the graph and then to sort them by sum of edge weights.
Do you know some algorithm that gives as output a list of all circuits present in a directed graph? Or one that find the circuit with max weight ?
I have found this, but it's not exactly what i need.
http://epubs.siam.org/doi/abs/10.1137/0205007
However do you confirm the computational complexity of these problems ?
You could do sth. like here: Finding all cycles in a directed graph.
You do this search for every node and parallelize that so as to reduce runtime. Afterwards you apply an efficient sort-algorithm to your list of cycle where each cycle is a list of nodes. Sorting algorithms may me Mergesort or Quicksort for instance, but choose which ever u prefer..
I hope that brings u forward.

Efficient way to find path between any two nodes in a undirected graph

I got an undirected graph with such that all the edges with same weight and all the vertices are connected. I want to find path between any given pair of vertices.
A less efficient solution is:
To perform BFS starting with one of the vertices, keep track of the visited vertices till the destination vertex is reached. This would perform in O(V + E). But this will have to be done for every pair of vertices given. Hence if there are M number of queries to find path, complexity would be O(M *(E+V)).
Can we do it better? Is it possible to leverage the output a BFS and solve the rest?
As you state that the graph is connected, it is not necessary to call a searching algorithm for the graph more than once. It is sufficient to use a single call to BFS (or DFS, this should make no major difference) to generate a spanning tree for the input. As from your problem statement it is apparently not necessary to find a shortest path between the vertices, any pair (a,b) of vertices is connected via the path from a to the root r of the spanning tree and the path from r to b.
The runtime would be O(V+E), namely the runtime of te searching algorithm; an additional computational cost might be necessary for the generation of the actual paths themselves.

Most efficient algorithm to know if undirected graph is connected

I´ve been trying to find an algorithm to search if a graph is connected. The graph is undirected and I only want to find a solution (there can be multiple) or if there is none. I was looking for a alg. that performs near linear time, maybe O(logN) or O(NlogN).
Can DFS be up to the task or is there another alternative for this specific problem?
It's going to depend on how you define N, if N is number of vertices, the input itself can be of size O(N^2), and you will be needing to read all of it (unless you have some specific ordering of the input, and than that might change).
DFS runs in O(|V|+|E|) (number of nodes + number of edges), and can find if the graph is connected by simply counting the number of new vertices you discover, and when done, checking if this number is |V|.

Cycle detection in graphs containing multiple cycles

I have the following graph:
Is there a way I can identify all cycles in this graph? I know that DFS can be used to detect cycles by simply doing DFS until a back edge is found, but I was wondering if there is a computationally efficient way to return the individual cycles, considering that there are actually 3 cycles in the graph (1-2-3-4-5-6, 4-5-7-8-9, 1-2-3-4-9-8-7-5-6). I am a bit stuck because it seems like the carbon atom belongs to multiple graphs and I can't think of any way other than brute-forcing all possible paths originating from every vertex.
You don't have to find all pathes from EVERY vertex.
Only vertex refering to 3 or more other may belong to multiple cycles
You have to check only 4,5,6(,9)

Resources