简体   繁体   中英

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

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' is initialized with all nodes having themselves as parent.
  • We have a function find_root , which follows a chain of 'par' pointers until it reaches a node for which 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 :
    • k = find_root(i)
    • `l = find_root(j)
    • par[k] = l // this puts the tree of i as a sibling to j in j's tree
    • 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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