并查集---判断图中是否存在环

算法描述:

并查集(union-find sets)是一种简单的用途广泛的集合. 并查集是若干个不相交集合,能够实现较快的合并和判断元素所在集合的操作,应用很多,如其求无向图的连通分量个数最小公共祖先带限制的作业排序,还有最完美的应用:实现Kruskar算法求最小生成树

算法实现:

//并查集判断是否存在环
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
//图中的边
struct Edge
{
    int start;//边的起点
    int end;//边的终点
};
//图结构体
struct Graph
{
    // V顶点个数, E边的个数
    int V,E;
    // 图就是边的集合!!!!!
    struct Edge* edge;
};
//创建一个图
struct Graph* createGraph(int V, int E)
{
    struct Graph* graph=(struct Graph*) malloc(sizeof(struct Graph));
    graph->V = V;
    graph->E = E;
    //生成graph->E条边的图
    graph->edge = (struct Edge*) malloc(graph->E*sizeof(struct Edge));
    return graph;
};
//查找元素i所在集合(根节点)
int find(int parent[], int i)
{
    //一维数组parent[] 来记录子集合
    if(parent[i]==-1)
    {//i的根节点是其自身
        return i;
    }
    //查找i的根节点所在集合
    return find(parent, parent[i]);
}
//合并集合
void join(int parent[], int x, int y)
{
    int xset=find(parent,x);
    int yset=find(parent,y);
    if(xset != yset)
    {
        parent[xset]=yset;
    }
}
//检测是否有环
int isCycle(struct Graph* graph)
{
    int *parent = (int*) malloc(graph->V * sizeof(int));
    //初始化所有集合(每个节点)
    //parent[n] 的每个元素都为-1,共有n个子集合,表示集合只有当前顶点一个元素
    memset(parent, -1, sizeof(int) * graph->V);
    //遍历所有边
    for(int i=0;i<graph->E;++i)
    {
        int x = find(parent, graph->edge[i].start);
        int y = find(parent, graph->edge[i].end);
        if(x==y)
        {//如果在一个集合就找到了环
            return 1;
        }
        join(parent, x, y);
    }
    return 0;
}
int main()
{
    /* 创建图
         0
         | \
         |   \
         1----2 */
    struct Graph* graph = createGraph(3,3);
    // 添加边 0-1
    graph->edge[0].start=0;
    graph->edge[0].end=1;
    // 添加边 1-2
    graph->edge[1].start=1;
    graph->edge[1].end=2;
    // 添加边 0-2
    graph->edge[2].start=0;
    graph->edge[2].end=2;
    if (isCycle(graph))
    {
        cout<<"graph contains cycle"<<endl;
    }
    else
    {
        cout<<"graph doesn't contain cycle"<<endl;
    }
    return 0;
}

九度1024 畅通工程

//使用并查集实现了kruskal算法求最小花费树
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct node
{
    int start;
    int end;
    int cost;
}Node;
int father[101];
int rank[101];
void Make_Set(int M)
{
    for(int i=1;i<=M;i++)//村庄从1开始编号
    {
        father[i]=i;
        rank[i]=0;
    }
}
int Find_Set(int x)
{
    if(x != father[x])
    {
        father[x]=Find_Set(father[x]);
    }
    return father[x];
}
int Union(int x,int y)
{
    x=Find_Set(x);
    y=Find_Set(y);
    if(x==y) return 0;//若没有合并,则返回0
    if(rank[x]>rank[y])
    {
        father[y]=x;
        rank[x] += rank[y];
    }
    else
    {
        if(rank[x]==rank[y])
        {
            ++rank[y];
        }
        father[x]=y;
    }
    return 1;//若进行了合并,则返回1
}

int cmp(const void *p, const void *q)
{
    Node * p1=(Node *)p;
    Node * q1=(Node *)q;
    return p1->cost - q1->cost;//从小到大
}

int main()
{
    int N,M;
    int ans;
    int cnt;
    Node road[5000];
    while(cin>>N>>M&&N)
    {
        for(int i=0;i<N;i++)
        {
            scanf("%d%d%d",&road[i].start,&road[i].end,&road[i].cost);
        }
        qsort(road,N,sizeof(Node),cmp);//将N条道路的花费值从小到大排序
        Make_Set(M);
        ans=0;
        cnt=0;
        for(int i=0;i<N;i++)
        {
            if(cnt==M-1)//按照树的定义,M个点有M-1条连线
                break;
            if(Union(road[i].start,road[i].end))
            {
                ++cnt;
                ans += road[i].cost;
            }
        }
        if(cnt==M-1)//实现了kruskal算法求最小花费树
        {
            printf("%d\n",ans);
        }
        else
        {
            printf("?\n");
        }
    }
    return 0;
}
使用并查集判断无向是否存在的核心思想是:在遍历无向的边时,检查每条边的两个端点是否属于同一个集合。如果属于同一个集合,说明这两个端点已经通过其他路径相连,此时再添加这条边就会形成;如果不属于同一个集合,则将这两个集合合并。以下是详细的步骤及实现代码: ### 算法步骤 1. **初始化并查集**:将中每个顶点的根节点初始化为其自身,表示每个顶点都属于一个独立的集合。 2. **遍历边**:对于无向的每一条边,检查其两个端点的根节点。 3. **判断是否**:若两个端点的根节点相同,说明这两个端点在同一个集合中,添加这条边会形成;若不同,则将这两个集合合并。 4. **返回结果**:在遍历完所有边后,若未发现形成的情况,则判定中无。 ### 代码实现 ```cpp #include <iostream> using namespace std; // 查找元素x所在集合的根节点,并进行路径压缩 int find(int x, int pre[]) { int p = x, temp; while (x != pre[x]) { pre[x] = pre[pre[x]]; x = pre[x]; } return x; } // 判断无向是否 int hasHuan(int map[5][5]) { int pre[5]; // 初始化每个顶点的根节点为其自身 for (int i = 0; i < 5; i++) { pre[i] = i; } // 遍历无向的上三角矩阵,避免重复检查边 for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { if (map[i][j] == 1) { // 查找顶点i和j的根节点 int a = find(i, pre); int b = find(j, pre); if (a != b) { // 若根节点不同,合并两个集合 pre[a] = b; } else { // 若根节点相同,说明存在 return 1; } } } } // 遍历完所有边后未发现 return 0; } int main() { int map1[5][5] = {{0, 1, 1, 0, 0}, {1, 0, 1, 1, 1}, {1, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}}; int map2[5][5] = {{0, 1, 1, 0, 0}, {1, 0, 0, 1, 1}, {1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}}; cout << hasHuan(map1) << endl; // 输出 1,表示有 cout << hasHuan(map2) << endl; // 输出 0,表示无 return 0; } ``` 上述代码通过并查集算法有效地判断了无向是否存在。首先,`find` 函数用于查找元素所在集合的根节点,并进行路径压缩以提高查找效率;`hasHuan` 函数用于遍历的边,判断是否存在;`main` 函数提供了两个测试用例,分别展示有和无的情况。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值