sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.43k stars 479 forks source link

Linear time implementation of Modular Decomposition for Undirected Graphs #23487

Closed lokeshj1703 closed 7 years ago

lokeshj1703 commented 7 years ago

This is aimed at providing linear time implementation for finding modular decomposition of undirected graphs, fixing the currently broken Sage's graph modular decomposition.

CC: @dimpase @dcoudert

Component: graph theory

Keywords: modular decomposition, gsoc2017

Author: Lokesh Jain

Branch: 6688bac

Reviewer: David Coudert, Dima Pasechnik

Issue created by migration from https://trac.sagemath.org/ticket/23487

dimpase commented 7 years ago

Changed keywords from modular decomposition to modular decomposition, GSOC 2017

dimpase commented 7 years ago
comment:1

It will use the implementation from https://github.com/lokeshj1703/Undirected-Modular-Decomposition

dimpase commented 7 years ago

Description changed:

--- 
+++ 
@@ -1 +1 @@
-This ticket is related to GSOC 2017 and is aimed at providing linear time implementation for finding modular decomposition of undirected graphs.
+This is aimed at providing linear time implementation for finding modular decomposition of undirected graphs, fixing the currently broken Sage's graph modular decomposition.
dimpase commented 7 years ago

Changed keywords from modular decomposition, GSOC 2017 to modular decomposition, gsoc2017

lokeshj1703 commented 7 years ago

Branch: u/jlokesh/23487

lokeshj1703 commented 7 years ago

New commits:

340580etrac #23487: added modular decomposition module + removed old one
lokeshj1703 commented 7 years ago

Commit: 340580e

dcoudert commented 7 years ago
comment:4

Dear Lokesh,

why have you removed file modular_decomposition.pyx ? You should put it back for the moment. There are dependencies and so I'm unable to compile sage with your patch.

Many improvements are needed in your code that we will slowly address. Dima is certainly more expert than me in this. For instance:

dcoudert commented 7 years ago

Reviewer: David Coudert, Dmitrii Pasechnik

dimpase commented 7 years ago
comment:5

Replying to @dcoudert:

Dear Lokesh,

why have you removed file modular_decomposition.pyx ? You should put it back for the moment. There are dependencies and so I'm unable to compile sage with your patch.

this should be trivial to fix. Let's remove it now for good.

dimpase commented 7 years ago

Changed commit from 340580e to dd540d6

dimpase commented 7 years ago
comment:6

this is the updates needed for Sage to build and run. modular_decomposition doctests in graph.py fail due to some format incompatibility of output of the new implementation. I'll leave it to Lokesh to fix, as well as the David's comments, naturally.


New commits:

dd540d6updates to allow sage build. doctests in graph.py still fail
dimpase commented 7 years ago

Changed branch from u/jlokesh/23487 to public/gsoc17_t23487

lokeshj1703 commented 7 years ago
comment:7

Sorry for the late reply. I have started working on the updates. I will push the commit shortly.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

83387cbtrac #23487: improved readability
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from dd540d6 to 83387cb

lokeshj1703 commented 7 years ago
comment:9

I have added a commit for improved readability in the code. This commit addresses first four issues pointed out by David. I will soon add a commit for including examples, test cases and for handling failure of doctests.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 83387cb to 0a5a504

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

0a5a504trac #23487: added tests, examples + fixed doctests
lokeshj1703 commented 7 years ago
comment:11

I have added the commit for fixing the doctests and added the Tests and Examples for modular_decomposition function. Should I add the examples for other functions as well? Because most of the functions are called from modular_decomposition itself.

For the tests for modular_decomposition I have added the graph from Marc Tedder research paper and an example from the wikipedia modular decomposition page. Further I have added a series graph (Tetrahedral Graph).

f29946bc-ee7b-48cd-9abc-3445948c551d commented 7 years ago
comment:13

Is #13744 a duplicate of this?

dimpase commented 7 years ago
comment:14

Replying to @jm58660:

Is #13744 a duplicate of this?

Yes, the latter can be closed as won't fix, with the fixing done here.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 0a5a504 to 1e6c92f

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

62e5487Merge branch 'public/gsoc17_t23487' of trac.sagemath.org:sage into moddec
1e6c92frebased over curr. beta and removed an obsolete NOTE
dimpase commented 7 years ago
comment:16

As this is to be included in 8.1, the branch should be based on the current beta - as I just did in 1e6c92f.

dimpase commented 7 years ago
comment:17

I will clean up the import statements from sage.*. Specifically, there is a handy import_statements() command in Sage:

sage: import_statements('Graph')
from sage.graphs.graph import Graph

importing from sage.all instead is much less efficient.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

646507efixed import and name/address in copyright
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 1e6c92f to 646507e

dimpase commented 7 years ago
comment:19

Is there a function to create the quotient graph with the vertices being the max. modules?

Please also make it clear which functions are a part of the main algorithm, and which ones are only used for testing.

Please also add more TEST and EXAMPLE; e.g. the functions to compute mu certainly need tests. In general, all the functions, except 1-liners, will need tests and docs.

dcoudert commented 7 years ago
comment:20

Some comments:

while tree[0].node_type != NORMAL:
    tree = tree[1][0]
return tree[1][0]

The writing style in sagemath is to start methods with a 1 line description of the functionality, then a blank line, then a longer description of the functionality if needed. So

def recursively_number_cocomponents(tree, cocomp_num, by_type):
    """
    Recursively number the nodes in the (co)components.

    If the tree node_type is same as by_type then cocomp_num is incremented
    before assigning to the subtree else entire tree is numbered by cocomp_num.

    INPUTS:
    """

It's a long patch to review...

lokeshj1703 commented 7 years ago
comment:21

Replying to @dimpase:

Is there a function to create the quotient graph with the vertices being the max. modules?

Currently there is no function which creates a quotient graph with the vertices being the maximal modules. I can create such a function though?

Please also make it clear which functions are a part of the main algorithm, and which ones are only used for testing.

David has asked me to move the test functions at the end after a separator, it will solve this problem.

Please also add more TEST and EXAMPLE; e.g. the functions to compute mu certainly need tests. In general, all the functions, except 1-liners, will need tests and docs.

I will definitely add more TESTS and EXAMPLES. It might take some time as each function requires some arguments mostly a tree or a module. I will need to input that manually.

Thanks for fixing the import and copyright!

lokeshj1703 commented 7 years ago
comment:22

Replying to @dcoudert:

Thanks for reviewing the patch! I will make the changes and add a commit soon.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 646507e to 433ee5e

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

433ee5etrac #23487: improved code + documentation
dcoudert commented 7 years ago
comment:24

It is much better now. Some more comments.

lokeshj1703 commented 7 years ago
comment:25

Replying to @dcoudert:

  • I have an identation issue with the """ before the code

I could not understand which """ you are talking about? Is it the one in the beginning? Is the indentation issue in the doc build?

  • in compute_mu_for_component: why not using range(source_index-1, -1, -1) and returning the first found value ?
  if mu_for_component == root[1][index] and \

Suppose [c01, c02, c03, source, c1, c2, c3] is the forest. If mu value for c2 is c03 then c01 and c02 must be connected to c2. Therefore the iteration from 0 is required.

I have incorporated the other changes and will add a commit for it.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

72d87e8improvements in the code and documentation
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 433ee5e to 72d87e8

dcoudert commented 7 years ago
comment:27

Replying to @lokeshj1703:

Replying to @dcoudert:

  • I have an identation issue with the """ before the code

I could not understand which """ you are talking about? Is it the one in the beginning? Is the indentation issue in the doc build?

The one before if graph._directed.

Note that it is better to use if graph.is_directed() ;)

dcoudert commented 7 years ago
comment:28

When you add a link to a wikipedia page, the nice way to do it is: :wikipedia:`Modular_decomposition`.

lokeshj1703 commented 7 years ago
comment:29

Replying to @dcoudert:

The one before if graph._directed.

Note that it is better to use if graph.is_directed() ;)

Found it. :) I will include these changes in the next commit along with TESTS and EXAMPLES for other functions.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 72d87e8 to 6f7af1e

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

6f7af1etrac #23487: added TESTS and EXAMPLES
2c00b582-cfe9-43b9-8124-fec470060704 commented 7 years ago
comment:31

Out of curiosity, have you considered using collections.dequeue instead of making your own (inefficient) Queue class?

https://wiki.python.org/moin/TimeComplexity

dcoudert commented 7 years ago
comment:32

I was not aware of this data structure. It's certainly what we need here.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

92c1de0Merge branch 'develop' into moddec
1ff6668Merge branch 'public/gsoc17_t23487' of trac.sagemath.org:sage into moddec
ab14394removed "print " from tests/examples
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from 6f7af1e to ab14394

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

caedc65trac #23487: Used colloection.deque instead of Queue
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from ab14394 to caedc65

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 7 years ago

Changed commit from caedc65 to 12f3f15