-
Notifications
You must be signed in to change notification settings - Fork 23
Add max flow #10
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
Add max flow #10
Conversation
|
|
||
| def bfs() -> bool: | ||
| fill(level, self._n) | ||
| queue = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried reusing this queue, stack and edge_stack, though the difference wasn't significant.
$ hyperfine 'python ./atcoder/maxflow.py < input' 'python ./atcoder/maxflow2.py < input' -w 10 -r 30
Benchmark #1: python ./atcoder/maxflow.py < input
Time (mean ± σ): 164.2 ms ± 4.2 ms [User: 157.1 ms, System: 6.8 ms]
Range (min … max): 156.6 ms … 171.8 ms 30 runs
Benchmark #2: python ./atcoder/maxflow2.py < input
Time (mean ± σ): 167.4 ms ± 4.0 ms [User: 159.8 ms, System: 7.3 ms]
Range (min … max): 160.5 ms … 177.8 ms 30 runs
Summary
'python ./atcoder/maxflow.py < input' ran
1.02 ± 0.04 times faster than 'python ./atcoder/maxflow2.py < input'
|
|
atcoder/maxflow.py
Outdated
| from typing import NamedTuple, Optional, List | ||
|
|
||
|
|
||
| class MaxFlow: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@not522 What do you prefer fro the name? Maybe rename to MFGraph to match the original impl?
| def min_cut(self, s: int) -> List[bool]: | ||
| visited = [False] * self._n | ||
| stack = [s] | ||
| visited[s] = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: The original impl had visited[v] = True after stack.pop() instead of this.
atcoder/maxflow.py
Outdated
| queue.append(e.dst) | ||
| return False | ||
|
|
||
| def dfs(lim) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Unlike the original recursive implementation, this version uses stack and augment on a single path.
| src: int | ||
| dst: int | ||
| cap: int | ||
| flow: int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used src, dst instead of from, to because from is a reserved word in python.
| assert 0 <= t < self._n | ||
| assert s != t | ||
| if flow_limit is None: | ||
| flow_limit = sum(e.cap for e in self._g[s]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Used the sum of capacity of the edges leaving s instead of numeric_limits<T>::max().
|
Can you switch the target branch to |
not522
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added nitpicky comments.
Use key function Co-authored-by: Naoto Mizuno <naotomizuno@preferred.jp>
f17d1ee to
d70ad9b
Compare
More type hints Co-authored-by: Naoto Mizuno <naotomizuno@preferred.jp>
|
LGTM, thanks! |
https://atcoder.jp/contests/practice2/submissions/16651973
I'm not really a Pythonist. Please feel free to be nitpicky, or even add commits on the branch if that's easier.