简体   繁体   中英

Simplest algorithm to find 4-cycles in an undirected graph

I have an input text file containing a line for each edge of a simple undirected graph. The file contains reciprocal edges, ie if there's a line u,v , then there's also the line v,u .

I need an algorithm which just counts the number of 4-cycles in this graph. I don't need it to be optimal because I only have to use it as a term of comparison. If you can suggest me a Java implementation, I would appreciate it for the rest of my life.

Thank you in advance.

Construct the adjacency matrix M , where M[i,j] is 1 if there's an edge between i and j. is then a matrix which counts the numbers of paths of length 2 between each pair of vertices.

The number of 4-cycles is sum_{i<j}(M²[i,j]*(M²[i,j]-1)/2)/2 . This is because if there's n paths of length 2 between a pair of points, the graph has n choose 2 (that is n*(n-1)/2) 4-cycles. We sum only the top half of the matrix to avoid double counting and degenerate paths like ababa. We still count each 4-cycle twice (once per pair of opposite points on the cycle), so we divide the overall total by another factor of 2.

If you use a matrix library, this can be implemented in a very few lines code.

深度优先搜索,DFS-这就是您需要的

Detecting a cycle is one thing but counting all of the 4-cycles is another. I think what you want is a variant of breadth first search (BFS) rather than DFS as has been suggested. I'll not go deeply into the implementation details, but note the important points.

1) A path is a concatenation of edges sharing the same vertex.

2) A 4-cycle is a 4-edge path where the start and end vertices are the same.

So I'd approach it this way.

Read in graph G and maintain it using Java objects Vertex and Edge. Every Vertex object will have an ArrayList of all of the Edges that are connected to that Vertex.

The object Path will contain all of the vertexes in the path in order. PathList will contain all of the paths.

Initialize PathList to all of the 1-edge paths which are exactly all of edges in G. BTW, this list will contain all of the 1-cycles (vertexes connected to themselves) as well as all other paths.

Create a function that will (pseudocode, infer the meaning from the function name)

PathList iterate(PathList currentPathList)
{
   newPathList = new PathList(); 

   for(path in currentPathList.getPaths())
   {
      for(edge in path.lastVertexPath().getEdges())
      {  
         PathList.addPath(Path.newPathFromPathAndEdge(path,edge));
      }
   }

   return newPathList;
}

Replace currentPathList with PathList.iterate(currentPathList) once and you will have all of the 2-cyles, call it twice and you will have all of the 3 cycles, call it 3 times and you will have all of the 4 cycles.

Search through all of the paths and find the 4-cycles by checking

Path.firstVertex().isEqualTo(path.lastVertex())

Construct an adjacency matrix, as prescribed by Anonymous on Jan 18th, and then find all the cycles of size 4.

It is an enumeration problem. If we know that the graph is a complete graph, then we know off a generating function for the number of cycles of any length. But for most of other graphs, you have to find all the cycles to find the exact number of cycles.

Depth first search with backtracking should be the ideal strategy. Implement it with each node as the starting node, one by one. Keep track of visited nodes. If you run out of nodes without finding a cycle of size 4, just backtrack and try a different route.

Backtrack is not ideal for larger graphs. For example, even a complete graph of order 11 is a little to much for backtracking algorithms. For larger graphs you can look for a randomized algorithm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM