Find the girth of a graph

两盒软妹~` 提交于 2019-12-04 12:41:29

This algorithm will find the length of the shortest cycle:

- set `girth` to infinity
- for each edge
-- remove the edge from the graph
-- measure the distance between the edge endpoints
-- if `girth` is longer than distance+1
--- set `girth` to distance+1
-- return the edge to the graph.

The time complexity is of the Dijkstra's algorithm is O(v^2), so this algorithm is O(v^4).

If your graph is sparse, you can convert to a neighbor-list representation and then run the previous algorithm in O(v^2+e*(e+v*log(v)))=O(v^2+e^2+v*e*log(v))

There is an algorithm at http://webcourse.cs.technion.ac.il/234247/Winter2003-2004/ho/WCFiles/Girth.pdf based on BFS, with complexity O(VE). This is for undirected graphs (e.g., your example adjaceny matrices are symmetric). See also https://github.com/jaspervdj/Genus/blob/master/src/genus/FindGirth.java.

It's cheaper(O(v^3)) if you run dijkstra from all points and take each round the maximum distance from that given source. Then take the maximum of these elements and that's the girth.

Girth of a graph is the length of the shortest cycle contained in a graph i.e. a cycle with the least possible sum( can be negative , if graph has a negative cycle). The easiest way to find girth is to run Floyd Warshall algorithm( in O(V^3) time ) on the given graph( having V<= 400) and storing the distance between every pair in 2-D array. After that iterate over the dist[i][i] for every possible vertex( in O(V) time ) , to check if there exist a cycle that start with vertex i and end at vertex i, and then take the minimum of all possible values obtained from dist[i][i].

In Sage

sage: G1 = Matrix([[0,1,0,0],[1,0,0,1],[0,0,0,0],[0,1,0,0]])
sage: G2 = Matrix([[0,1,0,0,1],[1,0,1,0,0],[0,1,0,1,0],[0,0,1,0,1],[1,0,0,1,0]])
sage: G1 = Graph(G1)
sage: G2 = Graph(G2)
sage: G1.girth()
+Infinity
sage: G2.girth()
5
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!