算法基础课—搜索与图论(四)最小生成树与二分图
最小生成树
应用——无向图
稀疏图一般用堆优化Prim、稠密图用朴素版Prim
在真实应用中,稠密图一般用朴素版Prim,稀疏图用Kruscal
prim算法——最小生成树
需要的数据结构
1、邻接矩阵——g[][] 由于prim算法一般用于稠密图,所以往往使用邻接矩阵
2、dist[]数组——记录各个点到当前集合内的最小距离
3、st[]——记录是否加入集合
算法思想
1、初始化:邻接矩阵初始化为∞,dist数组初始化,可以选择任意一个点加入集合st,st初始化
2、构造图,无向图的边赋值 g[a][b] = g[b][a] = w
3、for(i = 1; i <= n; i ++)
4、找到集合中dist最小的点,及距离集合最近的点
5、用t 更新其他点到集合的距离
6、st设置
模板
int n; // n表示点数
int g[N][N]; // 邻接矩阵,存储所有边
int dist[N]; // 存储其他点到当前最小生成树的距离
bool st[N]; // 存储每个点是否已经在生成树中
// 如果图不连通,则返回INF(值是0x3f3f3f3f), 否则返回最小生成树的树边权重之和
int prim()
{
memset(dist, 0x3f, sizeof dist);
int res = 0;
for (int i = 0; i < n; i ++ )
{
int t = -1;
for (int j = 1; j <= n; j ++ )
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
if (i && dist[t] == INF) return INF;
if (i) res += dist[t];
st[t] = true;
for (int j = 1; j <= n; j ++ ) dist[j] = min(dist[j], g[t][j]);
}
return res;
}
自己的代码
#include<iostream>
#include <cstring>
using namespace std;
const int N = 503;
int n, m;
int g[N][N];
int dist[N];
bool st[N];
int prim(){
int i, j, res = 0;
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for(i = 1; i <= n; i ++){
int t = -1;
for(j = 1; j <= n; j ++)
//寻找当前距离集合最小的加入集合
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
if(dist[t] == 0x3f3f3f3f) return 0x3f3f3f3f;
st[t] = true;
res += dist[t];
//遍历其所有边,更改dist
for(j = 1; j <= n; j ++){
if(!st[j])//这个if判断加不加都可以,因为判断过的dist[j]都不会再用到了
dist[j] = min(dist[j],g[t][j]);
}
}
return res;
}
int main(){
cin>>n>>m;
int i, a, b, w;
memset(g, 0x3f, sizeof g);
for(i = 0; i < m; i ++){
cin>>a>>b>>w;
g[a][b] = g[b][a]= min(g[a][b], w);//无向边,倒序逆序都要赋值
}
int result = prim();
if(result == 0x3f3f3f3f) cout<<"impossible"<<endl;
else cout<<result<<endl;
}
Kruskal算法
其实是并查集
需要的数据结构
1、p[]——并查集
2、结构体数组edges[],同时在设置时进行运算符重载,方便之后的排序操作
3、cnt——判断结束标志,合并两个不连通集的时候cnt++,如果图是连通图,则cnt要=n-1,如果要与n-1说明图不是连通图,没有最小生成树
算法思想
1、初始化:
结构体的初始化
struct Edge // 存储边
{
int a, b, w;
bool operator< (const Edge &W)const
{
return w < W.w;
}
}edges[M];
并查集的初始化
for (int i = 1; i <= n; i ++ ) p[i] = i; // 初始化并查集
2、将所有边按照权重排序
sort(edges, edges + m);
3、对每条边进行循环
判断是否联通——
a = find(a), b = find(b);
if (a != b) // 如果两个连通块不连通,则将这两个连通块合并
如果不连通,则将这两个连通块合并
if (a != b) // 如果两个连通块不连通,则将这两个连通块合并
{
p[a] = b;
res += w;
cnt ++ ;
}
4、判断是否存在最小生成树——cnt
if (cnt < n - 1) return INF;
模板
int n, m; // n是点数,m是边数
int p[N]; // 并查集的父节点数组
struct Edge // 存储边
{
int a, b, w;
bool operator< (const Edge &W)const
{
return w < W.w;
}
}edges[M];
int find(int x) // 并查集核心操作
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int kruskal()
{
sort(edges, edges + m);
for (int i = 1; i <= n; i ++ ) p[i] = i; // 初始化并查集
int res = 0, cnt = 0;
for (int i = 0; i < m; i ++ )
{
int a = edges[i].a, b = edges[i].b, w = edges[i].w;
a = find(a), b = find(b);
if (a != b) // 如果两个连通块不连通,则将这两个连通块合并
{
p[a] = b;
res += w;
cnt ++ ;
}
}
if (cnt < n - 1) return INF;
return res;
}
自己的代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//因为要排序,所以更好的是用结构体写
const int M = 2e5 + 10, N = 1e5 + 10;
int n, m;
int p[N];
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
struct Edge{
int a, b, w;
bool operator<(const Edge e) const{
return w < e.w;
}
}edges[M];
int kruscal(){
int i, cnt, res = 0;
for(i = 1; i <= n; i ++) p[i] = i;//初始化并查集
sort(edges, edges + m);//结构体排序
for(i = 0; i < m; i ++){
int a = edges[i].a;
int b = edges[i].b;
int w = edges[i].w;
int ax = find(a), bx = find(b);
if(ax != bx){
p[ax] = bx;
res += w;
cnt ++;//类似于统计边数?由于那些联通块内的不影响所以可以不统计
}
}
if(cnt < n - 1) return 0x3f3f3f3f;//最小生成树最少要有n-1条边并且能够联通
else return res;
}
int main(){
cin>>n>>m;
int i, a, b, w;
for(i = 0; i < m; i ++){
cin>>a>>b>>w;
if(a != b)
edges[i] = {a, b, w};//如果有重边怎么办?
}
int result = kruscal();
if(result == 0x3f3f3f3f) cout<<"impossible"<<endl;
else cout<<result<<endl;
}
二分图
奇数环——环中边的个数是奇数
如果图中不含有奇数环——二分图。如果是二分图——不含有奇数环
匈牙利算法
可以返回匹配数量最大的
染色法判断二分图
染色问题
一条边的左右端点一定不属于相同集合。一个是1,则与他相连的点是2
染色法判断二分图
如果在染色过程中没有矛盾则是二分图,有矛盾则是二分图
算法思想——染色法判断二分图
进行搜索,可以用深度优先也可以用宽度优先
如果在搜索的过程中,color == 0 ,则说明还没有被染色,则染色成对应颜色
遍历他们的边,如果某个邻点的color和当前相同,return false 。如果没有被染色,则染成相反颜色,继续深度优先搜索。
注意的点,由于图可能不是连通图,所以要以每个起点开始都遍历一遍。
模板
int n; // n表示点数
int h[N], e[M], ne[M], idx; // 邻接表存储图
int color[N]; // 表示每个点的颜色,-1表示未染色,0表示白色,1表示黑色
// 参数:u表示当前节点,c表示当前点的颜色
bool dfs(int u, int c)
{
color[u] = c;
for (int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if (color[j] == -1)
{
if (!dfs(j, !c)) return false;
}
else if (color[j] == c) return false;
}
return true;
}
bool check()
{
memset(color, -1, sizeof color);
bool flag = true;
for (int i = 1; i <= n; i ++ )
if (color[i] == -1)
if (!dfs(i, 0))
{
flag = false;
break;
}
return flag;
}
自己的代码
//注意有可能图不连通,所以不能只固定一个起点
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010, M = 200010;
int n, m;
int h[N], e[M], ne[M], idx;
int color[N];
void add(int a, int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx;
idx ++;
}
bool dfs(int u, int c){
int i;
color[u] = c;
for(i = h[u]; i != -1; i = ne[i]){
int j = e[i];
if(!color[j]){
if(!dfs(j, 3 - c)) return false;//注意这一步,要把bool结果返回回去
}
else if(color[j] == c) return false;
}
return true;
}
int main(){
int i, a, b;
bool flag = true;
memset(h,-1,sizeof h);
cin>>n>>m;
for(i = 0; i < m; i ++){
cin>>a>>b;
add(a, b);
add(b, a);//注意无向图两个都要加!!!!
}
for (int i = 1; i <= n; i ++ )
if (!color[i])
{
if (!dfs(i, 1))
{
flag = false;
break;
}
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
匈牙利算法——二分图的最大匹配
返回当前已经分好的二分图,可以匹配的最大匹配数
思想:如果可以匹配,就选取序号靠前的匹配(即第一个中意的),如果不行,依次查看其它中意的,如果中意的都有男朋友了,则看依次看其中意的男朋友能不能换一个女朋友
例子
第一个男的根据第一个中意的匹配大女孩子,第二个也类似
第三个男的匹配女的时候发现自己中意的女孩子,不是单身了,于是找到中意的女孩子的男朋友,看他是否可以换个女朋友,发现可以,于是各自匹配
第四个男生匹配
需要的数据结构
1、match[i]——记录第i个女生匹配到的男生序号
2、st[]——当男生在匹配女生时,记录这个女生是否被访问过,这个很需要,否则会无限循环
算法思想
1、依次遍历男生进行匹配
2、匹配——find函数
遍历其所有边
如果没有被访问过则查看女生是否有男朋友,如果没有,则匹配,如果有,则查看其所属男朋友是否可以找到其他女生。
if (!st[j])
{
st[j] = true;
if (match[j] == 0 || find(match[j]))
{
match[j] = x;
return true;
}
}
模板
时间复杂度是 O(nm)O(nm), nn 表示点数,mm 表示边数
int n1, n2; // n1表示第一个集合中的点数,n2表示第二个集合中的点数
int h[N], e[M], ne[M], idx; // 邻接表存储所有边,匈牙利算法中只会用到从第一个集合指向第二个集合的边,所以这里只用存一个方向的边
int match[N]; // 存储第二个集合中的每个点当前匹配的第一个集合中的点是哪个
bool st[N]; // 表示第二个集合中的每个点是否已经被遍历过
bool find(int x)
{
for (int i = h[x]; i != -1; i = ne[i])
{
int j = e[i];
if (!st[j])
{
st[j] = true;
if (match[j] == 0 || find(match[j]))
{
match[j] = x;
return true;
}
}
}
return false;
}
// 求最大匹配数,依次枚举第一个集合中的每个点能否匹配第二个集合中的点
int res = 0;
for (int i = 1; i <= n1; i ++ )
{
memset(st, false, sizeof st);
if (find(i)) res ++ ;
}
代码
//关于为什么一定要st数组,不加st数组会超出内存
//关于为什么要st数组是理解这道题的一个核心难点,我觉得最容易理解的一种说法是:
//如果没有st数组,那么假设左边1与右边1匹配,左边2也与右边1匹配,那么在给左边第二个点找匹配的时候,就会有,去递归右边1的对象(左边1),继而左边1执行find(1)的时候因为不加st数组的原因会无限递归find(1),从而导致爆栈
#include <iostream>
#include <cstring>
using namespace std;
const int N = 502, M = 1e5 + 10;
int n, m;
int match[N] = {0};
int h[N], e[M], ne[M], idx = 0;
bool st[N];
void add(int a,int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx;
idx++;
}
bool find(int l){
int i;
for(i = h[l]; i != -1; i = ne[i]){
int j = e[i];
if (!st[j])
{
st[j] = true;
if (match[j] == 0 || find(match[j]))
{
match[j] = l;
return true;
}
}
}
return false;
}
int main(){
int n1, n2, m, res = 0;
cin>>n1>>n2>>m;
int a, b, i;
memset(h, -1, sizeof h);
while(m--){
cin>>a>>b;
add(a, b);
}
for (int i = 1; i <= n1; i ++ )
{
memset(st, false, sizeof st);
if (find(i)) res ++ ;
}
cout<<res<<endl;
}