English 中文(简体)
如何计算相关图表的数量?
原标题:How do you calculate the number of connected graphs?

Given an array of node and an array of edges, how do you calculate the number of connected graphs?

问题回答

你们可以使用工会(你可以找到)。 先把所有节点作为单独组别,然后,对每个对口单位而言,它们会加入两条节点,即对等同一组。 然后通过所有节点来检查那里有多少不同组别,并发现有多少不同代表。

  • Start at one of the node do the BFS or DFS to get all the nodes connected from this node.
  • Now iterate through the node list to find any node which is not included in already,
  • do the same procedure on the node. Repeat till all the nodes are visited.
  • By now you will have all the graphs in your data.

详细论述准反面的回答,这里是一部简短的假冒代码:

因此,第(五)条创建了一套新规定,其唯一成员是:

工会(x, y)分两组×和。 新组的代议部分从两组中选取。

• 代表(五)返回指定代表为成员。

图G = (V, E)中相关部分:

foreach vertex v in V:
    make_set(v)

foreach edge (u, v) in E:
    if get_representatve(u) != get_representatve:
         union(u, v)

履行必要职能是读者的工作...... 不管怎么说,如果你想要紧密相连的成分,你就应当研究Tarjan的算法。

在平行实施方面,没有使用效率高的决定性算法,但有一些有趣的随机算法。

是的,有一张算法,其大小与图表(O:V)的大小一致。

visited := the empty set
count := 0
for each vertex v in vertices:
    if v not in visited:
        count++
        add v and all nodes reachable from v to visited
return count

对于未经指示的图表,可在O(n)时间进行,并配有简单的每日生活津贴。 在这方面:

定义<代码>explore,作为从某一天起可达到的所有节点的程序。 这只是一种反省式的程序,在每一个节点,你发现这个节点的所有孩子,把他们推向上。

为了找到答案,从外勤支助部开始,并启动<代码>explore关于该术语的程序。 保持分类(编号为<>cc)并将之通过<代码>explore程序。 此外,还保留一个海图/字典,或将地图cc贴在相应的代号上。 在<代码>explore程序的每一层次,目前将目前的节点绘制为cc<>/code>。 每当探索程序被重新采用时,其数值即为<条码>。

每次<代码>explore 返回外勤部的路程,加固<条码> cc<>>>>/代码,并随时间予以通过。 一旦与整个外勤部一起工作,你就会有一张字典,每个线标有相应的连接部分。 在这一程序结束时,<代码>cc的价值可向您提供图表中的相关部分。

The pseudo-code:

function explore(node, graph, cc, map){
    map(currentNode) = cc
    //find all children of current node, and push onto stack. 
    //mark current node as visited
    for i in stack:
        explore(i, graph, cc, map)
}

function DFS{
     int cc = -1
     for node in keysOfGraph:
          if not visited:
              cc++
              explore(node, graph, cc, map)

return cc
}

http://www.cs.berkeley.edu/~vazirani/algorithms/chap3.pdf”rel=“nofollow”>Dasgupta Algorithms (第3.2.3节)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签