根据邻接表的定义可知,对于n个顶点和e条边的无向图,其邻接表有n个表头结点和2e个边结点。对于n个结点和e条边的有向图,其邻接表有n个表头结点和e个边结点。
#define MaxVex 20//自定义最大顶点数
typedef enum
{
DG,UDG,DN,UDN
}GraphKind;//有向图,无向图,有向网,无向网
typedef int VElemType;
typedef struct ArcNode//边结点定义
{
int adjvex;//终点(或弧尾)在数组表中的下标
int info;///该边(弧)相关信息(权值)
ArcNode* nextarc;//存储下一条边(或弧)结点的地址
}ArcNode;
typedef struct//表头结点的定义
{
VElemType data;
ArcNode* firstarc;//存储第一条依附该顶点的边(或弧)结点地址
}VNode;
typedef struct
{
VNode vertices[MaxVex];
int vexnum;
int arcnum;
GraphKind kind;
}AdjLGraph;
class ALGraph
{
private:
AdjLGraph ag;
public:
void CreateGraph(int n, int m);//创建n个顶点,m条边的图,以无向网为例
int LocateVex(VElemType u);//图中存在顶点u,则返回该顶点在数组中的下标,否则返回-1
int Degree(VElemType u);//计算顶点u的度数
void InsertArcGraph(VElemType u, VElemType v, int info);//插入一条边
void BFS(VElemType v);//以v为初始点的连通分量的广度优先搜索
void DFS(VElemType v);//以v为初始点的连通分量的深度优先搜索
void BFSTraverse();//图的广度优先搜索
void DFSTreverse();//图的深度优先搜索
int Connected();//计算连通分量的个数
Edge* Kruskal();//Kruskal算法求最小生成树
Edge* Prim(VElemTyp u);//prim算法求最小生成树
int TopSort();//拓扑排序
int CriticalPath();//求关键路径
AdjLGraph GetAg()
{
return ag;//返回私有成员
}
};
void ALGraph::CreateGraph(int n, int m)//以无向网为例
{
ag.vexnum = n;
ag.arcnum = m;
ag.kind = UDN;
int i, j, w, h, t;
VElemType u, v;
ArcNode* p;
for (i = 0; i < n; i++)
{
cout << "请输入" << n << "个顶点:";
cin >> ag.vertices[i].data;
ag.vertices[i].firstarc = NULL;
}
for (j = 0; j < m; j++)//建立边集
{
cin >> u >> v >> w;//输入一条弧
h = LocateVex(u);
t = LocateVex(v);
p = new ArcNode;
p->adjvex = t;
p->info = w;
p->nextarc = ag.vertices[h].firstarc;
ag.vertices[h].firstarc = p;
p = new ArcNode;
p->adjvex = h;
p->info = w;
p->nextarc = ag.vertices[t].firstarc;
ag.vertices[t].firstarc = p;
}
}
void ALGraph::CreateGraph(int n, int m)
{
ag.vexnum = n;
ag.arcnum = m;
ag.kind = UDN;
int i, j, w, h, t;
VElemType u, v;
ArcNode* p;
for (i = 0; i < n; i++)
{
cout << "请输入" << n << "个顶点:";
cin >> ag.vertices[i].data;
ag.vertices[i].firstarc = NULL;
}
for (j = 0; j < m; j++)//建立边集
{
cin >> u >> v >> w;//输入一条弧
h = LocateVex(u);
t = LocateVex(v);
p = new ArcNode;//
p->adjvex = t;
p->info = w;
p->nextarc = ag.vertices[h].firstarc;
ag.vertices[h].firstarc = p;
}
}
int ALGraph::LocateVex(VElemType u)
{
for (int i = 0; i < ag.vexnum; i++)
{
if (u == ag.vertices[i].data)
return i;
}
return -1;
}
int ALGraph::Degree(VElemType u)
{
int h = LocateVex(u);//结点u的下标
int count = 0;
ArcNode* p = ag.vertices[h].firstarc;//p指向第h条链表的第一个结点
while (p)
{
count++;
p = p->nextarc;
}
return count;
}
void ALGraph::InsertArcGraph(VElemType u, VElemType v, int info)//无向网为例
{
int h = LocateVex(u);
int t = LocateVex(v);
ArcNode* p;
if (h == -1)
{
ag.vertices[ag.vexnum].data = u;
ag.vertices[ag.vexnum].firstarc = NULL;
h = ag.vexnum;
ag.vexnum++;
}
if (t == -1)
{
ag.vertices[ag.vexnum].data = v;
ag.vertices[ag.vexnum].firstarc = NULL;
t = ag.vexnum;
ag.vexnum++;
}
p = new ArcNode;
p->adjvex = t;
p->info = info;
p->nextarc = ag.vertices[h].firstarc;
ag.vertices[h].firstarc = p;
p = new ArcNode;
p->adjvex = h;
p->info = info;
p->nextarc = ag.vertices[t].firstarc;
ag.vertices[t].firstarc = p;
ag.arcnum++;
}
✅深度优先搜索类似于树的先序遍历,是树的先序遍历的推广。深度优先搜索是一个不断探查和回溯的过程,具体过程如下:
❗说明:
int visited[MaxVex];//访问标志数组,初始化所有元素值为0
void ALGraph::DFS(VElemType v)//以v为初始点的连通分量的深度优先搜索算法如下
{
ArcNode* p;
int h = LocateVex(v);
cout << v;//访问该顶点
visited[h] = 1;//置访问标记为1
for (p = ag.vertices[h].firstarc; p; p = p->nextarc)
{
if (visited[p->adjvex] == 0)
DFS(ag.vertices[p->adjvex].data);
}
}
void ALGraph::DFSTreverse()//对图作深度优先搜索
{
int i;
for (i = 0; i < ag.vexnum; i++)
{
visited[i] = 0;//访问标志初始化
}
for (i = 0; i < ag.vexnum; i++)
{
if (!visited[i])//对尚未访问的顶点调用DFS
DFS(ag.vertices[i].data);
}
}
广度优先搜索类似于树的层次遍历方法,其搜索过程如下:
void ALGraph::BFS(VElemType v)//以v为初始点的连通分量的广度优先搜索
{
int h = LocateVex(v);
ArcNode* p;
LinkQueue lq;
lq.DeQueue(h);
visited[h] = 1;
while (!lq.EmptyQueue())
{
lq.DeQueue(h);
cout << ag.vertices[h].data;
for (p = ag.vertices[h].firstarc; p; p = p->nextarc)
{
if (!visited[p->adjvex])
{
lq.EnQueue(p->adjvex);
visited[p->adjvex] = 1;
}
}
}
}
void ALGraph::BFSTraverse()
{
int i;
for (i = 0; i < ag.vexnum; i++)
{
visited[i] = 0;
}
for (i = 0; i < ag.vexnum; i++)
{
if (!visited[i])
BFS(ag.vertices[i].data);
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
using namespace std;
#define MaxVex 20//自定义最大顶点数
typedef char VElemType;
typedef struct ArcNode//边结点定义
{
int adjvex;//终点(或弧尾)在数组表中的下标
int info;///该边(弧)相关信息(权值)
ArcNode* nextarc;//存储下一条边(或弧)结点的地址
}ArcNode;
typedef struct//表头结点的定义
{
VElemType data;
ArcNode* firstarc;//存储第一条依附该顶点的边(或弧)结点地址
}VNode;
typedef struct
{
VNode vertices[MaxVex];
int vexnum;
int arcnum;
}AdjLGraph;
class ALGraph
{
private:
AdjLGraph ag;
public:
void CreateGraph(int n, int m);//创建n个顶点,m条边的图,以无向网为例
int LocateVex(VElemType u);//图中存在顶点u,则返回该顶点在数组中的下标,否则返回-1
int Degree(VElemType u);//计算顶点u的度数
void InsertArcGraph(VElemType u, VElemType v, int info);//插入一条边
void BFS(VElemType v);//以v为初始点的连通分量的广度优先搜索
void DFS(VElemType v);//以v为初始点的连通分量的深度优先搜索
void BFSTraverse();//图的广度优先搜索
void DFSTreverse();//图的深度优先搜索
AdjLGraph GetAg()
{
return ag;//返回私有成员
}
};
void ALGraph::CreateGraph(int n, int m)//以无向网为例
{
ag.vexnum = n;
ag.arcnum = m;
int i, j, w, h, t;
VElemType u, v;
ArcNode* p;
cout << "请输入" << n << "个顶点:";
for (i = 0; i < n; i++)
{
cin >> ag.vertices[i].data;
ag.vertices[i].firstarc = NULL;
}
cout << "请输入" << m << "条边(u,v,w):" << endl;
for (j = 0; j < m; j++)//建立边集
{
cin >> u >> v >> w;//输入一条弧
h = LocateVex(u);
t = LocateVex(v);
p = new ArcNode;//
p->adjvex = t;
p->info = w;
p->nextarc = ag.vertices[h].firstarc;
ag.vertices[h].firstarc = p;
p = new ArcNode;//
p->adjvex = h;
p->info = w;
p->nextarc = ag.vertices[t].firstarc;
ag.vertices[t].firstarc = p;
}
}
int ALGraph::LocateVex(VElemType u)
{
for (int i = 0; i < ag.vexnum; i++)
{
if (u == ag.vertices[i].data)
return i;
}
return -1;
}
int ALGraph::Degree(VElemType u)
{
int h = LocateVex(u);//结点u的下标
int count = 0;
ArcNode* p = ag.vertices[h].firstarc;//p指向第h条链表的第一个结点
while (p)
{
count++;
p = p->nextarc;
}
return count;
}
void ALGraph::InsertArcGraph(VElemType u, VElemType v, int info)//无向网为例
{
int h = LocateVex(u);
int t = LocateVex(v);
ArcNode* p;
if (h == -1)
{
ag.vertices[ag.vexnum].data = u;
ag.vertices[ag.vexnum].firstarc = NULL;
h = ag.vexnum;
ag.vexnum++;
}
if (t == -1)
{
ag.vertices[ag.vexnum].data = v;
ag.vertices[ag.vexnum].firstarc = NULL;
t = ag.vexnum;
ag.vexnum++;
}
p = new ArcNode;
p->adjvex = t;
p->info = info;
p->nextarc = ag.vertices[h].firstarc;
ag.vertices[h].firstarc = p;
p = new ArcNode;
p->adjvex = h;
p->info = info;
p->nextarc = ag.vertices[t].firstarc;
ag.vertices[t].firstarc = p;
ag.arcnum++;
}
int visited[MaxVex];//访问标志数组,初始化所有元素值为0
void ALGraph::DFS(VElemType v)//以v为初始点的连通分量的深度优先搜索算法如下
{
ArcNode* p;
int h = LocateVex(v);
cout << v;//访问该顶点
visited[h] = 1;//置访问标记为1
for (p = ag.vertices[h].firstarc; p; p = p->nextarc)
{
if (visited[p->adjvex] == 0)
DFS(ag.vertices[p->adjvex].data);
}
}
void ALGraph::DFSTreverse()//对图作深度优先搜索
{
cout << "深度优先搜索的序列为:";
int i;
for (i = 0; i < ag.vexnum; i++)
{
visited[i] = 0;//访问标志初始化
}
for (i = 0; i < ag.vexnum; i++)
{
if (!visited[i])//对尚未访问的顶点调用DFS
DFS(ag.vertices[i].data);
}
cout << endl;
}
void ALGraph::BFS(VElemType v)//以v为初始点的连通分量的广度优先搜索
{
int h = LocateVex(v);
ArcNode* p;
queue<VElemType> lq;
lq.push(h);
visited[h] = 1;
while (!lq.empty())
{
h = lq.front();
lq.pop();
cout << ag.vertices[h].data;
for (p = ag.vertices[h].firstarc; p; p = p->nextarc)
{
if (!visited[p->adjvex])
{
lq.push(p->adjvex);
visited[p->adjvex] = 1;
}
}
}
}
void ALGraph::BFSTraverse()
{
cout << "广度优先搜索的序列为:";
int i;
for (i = 0; i < ag.vexnum; i++)
{
visited[i] = 0;
}
for (i = 0; i < ag.vexnum; i++)
{
if (!visited[i])
BFS(ag.vertices[i].data);
}
cout << endl;
}
int main()
{
ALGraph p;
p.CreateGraph(8, 9);
p.BFSTraverse();
p.DFSTreverse();
return 0;
}