Skip to content

Conversation

@MiSawa
Copy link
Contributor

@MiSawa MiSawa commented Sep 12, 2020

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.


def bfs() -> bool:
fill(level, self._n)
queue = []
Copy link
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
Copy link
Contributor Author

MiSawa commented Sep 12, 2020

Oops forgot to add min cut. Will do done

from typing import NamedTuple, Optional, List


class MaxFlow:
Copy link
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?

def min_cut(self, s: int) -> List[bool]:
visited = [False] * self._n
stack = [s]
visited[s] = True
Copy link
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.

queue.append(e.dst)
return False

def dfs(lim) -> int:
Copy link
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.

src: int
dst: int
cap: int
flow: int
Copy link
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.

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
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
Copy link
Owner

not522 commented Sep 12, 2020

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

Copy link
Owner

@not522 not522 left a 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.

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
Copy link
Owner

not522 commented Sep 19, 2020

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