【问题标题】:Find the girth of a graph求图形的周长
【发布时间】:2012-10-15 06:03:26
【问题描述】:

我需要输出表示为邻接矩阵的给定图的周长。谁能给我一些提示,我可以如何使用邻接矩阵或邻接列表来获得图的周长?

例子:

graph one:
0 1 0 0
1 0 0 1
0 0 0 0
0 1 0 0 

graph two:
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

The result:
Girth of graph 1: infinity
Girth of graph 2: 5

【问题讨论】:

  • "图的周长是图中包含的最短环的长度"1
  • 我知道这一点。问题是如何使用矩阵或列表获得最短的循环。

标签: graph cycle


【解决方案1】:

http://webcourse.cs.technion.ac.il/234247/Winter2003-2004/ho/WCFiles/Girth.pdf 有一个基于 BFS 的算法,复杂度 O(VE)。这适用于无向图(例如,您的示例邻接矩阵是对称的)。另见https://github.com/jaspervdj/Genus/blob/master/src/genus/FindGirth.java

【讨论】:

    【解决方案2】:

    这个算法会找到最短循环的长度:

    - 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.
    

    Dijkstra's algorithm的时间复杂度是O(v^2),所以这个算法是O(v^4)

    如果您的图是稀疏的,您可以转换为邻居列表表示,然后在O(v^2+e*(e+v*log(v)))=O(v^2+e^2+v*e*log(v)) 中运行之前的算法

    【讨论】:

      【解决方案3】:

      如果你从所有点运行 dijkstra 并且每一轮都取到与给定源的最大距离,它会更便宜 (O(v^3))。然后取这些元素中的最大值,这就是周长。

      【讨论】:

      • 不,那是直径。
      【解决方案4】:

      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
      

      【讨论】:

        【解决方案5】:

        图的周长是图中包含的最短环的长度,即具有最小可能和的环(如果图有负环,则可以是负数)。 找到周长的最简单方法是在给定的图(V

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-18
          • 1970-01-01
          • 1970-01-01
          • 2014-04-14
          • 1970-01-01
          • 2023-01-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多