Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tree recognition functionality #925

Merged
merged 11 commits into from
Sep 12, 2013

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Aug 8, 2013

I have written a simple function to recognise if a certain graph is a tree or not. If the following is true, the input graph is considered a tree:

  • The number of edges is equal to the number of nodes minus one
  • It has no self-loops
  • It is connected
  • It is undirected (an object of the Graph class). Actually I'm not really sure that using the type function is a good approach. Also an exception could be raised if another type of graph is given as input.
    In my implementation, an empty graph is considered a tree too.

This is my first attempt to collaborate on this project and also to an open source project in general, so let me know if I'm going in the right direction, if I need to modify something, what I'm doing wrong and so on. I have also tried to write a series of tests for the function.
If I'm going well I would like to add some other small functions, like a forest recognition. I know that these are really simple functions but I've encountered the need of them in a school project and I had to implement them myself, using a really stupid implementation at that time. So I think that this could be useful to someone else.
So, be kind with me and let me know what you think :)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0%) when pulling e28f382 on markusian:trees_forests_recognition into 771b964 on networkx:master.

"""

if type(G) == nx.Graph:
return nx.number_of_nodes(G) == 0 or (nx.is_connected(G) and ( nx.number_of_edges(G) == nx.number_of_nodes(G)-1 ) and G.number_of_selfloops()==0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last check, G.number_of_selfloops()==0, is useless: a connected graph with self-loops necessarily has more than n - 1 edges.

On a different note, try to format the code so that line length is at most 79 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are completely right. I have made a new commit to remove the useless check and improve the formatting.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0%) when pulling 13251c6 on markusian:trees_forests_recognition into 771b964 on networkx:master.

@loicseguin
Copy link
Member

I am wondering if creating a new package for this is not overkill. If only a few functions would end up in this tree/forrest recognition, it is probably better to just make it a module in the algorithms package.

@Markusian how many more such functions were you thinking about?

@papafe
Copy link
Contributor Author

papafe commented Aug 9, 2013

I think that you are probably right. My initial plan was to implement a tree and a forest recognition and then see if someone else has other ideas that could be implemented.
Another thing that could be done is a function that, given a tree, return the list of leaf nodes, but I do not have many other ideas at the moment.

For undirected graphs only.
"""

if type(G) == nx.Graph:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually tests using isinstance are preferable since subclasses of nx.Graph will pass as well:

if isinstance(G, nx.Graph):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better in this case would be to check if the graph is directed using G.is_directed().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure of type either, but using isinstance was a problem for inheritance as DiGraph is a subclass of Graph.
I think that it is a very good idea to use is_directed

…f the input graph

with the appropriate is_directed() function.

With this change, graph of the class MultiGraph can be correctly recognised as tree too.
The test suite has been modified accordingly
@coveralls
Copy link

Coverage Status

Coverage decreased (-0%) when pulling 22f39f8 on markusian:trees_forests_recognition into 771b964 on networkx:master.

Slightly modified the is_tree() function.
@ghost ghost assigned hagberg Sep 8, 2013
hagberg added a commit that referenced this pull request Sep 12, 2013
@hagberg hagberg merged commit a0ccc00 into networkx:master Sep 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants