C中图的深度优先遍历算法
来源:互联网 发布:小熊电蒸锅 淘宝 编辑:程序博客网 时间:2024/06/06 04:51
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAX_VERTEX_NUM 50
typedef char VertexType[3];
typedef struct edgenode
{
int adjvex;
int value;
struct edgenode *next;
}ArcNode;
typedef struct vexnode{
VertexType data;
ArcNode *firstarc;
}VHeadNode;
typedef struct
{
int Vertex_Num,Edge_Num;
VHeadNode adjlist[MAX_VERTEX_NUM];
}AdjList;
int visited[MAX_VERTEX_NUM];
int Create_Adj_List_undirected_graph(AdjList *&G){
int i,b,t,w;
ArcNode *p;
G=(AdjList *)malloc(sizeof(AdjList));
printf("顶点数,边数\n");
scanf("%d%d",&G->Vertex_Num,&G->Edge_Num);
for(i=0;i<G->Vertex_Num;i++)
{
printf("序号为%d的顶点信息:",i);
scanf("%s",&G->adjlist[i].data);
G->adjlist[i].firstarc=NULL;
}
for(i=0;i<G->Edge_Num;i++)
{
printf("起点号 终点号 权值\n");
scanf("%d%d%d",&b,&t,&w);
if(b<G->Vertex_Num&&t<G->Vertex_Num&&w>0)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->value=w;
p->adjvex=t;
p->next=G->adjlist[b].firstarc;
G->adjlist[b].firstarc=p;
}
else
{
printf("输入有误!!\n");
return 0;
}
}
return 1;
}
void Disp_Adj_List(AdjList *G)
{
int i;
ArcNode *p;
printf("图的邻接表表示如下:\n");
for(i=0;i<G->Vertex_Num;i++)
{
printf("[%d,%3s]=>",i,G->adjlist[i].data);
p=G->adjlist[i].firstarc;
while(p!=NULL)
{
printf("(%d,%d)->",p->adjvex,p->value);
p=p->next;
}
printf("^\n");
}
}
//深度优先算法
void DFS(AdjList *g,int vi)
{
ArcNode *p;
printf("%d",vi);
visited[vi]=1;
p=g->adjlist[vi].firstarc;
while(p!=NULL)
{
if(visited[p->adjvex]==0)
DFS(g,p->adjvex);
p=p->next;
}
}
void main()
{
int i;
AdjList *G;
Create_Adj_List_undirected_graph(G);
Disp_Adj_List(G);
memset(visited,sizeof(visited),0);
printf("从顶点0深度优先遍历\n");
printf("递归算法:\n");
DFS(G,0);
printf("\n");
}
#include <string.h>
#include <malloc.h>
#define MAX_VERTEX_NUM 50
typedef char VertexType[3];
typedef struct edgenode
{
int adjvex;
int value;
struct edgenode *next;
}ArcNode;
typedef struct vexnode{
VertexType data;
ArcNode *firstarc;
}VHeadNode;
typedef struct
{
int Vertex_Num,Edge_Num;
VHeadNode adjlist[MAX_VERTEX_NUM];
}AdjList;
int visited[MAX_VERTEX_NUM];
int Create_Adj_List_undirected_graph(AdjList *&G){
int i,b,t,w;
ArcNode *p;
G=(AdjList *)malloc(sizeof(AdjList));
printf("顶点数,边数\n");
scanf("%d%d",&G->Vertex_Num,&G->Edge_Num);
for(i=0;i<G->Vertex_Num;i++)
{
printf("序号为%d的顶点信息:",i);
scanf("%s",&G->adjlist[i].data);
G->adjlist[i].firstarc=NULL;
}
for(i=0;i<G->Edge_Num;i++)
{
printf("起点号 终点号 权值\n");
scanf("%d%d%d",&b,&t,&w);
if(b<G->Vertex_Num&&t<G->Vertex_Num&&w>0)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->value=w;
p->adjvex=t;
p->next=G->adjlist[b].firstarc;
G->adjlist[b].firstarc=p;
}
else
{
printf("输入有误!!\n");
return 0;
}
}
return 1;
}
void Disp_Adj_List(AdjList *G)
{
int i;
ArcNode *p;
printf("图的邻接表表示如下:\n");
for(i=0;i<G->Vertex_Num;i++)
{
printf("[%d,%3s]=>",i,G->adjlist[i].data);
p=G->adjlist[i].firstarc;
while(p!=NULL)
{
printf("(%d,%d)->",p->adjvex,p->value);
p=p->next;
}
printf("^\n");
}
}
//深度优先算法
void DFS(AdjList *g,int vi)
{
ArcNode *p;
printf("%d",vi);
visited[vi]=1;
p=g->adjlist[vi].firstarc;
while(p!=NULL)
{
if(visited[p->adjvex]==0)
DFS(g,p->adjvex);
p=p->next;
}
}
void main()
{
int i;
AdjList *G;
Create_Adj_List_undirected_graph(G);
Disp_Adj_List(G);
memset(visited,sizeof(visited),0);
printf("从顶点0深度优先遍历\n");
printf("递归算法:\n");
DFS(G,0);
printf("\n");
}
- C中图的深度优先遍历算法
- 算法学习 - 图的深度优先遍历(DFS) (C++)
- 图的深度优先遍历和广度优先遍历算法
- 图的深度优先遍历算法
- 图的深度优先遍历算法
- 深度优先算法,图的遍历
- 图的深度优先遍历算法
- 图的深度优先遍历算法
- 图的深度优先遍历算法
- 【算法】图的深度优先遍历
- 深度优先算法、广度优先算法 与 图的遍历
- 图的遍历 -深度优先—C
- 图的邻接矩阵生成算法,深度优先遍历算法,广度优先遍历算法,Prime算法。):
- 深度、广度优先遍历算法C实现
- 深度、广度优先遍历算法C实现
- 深度优先遍历与广度优先遍历算法的C语言实现
- 图的遍历之深度优先搜索算法&&广度优先优先算法的实现
- 树的广度优先遍历与深度优先遍历算法
- alter system switch logfile和alter system archive log current的区别
- 短信猫linux使用
- 系统通常理解仅由交易员
- 标准I/O文件I/O的区别
- 做出决策最新和即时信息
- C中图的深度优先遍历算法
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。
- 终于能写博客了!
- oracle中to_char函数操作时间对象
- awk 的一些用法
- 单链表快速排序算法实现
- fork vfork函数区别
- 软件工程开发模型以及在软件工程中的作用
- WPS 常用操作