[英]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.这是一种找出图是否至少有一个生成树的算法。
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
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] then
: for i in nodes: for j in nodes: if i!=j: if adjacent[i][j] then
:
k = find_root(i)
par[k] = l
// this puts the tree of i as a sibling to j in j's tree par[k] = l
// 这将 i 的树作为 j 树中 j 的兄弟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] = l
和par[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.