图广度优先搜索BFS(邻接矩阵)C/C++

#include <bits/stdc++.h>

using namespace std;

typedef char VertexType;
typedef int EdgeType;
struct GraphStruct;
typedef struct GraphStruct *Graph;
struct GraphStruct{
    VertexType vexs[100];
    EdgeType edge[100][100];
    int visited[100];
    int vertexnum;
    int edgenum;
}; 

Graph CreateGraph()
{
    Graph g=(Graph)malloc(sizeof(struct GraphStruct));
    g->vertexnum=0;
    g->edgenum=0;
    return g;
}

void BFS(Graph g,int k)
{
    queue <int> q;
    q.push(k);
    g->visited[k]=1;
    int i;
    while(!q.empty()){
        i=q.front();
        q.pop();
        for(int j=0;j<g->vertexnum;j++){
            if(g->edge[i][j]==1 && g->visited[j]==0){
                g->visited[j]=1;
                q.push(j);
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/linyuan703/article/details/81288921