Skip to content

Commit

Permalink
sagemathgh-39300: faster method __mul__ for graphs
Browse files Browse the repository at this point in the history
Instead of $n-1$ additions of graphs, we use a logarithmic number of
additions of graphs to compute $G * n$.

Before
```sage
sage: def test(G):
....:     for i in [1, 2, 3, 4, 5, 10, 15, 20, 50, 100]:
....:         t = walltime()
....:         for _ in range(10):
....:             H = G * i
....:         t = walltime() - t
....:         print(f"{i}\t {round(t/10, 5)}")
sage: test(Graph([(0, 1)]))
1        3e-05
2        0.00011
3        0.00016
4        0.00024
5        0.00032
10       0.00072
15       0.00108
20       0.00139
50       0.00437
100      0.01272
sage: test(graphs.PetersenGraph())
1        5e-05
2        0.00017
3        0.00028
4        0.00042
5        0.00055
10       0.00148
15       0.0024
20       0.00343
50       0.01802
100      0.07057
```

Now
 ```sage
sage: test(Graph([(0, 1)]))
1        3e-05
2        0.0001
3        0.00018
4        0.00018
5        0.00025
10       0.00035
15       0.00053
20       0.00043
50       0.00083
100      0.00118
sage: test(graphs.PetersenGraph())
1        4e-05
2        0.00015
3        0.00028
4        0.00026
5        0.00041
10       0.00066
15       0.00117
20       0.00094
50       0.0024
100      0.00448
```

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

URL: sagemath#39300
Reported by: David Coudert
Reviewer(s): Frédéric Chapoton
  • Loading branch information
Release Manager committed Jan 9, 2025
2 parents 5016661 + 37729d5 commit 2569018
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tarball=configure-VERSION.tar.gz
sha1=852d0d200a6a73aa5ddb9e00874cbe4a61c211e9
sha256=c4b089d90850dfdf15b905f66e4f6a0d961b96eb0663d8603beaff1a9efb2cbe
sha1=95f56ac41944f659f82ba07ce5ba1f0f46bcdf72
sha256=8b762b01f240eed4b4f795e3cfcf6c2751c37f87dd778ce5ae6c75a92be63731
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a2ba1f943f88775218c385efe55509c4548d1b44
a8b7c202f19662523eac053ce778d270dc90011f
25 changes: 22 additions & 3 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,15 +768,34 @@ def __mul__(self, n):
[0, 1, 2, 3, 4, 5, 6, 7, 8]
sage: H = G * 1; H
Cycle graph: Graph on 3 vertices

TESTS::

sage: Graph(1) * -1
Traceback (most recent call last):
...
TypeError: multiplication of a graph and a nonpositive integer is not defined
sage: Graph(1) * 2.5
Traceback (most recent call last):
...
TypeError: multiplication of a graph and something other than an integer is not defined
"""
if isinstance(n, (int, Integer)):
if n < 1:
raise TypeError('multiplication of a graph and a nonpositive integer is not defined')
if n == 1:
return copy(self)
return sum([self] * (n - 1), self)
else:
raise TypeError('multiplication of a graph and something other than an integer is not defined')
# Use a logarithmic number of additions to build the result
bits = Integer(n).bits()
parts = [self]
parts.extend(parts[-1] + parts[-1] for _ in range(1, len(bits)))
H, _ = parts.pop(), bits.pop()
while bits:
g, b = parts.pop(), bits.pop()
if b:
H += g
return H
raise TypeError('multiplication of a graph and something other than an integer is not defined')

def __ne__(self, other):
"""
Expand Down

0 comments on commit 2569018

Please sign in to comment.