An-Li Alt Ting November 5, 2014
Contents
1 Eulerian path 1
1.1 Algorithms . . . 2 1.2 Problems on OJs . . . 2
2 Hierholzer’s algorithm 2
3 Maximum Flow 3
3.1 Algorithms . . . 4 3.2 Problems . . . 4
4 Ford-Fulkerson Algorithm 4
5 Maximum Bipartite Matching 5
5.1 Algorithms . . . 5 5.2 Problems on OJs . . . 5
6 Hungarian algorithm 5
7 Minimum Ratio Cycle 最小比率環 6
7.1 Problems on OJs . . . 6
8 References 6
1 Eulerian path
In graph theory, an Eulerian path of an undirected connected multigraph is a path whicth visits every edge exactly once. An Eulerian circuit is an Eulerian path with starts and ends on the same verte
For an undirected connected multigraph G = (V, E): Define S as the maximum set of vertices such that for each vertex v in S, δ(v) ≡ 1(mod 2). Eulerian path exists iff
|S| ∈ {0,2}. Specially, the Eulerian paths are Eulerian circuits iff |S|= 0.
1.1 Algorithms
Hierholzer’s algorithm Fleury’s algorithm
1.2 Problems on OJs
HOJ 148 海綿寶寶之泡芙阿姨駕訓班 (尤拉路徑)
SSUOC 101 Domino UVaOJ 117
UVaOJ 291 UVaOJ 302 UVaOJ 10054 UVaOJ 10129 UVaOJ 10441 UVaOJ 10506 UVaOJ 10596 UVaOJ 10735
In graph theory, Hierholzer’s algorithm is an algorithm finding an Eulerian path in an undirected connected multigraph.
2 Hierholzer’s algorithm
Hierholzer’s algorithm depends on a variant of depth-first search which is allowed to visited a vertex more than once. For given graphG= (V, E): Define S as the maximum set of vertices such that for each vertex v in S, δ(v) ≡1(mod 2). If |S|= 0, the search is started from one of the vertices in V, and the Eulerian path will be found to be an Eulerian circuit; if |S|= 2, the search is started from one of the vertices inS; otherwise, there is no Eulerian path.
# include < bits / stdc ++.h>
using namespace std ; typedef vector <int >VI;
# define pb push_back
# define F(n) FO(i,n)
# define FO(i,n) FI(i ,0 ,n)
# define FI(i,f,l) for ( int i=(f),ei =(l);i<ei;i ++)
# define FA(a) for ( __typeof (( a). begin ()) it =( a). begin () , ea =(a). end (); it !=\
ea ;++ it )
const int mv =1e3 ,me =1 e6; int cv ,ce;
bool es[me ]; int ev[me],s[mv ] ,* ls;VI ve[mv ];
int dfs ( int v){
FA(ve[v]) es [* it ]++|| dfs (ev [* it ]-v);
printf ("% i ",v);
}
int main (){
// freopen ("i. txt " ,"r", stdin );
cin >>cv >>ce;
FO(e,ce ){ int v,w;cin >>v>>w;ve[v]. pb (e); ve[w]. pb(e); ev[e]=v+w;}
ls=s;FO(v,ce)if( ve[v ]. size ()%2)* ls ++= v;
ls -s ==0? dfs (0): ls -s ==2? dfs (*s): puts (" -1");
}
Sample input:
5 8 0 1 0 3 0 4 1 2 1 3 1 4 2 3 3 4
3 Maximum Flow
In graph theory, maximum flow is the problem finding a feasiable flow through a single- source, single-sink flow network that is maximum.
3.1 Algorithms
Ford-Fulkerson Aalgorithm Edmonds-Karp algorithm
3.2 Problems
<a href=”./?p=468”>Maximum bipartite matching</a>
4 Ford-Fulkerson Algorithm
In graph theory, Ford-Fulkerson algorithm is an algorithm which solves the maximum flow problem.
Time complexity: O(F|E|)
# include < bits / stdc ++.h>
using namespace std ; typedef vector <int >VI;
# define pb push_back
# define F(n) FO(i,n)
# define FO(i,n) FI(i ,0 ,n)
# define FI(i,f,l) for ( int i=(f),ei =(l);i<ei;i ++)
# define FA(a) for ( __typeof (( a). begin ()) it =( a). begin () , ea =(a). end (); it != ea ;++ it) const int mv =1e3 ,me =1 e6; int cv ,ce ,s,t;
bool vs[mv ];
int ev[mv],ew[me],ec[me],ef[me ];
VI ve[mv ];
int dfs ( int v=s, int f= INT_MAX ){
vs[v ]=1;
if(v==t) return f;
FA(ve[v ]){ int &e=*it ,w=ev[e]-v;
if(vs[w]) continue ; int r =0;
if(w== ew [e ]){
if(ef[e]<ec[e])
ef[e ]+= r= dfs (w, min (f,ec[e]-ef[e ]));
} else {
if (0 < ef[e])
ef[e] -=r= dfs (w, min (f,ef[e ]));
}
if(r) return r;
}
int flow (){
int x=0 ,y;
do memset (vs ,0 , sizeof vs),x+=y= dfs (); while (y);
return x;
}
int main (){
// freopen ("i. txt " ,"r", stdin );
cin >>cv >>ce;
FO(e,ce ){ int v,w,c;cin >>v>>w>>c;
ve[v]. pb (e); ve[w]. pb(e); ev[e]=v+w;ew[e]=w;ec[e]=c;
}
s=0 ,t=cv -1;
printf ("% i\n", flow ());
}
邏輯腦中另有將邊關聯其反向邊的建圖方法,可以統一處理正反流量。
5 Maximum Bipartite Matching
Maximum bipartite matching is the special case of matching which the given graph is bipartite.
Maximum bipartite matching 在汉语中被称为最大二分匹配。
5.1 Algorithms
Hungarian algorithm
5.2 Problems on OJs
HOJ 155 線段相交
6 Hungarian algorithm
In graph theory, Hungarian algorithm is an algorithm which solves the maximum bipar- tite matching problem.
7 Minimum Ratio Cycle 最小比率環
k ≤ ∑∑ABii →0≤∑
(Ai−kBi) 必然存在足夠大的 k 使得 ∑
(Ai −kBi) < 0,故可以二分規劃 k 導致負環存在的 下界。
7.1 Problems on OJs
HOJ 057 那時,我位於世界的盡頭
8 References
http://en.wikipedia.org/wiki/Eulerian_path
http://www.csie.ntnu.edu.tw/~u91029/Circuit.html
https://zh.wikipedia.org/wiki/%E5%8C%B9%E9%85%8D_(%E5%9B%BE%E8%AE%BA) http://www.csie.ntnu.edu.tw/~u91029/Matching.html
https://en.wikipedia.org/wiki/Flow_network
https://en.wikipedia.org/wiki/Maximum_flow_problem http://www.csie.ntnu.edu.tw/~u91029/Cut.html
http://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html
http://zh.wikipedia.org/wiki/%E5%8C%88%E7%89%99%E5%88%A9%E7%AE%97%E6%B3%95 http://www.csie.ntnu.edu.tw/~u91029/Cycle.html#4