Graphs.jl icon indicating copy to clipboard operation
Graphs.jl copied to clipboard

`is_cyclic` for undirected graphs

Open samwycherley opened this issue 3 years ago • 6 comments

Issue previously open at #1518 for LightGraphs.jl.

The is_cyclic function in LightGraphs.jl works well for directed graphs but for undirected graphs returns true for any nonempty graph. The usual definition for undirected graphs is that cycles don't contain repeated links, so 1->2->1 is not considered a cycle (absent multiple edges.)

Useful for me because I want a function that will test whether an (undirected) network is a tree and easiest way to check that is to confirm the graph is acyclic+connected

samwycherley avatar Nov 02 '21 10:11 samwycherley

My understanding was that for an undirected graph g you can check if it is a tree if ne(g) == nv(g) - 1 and it is connected (https://en.wikipedia.org/wiki/Tree_(graph_theory)#Tree).

Maybe there should also be a generic is_tree function that covers both directed and undirected graphs?

mtfishman avatar Nov 02 '21 15:11 mtfishman

Ah yes thanks, that's a more efficient route. Still, would be nice to have is_cyclic working for undirected graphs

samwycherley avatar Nov 02 '21 17:11 samwycherley

@samwycherley, Are you interested in finding an algorithm for this? If not I'll close since @mtfishman solved your direct problem. The non-backtracking part makes its a bit harder.

jpfairbanks avatar Nov 08 '21 18:11 jpfairbanks

When I have time, I'll try and work on it and put up a PR. Feel free to close in the meantime.

samwycherley avatar Nov 08 '21 19:11 samwycherley

Is someone working on this Issue? If not, I would like to implement it as a first issue. If we want to add a generic function is_tree as suggested, we might need to consider the disconnected case as well (a graph could have multiple trees as components -> forest).

pgrepds avatar Jan 19 '22 16:01 pgrepds

@pgrepds feel free to contribute, it is a nice first issue to tackle!

gdalle avatar Mar 10 '22 07:03 gdalle

Because of this that say the behavior was intended, wouldn't it be a breaking change ? To clear the confusion, maybe we could deprecate this function , and have a is_directed_acyclic that would work only for directed graphs, and a is_forest that would work only for undirected graphs ?

etiennedeg avatar Aug 29 '22 14:08 etiennedeg

It seems that #168 is ready to be merged to close this. The pull request is by a student in our team at INRIA UCA and we hope to contribute more now that more members of the team are starting to use Julia.

natema avatar Sep 20 '22 09:09 natema

closed by #168

etiennedeg avatar Sep 21 '22 14:09 etiennedeg