Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non recursive version of method gomory_hu_tree for graphs #38791

Merged
merged 4 commits into from
Nov 16, 2024

Conversation

dcoudert
Copy link
Contributor

@dcoudert dcoudert commented Oct 9, 2024

This PR implements a non recursive version of method gomory_hu_tree for graphs. This fixes the max recursion depth error reported in https://ask.sagemath.org/question/79577/graphs-gomory-hu-tree-memory-blow-up-and-max-recursion-depth/. The memory consumption is seriously reduced.

Without this PR:

sage: def test(g):
....:     from datetime import datetime
....:     import psutil
....:     start_time = datetime.now()
....:     process = psutil.Process(os.getpid())
....:     mem = process.memory_info()[0] / float(2 ** 20)
....:     print("Mem usage at start:", mem, "MiB")
....:     try:
....:         print("Vertices found:", g.order(), "and edges:", g.size())
....:         T = g.gomory_hu_tree(algorithm="FF")
....:     except Exception as error:
....:         print("Error detected:", error)
....:     finally:
....:         end_time = datetime.now()
....:         print("Runtime =", end_time - start_time)
....:         mem = process.memory_info()[0] / float(2 ** 20)
....:         print("Mem usage at end:", mem, "MiB")
....: 
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 241.0234375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.243477
Mem usage at end: 252.1171875 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 252.1171875 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:02.050924
Mem usage at end: 324.30078125 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 324.30078125 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:21.207451
Mem usage at end: 968.97265625 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 950.25390625 MiB
Vertices found: 3282 and edges: 6561
Error detected: maximum recursion depth exceeded
Runtime = 0:04:36.154550
Mem usage at end: 6767.39453125 MiB

With this PR

sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 246.0859375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.219925
Mem usage at end: 246.7109375 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 247.0234375 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:01.900761
Mem usage at end: 248.2734375 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 248.5859375 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:18.535145
Mem usage at end: 252.4921875 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 253.1171875 MiB
Vertices found: 3282 and edges: 6561
Runtime = 0:02:57.325506
Mem usage at end: 265.15234375 MiB
sage: test(graphs.SierpinskiGasketGraph(9))
Mem usage at start: 263.625 MiB
Vertices found: 9843 and edges: 19683
Runtime = 0:29:03.321870
Mem usage at end: 296.8359375 MiB
sage: test(graphs.SierpinskiGasketGraph(10))
Mem usage at start: 303.11328125 MiB
Vertices found: 29526 and edges: 59049
Runtime = 5:01:45.355463
Mem usage at end: 399.3984375 MiB

📝 Checklist

  • The title is concise and informative.
  • 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

Copy link

github-actions bot commented Oct 9, 2024

Documentation preview for this PR (built with commit b844daa; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dcoudert
Copy link
Contributor Author

@maxale, this is ready to be reviewed.

@dcoudert dcoudert requested a review from dimpase October 25, 2024 17:24
@dimpase
Copy link
Member

dimpase commented Nov 3, 2024

I'm slow to respond in the coming few days. We've just moved Oxford U. -> Northwestern U.

Copy link
Member

@dimpase dimpase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

vbraun pushed a commit to vbraun/sage that referenced this pull request Nov 8, 2024
…r graphs

    
This PR implements a non recursive version of method `gomory_hu_tree`
for graphs. This fixes the max recursion depth error reported in
https://ask.sagemath.org/question/79577/graphs-gomory-hu-tree-memory-
blow-up-and-max-recursion-depth/. The memory consumption is seriously
reduced.

Without this PR:
```py
sage: def test(g):
....:     from datetime import datetime
....:     import psutil
....:     start_time = datetime.now()
....:     process = psutil.Process(os.getpid())
....:     mem = process.memory_info()[0] / float(2 ** 20)
....:     print("Mem usage at start:", mem, "MiB")
....:     try:
....:         print("Vertices found:", g.order(), "and edges:",
g.size())
....:         T = g.gomory_hu_tree(algorithm="FF")
....:     except Exception as error:
....:         print("Error detected:", error)
....:     finally:
....:         end_time = datetime.now()
....:         print("Runtime =", end_time - start_time)
....:         mem = process.memory_info()[0] / float(2 ** 20)
....:         print("Mem usage at end:", mem, "MiB")
....:
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 241.0234375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.243477
Mem usage at end: 252.1171875 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 252.1171875 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:02.050924
Mem usage at end: 324.30078125 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 324.30078125 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:21.207451
Mem usage at end: 968.97265625 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 950.25390625 MiB
Vertices found: 3282 and edges: 6561
Error detected: maximum recursion depth exceeded
Runtime = 0:04:36.154550
Mem usage at end: 6767.39453125 MiB
```

With this PR
```py
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 246.0859375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.219925
Mem usage at end: 246.7109375 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 247.0234375 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:01.900761
Mem usage at end: 248.2734375 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 248.5859375 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:18.535145
Mem usage at end: 252.4921875 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 253.1171875 MiB
Vertices found: 3282 and edges: 6561
Runtime = 0:02:57.325506
Mem usage at end: 265.15234375 MiB
sage: test(graphs.SierpinskiGasketGraph(9))
Mem usage at start: 263.625 MiB
Vertices found: 9843 and edges: 19683
Runtime = 0:29:03.321870
Mem usage at end: 296.8359375 MiB
sage: test(graphs.SierpinskiGasketGraph(10))
Mem usage at start: 303.11328125 MiB
Vertices found: 29526 and edges: 59049
Runtime = 5:01:45.355463
Mem usage at end: 399.3984375 MiB
```




### :memo: 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.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### :hourglass: 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#38791
Reported by: David Coudert
Reviewer(s): Dima Pasechnik
vbraun pushed a commit to vbraun/sage that referenced this pull request Nov 9, 2024
…r graphs

    
This PR implements a non recursive version of method `gomory_hu_tree`
for graphs. This fixes the max recursion depth error reported in
https://ask.sagemath.org/question/79577/graphs-gomory-hu-tree-memory-
blow-up-and-max-recursion-depth/. The memory consumption is seriously
reduced.

Without this PR:
```py
sage: def test(g):
....:     from datetime import datetime
....:     import psutil
....:     start_time = datetime.now()
....:     process = psutil.Process(os.getpid())
....:     mem = process.memory_info()[0] / float(2 ** 20)
....:     print("Mem usage at start:", mem, "MiB")
....:     try:
....:         print("Vertices found:", g.order(), "and edges:",
g.size())
....:         T = g.gomory_hu_tree(algorithm="FF")
....:     except Exception as error:
....:         print("Error detected:", error)
....:     finally:
....:         end_time = datetime.now()
....:         print("Runtime =", end_time - start_time)
....:         mem = process.memory_info()[0] / float(2 ** 20)
....:         print("Mem usage at end:", mem, "MiB")
....:
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 241.0234375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.243477
Mem usage at end: 252.1171875 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 252.1171875 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:02.050924
Mem usage at end: 324.30078125 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 324.30078125 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:21.207451
Mem usage at end: 968.97265625 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 950.25390625 MiB
Vertices found: 3282 and edges: 6561
Error detected: maximum recursion depth exceeded
Runtime = 0:04:36.154550
Mem usage at end: 6767.39453125 MiB
```

With this PR
```py
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 246.0859375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.219925
Mem usage at end: 246.7109375 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 247.0234375 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:01.900761
Mem usage at end: 248.2734375 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 248.5859375 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:18.535145
Mem usage at end: 252.4921875 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 253.1171875 MiB
Vertices found: 3282 and edges: 6561
Runtime = 0:02:57.325506
Mem usage at end: 265.15234375 MiB
sage: test(graphs.SierpinskiGasketGraph(9))
Mem usage at start: 263.625 MiB
Vertices found: 9843 and edges: 19683
Runtime = 0:29:03.321870
Mem usage at end: 296.8359375 MiB
sage: test(graphs.SierpinskiGasketGraph(10))
Mem usage at start: 303.11328125 MiB
Vertices found: 29526 and edges: 59049
Runtime = 5:01:45.355463
Mem usage at end: 399.3984375 MiB
```




### :memo: 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.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### :hourglass: 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#38791
Reported by: David Coudert
Reviewer(s): Dima Pasechnik
vbraun pushed a commit to vbraun/sage that referenced this pull request Nov 13, 2024
…r graphs

    
This PR implements a non recursive version of method `gomory_hu_tree`
for graphs. This fixes the max recursion depth error reported in
https://ask.sagemath.org/question/79577/graphs-gomory-hu-tree-memory-
blow-up-and-max-recursion-depth/. The memory consumption is seriously
reduced.

Without this PR:
```py
sage: def test(g):
....:     from datetime import datetime
....:     import psutil
....:     start_time = datetime.now()
....:     process = psutil.Process(os.getpid())
....:     mem = process.memory_info()[0] / float(2 ** 20)
....:     print("Mem usage at start:", mem, "MiB")
....:     try:
....:         print("Vertices found:", g.order(), "and edges:",
g.size())
....:         T = g.gomory_hu_tree(algorithm="FF")
....:     except Exception as error:
....:         print("Error detected:", error)
....:     finally:
....:         end_time = datetime.now()
....:         print("Runtime =", end_time - start_time)
....:         mem = process.memory_info()[0] / float(2 ** 20)
....:         print("Mem usage at end:", mem, "MiB")
....:
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 241.0234375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.243477
Mem usage at end: 252.1171875 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 252.1171875 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:02.050924
Mem usage at end: 324.30078125 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 324.30078125 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:21.207451
Mem usage at end: 968.97265625 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 950.25390625 MiB
Vertices found: 3282 and edges: 6561
Error detected: maximum recursion depth exceeded
Runtime = 0:04:36.154550
Mem usage at end: 6767.39453125 MiB
```

With this PR
```py
sage: test(graphs.SierpinskiGasketGraph(5))
Mem usage at start: 246.0859375 MiB
Vertices found: 123 and edges: 243
Runtime = 0:00:00.219925
Mem usage at end: 246.7109375 MiB
sage: test(graphs.SierpinskiGasketGraph(6))
Mem usage at start: 247.0234375 MiB
Vertices found: 366 and edges: 729
Runtime = 0:00:01.900761
Mem usage at end: 248.2734375 MiB
sage: test(graphs.SierpinskiGasketGraph(7))
Mem usage at start: 248.5859375 MiB
Vertices found: 1095 and edges: 2187
Runtime = 0:00:18.535145
Mem usage at end: 252.4921875 MiB
sage: test(graphs.SierpinskiGasketGraph(8))
Mem usage at start: 253.1171875 MiB
Vertices found: 3282 and edges: 6561
Runtime = 0:02:57.325506
Mem usage at end: 265.15234375 MiB
sage: test(graphs.SierpinskiGasketGraph(9))
Mem usage at start: 263.625 MiB
Vertices found: 9843 and edges: 19683
Runtime = 0:29:03.321870
Mem usage at end: 296.8359375 MiB
sage: test(graphs.SierpinskiGasketGraph(10))
Mem usage at start: 303.11328125 MiB
Vertices found: 29526 and edges: 59049
Runtime = 5:01:45.355463
Mem usage at end: 399.3984375 MiB
```




### :memo: 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.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### :hourglass: 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#38791
Reported by: David Coudert
Reviewer(s): Dima Pasechnik
@vbraun vbraun merged commit e1769fa into sagemath:develop Nov 16, 2024
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants