简体   繁体   English

邻接矩阵表示的图是否至少有一棵生成树?

[英]Has graph represented by adjacency matrix at least one spanning tree?

I am traveling through math&algorithms since two days, but I don't have more ideas.我从两天开始就在学习数学和算法,但我没有更多的想法。 I have adjacency matrix and I have laplasjan matrix.我有邻接矩阵,我有 laplasjan 矩阵。 I want to check is this graph consistent OR does it have spanning tree.我想检查这个图是否一致或它是否有生成树。

I was working with Kirchoff's Theorem and it works for me but it is too slow (more than second with 10x10 matrix).我正在使用基尔霍夫定理,它对我有用,但它太慢了(10x10 矩阵超过秒)。 Can I modify Kirchoff's Theorem to check has my matrix spanning tree (NOT how many)?我可以修改基尔霍夫定理来检查我的矩阵生成树(不是多少)吗?

I am trying to learn something new, so I don't want to use DFS and I really want to use adjacency matrix.我正在尝试学习新东西,所以我不想使用 DFS,我真的想使用邻接矩阵。

Here is an algorithm to find out whether a graph has at least one spanning tree.这是一种找出图是否至少有一个生成树的算法。

  • We represent the spanning tree as an array 'par', where each node points to its parent in the tree.我们将生成树表示为一个数组“par”,其中每个节点都指向树中的父节点。
  • 'par' is initialized with all nodes having themselves as parent. 'par' 初始化为所有节点都将自己作为父节点。
  • We have a function find_root , which follows a chain of 'par' pointers until it reaches a node for which par[node] == node我们有一个函数find_root ,它遵循一连串“par”指针,直到它到达par[node] == node
  • We loop through all the links;我们遍历所有链接; in a straightforward adjacency matrix, this would be: for i in nodes: for j in nodes: if i!=j: if adjacent[i][j] then :在一个简单的邻接矩阵中,这将是: for i in nodes: for j in nodes: if i!=j: if adjacent[i][j] thenfor i in nodes: for j in nodes: if i!=j: if adjacent[i][j] then
    • k = find_root(i)
    • `l = find_root(j) `l = find_root(j)
    • par[k] = l // this puts the tree of i as a sibling to j in j's tree par[k] = l // 这将 i 的树作为 j 树中 j 的兄弟
    • at the end, we check whether or not each node has the same root最后,我们检查每个节点是否具有相同的根

This way, one or more trees are constructed.这样,就构建了一棵或多棵树。 These trees can be used to cluster the nodes into connected groups.这些树可用于将节点聚集成连接的组。

To the basic algorithm, an important speed-up is to not only set par[k] = l but also par[i] = l and par[j] = l .对于基本算法,一个重要的加速是不仅设置par[k] = l还设置par[i] = lpar[j] = l The root will be found earlier the next time.下次会更早找到根。 If the graph is not directional, only one of adjacent[i][j] or adjacent[j][i] needs to be handled.如果图没有方向性,则只需要处理adjacent[i][j]adjacent[j][i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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