连通图的(领接矩阵储存)创建和遍历(包含BFS,DFS两种算法)

来源:互联网 发布:美工做不出图 编辑:程序博客网 时间:2024/05/22 17:10
#include<iostream>#include<queue>using namespace std;//采用邻接矩阵表示法创建无向网//用两个数组分别存储顶点表和邻接矩阵const int MVNum = 100; //最大顶点数 typedef char VerTexType; //假设顶点的数据类型为字符型typedef int ArcType;    //假设边的权值类型为整型 typedef struct {    VerTexType vexs[MVNum];    //顶点表     ArcType arcs[MVNum][MVNum]; //邻接矩阵     int vexnum, arcnum;}AMGraph;int LocateVex(AMGraph G, VerTexType u){//存在则返回u在顶点表中的下标;否则返回-1    int i;    for (i = 0; i<G.vexnum; ++i)        if (u == G.vexs[i])            return i;    return -1;}void CreateUDN(AMGraph &G){    cout << "请输入总顶点数,总边数" << endl;    cin >> G.vexnum >> G.arcnum;//输入总顶点数,总边数    for (int o = 0; o < G.vexnum; o++)    {        cout << "请依次输入点的信息" << endl;        cin >> G.vexs[o];//依次输入点的信息    }    for (int i = 0; i < G.vexnum; i++) //初始化邻接矩阵,边的权值均置为极大值    {        for (int j = 0; j < G.vexnum; j++)        {            G.arcs[i][j] = INT_MAX;//内置常量        }    }    for (int k = 0; k < G.arcnum; k++)    {        int m, n, w;        VerTexType v1, v2;        cout << "输入一条边依附的顶点及权值 " << endl;        cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值         m = LocateVex(G, v1);        n = LocateVex(G, v2);//确定v1和v2在G中的位置        G.arcs[m][n] = w; //边<v1, v2>的权值置为w //置<v1, v2>的对称边<v2, v1>的权值为w        G.arcs[n][m] = G.arcs[m][n];    }    cout << "采用邻接矩阵表示法创建无向网成功" << endl;    /*for (int h = 0; h < G.vexnum; h++)    {        for (int g = 0; g < G.vexnum; g++)        {            cout << G.arcs[h][g] << endl;        }    }*/    return;}int firstAdjVex(AMGraph G, int v0){    for (int i = 0; i<G.vexnum; i++)        //if (G.arcs[v0][i] == 1) //图            if (G.arcs[v0][i]<INT_MAX) //网                return i;    return -1;}int nextAdjVex(AMGraph G, int v0, int w){    for (int i = w + 1; i<G.vexnum; i++)        //if (G.arcs[v0][i] == 1) //图            if (G.arcs[v0][i]<INT_MAX) //网                return i;    return -1;}void DFS(AMGraph G, int v0){//从v0出发,遍历连通图G的所有顶点    static bool visited[MVNum] = { false };//静态变量    //cout << "从第" << v0 << "个顶点开始遍历" << endl;    cout << G.vexs[v0] << endl;//邻接矩阵//邻接表:G.vertices[v0].data    visited[v0] = true;    //对v0的所有邻接顶点作深度优先遍历    for (int w = firstAdjVex(G, v0); w >= 0; w = nextAdjVex(G, v0, w))        if (!visited[w])            DFS(G, w);}bool visited1[MVNum] = { false };void BFS(AMGraph G, int v){//从v0出发,遍历连通图G的所有顶点    //cout << "从第" << v << "个顶点开始遍历" << endl;    //cout << G.vexs[v] << endl;//邻接矩阵    visited1[v] = true;    //广度优先算法    queue<int> q1;    q1.push(v);    while (!q1.empty()) {        int u;        u = q1.front();        q1.pop();        cout << G.vexs[u] << endl;        for (int w = firstAdjVex(G, u); w >= 0; w = nextAdjVex(G, u, w))        {            if (!visited1[w]) {                //cout << G.vexs[w] << endl;//邻接矩阵                visited1[w] = true;                q1.push(w);            }        }    }}int main(){    AMGraph a;    CreateUDN(a);    cout << "从第0个顶点开始D遍历" << endl;    DFS(a, 0);    cout << "从第0个顶点开始B遍历" << endl;    BFS(a, 0);    return 0;}![运行截图](http://img.blog.csdn.net/20170525131148100?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVuc29ucmFjaGVs/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
阅读全文
0 0