Skip to content

Add max flow#10

Merged
not522 merged 8 commits into
not522:masterfrom
MiSawa:feature/max-flow
Sep 19, 2020
Merged

Add max flow#10
not522 merged 8 commits into
not522:masterfrom
MiSawa:feature/max-flow

Conversation

@MiSawa

@MiSawa MiSawa commented Sep 12, 2020

Copy link
Copy Markdown
Contributor

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.

Comment thread atcoder/maxflow.py

def bfs() -> bool:
fill(level, self._n)
queue = []

Copy link
Copy Markdown
Contributor Author

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'

@MiSawa

MiSawa commented Sep 12, 2020

Copy link
Copy Markdown
Contributor Author

Oops forgot to add min cut. Will do done

Comment thread atcoder/maxflow.py Outdated
from typing import NamedTuple, Optional, List


class MaxFlow:

Copy link
Copy Markdown
Contributor Author

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?

Comment thread atcoder/maxflow.py
def min_cut(self, s: int) -> List[bool]:
visited = [False] * self._n
stack = [s]
visited[s] = True

Copy link
Copy Markdown
Contributor Author

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.

Comment thread atcoder/maxflow.py Outdated
queue.append(e.dst)
return False

def dfs(lim) -> int:

Copy link
Copy Markdown
Contributor Author

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.

Comment thread atcoder/maxflow.py
src: int
dst: int
cap: int
flow: int

Copy link
Copy Markdown
Contributor Author

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.

Comment thread atcoder/maxflow.py
assert 0 <= t < self._n
assert s != t
if flow_limit is None:
flow_limit = sum(e.cap for e in self._g[s])

Copy link
Copy Markdown
Contributor Author

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().

@not522

not522 commented Sep 12, 2020

Copy link
Copy Markdown
Owner

Can you switch the target branch to master instead of tmp?

@not522 not522 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I added nitpicky comments.

Comment thread atcoder/maxflow.py Outdated
Comment thread atcoder/maxflow.py Outdated
Comment thread atcoder/maxflow.py Outdated
Comment thread atcoder/maxflow.py Outdated
Comment thread atcoder/maxflow.py Outdated
Comment thread atcoder/maxflow.py Outdated
Comment thread atcoder/maxflow.py
MiSawa and others added 4 commits September 13, 2020 13:45
Use key function

Co-authored-by: Naoto Mizuno <naotomizuno@preferred.jp>
@MiSawa MiSawa changed the base branch from tmp to master September 13, 2020 05:00
@not522

not522 commented Sep 19, 2020

Copy link
Copy Markdown
Owner

LGTM, thanks!

@not522 not522 merged commit 595e7d9 into not522:master Sep 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants