You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
upgrade is_valid_degree_sequence()? (migrated from Trac #323) #320
The answer seems to be 'sometimes' I wrote up the algorithm along with the speed up provided by Tripathy and Vijay. And ran it on a variety of graphs and a random degree sequence. Sometimes the Hakimi Havel defined in the paper above is faster sometimes its the Erdos-Gallai. The Erdos-Gallai method seems particularly suited to sparse graphs (excepting maybe the star graph?). Attached is the function, as well as a testing function included.
Also saw a redundant reverse call in the original is_valid_degree_sequence function. The diff is provided. It provides a slight speed up.
Some results from the included testing function
'''
EGdegseq.compare(2000)
Complete Graph HH 280.869007111 ms
Complete Graph EG 731.616020203 ms
Circular Ladder Graph HH 167.409181595 ms
Circular Ladder Graph EG 7.08389282227 ms
Cycle Graph HH 42.4380302429 ms
Cycle Graph EG 2.90894508362 ms
Path Graph HH 42.1850681305 ms
Path Graph EG 2.75802612305 ms
Star Graph HH 1.49607658386 ms
Star Graph EG 2.13098526001 ms
Wheel Graph HH 43.0288314819 ms
Wheel Graph EG 3.54909896851 ms
Erdos-Renyi Graph HH 120.871067047 ms
Erdos-Renyi Graph EG 380.343198776 ms
Barabasi-Albert Graph HH 40.2569770813 ms
Barabasi-Albert Graph EG 13.1220817566 ms
Random Sequence Graph HH 0.968933105469 ms
Random Sequence Graph EG 2.88414955139 ms
'''
Dug a little deeper into the original paper, Tripathy Vijay paper, and reimplemented. Seems to still work, and is slower on some things faster on others. See the second implementation.
'''
EGdegseq.compare(2000)
Complete Graph HH 286.116838455 ms
Complete Graph EG 2.06303596497 ms
Circular Ladder Graph HH 167.79088974 ms
Circular Ladder Graph EG 3.03816795349 ms
Cycle Graph HH 43.1599617004 ms
Cycle Graph EG 1.55591964722 ms
Path Graph HH 41.787147522 ms
Path Graph EG 1.53303146362 ms
Star Graph HH 1.54805183411 ms
Star Graph EG 4.35590744019 ms
Wheel Graph HH 42.9861545563 ms
Wheel Graph EG 4.49705123901 ms
Erdos-Renyi Graph HH 126.682043076 ms
Erdos-Renyi Graph EG 151.902914047 ms
Barabasi-Albert Graph HH 38.8259887695 ms
Barabasi-Albert Graph EG 85.8268737793 ms
Random Sequence Graph HH 0.967979431152 ms
Random Sequence Graph EG 12.6559734344 ms
'''
It looks like the histogram played a big role in reducing the computation time of EG in most cases. With the new set of timings, EG seems like a really nice algorithm.
Generally though, it seems like we should definitely keep both functions, perhaps eventually having a meta-function which picks the appropriate algorithm to use. Though, this would require us being able to quickly determine which method would be quicker...perhaps we might start a convention that if G.graph.get('sparse', False) is True, then we default to algorithms which are suited for sparse graphs.
I also wonder if we should consider Cython implementations for these functions. A quick test should really speed up the generation of random graphs...these is especially true now that there are good alternatives to the Configuration Model. See for example:
Did some thinking about this today. The Hakimi-Havel algorithm will run in at worst O(n^2log n) time. We have to sort each time, and we could potentially remove all values from the list. Even if we consider the fact that the list is getting smaller each time, it's still asymptotically the same.
The crux of the problem is that the list needs to be sorted each time. This might be avoided if we note that we essentially have two sorted sublists one of size of the prior max degree and the rest of the list. We simply need to merge them. This is going to take n time, so we are down to O(n^2).
In practice it's faster than that, because the algorithm devolves into a list of 0s long before we have to check every value. But you're right in saying that this is highly dependent on the degree histogram. I can't quite come up with a concrete determination though...
On the other hand, if u is the number unique items in the list the Erdos-Gallai algorithm will work in O(u*n). This is at worst O(n^2) as well, but again usually shorter, as the inequality will fail to hold earlier.
Another option might be to include both algorithms with an optional argument to the is_valid_degree_sequence function. Then we can test the various random graph algorithms to see which is suited best, and pass in the appropriate argument? It's then up to the user to decide which to use if they want to call it themselves.
It's times like these I wish python had easy multi-threading, just start a thread with each algorithm, and return when the first one finishes.
Is there much cython in networkx? In my little poking around, I haven't noticed a whole lot.
Looks, good though I think you have the labels backwards on your graph, Aric. Also should we generally try to make networkx functions pure python? I included the numpy cumsum, because I assumed it was faster, though I don't know that for sure.
Pure Python is preferred but if it makes sense to use numpy then we should do it. "Makes sense" is of course problem dependent. In case above I suspect the overhead of using a numpy array for parts of the algorithm is not worth if for small graphs - maybe for very large n it would be better.
Definitely a function of the number of unique values in the sequence. Borrowed your comparison function aric and rewrote it so we examine a bunch of random sequences with varying numbers of unique values. See the plot and the new compare function.
I also altered the function itself a bit. I was not getting agreement with the Havel-Hakimi function, because I didn't quite have my index variables in the correct place. It was returning True for some sequences that were not graphical. Now it agrees with Havel-Hakimi on all the test sequences I have run.
Looks like I was getting bogged down in the papers notation, which they primarily used for proof. A much simpler way to calculate the places we need to check the inequality, is index where the degree value changes in the sorted list. This speeds things up a bit.
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-02-07
In [3dab1ea/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="3dab1ea5e3f877b4bc2ca7df86f3a034d4e72d58"
Some spelling changes.
Comment back in integer check since otherwise tests fail.
Still need tests for both methods.
Addresses #323
'''
Activity
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-03
The answer seems to be 'sometimes' I wrote up the algorithm along with the speed up provided by Tripathy and Vijay. And ran it on a variety of graphs and a random degree sequence. Sometimes the Hakimi Havel defined in the paper above is faster sometimes its the Erdos-Gallai. The Erdos-Gallai method seems particularly suited to sparse graphs (excepting maybe the star graph?). Attached is the function, as well as a testing function included.
Also saw a redundant reverse call in the original is_valid_degree_sequence function. The diff is provided. It provides a slight speed up.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @bjedwards, 2010-08-03: valid_deg_seq_slight_speedup.diff
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @bjedwards, 2010-08-03: EGdegseq.py
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-03
Some results from the included testing function
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-03
Dug a little deeper into the original paper, Tripathy Vijay paper, and reimplemented. Seems to still work, and is slower on some things faster on others. See the second implementation.
'''
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @bjedwards, 2010-08-03: EGdegseq.2.py
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2010-08-03
It looks like the histogram played a big role in reducing the computation time of EG in most cases. With the new set of timings, EG seems like a really nice algorithm.
Generally though, it seems like we should definitely keep both functions, perhaps eventually having a meta-function which picks the appropriate algorithm to use. Though, this would require us being able to quickly determine which method would be quicker...perhaps we might start a convention that if G.graph.get('sparse', False) is True, then we default to algorithms which are suited for sparse graphs.
I also wonder if we should consider Cython implementations for these functions. A quick test should really speed up the generation of random graphs...these is especially true now that there are good alternatives to the Configuration Model. See for example:
http://arxiv.org/abs/1002.2975.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-04
Did some thinking about this today. The Hakimi-Havel algorithm will run in at worst O(n^2log n) time. We have to sort each time, and we could potentially remove all values from the list. Even if we consider the fact that the list is getting smaller each time, it's still asymptotically the same.
The crux of the problem is that the list needs to be sorted each time. This might be avoided if we note that we essentially have two sorted sublists one of size of the prior max degree and the rest of the list. We simply need to merge them. This is going to take n time, so we are down to O(n^2).
In practice it's faster than that, because the algorithm devolves into a list of 0s long before we have to check every value. But you're right in saying that this is highly dependent on the degree histogram. I can't quite come up with a concrete determination though...
On the other hand, if u is the number unique items in the list the Erdos-Gallai algorithm will work in O(u*n). This is at worst O(n^2) as well, but again usually shorter, as the inequality will fail to hold earlier.
Another option might be to include both algorithms with an optional argument to the is_valid_degree_sequence function. Then we can test the various random graph algorithms to see which is suited best, and pass in the appropriate argument? It's then up to the user to decide which to use if they want to call it themselves.
It's times like these I wish python had easy multi-threading, just start a thread with each algorithm, and return when the first one finishes.
Is there much cython in networkx? In my little poking around, I haven't noticed a whole lot.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2010-08-05
Here is a version that
I didn't test it carefully. It is faster.
Attached is a plot that shows run time vs density for a set of Gnp random graphs.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @hagberg, 2010-08-05: compare2.py
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-05
Looks, good though I think you have the labels backwards on your graph, Aric. Also should we generally try to make networkx functions pure python? I included the numpy cumsum, because I assumed it was faster, though I don't know that for sure.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @hagberg, 2010-08-05: hh-eg.2.png
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2010-08-05
Right - I fixed the labels.
Pure Python is preferred but if it makes sense to use numpy then we should do it. "Makes sense" is of course problem dependent. In case above I suspect the overhead of using a numpy array for parts of the algorithm is not worth if for small graphs - maybe for very large n it would be better.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-06
Definitely a function of the number of unique values in the sequence. Borrowed your comparison function aric and rewrote it so we examine a bunch of random sequences with varying numbers of unique values. See the plot and the new compare function.
I also altered the function itself a bit. I was not getting agreement with the Havel-Hakimi function, because I didn't quite have my index variables in the correct place. It was returning True for some sequences that were not graphical. Now it agrees with Havel-Hakimi on all the test sequences I have run.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @bjedwards, 2010-08-06: compare3.py
3 remaining items
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @bjedwards, 2010-08-09
Looks like I was getting bogged down in the papers notation, which they primarily used for proof. A much simpler way to calculate the places we need to check the inequality, is index where the degree value changes in the sorted list. This speeds things up a bit.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @bjedwards, 2010-08-09: compare4.py
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2010-09-06
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-02-06
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Chris Ellison <cellison, 2011-02-07
In [a8159f1/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="a8159f152993de59e0611d395abc5f03b9ec4cb9"
Removing redundent reverse call. Addresses #323
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Chris Ellison <cellison, 2011-02-07
In [3316f18/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="3316f18c7ecc9d351ab5ed3ec277231ec49c9cf4"
Adding Erdos-Gallai algorithm. Addresses #323
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2011-02-07
I've updated so that a meta method chooses one of the two algorithms.
Which should be the default though?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-02-07
I don't think we ever figured out which one we wanted as the default.
Maybe stay with the original (HH) method.
The tests should be updated to run on both of the algorithms.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-02-07
In [3dab1ea/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="3dab1ea5e3f877b4bc2ca7df86f3a034d4e72d58"
Some spelling changes.
Comment back in integer check since otherwise tests fail.
Still need tests for both methods.
Addresses #323
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Chris Ellison <cellison, 2011-02-07
In [4c71b02/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="4c71b0288bd4898929e47f014381ddf8a85b785c"
Change default method to 'hh'. Add some tests. Addresses #323
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Chris Ellison <cellison, 2011-02-07
In [ed6a5a9/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="ed6a5a90dd1f35b9126d7445fc1fb6625ff0b8ef"
Updating tests for hh and eg methods. Addresses #323
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-02-07
In [0e80981/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="0e809811e763fcc19ec3771753a8361daad5ea02"
Adjust for Python2ism.
Addresses #323
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-02-07
Looks great.
Shall we call it done?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2011-02-07
I think so! Eventually, we should add guidelines describing which algorithm to use and when...but I don't think we have a good answer yet.