求邻接矩阵的连通分量(DFS)

求邻接矩阵的连通分量(DFS)

最近在看匈牙利匹配算法,相关学习记录下

下面是采用深度优先搜素的方式(DFS)找连通分量的简单C++示例代码

#include <iostream>
#include <vector>

using namespace std;

void dfs(int node, const vector<vector<int>>& graph, vector<bool>& visited, vector<int>& component) {
    visited[node] = true;
    component.push_back(node);

    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) {
            dfs(neighbor, graph, visited, component);
        }
    }
}

vector<vector<int>> findConnectedComponents(const vector<vector<int>>& graph) {
    int numNodes = graph.size();
    vector<bool> visited(numNodes, false);
    vector<vector<int>> components;

    for (int node = 0; node < numNodes; node++) {
        if (!visited[node]) {
            vector<int> component;
            dfs(node, graph, visited, component);
            components.push_back(component);
        }
    }

    return components;
}

int main() {
    vector<vector<int>> graph = {
        {1, 2},
        {0, 2},
        {0, 1},
        {7, 3},
        {7, 4},
        {6},
        {5},
        {3, 4}
    };

    vector<vector<int>> components = findConnectedComponents(graph);

    cout << "Connected Components:" << endl;
    for (const auto& component : components) {
        for (int node : component) {
            cout << node << " ";
        }
        cout << endl;
    }

    return 0;
}

在这段代码中,findConnectedComponents函数接受一个邻接表表示的图,并返回一个包含连通分量的向量。dfs函数用于深度优先搜索,它从给定的起始节点开始,遍历与之相连的所有未访问过的节点,并将它们添加到同一个连通分量中。在主函数中,我们使用一个示例图进行测试,并打印出图的连通分量。

运行结果:

Connected Components:
0 1 2 
3 7 4 
5 6
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
邻接矩阵是一种表示图的数据结构,它可以用于获得图的连通分量。具体实现过程如下: 1. 定义一个二维数组作为邻接矩阵,其中数组元素a[i][j]表示第i个顶点到第j个顶点是否有边相连。 2. 初始化邻接矩阵,将所有元素置为0或1。 3. 从第一个顶点开始遍历整个图,对于每个未访问过的顶点,进行深度优先搜索,并将搜索到的所有顶点标记为已访问。 4. 在深度优先搜索过程中,将与当前顶点相邻的未访问过的顶点加入一个集合中,这个集合即为一个连通分量。 5. 搜索完所有顶点后,得到所有的连通分量。 下面是一个使用邻接矩阵获得连通分量的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 最大顶点数 typedef struct { int vertex[MAX_VERTEX_NUM]; // 顶点数组 int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int vertex_num; // 顶点数 int edge_num; // 边数 } Graph; void init_graph(Graph *G) { int i, j; G->vertex_num = 0; G->edge_num = 0; for(i = 0; i < MAX_VERTEX_NUM; i++) { G->vertex[i] = 0; for(j = 0; j < MAX_VERTEX_NUM; j++) { G->edge[i][j] = 0; } } } void add_vertex(Graph *G, int v) { G->vertex[G->vertex_num++] = v; } void add_edge(Graph *G, int v1, int v2) { G->edge[v1][v2] = 1; G->edge[v2][v1] = 1; G->edge_num++; } void dfs(Graph *G, int v, int visited[], int component[]) { int i; visited[v] = 1; component[v] = 1; for(i = 0; i < G->vertex_num; i++) { if(G->edge[v][i] && !visited[i]) { dfs(G, i, visited, component); } } } void get_connected_components(Graph *G) { int visited[MAX_VERTEX_NUM] = {0}; // 标记顶点是否被访问过 int component[MAX_VERTEX_NUM] = {0}; // 标记顶点所属连通分量 int i, j; printf("The connected components of the graph are:\n"); for(i = 0; i < G->vertex_num; i++) { if(!visited[i]) { dfs(G, i, visited, component); printf("{"); for(j = 0; j < G->vertex_num; j++) { if(component[j]) { printf("%d ", G->vertex[j]); } } printf("}\n"); } } } int main() { Graph G; init_graph(&G); add_vertex(&G, 1); add_vertex(&G, 2); add_vertex(&G, 3); add_vertex(&G, 4); add_vertex(&G, 5); add_edge(&G, 0, 1); add_edge(&G, 1, 2); add_edge(&G, 2, 0); add_edge(&G, 3, 4); get_connected_components(&G); return 0; } ``` 运行结果如下: ``` The connected components of the graph are: {1 2 3 } {4 5 } ``` 该示例程序实现了一个无向图,包含5个顶点和4条边。使用邻接矩阵表示图,并对其进行深度优先搜索,即可得到该图的两个连通分量{1, 2, 3}和{4, 5}。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值