520 likes | 541 Views
Atama ve eşleme (eşleştirme) problemleri (Matching and Assignment problems). Hall Teoremi : Sözel olarak: Eğer her bir erkek grubu için onların hoşlandıkları kızlar grubu daha genişse, her bir erkek isteğine göre kız bulabilir. a. x. b. y. c. z. d. w. e.
E N D
Atama ve eşleme (eşleştirme) problemleri(Matching and Assignment problems)
Hall Teoremi: Sözel olarak: • Eğer her bir erkek grubu için onların hoşlandıkları kızlar grubu daha genişse, • her bir erkek isteğine göre kız bulabilir
a x b y c z d w e
Sözel olarak: Eğer her bir erkek yeterince istekli ise (k´dan fazla kızla ilgileniyorsa) ve • hiç bir kız özel ilgi görmüyorsa (k´dan fazla ilgi görmüyorsa), • her bir erkek isteğine göre kız bulabilir.
Ör.: 50 öğrenciden oluşan bir sınıfta 25 erkek ve 25 kız vardır. Her erkek =5 kızdan hoşlanıyor. Her kız için ondan hoşlanan =5 erkek vardır. Bir okul partisine her erkek hoşlandığı bir kız ile katılabilir mi?
Ör.: 45 öğrenciden oluşan bir sınıfta 20 erkek ve 25 kız vardır. Her erkek =5 kızdan hoşlanıyor. Her kız için ondan hoşlanan =4 erkek vardır. Bir okul partisine her erkek hoşlandığı bir kız ile katılabilir mi?
Definitions Matching Free Vertex
Definitions • Maximum Matching: matching with the largest number of edges
Definition • Note that maximum matching is not unique.
Intuition • Let the top set of vertices be men • Let the bottom set of vertices be women • Suppose each edge represents a pair of man and woman who like each other • Maximum matching tries to maximize the number of couples!
Applications • Matching has many applications. • This lecture lets you know how to find maximum matching.
Alternating Path • Alternating between matching and non-matching edges. a c d e b f h i j g d-h-e: alternating path a-f-b-h-d-i: alternating path starts and ends with free vertices f-b-h-e: not alternating path e-j: alternating path starts and ends with free vertices
Idea • “Flip” augmenting path to get better matching • Note: After flipping, the number of matched edges will increase by 1!
Idea of Algorithm • Start with an arbitrary matching • While we still can find an augmenting path • Find the augmenting path P • Flip the edges in P
Breadth-First Search Algorithm for Augmented Path • Use Breadth-First Search: • LEVEL(0) = some unmatched vertex r • for odd L > 0, • LEVEL(L) = {u|{v,u} E – M • when v LEVEL(L -1) • and when u in no lower level} • For even L > 0, • LEVEL(L) = {u|{v,u} M • when v LEVEL(L -1) • and u in no lower level} • Assume G is bipartite graph with matching M.
Labelling Algorithm • Start with arbitrary matching
Labelling Algorithm • Pick a free vertex in the bottom
Labelling Algorithm • Run BFS
Labelling Algorithm • Alternate unmatched/matched edges
Labelling Algorithm • Until a augmenting path is found
Repeat • Pick another free vertex in the bottom
Repeat • Run BFS
Repeat • Flip
Answer • Since we cannot find any augmenting path, stop!
Overall algorithm • Start with an arbitrary matching (e.g., empty matching) • Repeat forever • For all free vertices in the bottom, • do bfs to find augmenting paths • If found, then flip the edges • If fail to find, stop and report the maximum matching.
Time analysis • We can find at most |V| augmenting paths (why?) • To find an augmenting path, we use bfs! Time required = O( |V| + |E| ) • Total time: O(|V|2 + |V| |E|)
Improvement • We can try to find augmenting paths in parallel for all free nodes in every iteration. • Using such approach, the time complexity is improved to O(|V|0.5 |E|)
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
Naïve solution • Starting from a feasible solution. • Check if it is stable. • If yes, done! • If not, remove an unstable couple. • Is this work?
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