采用邻接矩阵表示法创建无向网
//采用邻接矩阵表示法创建无向网
#include
using namespace std;
#define MaInt 32767
#define MVNum 100
#define OK 1
typedef char VerTextType;
typedef int ArcType;
typedef struct {
VerTextType Vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum, arcnum;
}AMGrach;
int LocateVex(AMGrach G, VerTextType v) {
for (int i = 0;i < G.vexnum;i++) {
if (G.Vexs[i] == v)
return i;
}
return -1;
}
int CreateUDN(AMGrach& G) {
int i, j, k;
cout << "请输入总顶点数,总边数,以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
cout << "输入点的名称,如a" << endl;
for (i = 0;i < G.vexnum;i++) {
cout << "input the " << i << " name";
cin >> G.Vexs[i];
}
cout << endl;
for (i = 0;i < G.vexnum;i++)
for (j = 0;j < G.vexnum;++j)
G.arcs[i][j] = MaInt;
cout << "输入边依附的顶点及权值,如 a b 5" << endl;
for (k = 0;k < G.arcnum;++k) {
VerTextType v1, v2;
ArcType w;
cout << "input the" << (k + 1) << " side of weigth";
cin >> v1 >> v2 >> w;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.arcs[i][j] = w;
G.arcs[j][i] = G.arcs[i][j];
}
return 0;
}
int main() {
cout << "采用邻接矩阵表示法创建无向网";
AMGrach G;
int i, j;
CreateUDN(G);
cout << endl;
for (i = 0;i < G.vexnum;i++) {
for (j = 0;j < G.vexnum;++j) {
if (j != G.vexnum - 1) {
if (G.arcs[i][j] != MaInt) {
cout << G.arcs[i][j] << "\t";
}
else {
cout << "~" << "\t";
}
}
else {
if (G.arcs[i][j] != MaInt)
cout << G.arcs[i][j] << endl;
else
cout << "~" << endl;
}
}
}
cout << endl;
return 0;
}
采用邻接表表示法创建无向图
//采用邻接表表示法创建无向图
#include
using namespace std;
#define MVNnm 100
#define OK 1
typedef char VerTexType;
typedef int OtherInfo;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
OtherInfo info;
}ArcNode;
typedef struct VNode {
VerTexType data;
ArcNode* firstarc;
}VNode, adjList[MVNnm];
typedef struct {
adjList vertices;
int vexnum, arcnum;
}ALGraph;
int LocateVex(ALGraph G, VerTexType v) {
for (int i = 0;i < G.vexnum;++i) {
if (G.vertices[i].data == v) {
return i;
}
}
return -1;
}
int CreatUDG(ALGraph& G) {
int i, k;
cout << "请输入总顶点数,总边数中间以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
for (i = 0;i < G.vexnum;++i) {
cout << "请输入第" << (i + 1) << "个点的名称:";
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
cout << endl;
cout << "请输入一条边依附的顶点,如 a b" << endl;
for (k = 0;k < G.arcnum;++k) {
VerTexType v1, v2;
int i, j;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
ArcNode* p1 = new ArcNode;
p1->adjvex = j;
p1->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p1;
ArcNode* p2 = new ArcNode;
p2->adjvex = j;
p2->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p2;
}
return OK;
}
int main() {
cout << "采用邻接表表示法创建无向图" << endl;
ALGraph G;
CreatUDG(G);
int i;
cout << endl;
cout << "邻接表表示法创建的无向图" << endl;
for (i = 0;i < G.vexnum;i++) {
VNode temp = G.vertices[i];
ArcNode* p = temp.firstarc;
if (p == NULL) {
cout << G.vertices[i].data;
cout << endl;
}
else {
cout << temp.data;
while (p) {
cout << "->";
cout << p->adjvex;
p = p->nextarc;
}
}
cout << endl;
}
return 0;
}
深度优先搜索遍历连通图的递归算法
//深度优先搜索遍历连通图的递归算法
#include
using namespace std;
#define MVNun 100
typedef char VerTexType;
typedef int ArcType;
typedef struct {
VerTexType vexs[MVNun];
ArcType arcs[MVNun][MVNun];
int vexnum, arcnum;
}Graph;
bool visited[MVNun];
int FirstAdjVex(Graph G, int v);
int NextAdjVex(Graph G, int v, int w);
int LocateVex(Graph G, VerTexType v) {
for (int i = 0;i < G.vexnum;++i) {
if (G.vexs[i] == v)
return i;
}
return -1;
}
void CreateUDN(Graph& G) {
int i, j, k;
cout << "请输入总顶点数,总边数 , 以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
cout << "输入点的名称,如 a:" << endl;
for (i = 0;i < G.vexnum;++i) {
cout << "请输入第" << (i + 1) << "请输入第";
cin >> G.vexs[i];
}
cout << endl;
for (i = 0;i < G.vexnum;++i)
for (j = 0;j < G.vexnum;++j)
G.arcs[i][j] = 0;
cout << "输入边依附的顶点,如:a b" << endl;
for (k = 0;k < G.arcnum;++k) {
VerTexType v1, v2;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.arcs[j][i] = G.arcs[i][j] = 1;
}
}
void DFS(Graph G, int v) {
cout << G.vexs[v] << " "; visited[v] = true;
int w;
for (w = FirstAdjVex(G, v); w >= 0; w = NextAdjVex(G, v, w))
if (!visited[w]) DFS(G, w);
}
int FirstAdjVex(Graph G, int v) {
int i;
for (i = 0;i < G.vexnum;++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int NextAdjVex(Graph G, int v, int w) {
int i;
for (i = w;i < G.vexnum;++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int main() {
cout << "深度优先搜索遍历连通图的递归算法";
Graph G;
CreateUDN(G);
cout << endl;
cout << "无向连通图G创建完成!" << endl;
cout << "请输入遍历连通图的起始点:";
VerTexType c;
cin >> c;
int i;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vexs[i])
break;
}
cout << endl;
while (i >= G.vexnum) {
cout << "该点不存在,请重新输入!" << endl;
cout << "请输入遍历连通图的起始点:";
cin >> c;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vexs[i]) {
break;
}
}
}
cout << "深度优先搜索遍历连通图结果:" << endl;
DFS(G, i);
cout << endl;
return 0;
}
深度优先搜索遍历非连通图
//深度优先搜索遍历非连通图
#include
using namespace std;
#define MVNum 100
typedef char VerTexType;
typedef int ArcType;
typedef struct {
VerTexType vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum, arcnum;
}Graph;
bool visited[MVNum];
int FirstAdjVex(Graph G, int v);
int NextAdjVex(Graph G, int v, int w);
int LocateVex(Graph G, VerTexType v) {
for (int i = 0;i < G.vexnum;++i) {
if (G.vexs[i] == v)
return i;
}
return -1;
}
void CreateUDN(Graph &G) {
int i, j, k;
cout << "请输入总顶点数,总边数,以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
cout << "输入点的名称,如a" << endl;
for (i = 0;i < G.vexnum;++i) {
cout << "请输入第" << (i + 1) << "个数的名称";
cin >> G.vexs[i];
}
cout << endl;
for (i = 0;i < G.vexnum;++i) {
for (j = 0;j < G.vexnum;++j) {
G.arcs[i][j] = 0;
}
}
cout << "输入边依附的顶点,如a b" << endl;
for (k = 0;k < G.arcnum;k++) {
VerTexType v1, v2;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.arcs[j][i] = G.arcs[i][j]=1;
}
}
void DFS(Graph G, int v) {
cout << G.vexs[v] << " ";
visited[v] = true;
int w;;
for (w = FirstAdjVex(G, v);w >= 0;w = NextAdjVex(G, v, w))
if (visited[w]) DFS(G, w);
}
//key
void DFSTraverse(Graph G) {
int v;
for (v = 0;v < G.vexnum;++v)
visited[v] = false;
for (v = 0;v < G.vexnum;++v)
if (!visited[v]) DFS(G, v);
}
int FirstAdjVex(Graph G, int v) {
int i;
for (i = 0;i < G.vexnum;++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int NextAdjVex(Graph G, int v, int w) {
int i;
for (i = w; i < G.vexnum; ++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int main() {
cout << "深度优先搜索遍历非连通图";
Graph G;
CreateUDN(G);
cout << endl;
cout << "无向图G创建完成!" << endl;
cout << "深度优先搜索遍历非连通图结果:" << endl;
DFSTraverse(G);
cout << endl;
return 0;
}
采用邻接矩阵表示图的深度优先搜索遍历(与深度优先搜索遍历连通图的递归算法仅仅是DFS的遍历方式变了)
//采用邻接矩阵表示图的深度优先搜索遍历(与深度优先搜索遍历连通图的递归算法仅仅是DFS的遍历方式变了)
#include
using namespace std;
#define MVNun 100
typedef char VerTexType;
typedef int ArcType;
typedef struct {
VerTexType vexs[MVNun];
ArcType arcs[MVNun][MVNun];
int vexnum, arcnum;
}Graph;
bool visited[MVNun];
int FirstAdjVex(Graph G, int v);
int NextAdjVex(Graph G, int v, int w);
int LocateVex(Graph G, VerTexType v) {
for (int i = 0;i < G.vexnum;++i) {
if (G.vexs[i] == v)
return i;
}
return -1;
}
void CreateUDN(Graph& G) {
int i, j, k;
cout << "请输入总顶点数,总边数 , 以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
cout << "输入点的名称,如 a:" << endl;
for (i = 0;i < G.vexnum;++i) {
cout << "请输入第" << (i + 1) << "请输入第";
cin >> G.vexs[i];
}
cout << endl;
for (i = 0;i < G.vexnum;++i)
for (j = 0;j < G.vexnum;++j)
G.arcs[i][j] = 0;
cout << "输入边依附的顶点,如:a b" << endl;
for (k = 0;k < G.arcnum;++k) {
VerTexType v1, v2;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.arcs[j][i] = G.arcs[i][j] = 1;
}
}
void DFS(Graph G, int v){
int w;
cout << G.vexs[v] << " "; visited[v] = true;
for(w = 0; w < G.vexnum; w++)
if((G.arcs[v][w] != 0)&& (!visited[w])) DFS(G, w);
}
int FirstAdjVex(Graph G, int v) {
int i;
for (i = 0;i < G.vexnum;++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int NextAdjVex(Graph G, int v, int w) {
int i;
for (i = w;i < G.vexnum;++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int main() {
cout << "深度优先搜索遍历连通图的递归算法";
Graph G;
CreateUDN(G);
cout << endl;
cout << "无向连通图G创建完成!" << endl;
cout << "请输入遍历连通图的起始点:";
VerTexType c;
cin >> c;
int i;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vexs[i])
break;
}
cout << endl;
while (i >= G.vexnum) {
cout << "该点不存在,请重新输入!" << endl;
cout << "请输入遍历连通图的起始点:";
cin >> c;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vexs[i]) {
break;
}
}
}
cout << "深度优先搜索遍历连通图结果:" << endl;
DFS(G, i);
cout << endl;
return 0;
}
采用邻接表表示图的深度优先搜索遍历
//采用邻接表表示图的深度优先搜索遍历
#include
using namespace std;
#define MVNum 100
typedef char VerTexType;
typedef char VerTexType;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
}ArcNode;
typedef struct VNode {
VerTexType data;
ArcNode* firstarc;
}VNode, AdjList[MVNum];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
}ALGraph;
bool visited[MVNum];
int LocateVex(ALGraph G, VerTexType v) {
for (int i = 0;i < G.vexnum;++i)
if (G.vertices[i].data == v)
return i;
return -1;
}
void CreateUDG(ALGraph& G) {
int i, k;
cout << "请输入总顶点数,总边数,以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
cout << "输入点的名称,如a" << endl;
for (i = 0;i < G.vexnum;++i) {
cout << "请输入第" << (i + 1) << "个点的名称:";
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
cout << endl;
cout << "输入边依附的顶点,如a b" << endl;
for (k = 0;k < G.arcnum;++k) {
VerTexType v1, v2;
int i, j;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
ArcNode* p1 = new ArcNode;
p1->adjvex = j;
p1->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p1;
ArcNode* p2 = new ArcNode;
p1->adjvex = j;
p1->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p2;
}
}
void DFS(ALGraph G, int v) {
cout << G.vertices[v].data << " ";
visited[v] = true;
ArcNode* p = G.vertices[v].firstarc;
while (p != NULL) {
int w = p->adjvex;
if (!visited[w]) DFS(G, w);
p = p->nextarc;
}
}
int main() {
cout << "采用邻接表表示图的深度优先搜索遍历";
ALGraph G;
CreateUDG(G);
cout << endl;
cout << "无向连通图G创建完成!" << endl;
cout << "请输入其实的点";
VerTexType c;
cin >> c;
int i;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vertices[i].data)
break;
}
cout << endl;
while (i >= G.vexnum) {
cout << "该点不存在,请重新输入!" << endl;
cout << "请输入遍历连通图的起始点:";
cin >> c;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vertices[i].data)
break;
}
}
cout << "深度优先搜索遍历图结果:" << endl;
DFS(G, i);
cout << endl;
return 0;
}
广度优先搜索遍历连通图
//广度优先搜索遍历连通图
#include
using namespace std;
#define MVNum 100
#define MAXQSIZE 100
typedef char VerTexType;
typedef int ArcType;
bool visited[MVNum];
typedef struct {
VerTexType vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum, arcnum;
}Graph;
typedef struct {
ArcType* base;
int front;
int rear;
}sqQueue;
void InitQueue(sqQueue& Q) {
Q.base = new ArcType[MAXQSIZE];
if (!Q.base)
exit(1);
Q.front = Q.rear = 0;
}
void EnQueue(sqQueue& Q, ArcType e) {
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
}
bool QueueEmpty(sqQueue Q) {
if (Q.rear == Q.front)
return true;
return false;
}
void Dequeue(sqQueue& Q, ArcType& u) {
u = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
}
int LocateVex(Graph G, VerTexType v) {
for (int i = 0;i < G.vexnum;++i)
if (G.vexs[i] == v)
return i;
return -1;
}
void CreateUDN(Graph& G) {
int i, j, k;
cout << "请输入总顶点数,总边数,以空格隔开:";
cin >> G.vexnum >> G.arcnum;
cout << endl;
cout << "输入点的名称,如a" << endl;
for (i = 0;i < G.vexnum;++i) {
cout << "请输入第" << (i + 1) << "个点的名称:";
cin >> G.vexs[i];
}
cout << endl;
for (i = 0;i < G.vexnum;++i) {
for (j = 0;j < G.vexnum;++i)
G.arcs[i][j] = 0;
}
cout << "输入边依附的顶点,如a b" << endl;
for (k = 0;k < G.vexnum;k++) {
VerTexType v1, v2;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G.arcs[i][j] = 1;
G.arcs[j][i] = G.arcs[i][j];
}
}
int FirstAdjVex(Graph G, int v) {
int i;
for (i = 0;i < G.vexnum;++i) {
if (G.arcs[v][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
int NextAdjVex(Graph G, int u, int w) {
int i;
for (i = w;i < G.vexnum;++i) {
if (G.arcs[u][i] == 1 && visited[i] == false)
return i;
}
return -1;
}
void BFS(Graph G, int v) {
sqQueue Q;
ArcType u;
ArcType w;
cout << G.vexs[v] << " ";
visited[v] = true;
InitQueue(Q);
EnQueue(Q, v);
while (!QueueEmpty(Q)) {
Dequeue(Q, u);
for (w = FirstAdjVex(G, u);w >= 0;w = NextAdjVex(G, u, w)) {
if (!visited[w]) {
cout << G.vexs[w] << " ";
visited[w] = true;
EnQueue(Q, w);
}
}
}
}
int main() {
cout << "广度优先搜索遍历连通图";
Graph G;
CreateUDN(G);
cout << endl;
cout << "无向连通图G创建完成!" << endl;
cout << "请输入遍历连通图的起始点:";
VerTexType c;
cin >> c;
int i;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vexs[i])
break;
}
cout << endl;
while (i >= G.vexnum) {
cout << "该点不存在,请重新输入!" << endl;
cout << "请输入遍历连通图的起始点:";
cin >> c;
for (i = 0;i < G.vexnum;++i) {
if (c == G.vexs[i])
break;
}
}
cout << "广度优先搜索遍历连通图结果:" << endl;
BFS(G, i);
cout << endl;
return 0;
}