以邻接多重表为存储结构,实现连通无向图的深度优先遍历和广度优先遍历。

以用户指定的结点为起点,分别输出每种遍历下的结点访问序列和相应生成树的边集。
设图的结点不超过30个,每个结点用一个编号表示(如果一个图有n个结点,则它们
的编号分别为1,2,3,……,n)。通过输入图的全部边输入一个图,每个边为一个数对,可以对变的输入作出某种限制。注意,生成树的边是有向边,端点顺序不能颠倒。
注意:以邻接多重表为存储结构

没有现成的,自己看看套用一下吧。
邻接多重表
/*********************************************************
Title : graph-multiadjacentlist.c
Author :
Time :
Purpose : 图的多重邻接链表表示法
Thread :
Comment :
Usage :
**********************************************************/
#include "stdio.h"
#include "stdlib.h"

/*=====================变量声明--variable declaration=================*/
struct edge /* 图形边线结构声明 */
{
int vertex1; /* 顶点1资料 */
int vertex2; /* 顶点2资料 */
struct edge *edge1; /* 顶点1下一边线 */
struct edge *edge2; /* 顶点2下一边线 */
};
typedef struct edge *nextedge; /* 图形的边线新型态 */

struct node /* 图形顶点结构宣告 */
{
int vertex; /* 顶点资料 */
struct edge *edge; /* 顶点下一边线 */
};
typedef struct node *graph; /* 图形的结构新型态 */
struct node head[6]; /* 图形顶点结构数组 */

/*=====================函数声明--function declaration=================*/
void creategraph(int *node,int num);

/*====================函数实现--function implementation================*/
/* --------------------------------------------------
Function : void creategraph()
Purpose :
Arguments :
Returns :
------------------------------------------------- */
void creategraph(int *node,int num)
{
nextedge newnode; /* 新边线指标 */
nextedge previous; /* 前一边线指标 */
nextedge ptr; /* 目前边线指标 */
int from; /* 边线的起点 */
int to; /* 边线的终点 */
int i;

for ( i = 0; i < num; i++ ) { /* 读取边线的回路 */
from = node[i*2]; /* 边线的起点 */
to = node[i*2+1]; /* 边线的终点 */
/* 建立新边线记忆体 */
newnode = ( nextedge ) malloc(sizeof(struct edge));
newnode->vertex1 = from; /* 建立顶点内容 */
newnode->vertex2 = to; /* 建立顶点内容 */
newnode->edge1 = NULL; /* 设定指标初值 */
newnode->edge2 = NULL; /* 设定指标初值 */
previous = NULL; /* 前一边线指标 */
ptr = head[from].edge; /* 目前边线指标 */

while ( ptr != NULL ) { /* 遍历到最后边线 */
previous = ptr; /* 保留前一边线 */
if ( ptr->vertex1 == from ) /* 决定走的边线 */
ptr = ptr->edge1; /* 下一边线 */
else
ptr = ptr->edge2; /* 下一边线 */
}

if ( previous == NULL )
head[from].edge = newnode; /* 设定顶点边线指标 */
else
if ( previous->vertex1 == from ) /* 决定走的边线 */
previous->edge1 = newnode; /* 连接下一边线 */
else
previous->edge2 = newnode; /* 连接下一边线 */
previous = NULL; /* 前一边线指标 */
ptr = head[to].edge; /* 目前边线指标 */

while ( ptr != NULL ) { /* 遍历到最后边线 */
previous = ptr; /* 保留前一边线 */
if ( ptr->vertex1 == to ) /* 决定走的边线 */
ptr = ptr->edge1; /* 下一边线 */
else
ptr = ptr->edge2; /* 下一边线 */
}

if ( previous == NULL )
head[to].edge = newnode; /* 设定顶点边线指标 */
else
if ( previous->vertex1 == to ) /* 决定走的边线 */
previous->edge1 = newnode; /* 连接下一边线 */
else
previous->edge2 = newnode; /* 连接下一边线 */
}
}

/*======================主函数--main function--建立图形后,将边线列印出========================*/
int main(int argc, char* argv[])
{
nextedge ptr;
int node[6][2] = { {1, 2}, /* 边线数组 */
{1, 3},
{2, 3},
{2, 4},
{3, 5},
{4, 5}, };
int i;

for ( i = 1; i <= 5; i++ ) {
head.vertex = i; /* 设定顶点值 */
head.edge = NULL; /* 清除图形指标 */
}

creategraph(node,6); /* 建立图形 */
printf("图形的多重邻接链表内容:\n");

for ( i = 1; i <= 5; i++ ) {
printf("顶点%d =>",head.vertex); /* 顶点值 */
ptr = head.edge;
/* 边线位置 */
while ( ptr != NULL ) { /* 遍历至链表尾 */
/* 印出边线 */
printf("(%d,%d)",ptr->vertex1,ptr->vertex2);
/* 决定下一边线指标 */
if ( head.vertex == ptr->vertex1 )
ptr = ptr->edge1; /* 下一个边线 */
else
ptr = ptr->edge2; /* 下一个边线 */
}

printf("\n"); /* 换行 */
}

return 1;
}

// 图的遍历 *
// 生成,深度、广度优先遍历 *
//* * * * * * * * * * * * * * * * * * * * * * * *
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX_NUM 20 //图的最大顶点数
#define MAXQSIZE 30 //队列的最大容量
enum BOOL {False,True};
typedef struct ArcNode
{int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
}ArcNode; //弧结点
typedef struct
{ArcNode* AdjList[MAX_VERTEX_NUM]; //指向第一条依附该顶点的弧的指针
int vexnum,arcnum; //图的当前顶点和弧数
int GraphKind; //图的种类,0---无向图,1---有向图
}Graph;
typedef struct //队列结构
{int elem[MAXQSIZE]; //数据域
int front; //队头指针
int rear; //队尾指针
}SqQueue;
BOOL visited[MAX_VERTEX_NUM]; //全局变量——访问标志数组
void CreateGraph(Graph &); //生成图的邻接表
void DFSTraverse(Graph); //深度优先搜索遍历图
void DFS(Graph,int);
void BFSTraverse(Graph); //广度优先搜索遍历图
void Initial(SqQueue &); //初始化一个队列
BOOL QueueEmpty(SqQueue); //判断队列是否空
BOOL EnQueue(SqQueue &,int); //将一个元素入队列
BOOL DeQueue(SqQueue &,int &); //将一个元素出队列
int FirstAdjVex(Graph,int); //求图中某一顶点的第一个邻接顶点
int NextAdjVex(Graph,int,int); //求某一顶点的下一个邻接顶点
void main()
{Graph G; //采用邻接表结构的图
char j='y';
//------------------程序解说----------------------------
printf("本程序将演示生成一个图,并对它进行遍历.
");
printf("首先输入要生成的图的种类.
");
printf("0---无向图, 1--有向图
");
printf("之后输入图的顶点数和弧数。
格式:顶点数,弧数;例如:4,3
");
printf("接着输入各边(弧尾,弧头).
例如:
1,2
1,3
2,4
");
printf("程序会生成一个图,并对它进行深度和广度遍历.
");
printf("深度遍历:1->2->4->3
广度遍历:1->2->3->4
");
//------------------------------------------------------
while(j!='N'&&j!='n')
{printf("请输入要生成的图的种类(0/1):");
scanf("%d",&G.GraphKind); //输入图的种类
printf("请输入顶点数和弧数:");
scanf("%d,%d",&G.vexnum,&G.arcnum); //输入图的顶点数和弧数
CreateGraph(G); //生成邻接表结构的图
DFSTraverse(G); //深度优先搜索遍历图
BFSTraverse(G); //广度优先搜索遍历图
printf("图遍历完毕,继续进行吗?(Y/N)");
scanf(" %c",&j);
}
}

void CreateGraph(Graph &G)
{//构造邻接表结构的图G
int i;
int start,end;
ArcNode *s;
for(i=1;i<=G.vexnum;i++) G.AdjList=NULL; //初始化指针数组
for(i=1;i<=G.arcnum;i++)
{scanf("%d,%d",&start,&end); //输入弧的起点和终点
s=(ArcNode *)malloc(sizeof(ArcNode)); //生成一个弧结点
s->nextarc=G.AdjList[start]; //插入到邻接表中
s->adjvex=end;
G.AdjList[start]=s;
if(G.GraphKind==0) //若是无向图,再插入到终点的弧链中
{s=(ArcNode *)malloc(sizeof(ArcNode));
s->nextarc=G.AdjList[end];
s->adjvex=start;
G.AdjList[end]=s;
}
}
}
void DFSTraverse(Graph G)
{//深度优先遍历图G
int i;
printf("DFSTraverse:");
for(i=1;i<=G.vexnum;i++) visited=False; //访问标志数组初始化
for(i=1;i<=G.vexnum;i++)
if(!visited) DFS(G,i); //对尚未访问的顶点调用DFS
printf("\b\b
");
}
void DFS(Graph G,int i)
{//从第i个顶点出发递归地深度遍历图G
int w;
visited=True; //访问第i个顶点
printf("%d->",i);
for(w=FirstAdjVex(G,i);w;w=NextAdjVex(G,i,w))
if(!visited[w]) DFS(G,w); //对尚未访问的邻接顶点w调用DFS
}

void BFSTraverse(Graph G)
{//按广度优先非递归的遍历图G,使用辅助队列Q和访问标志数组visited
int i,u,w;
SqQueue Q;
printf("BFSTreverse:");
for(i=1;i<= G.vexnum;i++) visited=False; //访问标志数组初始化
Initial(Q); //初始化队列
for(i=1;i<=G.vexnum;i++)
if(!visited)
{visited=True; //访问顶点i
printf("%d->",i);
EnQueue(Q,i); //将序号i入队列
while(!QueueEmpty(Q)) //若队列不空,继续
{DeQueue(Q,u); //将队头元素出队列并置为u
for(w=FirstAdjVex(G,u);w;w=NextAdjVex(G,u,w))
if(!visited[w]) //对u的尚未访问的邻接顶点w进行访问并入队列
{visited[w]=True;
printf("%d->",w);
EnQueue(Q,w);
}
}
}
printf("\b\b
");
}

int FirstAdjVex(Graph G,int v)
{//在图G中寻找第v个顶点的第一个邻接顶点
if(!G.AdjList[v]) return 0;
else return(G.AdjList[v]->adjvex);
}
int NextAdjVex(Graph G,int v,int u)
{//在图G中寻找第v个顶点的相对于u的下一个邻接顶点
ArcNode *p;
p=G.AdjList[v];
while(p->adjvex!=u) p=p->nextarc; //在顶点v的弧链中找到顶点u
if(p->nextarc==NULL) return 0; //若已是最后一个顶点,返回0
else return(p->nextarc->adjvex); //返回下一个邻接顶点的序号
}

void Initial(SqQueue &Q)
{//队列初始化
Q.front=Q.rear=0;
}
BOOL QueueEmpty(SqQueue Q)
{//判断队列是否已空,若空返回True,否则返回False
if(Q.front==Q.rear) return True;
else return False;
}

BOOL EnQueue(SqQueue &Q,int ch)
{//入队列,成功返回True,失败返回False
if((Q.rear+1)%MAXQSIZE==Q.front) return False;
Q.elem[Q.rear]=ch;
Q.rear=(Q.rear+1)%MAXQSIZE;
return True;
}

BOOL DeQueue(SqQueue &Q,int &ch)
{//出队列,成功返回True,并用ch返回该元素值,失败返回False
if(Q.front==Q.rear) return False;
ch=Q.elem[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return True; //成功出队列,返回True
}

转载
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-12-10
#include<iostream>
#define MAX_VERTEX_NUM 30
#define NULL 0
#define OK 1
using namespace std;
bool visit[MAX_VERTEX_NUM];
typedef struct ArcNode //定义边结点
{
int adjvex;
ArcNode *nextarc;
}ArcNode;
typedef struct VNode //定义顶点结点
{
char data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct //定义无向图
{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
typedef struct node //定义结点
{
int data;
node *next;
}*Link;
typedef struct //定义链表
{
Link head,tail;
int len;
}LinkList;
int InitList(LinkList &L)//构造一个带头结点和尾结点的空的线性链表L
{
L.head=new node;
L.head->next=L.tail=new node;
L.tail->next=NULL;
L.len=0;
return 0;
}
void add(LinkList &L,int e)//在线性链表L的结尾添加一个结点
{
Link q=new node;
L.tail->next=q;
L.tail->data=e;
L.tail=q;
L.tail->next=NULL;
L.len++;
}
void Delete(LinkList &L,int &e)//出列,并将出列的元素值用e返回
{
if(L.head->next==L.tail)
{
cout<<"队列为空"<<endl;
e=NULL;
}
else
{
Link p,q;
p=L.head->next;
q=p->next;
L.head->next=q;
e=p->data;
delete p;
L.len--;
}
}
void ArcAdd(ALGraph &G,int m,int n)//在无向图中添加以m,n为顶点的边
{
ArcNode *p,*h,*q;
p=new ArcNode;
p->adjvex=m;
p->nextarc=NULL;
h=q=G.vertices[n].firstarc;
if(q==NULL)
G.vertices[n].firstarc=p;
else
{
if((p->adjvex)>(q->adjvex))
{
p->nextarc=q;
G.vertices[n].firstarc=p;
}
else
{
while(G.vertices[n].firstarc!=NULL&&q->nextarc!=NULL&&(p->adjvex)<(q->adjvex))//使邻接表中边的数据按大到小排列。
{
h=q;
q=q->nextarc;
}
if(q->nextarc==NULL&&(p->adjvex)<(q->adjvex))
{
q->nextarc=p;
}
else
{
p->nextarc=q;
h->nextarc=p;
}
}
}
}
int CreateDG(ALGraph &G) //创建无向图
{
int v,a,m,n;
ArcNode *p,*q,*h;
cout<<"请输入顶点个数和边数."<<endl;
cout<<"请输入顶点数:";
cin>>v;
cout<<"请输入边数:";
cin>>a;
G.vexnum=v;
G.arcnum=a;
for(int i=1;i<=G.vexnum;i++)
{
char t;
cout<<"请输入顶点值:";
cin>>t;
G.vertices[i].data=t;
G.vertices[i].firstarc=NULL;
}
for(int k=1;k<=G.arcnum;k++)
{
cout<<"请输入边的信息."<<endl;
cout<<"请输入第"<<k<<"条边的起点:";
cin>>m;
cout<<"请输入第"<<k<<"条边的终点:";
cin>>n;
if(m<=v&&n<=v&&m>0&&n>0)
{
ArcAdd(G,m,n);
ArcAdd(G,n,m);
}
else
{
cout<<"你的输入有误."<<endl;
return 0;
}
}
return 0;
}
void show(ALGraph G) //在屏幕上输入无向图的邻接表存储形式
{
cout<<"无向图的创建完成,该图的邻接表表示为:"<<endl;
ArcNode *p;
for(int i=1;i<=G.vexnum;i++)
{
if(G.vertices[i].firstarc==NULL)
cout<<i<<G.vertices[i].data<<"-->NULL"<<endl;
else
{
p=G.vertices[i].firstarc;
cout<<i<<G.vertices[i].data<<"-->";
while(p->nextarc!=NULL)
{
cout<<p->adjvex<<"-->";
p=p->nextarc;
}
cout<<p->adjvex<<"-->NULL"<<endl;
}
}
}
void VisitFunc(char a) //对无向图的数据进行访问的函数
{
cout<<a<<" ";
}
int FirstAdjVex(ALGraph G,int v)//返回v的第一个邻接顶点。若顶点在G中没有邻接表顶点,则返回“空”。
{
if(G.vertices[v].firstarc)
return G.vertices[v].firstarc->adjvex;
else
return NULL;
}
int NextAdjVex(ALGraph G,int v,int w) //返回v的(相对于w的)下一个邻接顶点。若w是v的最后一个邻接点,则返回“回”。
{
ArcNode *p;
if(G.vertices[v].firstarc==NULL)
return NULL;
else
{
p=G.vertices[v].firstarc;
while(p->adjvex!=w)
{
p=p->nextarc;
}
if(p->nextarc==NULL)
return NULL;
else
return p->nextarc->adjvex;
}
}
void DFS(ALGraph G,int v)//从第v个顶点出发递归地深度优先遍历图G。
{
visit[v]=true;
VisitFunc(G.vertices[v].data);
for(int w=FirstAdjVex(G,v);w>=1;w=NextAdjVex(G,v,w))
if(!visit[w])
DFS(G,w);
}
void DFSTraverse(ALGraph G)//对图G作深度优先遍历。
{
cout<<"深度优先搜索的结果为:"<<endl;
for(int v=1;v<=G.vexnum;v++)
visit[v]=false;
for(int m=1;m<=G.vexnum;m++)
if(!visit[m])
DFS(G,m);
cout<<endl;
}
void BFSTraverse(ALGraph G)//对图G作广度优先遍历。
{
cout<<"广度优先搜索的结果为:"<<endl;
LinkList Q;
int u;
for(int m=1;m<=G.vexnum;m++)
visit[m]=false;
InitList(Q);//借助辅助队列。
for(int v=1;v<=G.vexnum;v++)
if(!visit[v])
{
visit[v]=true;
VisitFunc(G.vertices[v].data);
add(Q,v);
while(Q.len!=0)
{
Delete(Q,u);
for(int w=FirstAdjVex(G,u);w>=1;w=NextAdjVex(G,u,w))
if(!visit[w])
{
visit[w]=true;
VisitFunc(G.vertices[w].data);
add(Q,w);
}
}
}
cout<<endl;
}
int main()//主函数
{
ALGraph G;
CreateDG(G);
show(G);
DFSTraverse(G);
BFSTraverse(G);
return 0;
}
第2个回答  推荐于2016-09-01
原创一个
C++的

#include <iostream>
#include <memory>
#include <list>
#include <queue>
#define MAXN 30
using namespace std;
int n;//节点数
int m;//边数
int s;//起点
list <int> adj_list[MAXN];//邻接表

int color[MAXN];//记录一个节点的遍历状态,0表示未遍历到,1表示扩展中,2表示已扩展完毕

void Depth_First_Search(int s)
{
list <int>::iterator i;
color[s]=1;
for (i=adj_list[s].begin();i!=adj_list[s].end();i++)
if (color[*i]==0)
{
cout<<s+1<<' '<<(*i)+1<<endl;//输入生成树中的一条边,其中第一个数是亲节点,第二个数是子节点,下同
Depth_First_Search(*i);
}
color[s]=2;
return;
}

void Breadth_First_Search(int s)
{
queue<int,list<int> > Q;
Q.push(s);
color[s]=1;
while (Q.size())
{
int u;
list<int>::iterator i;
u=Q.front();
Q.pop();
for (i=adj_list[u].begin();i!=adj_list[u].end();i++)
if (color[*i]==0)
{
cout<<u+1<<' '<<(*i)+1<<endl;
color[*i]=1;
Q.push(*i);
}
color[u]=2;
}
return;
}

int main()
{
cin>>n>>m;
while (m--)
{
int u,v;
cin>>u>>v;//读入一条无向边(u,v)
u--;
v--;
adj_list[u].push_front(v);
adj_list[v].push_front(u);
}
cin>>s;//读入起点编号
s--;
memset(color,0,sizeof(color));
cout<<"深度优先生成树的边为:"<<endl;
Depth_First_Search(s);//深度优先遍历
memset(color,0,sizeof(color));
cout<<"广度优先生成树的边为:"<<endl;
Breadth_First_Search(s);//宽度优先遍历
return 0;
}

编译运行通过本回答被提问者和网友采纳
相似回答