//1图的邻接矩阵表示法和邻接表创建无向图存储
#include<iostream>
using namespace std;
#define MaxNum 100 //最大顶点数
//1.1图的邻接矩阵表示法创建无向图存储
#define MaxInt 32767 //无穷
typedef char VerType;//数据类型
typedef int ArcType;//权值类型
//图结构类型
typedef struct {
VerType v[MaxNum];//顶点表
ArcType a[MaxNum][MaxNum];//邻接矩阵
int vernum, arcnum; //点数,边数
}AMGraph;
//定位
int locate(AMGraph G, VerType v1) {
for (int i = 0; i < G.vernum; i++) {
if (G.v[i] == v1)
return i;
}
return -1;
}
//创建无向图
bool createUDN(AMGraph &G) {
cout << "Please input the vernum and arcnum" << endl;
cin >> G.vernum >> G.arcnum;
int i,j,p,q;
VerType v1, v2;
ArcType w;
//初始化点的名字
cout << "Please input the name of vernum in order " << endl;
for (i = 0; i < G.vernum; i++) cin >> G.v[i];
//初始化邻接矩阵
for (i = 0; i < G.vernum; i++) {
for (j = 0; j < G.vernum; j++) {
if (i != j)
G.a[i][j] = MaxInt;
else
G.a[i][j] = 0;
}
}
//开始操作,构造邻接表
cout << "Please input arcnum graph " << endl;
for (i = 0; i < G.arcnum; i++) {
cin >> v1 >> v2 >> w;
p = locate(G, v1); q = locate(G, v2);
G.a[p][q] = w;
G.a[q][p] = G.a[p][q];
}
return true;
}
void show(AMGraph G) {
int i, j;
for (i = 0; i < G.vernum; i++) {
for (j = 0; j < G.vernum; j++) {
if(G.a[i][j]==MaxInt) printf("INF\t");
else printf("%-3d\t", G.a[i][j]);
}
cout << endl;
}
}
bool visited[MaxNum] = { false };
//1.深度优先搜索
void Dfs_AM(AMGraph G, int v) {//从第v个
cout << G.v[v] << " ";
visited[v] = true;
for (int w = 0; w < G.vernum; w++) {
if ((G.a[v][w] != 0 )&& (G.a[v][w] != MaxInt) && (!visited[w])) {//表示是为未被访问过的邻接点
Dfs_AM(G, w);
}
}
}
//队的结构
typedef int QElemType;
typedef struct QNode {
QElemType node;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct {
QueuePtr front, rear;
}LinkQueue;
//1.初始化有带头结点的队
bool initQueue(LinkQueue &L) {
L.front = L.rear = new QNode;
L.front->next = NULL;
return true;
}
//2.判空
bool isEmpty(LinkQueue L) {
if (L.front == L.rear) return true;
else return false;
}
//3.入队
bool EnQueue(LinkQueue &L, QElemType e) {
QNode * p = new QNode;
p->node = e;
p->next = NULL;
L.rear->next = p;
L.rear = p;
return true;
}
//4.出队
bool DeQueue(LinkQueue&L, QElemType &node) {
if (isEmpty(L))
return false;
QNode * temp = L.front->next;//从首元结点开始出去
node = temp->node;
L.front->next = temp->next;
//队最后一出队
if (L.rear == temp) {
L.rear = L.front;
}
delete temp;
temp = NULL;
return true;
}
//广度搜索
int FirstAdjVex(AMGraph G, int u) {
for (int i = 0; i < G.vernum; i++) {
if (G.a[u][i] != 0 && G.a[u][i] != MaxInt) {
return i;
}
}
return -1;
}
int NextAdjVex(AMGraph G, int u, int w) {
for (int i = w+1; i < G.vernum; i++) {
if (G.a[u][i] != 0 && G.a[u][i] != MaxInt) {
return i;
}
}
return -1;
}
void BFS(AMGraph G, int v) {
LinkQueue L;
initQueue(L);
EnQueue(L, v);
visited[v] = true;
cout << G.v[v] << " ";
while (!isEmpty(L)) {
DeQueue(L,v);
for (int w = FirstAdjVex(G, v); w >= 0; w = NextAdjVex(G, v, w)) {
if (!visited[w]) {
cout << G.v[w]<<" ";
EnQueue(L, w); visited[w] = true;
}
}
}
}
int main() {
AMGraph G;
createUDN(G);
show(G);
char ch;
cout << "Please input the value of initial point" << endl;
cin >> ch;
int v = locate(G, ch);
cout << "深度搜索" << endl;
Dfs_AM(G, v); cout << endl;
memset(visited, false, sizeof(visited));
cout << "广度搜索" << endl;
BFS(G, v);
}
/*测试案例1:
7 9
0 1 2 3 4 5 6
0 1 28
1 2 16
2 3 12
3 6 18
6 4 24
4 5 25
3 4 22
5 0 10
1 6 14
*/
//
//
/*测试案例2:
7 9
a b c d e f g
a b 28
a f 10
b c 16
b g 14
f e 25
e g 24
e d 22
c d 12
d g 18
*/