#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;
}
数据结构图的BFS广度优先搜索C语言
最新推荐文章于 2023-11-08 20:36:31 发布