Princeton Alogorithm COS226 Week9 MaxFlow

min cut

there is an edge-weighted digraph, source vertex s, and target vertex t.
each edge has a positive capacity.
a st-cut is a partition of the vertices into two disjoint sets, with s in one set A and t in the other set B.
its capacity is the sum of the capacities of the edges from A to B.
minimum cut problem is find a cut of minimum capacity.


image.png

max flow

an st-flow is an assignment of values to the edges such that:
capacity constraint: 0 <= edge's flow <= edge's capacity
local equilibrium: inflow = outflow at every vertex (except s and t)
maximum st-flow problem is to find a flow of maximum value


image.png

image.png

how to find max flow

ford-fulkerson algorithm


image.png

find an undirected path from s to t such that it can increase flow on forward edges(not full), can decrease flow on backward edge(not empty)


image.png

image.png

when to terminate?

all paths from s to t are blocked by either, full forward edge or empty backward edge.


image.png

image.png

relationship between flows and cuts

Value of the maxflow = capacity of mincut


image.png

performance

image.png

bad news is that even wehn edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow


image.png

FF performance depends on choice of augmenting paths


image.png

implementation

image.png

image.png

image.png

bipartite matching problem

image.png

add this implementation in extra credit
image.png

QUIZ

Q1 Fattest path

image.png

this problem could be solved by two way.

  1. binary search
    design a linear-time subroutine that takes a real-number T and determines if there is a path from s to t of bottleneck capacity greater than or equal to T.
    because the bottleneck is in the range of min edge and max edge. so we could use binary search on it.
    the check method spend O(E) time, we just iterate from s go every possible point if there is an edge meet the weight, if we found we could enter destination, we know this weight is valid, and we can try bigger weight. ( we want to find the max bottleneck)

this solution could be seen in solution project.

  1. Dijkstra
    the relax operation is to fatter the path as much as possible. because we need to find the fattest path. every time we pop the Max Position, to run dijktra algorithm.

this solution could be seen in test

Q2 Perfect matchings in k-regular bipartite graphs

image.png

this question could be used to practice implementing fordfulkerson with int[][] graph.
check the max flow is equal to n, (men + women equal to 2 n)

Q3 Maximum weight closure problem

image.png

if we use some point, we have to contain all the point this vertex point out. we need to check it is deserved. so we only fetch the point which bring its neighbor, the weight is still positive component. to achieve that , we could build a graph, if we found negative paths is larger or equal to the positive paths, we just discard them. in max flow, it is a full path by the positive capacity, if positive is larger than negative, it is a full path by negative capacity. so we need to sum all the positive path then minus the max flow, it is the answer.
we add a source to link all the positive vertex, and assign a target be linked by all the negative vertex; assign edge (v, w) a weight of infinity if there is an edge from v to w in the original digraph.

the brute force solution is to considerate all the situation including add or discard this vertex, get the max from them.

Project Baseball Elimination

image.png

to solve this problem , we will build a flow network like above picture.
source vertex connect to all the matches info, and team vertex connect to target vertex. except the target team.
so the max flow means we need to find a maxflow which can as much as possible use the flow on the edge to the target to make the game left can be used up.
after we calculate the min cut and max flow. we could just compare the value to the total remaining games, if it is same, the team have possibility to win.

possible progress step

Write code to read in the input file and store the data.

Draw by hand the FlowNetwork graph that you want to construct for a few small examples. Write the code to construct the FlowNetwork, print it out using the toString() method, and and make sure that it matches your intent. Do not continue until you have thoroughly tested this stage.

Compute the maxflow and mincut using the FordFulkerson data type. You can access the value of the flow with the value() method; you can identify which vertices are on the source side of the mincut with the inCut() method.

The BaseballElimination API contains the public methods that you will implement. For modularity, you will want to add some private helper methods of your own.

course quiz and project : https://github.com/yixuaz/algorithm4-princeton-cos226

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,723评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,080评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,604评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,440评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,431评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,499评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,893评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,541评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,751评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,547评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,619评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,320评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,890评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,896评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,137评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,796评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,335评论 2 342

推荐阅读更多精彩内容