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 19, 2025
2 parents d21117f + 8e82bfc commit b59fef8
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,20 +763,40 @@ def __mul__(self, n):

sage: G = graphs.CycleGraph(3)
sage: H = G * 3; H
Cycle graph disjoint_union Cycle graph disjoint_union Cycle graph: Graph on 9 vertices
Disjoint union of 3 copies of Cycle graph: Graph on 9 vertices
sage: H.vertices(sort=True)
[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')
ns = self.order()
ntot = n * ns
vint = {u: i for i, u in enumerate(self)}
edges = ((i, j, l) for u, v, l in self.edge_iterator()
for i, j in zip(range(vint[u], ntot, ns),
range(vint[v], ntot, ns)))
return self.__class__([range(ntot), edges], format='vertices_and_edges',
loops=self.allows_loops(),
multiedges=self.allows_multiple_edges(),
immutable=self.is_immutable(),
name=f"Disjoint union of {n} copies of {str(self)}")
raise TypeError('multiplication of a graph and something other than an integer is not defined')

def __ne__(self, other):
"""
Expand Down Expand Up @@ -809,7 +829,7 @@ def __rmul__(self, n):

sage: G = graphs.CycleGraph(3)
sage: H = int(3) * G; H
Cycle graph disjoint_union Cycle graph disjoint_union Cycle graph: Graph on 9 vertices
Disjoint union of 3 copies of Cycle graph: Graph on 9 vertices
sage: H.vertices(sort=True)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
"""
Expand Down

0 comments on commit b59fef8

Please sign in to comment.