-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1958 lines (1928 loc) · 61.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>一般圖最大權匹配</title>
<meta name="description" content="General Graph Weighted Match Algorithm">
<meta name="author" content="SunMoon Master">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/black.css" id="theme">
<link rel="stylesheet" href="css/style.css"><!-- Code syntax highlighting -->
<link rel="stylesheet" href="reveal/lib/css/zenburn.css">
<!-- Printing and PDF exports --><script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal/css/print/pdf.css' : 'reveal/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script><!--[if lt IE 9]><script src="lib/js/html5shiv.js"></script><![endif]-->
</head>
<body>
<div></div>
<div class="reveal">
<div class="slides">
<span style='display:none;'>
\(
\newcommand{\ord}[1]{\mathcal{O}\left(#1\right)}
\newcommand{\abs}[1]{\lvert #1 \rvert}
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\ceil}[1]{\lceil #1 \rceil}
\newcommand{\opord}{\operatorname{\mathcal{O}}}
\newcommand{\argmax}{\operatorname{arg\,max}}
\newcommand{\str}[1]{\texttt{"#1"}}
\)
</span>
<!-- ... 這裡是註解文字 ... -->
<section>
<h1>一般圖最大權匹配</h1>
<p><div class="small">general graph weighted match</div></p>
<p><div class="small">南天門日月卦長</div></p>
</section>
<section>
<section>
<h1>甚麼是匹配?</h1>
</section>
<section>
<h2>匹配(最大邊獨立集)</h2>
一張圖$G=(V,E)$,這張圖的一個匹配$M$是邊集$E$的一個子集,其中每兩條邊都不相鄰(沒有公共頂點)<br/>
<img src="img/Maximal-matching.svg">
</section>
<section>
<h2>最大匹配</h2>
<p>
圖$G$的一個 <b>最大匹配</b> ,指邊數最多的匹配。
</p>
<b>最大匹配</b> 可能有不止一個,但 <b>最大匹配</b> 的邊數是確定的,並且不可能超過圖中頂點數的一半。<br/>
<img src="img/Maximum-matching-labels.svg">
</section>
<section>
<h2>完美匹配</h2>
圖$G$的 <b>最大匹配</b> 包含所有原圖的所有點,稱為 <b>完美匹配</b><br/>
若$G$為完全圖且頂點數為偶數時,必然存在 <b>完美匹配</b>
</section>
<section>
<h2>最大權匹配</h2>
<h5>maximum weight matching</h5>
<p>
一張圖$G=(V,E)$<br>
$\forall e \in E$存在一個函數$w(e)$表示$e$的權重
</p>
圖$G$的一個 <b>最大權匹配</b> $M=(V',E')$是圖$G$的一個子圖<br/>
滿足$ \sum w(e): e \in E'$為其所有匹配中最大的<br/>
<img src="img/example_weighted_graph.png">
</section>
<section>
<h2>最大權最大匹配</h2>
<h5>maximum weight maximum cardinality matching</h5>
圖$G$的一個 <b>最大權最大匹配</b> $M=(V',E')$<br/>
滿足$ \sum w(e): e \in E'$為其所有 <b>"最大匹配"</b> 中最大的<br/>
<img src="img/example_max_edge_weighted_graph.png">
</section>
<section>
<h2>最大權完美匹配</h2>
<h5>maximum weight maximum perfect matching</h5>
若圖$G$存在完美匹配<br/>
此時圖$G$的 <b>最大權最大匹配</b> 稱為 <b>最大權完美匹配</b>
</section>
</section>
<section>
<h1>匹配算法的轉換</h1>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 用
## 最大權最大匹配
## 求
## 最大權匹配
</script>
</section>
<section>
<p>
<b>最大權最大匹配</b> 允許負權邊$(w(e)<0)$<br/>
但是 <b>最大權匹配</b> 不會有負權邊
</p>
若一張圖$G$其所有的邊皆為負權<br/>
則其 <b>最大權匹配</b> $M=\varnothing$
</section>
<section>
<h2>調整邊的權重</h2>
先將圖$G$的所有負權邊其權重$w(e)$設為$0$<br/>
在進行接下來的步驟
</section>
<section>
<h2>完全圖性質</h2>
<p>在$G$為完全圖且沒有負權邊時</p>
<b>最大權最大匹配</b> = <b>最大權匹配</b>
</section>
<section>
所以把$G$補成完全圖,補上的邊其權重為$0$<br/>
計算 <b>最大權最大匹配</b> 後再把權重為$0$的邊去除即可<br/>
<img src="img/example_max_edge_weighted_graph-change.png">
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 用
## 最大權匹配
## 求
## 最大權最大匹配
</script>
</section>
<section>
<b>最大權匹配</b> 不會有負權邊<br/>
且零邊$ (w(e)=0) $可選可不選<br/>
但是 <b>最大權最大匹配</b> 允許負權邊和零邊
</section>
<section>
<h2>調整邊的權重</h2>
<p>令 $K = max(\{\abs{w(e)}: e \in E ,\; w(e) ≤ 0 \}) + 1 $</p>
若沒有負權邊或零邊則$K = 0$
</section>
<section>
<h2>調整邊的權重</h2>
<p>把圖$G$中所有的邊其權重$w(e)$加上$K$產生一張新圖$G'=(V,E')$</p>
此時的新圖$G'$不存在負權邊和零邊
</section>
<section>
<h2>問題來了</h2>
<p>
<b>最大權最大匹配</b> 不一定等於 <b>最大權匹配</b><br/>
但是把所有邊的邊權加上一個超大數字$P$的話<br/>
<b>最大權匹配</b> 的結果就是 <b>最大權最大匹配</b>
</p>
但是$P$取要多大?
</section>
<section>
<h2>調整邊的權重</h2>
<p>令$P = \sum{w(e): e \in E'}$</p>
把圖$G'$中所有的邊其權重$w(e)$加上$P$<br/>
產生一張新圖$G''=(V,E'')$
</section>
<section>
此時對圖$G''$進行 <b>最大權匹配</b><br/>
其結果可以對應原圖的 <b>最大權最大匹配</b><br/>
<img src="img/example_max_weighted_graph-change.png">
</section>
</section>
<section>
<h1>最大匹配算法</h1>
</section>
<section>
<section data-markdown>
<script type="text/template">
# 二分圖最大匹配
## Bipartite Matching
</script>
</section>
<section>
<h2>flow</h2>
最簡單的方法是用網路流,所以就不多講了<br/>
Dinic在這種特殊圖上複雜度為$ \ord{|E|\sqrt{|V|}} $<br/>
<img src="img/flow-matching.png">
</section>
<section data-markdown>
<script type="text/template">
## 匈牙利算法
這是這份投影片主要要講的
在學習他之前必須要有一些預備知識
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
# 匈牙利算法-預備知識
</script>
</section>
<section data-markdown>
<script type="text/template">
## 定義
**匹配點**
**匹配邊**
**未匹配點**
**非匹配邊**
它們的含義非常顯然
</script>
</section>
<section data-markdown>
<script type="text/template">
交替路(Alternating Path):從一個未匹配點出發,依次經過非匹配邊、匹配邊、非匹配邊…形成的路徑叫交替路。
增廣路(Agumenting Path):從一個未匹配點出發,走交替路,如果途徑另一個未匹配點(出發的點不算),則這條交替路稱為增廣路。
</script>
</section>
<section data-markdown>
<script type="text/template">
![Alt text](img/Bipartite.png)
![Alt text](img/BipartiteAgumentingPath.png)
原圖和增廣路
</script>
</section>
<section>
<h2>交錯樹</h2>
<div class="theorem">
<div class="label">交錯樹</div>
alternating tree<hr/>
在進行DFS或BFS尋找增廣路的過程產生的樹稱為交錯樹<br/>
設 $T=(V_t,E_t)$為在尋找增廣路時產生的交錯樹<br/>
定義:<br/>
偶點為樹上深度為偶數的點<br/>
奇點為樹上深度為奇數的點
</div>
</section>
<section data-markdown>
<script type="text/template">
## 二分圖的偶點奇點
二分圖是很特別的圖<br/>
可以把它的點分成兩個集合
其中一個集合的點在交錯樹上永遠是奇點<br/>
另一集合的點在交錯樹上永遠是偶點
</script>
</section>
<section data-markdown>
<script type="text/template">
## 原圖及交錯樹
![Alt text](img/originalB.png)
![Alt text](img/search tree.png)
</script>
</section>
<section data-markdown>
<script type="text/template">
有沒有人發現把增廣路的匹配邊和非匹配邊反轉就可以增加匹配數量?
匈牙利算法中只透過這樣的方式增加匹配數量
稱為Augment(擴充)
</script>
</section>
<section data-markdown>
<script type="text/template">
那如果有一個未匹配點,找不到以此點作為端點的增廣路(無法擴充)怎麼辦?
</script>
</section>
</section>
<section>
<section>
<h2>Berge's Theorem</h2>
<div class="theorem">
<div class="label">定理</div>
Berge's Theorem<hr/>
從圖上任取一個未匹配點,如果找不到以此點作為端點的增廣路,那麼這張圖會有一些最大匹配不會包含此點。
</div>
<div class="fragment">接下來我們要來證明Berge's Theorem</div>
</section>
<section>
<h2>對稱差集</h2>
<div class="theorem">
<div class="label">定義</div>
對稱差集<hr/>
兩個集合 $A$ 和 $B$ 的「對稱差集」定義為<br/>
$A⊕B = (A∪B) - (A∩B)$
</div>
</section>
<section>
<p>同一張圖上的兩種匹配$M$和$M^*$也可以計算對稱差集</p>
$M⊕M^*$總共會產生六大類 connected component
</section>
<section data-markdown>
<script type="text/template">
![Alt text](img/connected_component.png)
</script>
</section>
<section>
設當下的匹配$M$找不到以未匹配點$p$作為端點的擴充路徑<br/>
並令$M^*$是該圖的其中一個最大匹配。
</section>
<section>
<h2>如果$p$不在$M^*$上</h2>
<div class="theorem">
<div class="label">證明</div>
Berge's Theorem<hr/>
$p$不在$M^*$上定理成立。
</div>
</section>
<section>
<h2>如果$p$在$M^*$上</h2>
<div class="theorem">
<div class="label">證明</div>
Berge's Theorem<hr/>
1. $p$對於M來說是未匹配點。理所當然$p$不在$M$上。<br/>
2. 考慮$M⊕M^*$的六種情形。$p$不在$M$上,且$p$在$M^*$上,所以只有d或e符合條件。<br/>
3. $M$找不到以$p$作為端點的擴充路徑,所以d不符合條件,只有e符合條件。<br/>
4. 對於$M^*$來說,只要照著e顛倒匹配邊和未匹配邊,
就可以製造出另一個不會包含p的最大匹配,
成為1.的情形,定理還是成立。
</div>
<div class="fragment">結論:Berge's Theorem是正確的</div>
</section>
</section>
<section>
<section>
<h1>匈牙利算法</h1>
</section>
<section data-markdown>
<script type="text/template">
有了Berge's Theorem<br/>
我們可以用以下的方法找最大匹配
</script>
</section>
<section data-markdown>
<script type="text/template">
一開始圖上所有點都是未匹配點<br/>
將圖上每一個未匹配點都嘗試作為增廣路的端點
</script>
</section>
<section data-markdown>
<script type="text/template">
## 如果找得到增廣路:
沿著增廣路修改現有匹配(擴充)<br/>
此未匹配點變成了匹配點
</script>
</section>
<section data-markdown>
<script type="text/template">
## 如果找不到擴充路徑
直接刪除此點<br/>
繼續下去仍然可以找到原圖的其中一個最大匹配
</script>
</section>
<section data-markdown>
<script type="text/template">
## 接下來來看code
</script>
</section>
<section data-markdown>
<script type="text/template">
用vector存圖<br/>
若G[x][i]=y,則x和y之間有一條邊<br/>
match為匹配結果,若match[y]=x<br/>
則x和y之間有一條匹配邊
注意:因為是二分圖,可以把點分成兩個集合<br/>
x和y在不同的集合,所以編號可以一樣
``` cpp
#define MAXN1 505
#define MAXN2 505
int n1,n2;/*n1個點連向n2個點*/
int match[MAXN2];/*每個屬於n2的點匹配了哪個點*/
vector<int > g[MAXN1];/*圖*/
```
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
bool vis[MAXN2];/*是否走訪過*/
bool dfs(int u){
for(size_t i=0;i<g[u].size();++i){
int v=g[u][i];
if(vis[v])continue;
vis[v]=1;
if(match[v]==-1||dfs(match[v])){
match[v]=u;
return 1;
}
}
return 0;
}
```
如果有增廣路就進行擴充並return true<br/>
否則return false
</script>
</section>
<section data-markdown>
<script type="text/template">
## 最後是主函數
``` cpp
inline int max_match(){
int ans=0;
memset(match,-1,sizeof(int)*n2);
for(int i=0;i<n1;++i){
memset(vis,0,sizeof(bool)*n2);
if(dfs(i))++ans;
}
return ans;
}
```
ans為最大匹配數量
</script>
</section>
<section>
<h2>複雜度</h2>
總共做了$n1$次dfs<br/>
每次dfs花了$ \ord{|E|+n2+n1} $的時間<br/>
總複雜度為$ \ord{n1*(|E|+n2+n1)} $
</section>
<section data-markdown>
<script type="text/template">
## 複雜度
好像比網路流慢?
這個方法好寫,而且可以求字典序最小的最大匹配
</script>
</section>
</section>
<section>
<section>
<h1>匈牙利算法</h1>
多重匹配
</section>
<section>
<div class="prob">
這種問題的題目通常是這樣
<hr/>
給一張圖G有n1個點和n2個點,n1個點之間沒有邊,n2個點之間也沒有邊,但是n1和n2個點之間有m條邊(簡單的來說就是n1個點和n2個點的二分圖啦),沒有重邊;其中n2個點每個點u都有一個可接受匹配數$c_u$。
n1的點只能跟一個點匹配,但n2的點在不超過可接受匹配數的情況下,可以跟多個點匹配,求這張圖的最大匹配
</div>
</section>
<section>
這依然可以用普通的匈牙利演算法來解決<br/>
把可以多重匹配的點拆開,能匹配幾次就拆成幾個點<br/>
設n2個點每個點$u$都有一個值$E_u$表示和$u$相鄰的邊數<br/>
複雜度$\ord{n1*(\sum{c_u*E_u}+n1+n2)}$<br/>
但是空間複雜度為$\ord{\sum{c_u*E_u}+n1+n2}$
</section>
<section>
可以利用vector存每個點對那些點有匹配<br/>
雖然時間複雜度不變<br/>
但是效能變快了<br/>
空間複雜度也會和一般的匈牙利一樣<br/>
</section>
<section data-markdown>
<script type="text/template">
## 接下來來看code
</script>
</section>
<section data-markdown>
<script type="text/template">
用vector存圖<br/>
若G[x][i]=y,則x和y之間有一條邊<br/>
match為匹配結果,若match[y]=x<br/>
則x和y之間有一條匹配邊<br/>
``` cpp
#define MAXN1 1005
#define MAXN2 505
int n1,n2;//n1個點連向n2個點,其中n2個點可以匹配很多邊
vector<int > g[MAXN1];//圖
int c[MAXN2];//每個屬於n2點最多可以接受幾條匹配邊
vector<int > match_list[MAXN2];//每個屬於n2的點匹配了那些點
```
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
bool vis[MAXN2];//是否走訪過
bool dfs(int u){
for(size_t i=0;i<g[u].size();++i){
int v=g[u][i];
if(vis[v])continue;
vis[v]=true;
if((int)match_list[v].size()<c[v]){//如果還有匹配空間就直接匹配
match_list[v].push_back(u);
return true;
}else{
for(size_t j=0;j<match_list[v].size();++j){
int next_u=match_list[v][j];
if(dfs(next_u)){
match_list[v][j]=u;
return true;
}
}
}
}
return false;
}
```
如果有增廣路就進行擴充並return true<br/>
否則return false
</script>
</section>
<section data-markdown>
<script type="text/template">
## 最後是主函數
``` cpp
inline int max_match(){
for(int i=0;i<n2;++i)match_list[i].clear();
//for(int i=0;i<n2;++i)match_list[i]=vector<int >();
int ans=0;
for(int u=0;u<n1;++u){
memset(vis,0,sizeof(bool)*n2);
if(dfs(u))++ans;
}
return ans;
}
```
ans為最大匹配數量
</script>
</section>
</section>
<section data-markdown>
<script type="text/template">
# 一般圖最大匹配
## General Graph Matching
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
# 帶花樹算法
## Blossom algorithm
</script>
</section>
<section data-markdown>
<script type="text/template">
## 花的概念
一般圖跟二分圖最大的差別就是奇環<br/>
在交替路上行走的時候,如果遇到奇環<br/>
那就把奇環縮成一朵花(blossom)<br/>
並把花中所有點設成偶點
既然花上的點都可以成為偶點<br/>
那麼乾脆把花直接縮成一個偶點<br/>
會讓交錯樹變得更簡潔明白
</script>
</section>
<section data-markdown>
<script type="text/template">
![Alt text](img/750px-Edmonds_blossom.svg.png)
</script>
</section>
<section data-markdown>
<script type="text/template">
縮花之後把整個花當成一個點<br/>
值得注意的是,花裡可以包含其它花
繼續尋找增廣路,如果有找到增廣路<br/>
和匈牙利算法一樣,必須擴充路徑<br/>
這個時候要處理花的問題<br/>
</script>
</section>
<section data-markdown>
<script type="text/template">
![Alt text](img/750px-Edmonds_lifting_path.svg.png)
</script>
</section>
<section>
<p>這樣基本問題就解決了,來計算一下時間複雜度</p>
<p>
因為一朵花最少有三個點,縮花後成為一個點<br/>
由此推得:N個點的圖建立一棵交錯樹,最多縮花N/2次
</p>
所以找出增廣路的時間複雜度為:<br/>
$ \ord{|E|+|V|} + \ord{|E|+|V|} = \ord{|E|+|V|} $<br/>
BFS的時間加上縮花總時間
</section>
<section data-markdown>
<script type="text/template">
## 接著就用匈牙利算法來求最大匹配
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
# 實作細節
</script>
</section>
<section data-markdown>
<script type="text/template">
## BFS
首先是BFS,如果不用BFS的話
奇點變偶點的操作會很難處理
</script>
</section>
<section data-markdown>
<script type="text/template">
## 如何記錄花
1. disjoint set
* 想法簡單
* code略長
2. 只記錄有可能增廣的邊
* 效率高
* 好實作
##這裡使用方法2
</script>
</section>
<section data-markdown>
<script type="text/template">
## 可能增廣的邊
一朵花裡可能會有其他的花<br/>
那花裡也可能會包含其他花<br/>
層層疊疊
但是增廣的時候只會有一條路徑被增廣
</script>
</section>
<section data-markdown>
<script type="text/template">
## 可能增廣的邊
因為花是在BFS時建構出來的<br/>
BFS之後要把花拆掉<br/>
所以一朵花只會有一個奇環<br/>
其環上的某條鏈有機會被增廣<br/>
因此只需要紀錄哪些點在這個環上即可
</script>
</section>
<section data-markdown>
<script type="text/template">
圖中節點的編號為BFS順序<br/>
白色的部分表示還沒遍歷<br/>
灰色表示一般節點<br/>
相同顏色表示在相同的花裡面<br/>
可以看出 3 和 9 在黃色的花和藍色的花形成時被遺棄了
![Alt text](img/blossom.gif)
</script>
</section>
<section data-markdown>
<script type="text/template">
## 可能增廣的邊
不需要擔心被遺棄的節點<br/>
之後不會再經過且不可能被增廣
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
# 來看看code
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
#define MAXN 505
vector<int>g[MAXN];//用vector存圖
int pa[MAXN],match[MAXN],st[MAXN],S[MAXN],vis[MAXN];
//pa表示交錯樹每個節點的父母節點
//match[u]=v表示u和v匹配,同時match[v]=u
//st[u]=B表示節點u屬於B這朵花
//S[u]={-1:沒走過 0:偶點 1:奇點}
//vis只用在找lca的時候檢查是不是走過了
int n;//n個點,編號為1 ~ n
```
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
inline int lca(int u,int v){
//找花的花托,也就是交錯樹的lca
//這種方法可以不用清空vis陣列就可以判斷有沒有經過
static int t=0;
for(++t;;swap(u,v)){
if(u==0)continue;
if(vis[u]==t)return u;
vis[u]=t;
u=st[pa[match[u]]];
}
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
#define qpush(u) q.push(u),S[u]=0
//因為丟進queue裡的節點必為偶點,故把兩個操作寫在一起
inline void flower(int u,int v,int l,queue<int> &q){
//這個函數用來設定花裡面所有點的pa
while(st[u]!=l){
pa[u]=v;//所有未匹配邊的pa都是雙向的
v=match[u];
if(S[v]==1)qpush(v);//所有奇點變偶點
st[u]=st[v]=l;
//注意這邊以花的花托代表這個花
//所以 st[u]=st[v]=l 就是設定 u 和 v 屬於 l 這朵花
u=pa[v];
}
}
```
執行
``` cpp
flower(v,u,lca,q);
flower(u,v,lca,q);
```
就可以設定花裡面所有點的pa
</script>
</section>
<section data-markdown>
<script type="text/template">
執行
``` cpp
flower(v,u,lca,q);
flower(u,v,lca,q);
```
的結果
![Alt text](img/pa_set.png)
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
inline bool augment(int u,int v){
//擴充增廣路
for(int lst;u;v=lst,u=pa[v]){
lst=match[u];
match[u]=v;
match[v]=u;
}
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
inline bool bfs(int u){
for(int i=1;i<=n;++i)st[i]=i;//自己一個點也是奇環
memset(S+1,-1,sizeof(int)*n);
queue<int>q;
qpush(u);
while(q.size()){
u=q.front(),q.pop();
for(size_t i=0;i<g[u].size();++i){
int v=g[u][i];
if(S[v]==-1){
pa[v]=u;
S[v]=1;
if(!match[v]){//有增廣路直接擴充
augment(u,v);
return true;
}
qpush(match[v]);
}else if(!S[v]&&st[v]!=st[u]){
int l=lca(v,u);//遇到花,做花的處理
flower(v,u,l,q);
flower(u,v,l,q);
}
}
}
return false;
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## 最後是主函數
``` cpp
inline int blossom(){
//ans表示最大匹配數量
memset(pa+1,0,sizeof(int)*n);
memset(match+1,0,sizeof(int)*n);
int ans=0;
for(int i=1;i<=n;++i)
if(!match[i]&&bfs(i))++ans;
return ans;
}
```
</script>
</section>
<section>
<h2>複雜度分析</h2>
<p>
之前有討論過bfs加上縮花的時間為$\ord{|E|+|V|}$<br/>
總共會進行$|V|$次bfs
</p>
故總複雜度為$ \ord{|V|*(|E|+|V|)} $
</section>
</section>
<section data-markdown>
<script type="text/template">
# 最大權匹配算法
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
# 二分圖最大權匹配
</script>
</section>
<section data-markdown>
<script type="text/template">
# 預備知識
</script>
</section>
<section data-markdown>
<script type="text/template">
## 線性規劃的對偶(duality)
一個線性規劃問題可以從兩個不同的角度來分析<br/>
其一,評估每一產品的獲利情況,而使總利潤極大化<br/>
另一為評估生產產品所付出的成本,而使總成本極小化<br/>
實為一物的兩面,從不同的角度觀察同一問題
</script>
</section>
<section>
<h2>定義函數</h2>
設$S ⊆ V$<br/>
$δ(S) = \{ (u, v) ∈ E \; : \; u ∈ S, \; v ∈ V − S \} $<br/>
對單一點 $v ∈ V $ ,我們定義 $δ(v) = δ(\{v\}) $<br/>
</section>
<section>
<h2>二分圖最大權完美匹配的線性規劃</h2>
<div class="prob">
<div class="label">原始問題</div>
PRIMAL<hr/>
<p>$ max \; \sum_{e∈E} \; w(e)x_e $</p>
<p>
限制:<br/>
$x_e ≥ 0 \; : \forall e∈E $<br/>
$ x(δ(u)) = 1 \; : \forall u∈V $
</p>
定義:<br/>
$x(S) = \sum_{e∈S} \; x_e$
</div>
<div class="fragment">接下來進行對偶問題的轉換</div>
</section>
<section>
<h2>二分圖最大權完美匹配的對偶問題</h2>
<div class="prob">
<div class="label">對偶問題</div>
DUAL<hr/>
<p>$ min \; \sum_{u \in V} \; z_u $</p>
<p>
限制:<br/>
$z_e ≥ 0 \; : \forall e∈E $
</p>
設 $e=(u,v)$,這裡$z_e = z_u + z_v - w(e)$
</div>
<div class="fragment">所有條件都滿足時$\sum_{u∈V} \; z_u$就是最大權匹配的權重和</div>
<div class="fragment">原本求最大值問題就變成求最小值了!</div>
</section>
<section>
<h2>二分圖最大權完美匹配的對偶問題</h2>
<p>
在原始問題中,我們發現只要在$x_e \in \{0,1\} \; : \forall e \in E $時<br/>
$x_e = 1$的邊就是匹配邊,$x_e = 0$的邊就是非匹配邊
</p>
不過線性方程的解中,$x_e$不一定是整數<br/>
但是一定存在$x_e \in \{0,1\}$的整數解<br/>
所以想辦法讓他產成整數解即可
</section>
<section>
<h2>二分圖最大權完美匹配的對偶問題</h2>
<p>
我們稱$z_u$為u的vertex labeling<br/>
稱$z_e=0$的邊為等邊(Equality Edge)
</p>
只要在最大權完美匹配的時候<br/>
讓所有匹配邊都是等邊<br/>
這樣就可以滿足$x_e \in \{0,1\}$的條件了
</section>
<section>
<h2>寫成數學符號長這樣</h2>
<div class="theorem">
$x_e>0 \; \longrightarrow \; z_e=0 \qquad \forall e \in E$
</div>
</section>
<section>
<p>
以「等邊」的概念,結合之前的匈牙利算法:
</p>
<p>
用「等邊」構成的增廣路不斷進行擴充<br/>
由於用來擴充的邊全是「等邊」<br/>
最後得到的最大權完美匹配當然全是「等邊」
</p>
<div class="fragment">找不到「等邊」?只好調整vertex labeling了</div>
</section>
<section>
<h2>調整 vertex labeling</h2>
vertex labeling 仍要維持大於等於的性質<br/>
而且既有的「等邊」不能被改變。
</section>
<section>
<h2>調整 vertex labeling</h2>
<div class="theorem">
<div class="label">調整</div>
vertex labeling<hr/>
<p>
設交錯樹 $ T = (U_t,V_t) $<br/>
令 $ d = min(\{z_e : e \in δ(V_t)\})$<br/>
設$u^+$為樹上偶點,$v^-$為樹上奇點<br/>
讓 $z_{u^+}$-= $d, \;$$z_{v^-}$ += $d$
</p>
如此便製造了一條(以上)的等邊,且既有等邊保持不動<br/>
並維持了$z_e ≥ 0 \; : \forall e∈E $的性質<br/>
接著就繼續找增廣路!
</div>
</section>
<section>
因為是二分圖<br/>
所以可以事先知道哪些點是奇點<br/>
哪些點是偶點
<div class="theorem">
我們一開始的時候可以設定:<br/>
所有$u$為偶點,$z_u=max(\{w(e): e \in E\})$<br/>
所有$v$為奇點,$z_v=0$
</div>
</section>
<section data-markdown>
<script type="text/template">
## 還是會有問題
這樣做找出來的匹配是最大權完美匹配<br/>
我們想要的是最大權匹配
沒關係,把圖補成完全二分圖就行了<br/>
雖然有辦法直接做最大權匹配(等下會說)<br/>
但是這樣code會比較好寫
</script>
</section>
<section>
<h2>如果要做最大權二分匹配</h2>
<div class="theorem">
<p>
vertex labeling 額外增加一個限制:<br/>
對於所有匹配點$z_u > 0$
</p>
一開始先設所有$z_u=max(\{w(e): e \in E\})/2$<br/>
vertex labeling為 0 的點最後將成為未匹配點
</div>
<div class="fragment">實作也不會太難大家可以試試看</div>
</section>
</section>
<section>
<section>
<h1>KM演算法</h1>
Kuhn-Munkres algorithm
</section>
<section>
<h3>這個算法可以求二分圖的最大權完美匹配</h3>
</section>
<section>
<h1>code</h1>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
int n;
int g[MAXN][MAXN],lx[MAXN],ly[MAXN],slack_y[MAXN];
//用鄰接矩陣存二分圖,把點分成X、Y兩個集合
//lx[x],ly[y]為x,y的vertex labeling
//slack_y[y]紀錄和y相鄰的最小「非等邊」值
int match_y[MAXN];
//match_y[y]=x表示y和x匹配
bool vx[MAXN],vy[MAXN];
//vx,vy是dfs的時候判斷有沒有走過用的
```
</script>
</section>
<section data-markdown>
<script type="text/template">
``` cpp
bool dfs(int x){
if(vx[x])return 0;
vx[x]=1;
for(int y=0,d;y<n;++y){