root-11 / graph-theory

A simple graph library
MIT License
82 stars 19 forks source link

`has_cycles` allways returns false if the graph is disconnected #39

Closed Sappique closed 6 months ago

Sappique commented 6 months ago

The function has_cycles allways returns false if the graph is disconnected, including when a cycle exists.

If this is the intended behavior, the documentation should be modified to clearly convey that.

To reproduce

Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
>>> from graph import Graph
>>> g = Graph(from_list=[(1,2),(2,3),(3,1)])
>>> g.has_cycles()
True
>>> g.add_node(4)
>>> g.has_cycles()
False

Additional information

I'm using version 2023.7.4 of graph-theory with Python 3.10 on windows.

root-11 commented 6 months ago

Excellent catch. Let me fix that immediately.

Sappique commented 6 months ago

I just tested it again and figured out that has_cycles returns false if any of the disconnected parts of the graph have no cycles. If all disconnected parts have cycles it returns true (as expected):

Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
>>> from graph import Graph
>>> g = Graph(from_list=[(1,2),(2,3),(3,1)])
>>> g.has_cycles()
True
>>> g.add_edge(4,5)
>>> g.has_cycles()
False
>>> g.add_edge(5,4)
>>> g.has_cycles()
True
root-11 commented 6 months ago

Good catch. It's due to the same bug.

root-11 commented 6 months ago

I'm running the test suite right now. I'll release a new package in a minute or so when it finishes.

root-11 commented 6 months ago

Bugfix release created as 2023.7.5. Credit assigned to your github id.

The version is available on pypi.org as https://pypi.org/project/graph-theory/2023.7.5/

To upgrade use pip install --upgrade graph-theory

Thanks again ;-)

root-11 commented 6 months ago

@Sappique - feel free to close the ticket if you are satisfied.