当前位置:首页 » 编程语言 » 广度优先遍历动画演示c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

广度优先遍历动画演示c语言

发布时间: 2023-03-11 21:08:26

Ⅰ 求大神帮写一个c语言图的深度优先遍历,和广度优先遍历

/*深度优先*/
#include<stdio.h>
#include<stdlib.h>
struct node/*图的顶点结构*/
{
int vertex;
int flag;
struct node *nextnode;
};
typedef struct node *graph;
struct node vertex_node[10];
void creat_graph(int *node,int n)
{
graph newnode,p;/*定义一个新结点及指针*/
int start,end,i;
for(i=0;i<n;i++)
{
start=node[i*2];/*边的起点*/
end=node[i*2+1];/*边的终点*/
newnode=(graph)malloc(sizeof(struct node));
newnode->vertex=end;/*新结点的内容为边终点处顶点的内容*/
newnode->nextnode=NULL;
p=&(vertex_node[start]);/*设置指针位置*/
while(p->nextnode!=NULL)
p=p->nextnode;/*寻找链尾*/
p->nextnode=newnode;/*在链尾处插入新结点*/
}
}
void dfs(int k)
{
graph p;
vertex_node[k].flag=1;/*将标志位置1,证明该结点已访问过*/
printf("vertex[%d]",k);
p=vertex_node[k].nextnode;/*指针指向下个结点*/
while(p!=NULL)
{
if(vertex_node[p->vertex].flag==0)/*判断该结点的标志位是否为0*/
dfs(p->vertex);/*继续遍历下个结点*/
p=p->nextnode;/*若已遍历过p指向下一个结点*/
}
}
main()
{
graph p;
int node[100],i,sn,vn;
printf("please input the number of sides:\n");
scanf("%d",&sn);/*输入无向图的边数*/
printf("please input the number of vertexes\n");
scanf("%d",&vn);
printf("please input the vertexes which connected by the sides:\n");
for(i=0;i<4*sn;i++)
scanf("%d",&node[i]);/*输入每个边所连接的两个顶点,起始及结束位置不同,每边输两次*/
for(i=1;i<=vn;i++)
{
vertex_node[i].vertex=i;/*将每个顶点的信息存入数组中*/
vertex_node[i].nextnode=NULL;
}
creat_graph(node,2*sn);/*调用函数创建邻接表*/
printf("the result is:\n");
for(i=1;i<=vn;i++)/*将邻接表内容输出*/
{
printf("vertex%d:",vertex_node[i].vertex);/*输出顶点内容*/
p=vertex_node[i].nextnode;
while(p!=NULL)
{
printf("->%3d",p->vertex);/*输出邻接顶点的内容*/
p=p->nextnode;/*指针指向下个邻接顶点*/
}
printf("\n");
}
printf("the result of depth-first search is:\n");
dfs(1);/*调用函数进行深度优先遍历*/
printf("\n");
}
/***************************广度优先*******************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph;
struct node vertex_node[10];
#define MAXQUEUE 100
int queue[MAXQUEUE];
int front = - 1;
int rear = - 1;
int visited[10];
void creat_graph(int *node, int n)
{
graph newnode, p; /*定义一个新结点及指针*/
int start, end, i;
for (i = 0; i < n; i++)
{
start = node[i *2]; /*边的起点*/
end = node[i *2+1]; /*边的终点*/
newnode = (graph)malloc(sizeof(struct node));
newnode->vertex = end; /*新结点的内容为边终点处顶点的内容*/
newnode->nextnode = NULL;
p = &(vertex_node[start]); /*设置指针位置*/
while (p->nextnode != NULL)
p = p->nextnode;
/*寻找链尾*/
p->nextnode = newnode; /*在链尾处插入新结点*/
}
}

int enqueue(int value) /*元素入队列*/
{
if (rear >= MAXQUEUE)
return - 1;
rear++; /*移动队尾指针*/
queue[rear] = value;
}

int dequeue() /*元素出队列*/
{
if (front == rear)
return - 1;
front++; /*移动队头指针*/
return queue[front];
}

void bfs(int k) /*广度优先搜索*/
{
graph p;
enqueue(k); /*元素入队*/
visited[k] = 1;
printf("vertex[%d]", k);
while (front != rear)
/*判断是否对空*/
{
k = dequeue(); /*元素出队*/
p = vertex_node[k].nextnode;
while (p != NULL)
{
if (visited[p->vertex] == 0)
/*判断其是否被访问过*/
{
enqueue(p->vertex);
visited[p->vertex] = 1; /*访问过的元素置1*/
printf("vertex[%d]", p->vertex);
}
p = p->nextnode; /*访问下一个元素*/
}
}
}

main()
{
graph p;
int node[100], i, sn, vn;
printf("please input the number of sides:\n");
scanf("%d", &sn); /*输入无向图的边数*/
printf("please input the number of vertexes\n");
scanf("%d", &vn);
printf("please input the vertexes which connected by the sides:\n");
for (i = 0; i < 4 *sn; i++)
scanf("%d", &node[i]);
/*输入每个边所连接的两个顶点,起始及结束位置不同,每边输两次*/
for (i = 1; i <= vn; i++)
{
vertex_node[i].vertex = i; /*将每个顶点的信息存入数组中*/
vertex_node[i].nextnode = NULL;
}
creat_graph(node, 2 *sn); /*调用函数创建邻接表*/
printf("the result is:\n");
for (i = 1; i <= vn; i++)
/*将邻接表内容输出*/
{
printf("vertex%d:", vertex_node[i].vertex); /*输出顶点内容*/
p = vertex_node[i].nextnode;
while (p != NULL)
{
printf("->%3d", p->vertex); /*输出邻接顶点的内容*/
p = p->nextnode; /*指针指向下个邻接顶点*/
}
printf("\n");
}
printf("the result of breadth-first search is:\n");
bfs(1); /*调用函数进行深度优先遍历*/
printf("\n");
}

Ⅱ 广度优先搜索C语言算法

它没有固定的写法, 但是大框都差不多, 一定要使用队列, 因为队列的存在可以维护程序按照广度优先的方式进行搜索。即层次遍历

可以给你一份我作过的一个题的代码,大体上就是这个样子

/****************************************************\
*
* Title : Rescue
* From : HDU 1242
* AC Time : 2012.01.12
* Type : 广度优先搜索求最短步数
* Method :从目标结点向回搜索,初始结点有多个
*
\****************************************************/

#include <stdio.h>
#include <string.h>
#define DATASIZE 201
#define QUEUESIZE 65536

typedef struct
{
int x,y;
}CPOINT;

int bfs(char map[][DATASIZE], int n, int m, CPOINT cpa);
int direction[][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int main(void)
{
int m,n,i,j,res;
CPOINT cpa;
char map[DATASIZE][DATASIZE];
freopen("c:\\in.data","r",stdin);
while(scanf("%d%d%*c",&n,&m) != EOF) {
for(i = 0 ; i < n ; i++) {
gets(map[i]);
for(j = 0 ; j < m ; j++) {
if(map[i][j] == 'a') {
cpa.x = i;
cpa.y = j;
}
}
}
res = bfs(map, n, m, cpa);
if(res) {
printf("%d\n",res);
} else {
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
return 0;
}

int bfs(char map[][DATASIZE], int n, int m, CPOINT cpa)
{
CPOINT q[QUEUESIZE],u,np;
int vis[DATASIZE][DATASIZE],step[DATASIZE][DATASIZE],i,front,rear,res;
memset(q, 0, sizeof(q));
memset(vis, 0, sizeof(vis));
memset(step, 0, sizeof(step));
front = rear = res = 0;
q[rear++] = cpa;
vis[cpa.x][cpa.y] = 1;
step[cpa.x][cpa.y] = 0;
while(front <= rear) {
u = q[front++];
if(map[u.x][u.y] == 'r') {
res = step[u.x][u.y];
break;
}
for(i = 0 ; i < 4; i++) {
np.x = u.x + direction[i][0];
np.y = u.y + direction[i][1];
if(np.x >= 0 && np.x < n && np.y >= 0 && np.y < m && !vis[np.x][np.y] && map[np.x][np.y] != '#' ) {
vis[np.x][np.y] = 1;
q[rear++] = np;
step[np.x][np.y] = step[u.x][u.y] + 1;
if(map[np.x][np.y] == 'x') {
++step[np.x][np.y];
}
}
}
}
return res;
}

Ⅲ 求一个广度优先算法的实例及其C语言程序(L-dequeue)

#include <stdio.h>
#define max 100
typedef struct anode
{
int adjvex; //边的终点位置
struct anode *nextarc;
}arcnode;

typedef struct node
{
int data;
arcnode *firstout;
}vnode;

typedef struct
{
vnode adjlist[max];
int n;
int e;
}Agraph;

static int visit[max];
//深度遍历
void DFS(Agraph &G,int v) //v为初始顶点编号
{
int k;
arcnode *p;
for(k=0;k<G.n;k++)
visit[k]=0;
printf("%d ",v);
p=G.adjlist[v].firstout;
while(p)
{
if(!visit[p->adjvex])
DFS(G,p->adjvex);
p=p->nextarc;
}
}

void BFS(Agraph &G,int v)
{
arcnode *p;
int q[max];
int front=0;
int rear=0;
int w,i;
for(i=0;i<G.n;i++)
visit[i]=0;
printf("%d ",v);
visit[v]=1;
rear=(rear+1)%max;
q[rear]=v;
while(front!=rear)
{
front=(front+1)%max;
w=q[front];
p=G.adjlist[w].firstout;
while(p)
{
if(!visit[p->adjvex])
{
printf("%d ",p->adjvex);
visit[p->adjvex]=1;
rear=(rear+1)%max;
q[rear]=p->adjvex;
}
p=p->nextarc;
}
printf("\n");
}
}

//层序遍历二叉树
struct btnode
{
int data;
btnode *lchild,*rchild;
};

void level(struct btnode *bt)
{
if(!bt)
return;
btnode *q[max];
int front,rear;
front=0;
rear=0;
printf("%d ",bt->data);
rear=(rear+1)%max;
q[rear]=bt;
while(front!=rear)
{
front=(front+1)%max;
bt=q[front];
if(bt->lchild)
{
printf("%d ",bt->lchild->data);
rear=(rear+1)%max;
q[rear]=bt->lchild;
}
if(bt->rchild)
{
printf("%d ",bt->rchild->data);
rear=(rear+1)%max;
q[rear]=bt->rchild;
}

}
}

void DFS1(Agraph &G,int v)
{
arcnode *p;
printf("%d ",v);
visit[v]=1;
p=G.adjlist[v].firstout;
while(p)
{
if(!visit[p->adjvex])
{
DFS1(G,p->adjvex);
}
p=p->nextarc;
}
}

void level1(struct btnode *bt)
{
if(!bt)
return;
printf("%d ",bt->data);
struct btnode *q[max];
int front=0;
int rear=0;
rear=(rear+1)%max;
q[rear]=bt;
while(front!=rear)
{
front=(front+1)%max;
bt=q[front];
if(bt->lchild)
{
printf("%d ",bt->lchild->data);
rear=(rear+1)%max;
q[rear]=bt->lchild;

}
if(bt->rchild)
{
printf("%d ",bt->rchild->data);
rear=(rear+1)%max;
q[rear]=bt->rchild;
}
}
}

void BFS1(Agraph &G,int v)
{
int q[max];
int front=0;
int rear=0;
int i;
for(i=0;i<G.n;i++)
visit[i]=0;
printf("%d ",v);
visit[v]=1;
rear=(rear+1)%max;
q[rear]=v;
arcnode *p;

while(front!=rear)
{
front=(front+1)%max;
i=q[front];
p=G.adjlist[i].firstout;
while(p)
{
if(!visit[p->adjvex])
{
printf("%d ",p->adjvex);
visit[p->adjvex]=1;
rear=(rear+1)%max;
q[rear]=p->adjvex;
}
p=p->nextarc;

}
}
}

Ⅳ C语言广度优先搜索遍历图的算法

visited[v]=_________; FAULT

EnQueue(Q,w);