390 likes | 409 Views
Explore the concept of maximum flow problems through the analogy of network pipelines, such as oil and data flows, aiming to maximize the flow from source to destination. Understand the Ford-Fulkerson method and augmenting paths in flow networks to ensure efficient flow allocation.
E N D
Akış problemi • s kaynak köşesidir • Akacak olanın (petrol, gaz veya veri) üretildiği yerdir • t hedef köşesidir • Akacak olanın ulaşması gereken yerdir • Diğer tüm köşeler akacak olanın geçeceği ara köşelerdir • Amaç: Kaynaktan hedefe olabildiğince maksimum akışı sağlamaktır
Örnek Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v3 v1 v1 S S t t kaynak v2 v4 v2 v4 hedef
Kapasite 3 v3 v1 6 8 3 S t 3 6 8 v2 v4 6 c(u,v)=12 6 c(u,v)=6 u u v v Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4 12 Büyük boru Küçük boru
Introduction - capacity Representation Example: oil pipeline Flow network: directed graph G=(V,E) 3 v3 v3 v1 v1 6 8 3 S S t t 3 6 8 v2 v4 v2 v4 6 If (u,v) E c(u,v) = 0 6 v2 v4 0 0 v4 v3 0
Akış 3 v3 v1 6 8 3 S t 3 6 8 v2 v4 6 f(u,v)=6 f(u,v)=6 u u v v Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4 6/12 Kapasite altı akış 6/6 Tam kapasiteli akış
Akış 3 v3 v1 6 8 3 S t 3 6 8 v2 v4 6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4
Introduction – flow 3 v3 v1 6 8 3 S t 3 6/6 6/8 v2 v4 6/6 Representation Example: oil pipeline Flow network: directed graph G=(V,E) v3 v1 S t v2 v4
Akış 3 v3 v1 6 8 3 S t 3 6/6 6/8 v2 v4 6/6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4
Akış 3/3 v3 v1 3/6 3/8 3 S t 3 6/6 6/8 v2 v4 6/6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4
Akış 3/3 v3 v1 3/6 3/8 3 S t 3 6/6 6/8 v2 v4 6/6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4
Akış 3/3 v3 v1 3/6 5/8 2/3 S t 3 6/6 8/8 v2 v4 6/6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4
Akışı iptal etme 3/3 v3 v1 3/6 5/8 2/3 S t 3 6/6 8/8 v2 v4 6/6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4
Akışı iptal etme 3/3 v3 v1 4/6 6/8 3/3 S t 1/3 6/6 8/8 v2 v4 5/6 Model Örnek: petrol hattı Akış ağı: yönlü çizge G=(V,E) v3 v1 S t v2 v4 u u u u 8/10 3/4 5/10 4 10 4 8/10 4 v v v v
The Ford-Fulkerson yöntemi 2 önemli kavram var: 1) kalan ağ 2) artırılan yol
Ford Fulkerson – kalan ağ 5 11 5 8 cf(u,v) = c(u,v) – f(u,v) Akış ağı G = (V,E) Kalan ağ Gf = (V,Ef) 12/12 v3 v3 v1 v1 11/16 15/20 10 1/4 7/7 S t S t 4/9 4/4 8/13 v2 v4 v2 v4 11/14
Ford Fulkerson – kalan ağ cf(u,v) = c(u,v) – f(u,v) Akış ağı G = (V,E) Kalan ağ Gf = (V,Ef) 12 12/12 v3 v3 v1 v1 5 5 11/16 15/20 4 11 10 1/4 11 7/7 7 15 3 S t S t 4/9 5 5 4/4 4 3 8/13 8 v2 v4 v2 v4 11/14 11
Ford Fulkersen-artırılan yol Definition: Kalan ağda s den t ye her hangi bir basit (döngüsüz) p yolu p yoluna göre kalan kapasite cf(p) = min{cf (u,v): (u,v) p üzerinde} Akış ağı G = (V,E) Kalan ağ Gf = (V,Ef) 12 12/12 v3 v3 v1 v1 5 5 11/16 15/20 4 11 10 1/4 11 7/7 7 15 3 S t S t 4/9 5 5 4/4 4 3 8/13 8 v2 v4 v2 v4 11/14 11
Ford Fulkerson – artırılan yol 12 12/12 v3 v3 v1 v1 5 5 11/16 15/20 4 11 10 1/4 11 7/7 7 15 3 S t S t 4/9 5 5 4/4 4 3 8/13 8 v2 v4 v2 v4 11/14 11 Artırılan yol
Ford Fulkerson – artırılan yol Akış: fp: V x V R: cf(p) eğer (u,v) p de ise fp(u,v) = - cf(p) eğer (v,u) p de ise 0 aksi durumda Akış ağı: G = (V,E) Kalan ağ Gf = (V,Ef) 12 12/12 v3 v3 v1 v1 5 5 11/16 15/20 4 11 10 1/4 11 7/7 7 15 3 S t S t 4/9 5 5 4/4 4 3 8/13 8 v2 v4 v2 v4 11/14 11
Ford Fulkerson – augmenting paths cf(p) if (u,v) is on p fp(u,v) = - cf(p) if (v,u) is on p 0 otherwise We define a flow: fp: V x V R such as: Flow network G = (V,E) residual network Gf = (V,Ef) 12 12/12 v3 v3 v1 v1 5 4/5 11/16 15/20 4/4 11 10 1/4 11 7/7 7 -4/15 3 S t S t 4/9 -4/5 4/5 4/4 4 3 8/13 -4/8 v2 v4 v2 v4 11/14 11 Our virtual flow fp along the augmenting path p in Gf
Ford Fulkerson – augmenting paths cf(p) if (u,v) is on p fp(u,v) = - cf(p) if (v,u) is on p 0 otherwise We define a flow: fp: V x V R such as: Flow network G = (V,E) residual network Gf = (V,Ef) 12 12/12 v3 v3 v1 v1 5 4/5 11/16 15/20 4/4 11 10 1/4 11 7/7 7 -4/15 3 S t S t 4/9 -4/5 4/5 4/4 4 3 8/13 -4/8 v2 v4 v2 v4 11/14 11 Our virtual flow fp along the augmenting path p in Gf
Ford Fulkerson – augmenting the flow cf(p) if (u,v) is on p fp(u,v) = - cf(p) if (v,u) is on p 0 otherwise We define a flow: fp: V x V R such as: Flow network G = (V,E) residual network Gf = (V,Ef) 12 12/12 v3 v3 v1 v1 5 4/5 11/16 19/20 4/4 11 10 1/4 11 7/7 7 15 3 S t S t 0/9 5 4/5 4/4 4 3 12/13 8 v2 v4 v2 v4 11/14 11 New flow: f´: V x V R : f´=f + fp Our virtual flow fp along the augmenting path p in Gf
The Ford-Fulkerson method Ford-Fulkerson(G,s,t) 1 for each edge (u,v) in G.E do 2 f(u,v) ¬ f(v,u) ¬ 0 3 while there exists a path p from s to t in residual network Gfdo 4 cf = min{cf(u,v): (u,v) is in p} 5 for each edge (u,v) in p do 6 f(u,v) ¬ f(u,v) + cf 7 f(v,u)¬-f(u,v) 8 return f The algorithms based on this method differ in how they choose p in step 3. If chosen poorly the algorithm might not terminate.
Örnek (1) Left Side = Residual Graph Right Side = Augmented Flow
Örnek(2) Left Side = Residual Graph Right Side = Augmented Flow
Worst Case Running Time • Assuming integer flow • Each augmentation increases the value of the flow by some positive amount. • Augmentation can be done in O(E). • Total worst-case running time O(E|f*|), where f* is the max-flow found by the algorithm. • Example of worst case: Augmenting path of 1 Resulting Residual Network Resulting Residual Network
Application – Bipartite Matching • Example – given a community with n men and m women • Assume we have a way to determine which couples (man/woman) are compatible for marriage • E.g. (Joe, Susan) or (Fred, Susan) but not (Frank, Susan) • Problem: Maximize the number of marriages • No polygamy allowed • Can solve this problem by creating a flow network out of a bipartite graph
Bipartite Graph • A bipartite graph is an undirected graph G=(V,E) in which V can be partitioned into two sets V1 and V2 such that (u,v) E implies either u V1 and v V12 or vice versa. • That is, all edges go between the two sets V1 and V2 and not within V1 and V2.
Model for Matching Problem • Men on leftmost set, women on rightmost set, edges if they are compatible A A A X X X B B B Y Y Y C C C Z Z Z D D D Men Women Optimal matching A matching
Solution Using Max Flow • Add a supersouce, supersink, make each undirected edge directed with a flow of 1 A A X X B B t Y s Y C C Z Z D D Since the input is 1, flow conservation prevents multiple matchings
Stable Marriage Problem • Given N men and N women, each person list in order of preference all the people of the opposite sex who would like to marry. • Problem: • Engage all the women to all the men in such a way as to respect all their preferences as much as possible.
A B C D E 1 2 3 4 5 2 1 2 1 5 E D A C D 5 2 3 3 3 A E D B B 1 3 5 2 2 D B B D C 3 4 4 4 1 B A C A E 4 5 1 5 4 C C E E A Stable? • A set of marriages is unstable if • two people who are not married both prefer each other than their spouses • E.g. Suppose we have A1 B3 C2 D4 E5. This is unstable since • A prefer 2 more than 1 • 2 prefer A more than C
A B C D E 1 2 3 4 5 2 1 2 1 5 E D A C D 5 2 3 3 3 A E D B B 1 3 5 2 2 D B B D C 3 4 4 4 1 B A C A E 4 5 1 5 4 C C E E A Naïve solution (2) • Does not work! • E.g. • A1 B3 C2 D4 E5 • A2B3 C1 D4 E5 • A3 B2 C1 D4 E5 • A3 B1 C2 D4 E5
Solution • Let X be the first man. • X proposes to the best woman in the remaining on his list. (Initially, the first woman on his list!) • If α is not engaged • Pair up (X, α). Then, set X=next man and goto 1. • If α prefers X more than her fiancee Y, • Pair up (X, α). Then, set X=Y and goto 1. • Goto 1
A B C D E 1 2 3 4 5 2 1 2 1 5 E D A C D 5 2 3 3 3 A E D B B 1 3 5 2 2 D B B D C 3 4 4 4 1 B A C A E 4 5 1 5 4 C C E E A Example A B C D E 2 1 2 1 5 5 2 3 3 3 1 3 5 2 4
Time analysis • If there are N men and N women, • O(N2) time