数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx

上传人:b****4 文档编号:18284558 上传时间:2022-12-15 格式:DOCX 页数:20 大小:136.75KB
下载 相关 举报
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx_第1页
第1页 / 共20页
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx_第2页
第2页 / 共20页
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx_第3页
第3页 / 共20页
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx_第4页
第4页 / 共20页
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx

《数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx(20页珍藏版)》请在冰豆网上搜索。

数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历Word下载.docx

}

for(k=0;

k<

k++){

cout<

输入第"

k+1<

边依附的两个顶点:

v1>

v2;

i=LocateVex(G,v1);

j=LocateVex(G,v2);

G.arcs[i][j]=1;

G.arcs[j][i]=1;

//置<

v1,v2>

的对称弧<

v2,v1>

return0;

}

G,stringu){//确定u在G中序号

inti;

for(i=0;

i++){

if(u==G.vexs[i])

returni;

if(i==G.vexnum){

Erroru!

endl;

exit

(1);

voidShowG(MGraph&

G){

inti,j;

G.vexs[i]<

"

cout<

G.arcs[i][j]<

main(){

MGraphA;

inta;

a=CreateUDN(A);

ShowG(A);

2.分别使用邻接矩阵表示法和邻接表表示法,用深度优先搜索法遍历该图。

#include<

string.h>

malloc.h>

conio.h>

intvisited[30];

#defineMAX_VERTEX_NUM30

#defineOK1

//typedefintVertexType;

typedefintInfoType;

typedefstructArcNode//弧

{

intadjvex;

structArcNode*nextarc;

}ArcNode;

typedefstructVNode//表头

intdata;

ArcNode*firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedefstruct//图

{

AdjListvertices;

intkind;

}ALGraph;

voidCreateDG(ALGraph&

G)

intk,i,v1;

endl<

请输入结点个数:

请输入弧的个数:

for(i=1;

=G.vexnum;

i++)//初使化表头

{

G.vertices[i].data=i;

G.vertices[i].firstarc=NULL;

for(k=1;

k++)//输入边

intv2;

请输入与结点"

相邻的边数:

请输入与第"

个结点相连的结点编号:

v1;

ArcNode*p;

p=(ArcNode*)malloc(sizeof(ArcNode));

if(!

p)exit(-1);

p->

adjvex=v1;

nextarc=NULL;

G.vertices[k].firstarc=p;

for(inti=1;

i++)

{

intm;

m;

ArcNode*q;

q=(ArcNode*)malloc(sizeof(ArcNode));

//动态指针

q)exit(-1);

q->

adjvex=m;

//顶点给P

nextarc=q;

p=q;

//free(q);

//free(p);

}

}

 

voidDFS(ALGraphG,intv)//深度搜索

visited[v]=1;

G.vertices[v].data<

ArcNode*x;

x=(ArcNode*)malloc(sizeof(ArcNode));

x)exit(-1);

x=G.vertices[v].firstarc;

intw;

for(;

x;

x=x->

nextarc)

{w=x->

adjvex;

if(visited[w]==0)

DFS(G,w);

voidDFSB(ALGraphG,intv)//深度搜索的边集

ArcNode*y;

y=(ArcNode*)malloc(sizeof(ArcNode));

y)exit(-1);

y=G.vertices[v].firstarc;

intu=G.vertices[v].data;

for(;

y;

y=y->

{w=y->

u<

--->

w<

DFSB(G,w);

typedefstructQNode

QNode*next;

}QNode,*QueuePtr;

typedefstruct

QueuePtrfront;

QueuePtrrear;

}LinkQueue;

voidInitQueue(LinkQueue&

Q)//建立一个空队列

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

Q.front)exit(-1);

Q.front->

next=NULL;

voidEnQueue(LinkQueue&

Q,inte)//进队

QNode*p;

p=(QNode*)malloc(sizeof(QNode));

data=e;

Q.rear->

next=p;

Q.rear=p;

intDeQueue(LinkQueue&

Q,int&

e)//出队

if(Q.front==Q.rear)

return-1;

p=Q.front->

next;

e=p->

data;

next=p->

if(Q.rear==p)

Q.rear=Q.front;

free(p);

returne;

intQueueEmpty(LinkQueueQ)//判断队列是否为空

return1;

voidBFS(ALGraphG,intv)//广度搜索

intu;

LinkQueueQ;

InitQueue(Q);

if(visited[v]==0)

EnQueue(Q,v);

while(QueueEmpty(Q)!

=1)

DeQueue(Q,u);

ArcNode*z;

z=(ArcNode*)malloc(sizeof(ArcNode));

z)exit(-1);

z=G.vertices[u].firstarc;

/*

for(intw=z->

w>

=0;

w=z->

nextarc->

adjvex)

visited[w]=1;

EnQueue(Q,w);

}*/

z;

z=z->

{w=z->

voidBFSB(ALGraphG,intv)//广度搜索的边集

ArcNode*r;

r=(ArcNode*)malloc(sizeof(ArcNode));

r)exit(-1);

r=G.vertices[u].firstarc;

r!

=NULL;

r=r->

{w=r->

intmain()

ALGraphG;

CreateDG(G);

intx;

请输入结点数:

邻接表为:

for(intj=1;

=x;

j++)

G.vertices[j].data<

p=G.vertices[j].firstarc;

while(p)

p->

adjvex<

p=p->

nextarc;

请输入第一个要访问的结点序号:

intn;

n;

for(i=0;

30;

visited[i]=0;

广度搜索:

BFS(G,n);

边集:

BFSB(G,n);

深度搜索:

DFS(G,n);

DFSB(G,n);

//system("

pause"

);

3.对学生选课工程图进行拓扑排序.

stdio.h>

stdlib.h>

#defineMAX_VEXTEX_NUM20

#defineM20

#defineSTACK_INIT_SIZE100

#defineSTACKINCREMENT10

#defineOK1

#defineERROR0

typedefintElemType;

typedefstructArcNode

intadjvex;

structArcNode*nextarc;

typedefstructVNode

intdata;

ArcNode*firstarc;

}VNode,AdjList[MAX_VEXTEX_NUM];

AdjListvertices;

intvexnum,arcnum;

typedefstruct//构件栈

ElemType*base;

ElemType*top;

intstacksize;

}SqStack;

voidInitStack(SqStack*);

//函数声明

intPop(SqStack*,ElemType*);

voidPush(SqStack*,ElemType);

intStackEmpty(SqStack*);

voidCreatGraph(ALGraph*);

voidFindInDegree(ALGraph,int*);

voidTopologicalSort(ALGraph);

voidInitStack(SqStack*S)//初始化栈

S->

base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));

if(!

base)

printf("

memoryallocationfailed,goodbye"

exit

(1);

top=S->

base;

stacksize=STACK_INIT_SIZE;

intPop(SqStack*S,ElemType*e)//出栈操作

if(S->

top==S->

{returnERROR;

*e=*--S->

top;

//printf("

%d\n"

e);

//returne;

return0;

voidPush(SqStack*S,ElemTypee)//进栈操作

{if(S->

top-S->

base>

=S->

stacksize)

base=(ElemType*)realloc(S->

base,(S->

stacksize+STACKINCREMENT)*sizeof(ElemType));

top=S->

base+S->

stacksize;

stacksize+=STACKINCREMENT;

}*S->

top++=e;

intStackEmpty(SqStack*S)//判断栈是否为空

returnOK;

else

returnERROR;

voidCreatGraph(ALGraph*G)//构件图

{intm,n,i;

ArcNode*p;

请输入顶点数和边数:

scanf("

%d%d"

&

G->

vexnum,&

arcnum);

for(i=1;

i<

=G->

vexnum;

i++)

{G->

vertices[i].data=i;

vertices[i].firstarc=NULL;

arcnum;

i++)//输入存在边的点集合

\n请输入存在边的两个顶点的序号:

n,&

m);

while(n<

0||n>

G->

vexnum||m<

0||m>

vexnum)

{printf("

输入的顶点序号不正确请重新输入:

p=(ArcNode*)malloc(sizeof(ArcNode));

if(p==NULL)

memoryallocationfailed,goodbey"

adjvex=m;

nextarc=G->

vertices[n].firstarc;

vertices[n].firstarc=p;

建立的邻接表为:

\n"

//输出建立好的邻接表

for(i=1;

%d"

G->

vertices[i].data);

for(p=G->

vertices[i].firstarc;

p;

p=p->

%3d"

p->

adjvex);

}}

voidFindInDegree(ALGraphG,intindegree[])//求入度操作

inti;

=G.vexnum;

indegree[i]=0;

{while(G.vertices[i].firstarc)

{indegree[G.vertices[i].firstarc->

adjvex]++;

G.vertices[i].firstarc=G.vertices[i].firstarc->

voidTopologicalSort(ALGraphG)//进行拓扑排序

intindegree[M];

inti,k,n;

intcount=0;

SqStackS;

FindInDegree(G,indegree);

InitStack(&

S);

第%d个点的入度为%d\n"

i,indegree[i]);

for(i=1;

if(!

indegree[i])

Push(&

S,i);

进行拓扑排序输出顺序为:

//输出结果

while(!

StackEmpty(&

S))

Pop(&

S,&

n);

%4d"

G.vertices[n].data);

count++;

for(p=G.vertices[n].firstarc;

p!

=NULL;

k=p->

(--indegree[k]))

S,k);

}printf("

if(count<

G.vexnum)

出现错误\n"

排序成功\n"

intmain(void)//主函数

ALGraphG;

CreatGraph(&

G);

TopologicalSort(G);

system("

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1