31 图的邻接表:深度优先遍历
作者: 冯向阳时间限制: 1S章节: DS:图
截止日期: 2022-06-30 23:55:00
问题描述 :
目的:使用C++模板设计并逐步完善图的邻接表抽象数据类型(ADT)。
内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接表ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)
(2)设计并实现一个算法,应用递归的程序设计方法,对一个已存在的图进行深度优先遍历(DFS),并输出遍历的顶点线性序列。遍历的起点通过输入指定。注意:遍历时,仅从该点出发遍历整个图,如果图不连通,则只遍历一个子图。图的存储结构采用邻接表。将其加入到ADT中。
注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
参考函数原型:
//DFS遍历(外壳部分,公有成员函数)
template
void adjlist_graph
//DFS遍历(递归部分,私有成员函数)
template
bool adjlist_graph
图的邻接表模板类原型参考如下:
/* 边表的结点定义 */
template
struct edgeNode
{
int data;
TypeOfEdge weight;
edgeNode
edgeNode(const int &d, edgeNode
//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
{
next = ptr;
data = d;
}
edgeNode(const int &d, const TypeOfEdge &w, edgeNode
//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
{
next = ptr;
data = d;
weight = w;
}
int getData(){ return data;} //取得结点的序号(顶点集)
TypeOfEdge getWeight(){ return weight;} //取得边集中对应边的权值
void SetLink( edgeNode
void SetData( int value ){ data = value; } //修改结点的序号(顶点集)
void SetWeight(TypeOfEdge value ){ weight = value; } //修改边集中对应边的权值
};
//图的邻接表类
template
struct verNode
{
TypeOfVer ver;
edgeNode
verNode(edgeNode
TypeOfVer getVer(){ return ver;} //取得结点值(顶点集)
edgeNode
void setVer(TypeOfVer value){ ver = value;} //设置结点值(顶点集)
void setHead(edgeNode
};
template
class adjlist_graph{
private:
int Vers; //顶点数
int Edges; //边数
verNode
string GraphKind; //图的种类标志
bool Delete_Edge( int u, int v );
bool DFS(int u, int &num, int visited[]); //DFS遍历(递归部分)
public:
adjlist_graph( const string &kd, int vSize, const TypeOfVer d[]); //构造函数构造一个只有结点没有边的图。
adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e); 构造函数构造一个无权图。5个参数的含义:图的类型、结点数、边数、结点集和边集
adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e, const TypeOfEdge w[]); //构造函数构造一个有权图。
bool GraphisEmpty() { return Vers == 0; } //判断图空否
string GetGraphKind(){ return GraphKind; }
bool GetVer(int u, TypeOfVer &data); //取得G中指定顶点的值
int GetFirstAdjVex(int u, int &v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1
int GetNextAdjVex(int u, int v, int &w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回false
bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值
bool InsertVer(const TypeOfVer &data); //往G中添加一个顶点
int LocateVer(TypeOfVer data); //返回G中指定顶点的位置
bool ExistEdge(int u, int v);
bool PrintVer(); //输出顶点集
bool PrintAdjList(); //输出邻接矩阵
int GetVerNum(){ return Vers;} //取得当前顶点数
int GetEdgeNum(){ return Edges;} //取得当前边数
bool Insert_Edge(int u, int v); //无权图插入一条边
bool Insert_Edge(int u, int v, TypeOfEdge w); //有权图插入一条边
bool DeleteVer(const TypeOfVer &data); //往G中删除一个顶点
bool DeleteEdge( int u, int v ); //删除边 (外壳:有向(删除1条边), 无向(删除2条边))
void DFS_Traverse(int u); //DFS遍历(外壳部分)
void BFS_Traverse(int u); //BFS遍历
~adjlist_graph(); //析构函数
};
输入说明 :
建图的输入数据格式参见建图的算法说明。(以无权图为例)
第一行:图的类型
第二行:结点数
第三行:结点集
第四行:边数
第五行:边集
第六行:起始顶点的位序
输出说明 :
第一行:顶点集
第二行:邻接表
空行
第三行:DFS遍历序列(结点之间用->分隔)
输入范例 :
UDG
8
V1 V2 V3 V4 V5 V6 V7 V8
8
0 1
0 2
1 3
1 4
2 5
2 6
3 7
4 7
3
---------------
V1 V2 V3 V4 V5 V6 V7 V8
V1->2->1->nullptr
V2->4->3->0->nullptr
V3->6->5->0->nullptr
V4->7->1->nullptr
V5->7->1->nullptr
V6->2->nullptr
V7->2->nullptr
V8->4->3->nullptr
V4->V8->V5->V2->V1->V3->V7->V6
核心代码DFS函数---我自己想出来的!
void DFS(graph& g, int am)
{
if (k == 1)
{
cout << "->";
}
k = 1;
cout << g.m[am].letter ;
arr[am] = 1;
bian* p = g.m[am].next;
while (p)
{
if (arr[p->num] == 0)
{
DFS(g, p->num);
}
p = p->next;
}
}
下面是AC全代码
#include
#include
using namespace std;
int k = 0;
int m = 0;
int temp = 0;
int arr[100] = { 0 };
struct bian
{
int num;
int weight;
bian* next = NULL;
};
struct point
{
string letter;
bian* next = NULL;
};
struct graph
{
point m[100];
int number_of_point;
int number_of_bian;
};
void DFS(graph& g, int am)
{
if (k == 1)
{
cout << "->";
}
k = 1;
cout << g.m[am].letter ;
arr[am] = 1;
bian* p = g.m[am].next;
while (p)
{
if (arr[p->num] == 0)
{
DFS(g, p->num);
}
p = p->next;
}
}
void creat_no_direction(graph& g)
{
int e;
int f;
cin >> e;
g.number_of_point = e;
for (int i = 0; i < e; i++)
{
cin >> g.m[i].letter;
}
//上面是输入的顶点的字母
cin >> f;
g.number_of_bian = f;
for (int i = 0; i < f; i++)
{
int v1;
int v2;
cin >> v1 >> v2;
bian* p1 = new bian;
p1->num = v2;
p1->next = g.m[v1].next;
g.m[v1].next = p1;
bian* p2 = new bian;
p2->num = v1;
p2->next = g.m[v2].next;
g.m[v2].next = p2;
}
}
void creat_direction(graph& g)
{
int e;
int f;
cin >> e;
g.number_of_point = e;
for (int i = 0; i < e; i++)
{
cin >> g.m[i].letter;
}
//上面是输入的顶点的字母
cin >> f;
g.number_of_bian = f;
for (int i = 0; i < f; i++)
{
int v1;
int v2;
cin >> v1 >> v2;
bian* p1 = new bian;
p1->num = v2;
p1->next = g.m[v1].next;
g.m[v1].next = p1;
}
}
void display(graph& g)
{
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
cout << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
}
int main()
{
string kind;
cin >> kind;
if (kind == "DN")//
{
graph g;
creat_direction(g);
int am;
cin >> am;
display(g);
cout << endl;
DFS(g, am);
}
else if (kind == "DG")
{
graph g;
creat_direction(g);
int am;
cin >> am;
display(g);
cout << endl;
DFS(g, am);
}
else if (kind == "UDN")//
{
graph g;
creat_no_direction(g);
int am;
cin >> am;
display(g);
cout << endl;
DFS(g, am);
}
else if (kind == "UDG")
{
graph g;
creat_no_direction(g);
int am;
cin >> am;
display(g);
cout << endl;
DFS(g, am);
}
return 0;
}