8648 图的深度遍历

Description 实现图的邻接表存储结构及一些基本操作函数。在此基础上实现图的深度遍历算法并加以测试。本题只给出部分代码,请补全内容。
#include"string.h"
#include"malloc.h" /* malloc()等 /
#include"stdio.h" /
EOF(=^Z或F6),NULL /
#include"stdlib.h" /
exit() /
typedef int InfoType; /
顶点权值类型 /
#define MAX_NAME 3 /
顶点字符串的最大长度+1 /
typedef char VertexType[MAX_NAME]; /
字符串类型 */
/*图的邻接表存储表示 /
#define MAX_VERTEX_NUM 20
typedef enum{DG,DN,AG,AN}GraphKind; /
{有向图,有向网,无向图,无向网} /
typedef struct ArcNode
{
int adjvex; /
该弧所指向的顶点的位置 */
struct ArcNode nextarc; / 指向下一条弧的指针 */
InfoType info; / 网的权值指针) /
}ArcNode; /
表结点 */

typedef struct
{
VertexType data; /* 顶点信息 */
ArcNode firstarc; / 第一个表结点的地址,指向第一条依附该顶点的弧的指针 /
}VNode,AdjList[MAX_VERTEX_NUM]; /
头结点 */

typedef struct
{
AdjList vertices;
int vexnum,arcnum; /* 图的当前顶点数和弧数 /
int kind; /
图的种类标志 */
}ALGraph;

int LocateVex(ALGraph G,VertexType u)
{ /* 初始条件: 图G存在,u和G中顶点有相同特征 /
/
操作结果: 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1 */
int i;
for(i=0;i<G.vexnum;++i)
if(strcmp(u,G.vertices[i].data)==0)
return i;
return -1;
}

void CreateGraph(ALGraph G)
{ /
采用邻接表存储结构,构造没有相关信息的图G(用一个函数构造4种图) /
int i,j,k;
int w; /
权值 */
VertexType va,vb;
ArcNode *p;
//printf(“Enter the type of map:(0~3): “);
scanf(”%d”,&(*G).kind);
//printf(“Enter Vertex number,Arc number: “);
scanf(”%d%d”,&(*G).vexnum,&(*G).arcnum);
//printf(“Enter %d Vertex :\n”,(*G).vexnum);
for(i=0;i<(G).vexnum;++i) / 构造顶点向量 */
{
scanf("%s",(*G).vertices[i].data);
(*G).vertices[i].firstarc=NULL;
}
//if((G).kind1||(*G).kind3) //
// printf(“Enter order every arc weight,head and tail (Takes the gap by the blank space ):\n”);
//else /
图 */
// printf(“Enter order every arc head and tail (Takes the gap by the blank space ):\n”);
for(k=0;k<(G).arcnum;++k) / 构造表结点链表 */
{
if((G).kind1||(*G).kind3) //
scanf("%d%s%s",&w,va,vb);
else /
图 */
scanf("%s%s",va,vb);
i=LocateVex(G,va); / 弧尾 */
j=LocateVex(G,vb); / 弧头 /
p=(ArcNode
)malloc(sizeof(ArcNode));
p->adjvex=j;
if((G).kind1||(*G).kind3) / 网 */
{
p->info=(int *)malloc(sizeof(int));
(p->info)=w;
}
else
p->info=NULL; /
图 */
p->nextarc=(G).vertices[i].firstarc; / 插在表头 */
(*G).vertices[i].firstarc=p;
if((G).kind>=2) / 无向图或网,产生第二个表结点 /
{
p=(ArcNode
)malloc(sizeof(ArcNode));
p->adjvex=i;
if((G).kind==3) / 无向网 /
{
p->info=(int
)malloc(sizeof(int));
(p->info)=w;
}
else
p->info=NULL; /
无向图 */
p->nextarc=(G).vertices[j].firstarc; / 插在表头 */
(*G).vertices[j].firstarc=p;
}
}
}

VertexType* GetVex(ALGraph G,int v)
{ /* 初始条件: 图G存在,v是G中某个顶点的序号。操作结果: 返回v的值 */
if(v>=G.vexnum||v<0)
exit(0);
return &G.vertices[v].data;
}

int FirstAdjVex(ALGraph G,VertexType v)
{ /* 初始条件: 图G存在,v是G中某个顶点 /
/
操作结果: 返回v的第一个邻接顶点的序号。若顶点在G中没有邻接顶点,则返回-1 */
ArcNode p;
int v1;
v1=LocateVex(G,v); /
v1为顶点v在图G中的序号 */
p=G.vertices[v1].firstarc;
if§
return p->adjvex;
else
return -1;
}

int NextAdjVex(ALGraph G,VertexType v,VertexType w)
{ /* 初始条件: 图G存在,v是G中某个顶点,w是v的邻接顶点 /
/
操作结果: 返回v的(相对于w的)下一个邻接顶点的序号。 /
/
若w是v的最后一个邻接点,则返回-1 */
ArcNode p;
int v1,w1;
v1=LocateVex(G,v); /
v1为顶点v在图G中的序号 /
w1=LocateVex(G,w); /
w1为顶点w在图G中的序号 /
p=G.vertices[v1].firstarc;
while(p&&p->adjvex!=w1) /
指针p不空且所指表结点不是w /
p=p->nextarc;
if(!p||!p->nextarc) /
没找到w或w是最后一个邻接点 /
return -1;
else /
p->adjvex==w /
return p->nextarc->adjvex; /
返回v的(相对于w的)下一个邻接顶点的序号 */
}

/深度遍历/
int visited[MAX_VERTEX_NUM]; /* 访问标志数组(全局量),未访问标记0,访问标记1 /
void(VisitFunc)(char v); /
函数变量(全局量) /
void DFS(ALGraph G,int v)
{ /
从第v个顶点出发递归地深度优先遍历图G。算法7.5 /
/
设置访问标志为TRUE(已访问) /
/
访问第v个顶点 /
/
对v的尚未访问的邻接点w递归调用DFS */

}
void DFSTraverse(ALGraph G)
{ /* 对图G作深度优先遍历。算法7.4 /
/
使用全局变量VisitFunc,使DFS不必设函数指针参数 /
/
访问标志数组初始化 /
/
对尚未访问的顶点调用DFS */
printf("\n");
}

void print(char *i)
{
printf("%s ",i);
}

int main()
{
ALGraph g;
CreateGraph(&g);
DFSTraverse(g);
return 1;
}

输入格式
第一行:输入0到3之间整数(有向图:0,有向网:1,无向图:2,无向网:3);
第二行:输入顶点数和边数;
第三行:输入各个顶点的值(字符型,长度〈3);(遍历从输入的第一个顶点开始)
第四行:输入每条弧(边)弧尾和弧头(以空格作为间隔),如果是网还要输入权值;

输出格式
输出对图深度遍历的结果。

输入样例
0
3 3
a b c
a b
b c
c b

输出样例
a b c

提示
注意题目的邻接表采用的是头插法,也就是后出现的边节点先被访问。

此处另外给出一个样例:

标准输入数据:
2
4 4
a b c d
a c
b d
a d
d c

标准输出答案:

1|a d c b

图的存储方式主要分为两种:邻接矩阵和邻接表

一、邻接矩阵存储顶点关系

代码:

#include <cstdio>
#include <iostream>

using namespace std;

int type, n, m;//图类型,顶点数,边数
char point[100000] ={0};//图顶点数组
int v[100000] = {0};//标记数组
int Map[100][100] = {0};//邻接矩阵
int findIndex(char ch)
{
    int i;
    for (i = 1; i <= n; i++)
        if (ch == point[i])
            return i;
    return -1;
}

int dfs(int cur)
{
    int i, j;
    //本题输出样例是从右往左遍历邻接矩阵的一行进行搜索连接的顶点
    for(i = n; i >= 1; i--)//遍历邻接矩阵的第cur行,查看下标为cur的顶点是否跟别的顶点相连
    {
        if (Map[cur][i] != 0 && v[i] == 0)//如果下标为cur的顶点跟下标为i的顶点相连  并且 下标为i的顶点没有访问过
        {
            v[i] = 1;//标记下标为i的顶点访问过
            cout<<point[i]<<' ';//输出下标为i的顶点,即输出跟cur相连的顶点
            dfs(i);//深搜跟下标i的顶点相连的顶点
        }
    }
}

int main()
{
    cin>>type;//输入图的类型
    cin>>n>>m;//输入顶点数和边数
    getchar();
    int i, j, w;
    char c1, c2;


    for (i = 1; i <= n; i++)
    {
        cin>>point[i];//输入顶点的值
        getchar();
    }

    //输入顶点关系,即边信息
    for (i = 1; i <= m; i++)
    {
        if (type == 1 || type == 3)//如果图的类型为有向网或者无向网
            cin>>c1>>c2>>w;//输入两个相连的顶点和边权值
        else//如果图的类型为无向图或者无向网
            cin>>c1>>c2;//输入相连的两个顶点的值
        getchar();
        //在存储图顶点的数组中查找相连接的两个顶点的下标
        int index_1 = findIndex(c1), index_2 = findIndex(c2);

        //构建邻接矩阵
        if(type == 0)
            Map[index_1][index_2] = 1;//构建 有向图 邻接矩阵
        else if (type == 1)
            Map[index_1][index_2] = w;//构建 有向网 邻接矩阵
        else if (type == 2)
            Map[index_2][index_1] = Map[index_1][index_2] = 1;//构建 无向图 邻接矩阵
        else if (type == 3)
            Map[index_2][index_1] = Map[index_1][index_2] = w;//构建 无向网 邻接矩阵
    }

    for (i = 1; i <= n; i++)
    {
        if (v[i] == 0)//如果有仍未访问过的顶点,则从该顶点开始深搜访问
        {
            cout<<point[i]<<' ';
            v[i] = 1;//标记第i个顶点已经访问过
            dfs(i);//从第i个顶点开始深搜,如果是连通图,那么深搜完毕一定所有顶点都访问过
        }
    }
    return 0;
}

二、邻接表存储顶点关系:

代码:

#include <cstdio>
#include <iostream>
#include <malloc.h>
using namespace std;
int n, m;

struct node
{
    int index;//下标
    int w;//权值
    struct node* next;//顶点指针
}Map[1000];//邻接表
int v[1000];//标记数组
char point[1000];//图的顶点数组
typedef struct node Node, * pNode;

//查找值为ch的顶点在图顶点数组中的下标
int findIndex(char ch)
{
    //查找ch在图顶点数组中所在下标
    int i;
    for (i = 1; i <= n; i++)
        if (ch == point[i])
            return i;
    return -1;
}

//对邻接表进行深搜
void dfs(int cur)
{
    pNode p = Map[cur].next;//初始化p指向与下标为cur的顶点相连的第一个顶点
    while (p)//遍历与下标为cur的顶点相连的顶点
    {
        if (v[p->index] == 0)//如果与下标为cur相连的顶点没有访问过
        {
            cout << point[p->index] << ' ';//输出这个顶点的值
            v[p->index] = 1;//标记这个顶点访问过
            dfs(p->index);//对这个顶点进行深搜
        }
        p = p->next;
    }
}

int main()
{
    int type;
    //输入图的类型、顶点数和边数
    cin >> type >> n >> m; getchar();
    int i;
    //输入顶点的值,同时对邻接表的顶点数组进行初始化
    for (i = 1; i <= n; i++)
    {
        cin >> point[i];//输入顶点的值
        Map[i].index = i;//邻接表顶点数组的下标 对应 存储图顶点值的数组下标
        Map[i].next = NULL;//初始化下标为i的顶点的 边表 为空
        Map[i].w = 0;//初始化下标为i的顶点的权值为0,顶点的权值没有用处,网的权值在邻接表中是存储在边表的各个顶点中
        getchar();
    }
    for (i = 1; i <= m; i++)
    {
        char c1, c2;
        int w;
        if (type == 1 || type == 3)//如果图的类型是有向网或者无向网
        {
            cin >> c1 >> c2 >> w;
            getchar();
        }
        else//如果图的类型是有向图或者无向图
        {
            cin >> c1 >> c2;
            getchar();
        }
        //查找输入的两个顶点在图顶点数组中的下标
        int index_1 = findIndex(c1), index_2 = findIndex(c2);
        //有向图:0,有向网:1,无向图:2,无向网:3
        if (type == 0)//如果为有向图
        {
            pNode newNode = (pNode)malloc(sizeof(Node));//声明一个顶点
            newNode->w = 1;//有向图的权值没有用处,默认为1
            newNode->index = index_2;//与index_1相连的顶点的下标为index_2
            //头插法插入顶点关系,将顶点插入index_1顶点的边表中
            newNode->next = Map[index_1].next;
            Map[index_1].next = newNode;
        }
        else if (type == 1)//如果为有向网
        {
            pNode newNode = (pNode)malloc(sizeof(Node));
            newNode->w = w;
            newNode->index = index_2;
            //头插法插入顶点关系
            newNode->next = Map[index_1].next;
            Map[index_1].next = newNode;
        }
        else if (type == 2)//如果是无向图,则需要声明两个顶点,做两次插入
        {
            pNode newNode = (pNode)malloc(sizeof(Node));
            newNode->w = 1;
            newNode->index = index_2;
            newNode->next = Map[index_1].next;
            Map[index_1].next = newNode;

            pNode newNode1 = (pNode)malloc(sizeof(Node));
            newNode->w = 1;
            newNode1->index = index_1;
            newNode1->next = Map[index_2].next;
            Map[index_2].next = newNode1;
        }
        else if (type == 3)//如果是无向网,也需要声明两个顶点,做两次插入
        {
            pNode newNode = (pNode)malloc(sizeof(Node));
            newNode->w = w;
            newNode->index = index_2;
            newNode->next = Map[index_1].next;
            Map[index_1].next = newNode;

            pNode newNode1 = (pNode)malloc(sizeof(Node));
            newNode->w = w;
            newNode1->index = index_1;
            newNode->next = Map[index_2].next;
            Map[index_2].next = newNode1;
        }
    }

    //遍历图顶点数,深搜仍为访问的顶点
    for (i = 1; i <= n; i++)
    {
        if (v[i] == 0)//如果下标为i的顶点还没访问过
        {
            cout << point[i] << ' ';//输入当前访问的顶点的值
            v[i] = 1;//标记下标为i的顶点已访问过
            dfs(i);//对下标为i的顶点进行深搜
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值