图的深度优先搜索 c代码
来源:互联网 发布:崩坏3淘宝 编辑:程序博客网 时间:2024/06/05 14:56
#include<stdio.h>
#include<stdlib.h>
#define MAX 30
typedef struct node{
int adjvex;
struct node *next;
}EdgeNode;
typedef struct vnode{
int vertex;
EdgeNode *firstedge;
}VertexNode;
void CreatAdjlist(VertexNode g[],int e,int n){
EdgeNode *p;
int k ,j,i;
printf("Input date of vetex(1~~n-1):\n");
for(i=0;i<n;i++)
{
g[i].vertex=i; //顶点信息
g[i].firstedge=NULL;
}
for(k=1;k<=e;k++) //e条边
{
printf("Input edge of (i,j):");
scanf("%d,%d",&i,&j);
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex=j; //在顶点vi的邻接表中添加邻接点为i的节点
p->next=g[i].firstedge;//插入在表头
g[i].firstedge=p;
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex=i; //在顶点vj的邻接表中添加邻接点为j的节点
p->next=g[j].firstedge;
g[j].firstedge=p;
}
}
int visited[MAX];
void DFS(VertexNode g[],int i){
EdgeNode *p;
printf("%4d",g[i].vertex);
visited[i]=1;
p=g[i].firstedge;
while(p){
if(!visited[p->adjvex])
DFS(g,p->adjvex);
p=p->next;
}
}
void DFSTraverse(VertexNode g[],int n){
int i;
for(i=0;i<n;i++)
visited[i]=0;
for(i=0;i<n;i++)
if(!visited[i])
DFS(g,i);
}
void main()
{
int e,n;
VertexNode g[MAX];
printf("Input number of Node\n");
scanf("%d",&n);
printf("INput number of edge:\n");
scanf("%d",&e);
printf("Make adjlist:\n");
CreatAdjlist(g,e,n);
printf("DFStraverse:\n");
DFSTraverse(g,n);
printf("\n");
}
/*Input number of Node
4
INput number of edge:
4
Make adjlist:
Input date of vetex(1~~n-1):
Input edge of (i,j):0,1
Input edge of (i,j):0,3
Input edge of (i,j):1,3
Input edge of (i,j):1,2
DFStraverse:
0 3 1 2
Press any key to continue*/
- 图的深度优先搜索 c代码
- 深度优先搜索和广度优先搜索的代码实现
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度、广度优先搜索——C/C++
- mac os下py2app打包python程序
- java jar包的各种作用
- solaris下如何创建新用户
- VS2010安装项目的系统必备中添加.NET 2.0
- jQuery通过ajax获得后台json数据给form表单赋值
- 图的深度优先搜索 c代码
- android如何使用release证书替换debug证书签名来调试
- 多Makefile写法实例
- 加密和解密 DES RSA
- 基于SaltStack完成LVS的配置管理
- VisionMobile:非对称商务模式(一)破坏移动行业的背后
- poj 1730 Perfect Pth Powers 精度修正
- 第十四周项目3多可成绩单
- C++ 关键字 static register const volatile extern