数据结构图的BFS广度优先搜索C语言

#include <iostream>
#include <queue>
using namespace std;
#define maxSize 100
#define Elemtype int
bool visited[maxSize];
typedef struct {
    Elemtype vexs[maxSize];
    int arcs[maxSize][maxSize];
    int numVexs;
    int numArcs;
}adjacenryMatrixGraph;

void initAdjacenryMatrixGraph(adjacenryMatrixGraph &G) {
    G.numArcs = 0;
    G.numVexs = 0;
}

void outputAdjacenryMatrixGraph(adjacenryMatrixGraph G) {
    for(int i = 0; i < G.numVexs; i ++) {
        for(int j = 0; j < G.numVexs; j ++) {
            if(j != G.numVexs - 1) cout << G.arcs[i][j] << " ";
            else cout << G.arcs[i][j] << endl;
        }
    }
    cout << endl;
}

int getVexIndexAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype e) {
    for(int i = 0; i < G.numVexs; i ++) {
        if(e == G.vexs[i]) return i;
    }
    return -1;
}

Elemtype getIndexVexAdjacenryMatrixGraph(adjacenryMatrixGraph G, int index) {
    if(index < G.numVexs) {
        return G.vexs[index];
    }
    else return -1;
}

void createAdjacenryMatrixGraph(adjacenryMatrixGraph &G) {
    cout << "please cin number of vexs: ";
    int numberVexs;
    cin >> numberVexs;
    cout << "please cin every vexs: ";
    G.numVexs = numberVexs;
    for(int i = 0; i < numberVexs; i ++) {
        cin >> G.vexs[i];
    }
    Elemtype firstVex;
    Elemtype lastVex;
    cout << "please cin two vexs where the edge is connected: ";
    cin >> firstVex >> lastVex;
    int firstIndex;
    int lastIndex;
    while(firstVex != -1 && lastVex != -1) {
        firstIndex = getVexIndexAdjacenryMatrixGraph(G, firstVex);
        lastIndex = getVexIndexAdjacenryMatrixGraph(G, lastVex);
        if(firstIndex == -1 || lastVex == -1) {
            cout << "cin ERROR, please cin again!!!" << endl;
            continue;
        }
        else {
            G.arcs[firstIndex][lastIndex] = 1;
            G.arcs[lastIndex][firstIndex] = 1;
            G.numArcs ++;
            cout << "please cin two vexs where the edge is connected: ";
            cin >> firstVex >> lastVex;
        }
    }
}

bool adjacentAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype a, Elemtype b) {
    int firstIndex = getVexIndexAdjacenryMatrixGraph(G, a);
    int lastIndex = getVexIndexAdjacenryMatrixGraph(G, b);
    if(firstIndex == -1 || lastIndex == -1) {
        cout << "not exits" << endl;
        return false;
    }
    else if(G.arcs[firstIndex][lastIndex] == 1) {
        cout << "exits" << endl;
        return true;
    }
    else {
        cout << "not exits" << endl;
        return false;
    }
}

void listNeighborsAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype a) {
    int index = getVexIndexAdjacenryMatrixGraph(G, a);
    if(index == -1) {
        cout << "cin vex ERROR";
        return ;
    }
    else {
        cout << "The point adjacent to the point: ";
        for(int i = 0; i < G.numVexs; i ++) {
            if(G.arcs[index][i] == 1) {
                Elemtype res = getIndexVexAdjacenryMatrixGraph(G, i);
                if(res != -1) {
                    cout << res << " ";
                }
            }
        }
        cout << endl;
        return ;
    }
}

bool insertVexAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype e) {
    if(G.numVexs == maxSize - 1) {
        cout << "this adj filled" << endl;
        return false;
    }
    else {
        G.vexs[G.numVexs - 1] = e;
        G.numVexs ++;
        return true;
    }
}

bool deleteVexAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype e) {
    int index = getVexIndexAdjacenryMatrixGraph(G, e);
    if(index == -1) {
        cout << "vex not exits" << endl;
        return false;
    }
    else {
        for(int i = 0; i < G.numVexs; i ++) {
            if(G.arcs[index][i] == 1) G.numArcs --;
        }
        for(int i = 0; i < G.numVexs; i ++) {
            for(int j = 0; j < G.numVexs; j ++) {
                if(i > index && j > index)
                    G.arcs[i - 1][j - 1] = G.arcs[i][j];
                else if(i > index)
                    G.arcs[i - 1][j] = G.arcs[i][j];
                else if(j > index)
                    G.arcs[i][j - 1] = G.arcs[i][j];
            }
        }
        for(int i = index; i < G.numVexs; i++) {
            G.vexs[i]=G.vexs[i+1];
        }
        G.numVexs--;
    }
}

bool insertArcAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype a, Elemtype b) {
    int firstIndex = getVexIndexAdjacenryMatrixGraph(G, a);
    int lastIndex = getVexIndexAdjacenryMatrixGraph(G, b);
    if(firstIndex == -1 || lastIndex == -1) {
        cout << "vexs has not exits" << endl;
        return false;
    }
    else {
        if(G.arcs[firstIndex][lastIndex] == 1) {
            cout << "this edge has exits" << endl;
            return false;
        }
        else {
            G.arcs[firstIndex][lastIndex] = 1;
            G.arcs[lastIndex][firstIndex] = 1;
            G.numArcs ++;
            return true;
        }
    }
}

bool deleteArcAdjacenryMatrixGraph(adjacenryMatrixGraph &G, Elemtype a, Elemtype b) {
    int firstIndex = getVexIndexAdjacenryMatrixGraph(G, a);
    int lastIndex = getVexIndexAdjacenryMatrixGraph(G, b);
    if(firstIndex == -1 || lastIndex == -1) {
        cout << "vexs has not exits" << endl;
        return false;
    }
    else {
        if(G.arcs[firstIndex][lastIndex] == 0) {
            cout << "this edge not exits" << endl;
            return false;
        }
        else {
            G.arcs[firstIndex][lastIndex] = 0;
            G.arcs[lastIndex][firstIndex] = 0;
            G.numArcs --;
            return true;
        }
    }
}

Elemtype getFirstNextAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype vex) {
    int index = getVexIndexAdjacenryMatrixGraph(G, vex);
    if(index != -1) {
        int res = -1;
        for(int i = 0; i < G.numVexs; i ++) {
            if(G.arcs[index][i] == 1) {
                res = i;
                break;
            }
        }
        Elemtype ans = getIndexVexAdjacenryMatrixGraph(G, res);
        return ans;
    }
    else {
        cout << "vex not exits" << endl;
        return -1;
    }
    return -1;
}

Elemtype getNextAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype w, Elemtype v) {
    int index1 = getVexIndexAdjacenryMatrixGraph(G, w);
    int index2 = getVexIndexAdjacenryMatrixGraph(G, v);
    if(index1 == -1 || index2 == -1) return -1;
    else {
        int res = -1;
        for(int i = index2 + 1; i < G.numVexs; i ++) {
            if(G.arcs[index1][i] == 1) {
                res = i;
                break;
            }
        }
        return getIndexVexAdjacenryMatrixGraph(G, res);
    }
};

void visit(Elemtype e) {
    cout << e << " ";
}

void BFSAdjacenryMatrixGraph(adjacenryMatrixGraph G, Elemtype e) {
    queue <Elemtype> q;
    q.push(e);
    Elemtype v = q.front();
    Elemtype w;
    visit(v);
    int elemindex = getVexIndexAdjacenryMatrixGraph(G, v);
    visited[elemindex] = true;
    while(! q.empty()) {
        q.pop();
        for(Elemtype w = getFirstNextAdjacenryMatrixGraph(G, v); w != -1; w = getNextAdjacenryMatrixGraph(G, v, w)) {
            int windex = getVexIndexAdjacenryMatrixGraph(G, w);
            if(visited[windex] == true) {
                ;
            }
            else {
                visit(w);
                visited[windex] = true;
                q.push(w);
            }
        }
    }
}

void BFSTravelAdjacenryMatrixGraph(adjacenryMatrixGraph G) {
    for(int i = 0; i < maxSize; i ++) {
        visited[i] = false;
    }
    for(int i = 0; i < G.numVexs; i ++) {
        if(visited[i] == false) {
            BFSAdjacenryMatrixGraph(G, getIndexVexAdjacenryMatrixGraph(G, i));
        }
    }
    cout << endl;
}

int main() {
    adjacenryMatrixGraph G;
    initAdjacenryMatrixGraph(G);
    createAdjacenryMatrixGraph(G);
    outputAdjacenryMatrixGraph(G);
    // deleteVexAdjacenryMatrixGraph(G, 20);
    // outputAdjacenryMatrixGraph(G);
    // cout << getFirstNextAdjacenryMatrixGraph(G, 10) << endl;
    // cout << getNextAdjacenryMatrixGraph(G, 10, 20) << endl;
    BFSTravelAdjacenryMatrixGraph(G);
    BFSAdjacenryMatrixGraph(G, 10);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值