1

I am studying the solutions posted by Marko Riedel at https://math.stackexchange.com/questions/689526/how-many-connected-graphs-over-v-vertices-and-e-edges. I am porting some of the techniques listed there to another language (Python). I have two problems that are keeping me from succeeding.

1) When the code examples make use of the variable u, where (and how) is that variable defined? I see where it appears as part of a generating function, but I don't yet understand why it appears there. My immediate concern is that in the coding snippets, I don't see it defined anywhere. What should I do if I am attempting to port code that references u, such as gf3, gf4, and gf5?

2) In the function qq (accompanying the code for function gf5), there is a line that reads like this:

for p from max(0, k-1/2*(m+1)*m) to k-m do

I have a question about the k-1/2*(m+1)*m part. In languages like Python, where integer arithmetic is performed such that 1/2 = 0), should this be coded as something like k-((m+1)*m/2)?

Thanks a lot. As is probably obvious, I'm diving into something that's a little deep in math for me, and appreciate any help.

chris
  • 111
  • It is strange indeed that this one post of mine should generate that much traffic. If this is homework please do remember that the fastest way to clear up your doubts is to make an appointment with your professor during office hours. The variable u is a symbolic variable from a generating function. You need a symbolic math package that works with polynomials at the very least. That said there are some functions in the answer where z and u have been optimized out. You could try working with these or port the C code. Note that the bivariate GF gf3 is the most efficient answer. – Marko Riedel Feb 16 '15 at 22:20
  • Thank you, Marko. Definitely not homework, at least not for me. I will probably port the C code, because I can't get all of my test cases to come out right with the other functions. I'm not sure if that's because I'm mishandling large numbers (I'm new to Python) or if I've implemented something wrong. After I have this immediate task knocked down, I will try to read up on generating functions (and symbolic variables generated by them). Thanks! – chris Feb 16 '15 at 23:25
  • I wanted to let you know that I have successfully ported the code to Python, following your suggestion to focus on sections that had z and u optimized out. I can run all iterations for n <= 20 in about 1.25 seconds, and for n <= 30 in about 11.5 seconds on my Macbook. Now I just need to optimize a bit more, and all will be well. Thanks again for your help! – chris Feb 17 '15 at 21:09
  • Final comment: I found one more optimization that let me get done with all iterations for 2 <= n <= 20 in 0.4 seconds and 2 <= n <= 30 in 5.4 seconds. This if for (n-1) <= k <= n*(n-1)/2. Now I'm happy. Thanks again, Marko.

    I'm thinking about going back to grad school to finish my PhD (I bailed the first time with a Masters), and I have to admit, this whets my appetite a little bit. :)

    – chris Feb 17 '15 at 22:57
  • Good to know that you were able to confirm these values. The generating function in $u$ is faster than the formulae where $u$ has been optimized out and I find it interesting that these were sufficient for your purposes. – Marko Riedel Feb 18 '15 at 03:53
  • Well, I implemented two different ways in Python. One was a straight-up implementation of the 3-part, bracketed formula at the start of the "Definitely the last addendum" section (call that implementation A). The other was based primarily on qq (used with gf5) (call this implementation B). I also built the C code for comparison purposes (call this one ... C, of course). (1/3) – chris Feb 19 '15 at 05:08
  • The output of A did not agree with the output of C, but the output of B did. It turns out that the problem with A was that I had an line that read return(n**(nm2)) (where nm2 was set to n - 2). Because there were instances where n was 1, this became return(1**(-1)) in Python, which evaluates to the float -1.0. Of course, evil ensued. By changing that line to read return(int(n**(nm2))), evil was averted, and I got identical output for A, B, and C. (2/3) – chris Feb 19 '15 at 05:08
  • Because someone will want to know (maybe you, Marko!), here are the timings for calculating 2 <= n <= 20 and 0 <= k <= n*(n-1)/2. I tried to format this nicely but failed. Program A: real 0m0.406s, user 0m0.402s, sys 0m0.004s. Program B: real 0m0.342s, user 0m0.333s, sys 0m0.008s. Program C: real 0m0.079s, user 0m0.057s, sys 0m0.022s

    I had to stick to standard Python libraries (mostly), so no fancy math libraries used, and just the standard python 2.7.6 executable. This was fun!

    – chris Feb 19 '15 at 05:38

0 Answers0