图的基本遍历算法的实现(BFS & DFS)复习
#include <stdio.h> #define INF 32767
typedef struct MGraph{
char vertexs[];
int edge[][];
int ver_num, edge_num;
}MGraph; void create_graph(MGraph *graph)
{
int i, j;
getchar();
for(i = ; i < graph->ver_num; ++i)
scanf("%c%*c",&(graph->vertexs[i])); for(i = ; i < graph->ver_num; ++i)
{
for(j = ; j < graph->ver_num; ++j)
{
if(i == j)
graph->edge[i][i] = ;
else
graph->edge[i][j] = INF;
}
}
int vi, vj, val;
for(i = ; i < graph->edge_num; ++i)
{
scanf("%d %d %d", &vi, &vj, &val);
graph->edge[vi][vj] = graph->edge[vj][vi] = val;
}
} void print_graph(MGraph graph)
{
int i,j;
for(i = ; i < graph.ver_num; ++i)
{
for(j = ; j < graph.ver_num; ++j)
{
printf("%d\t", graph.edge[i][j]);
}
puts("");
}
} int vis[];
void dfs(MGraph graph, int vertex)
{
int i;
vis[vertex] = ; //将该结点标记为访问过
printf("%d -> %c\t", vertex, graph.vertexs[vertex]); //打印结点信息,当然也可以是其他操作 for(i = ; i < graph.ver_num; ++i)
{ //从vertex 到 i 有路径存在,而且 i 结点未被访问过
if(graph.edge[vertex][i] > && graph.edge[vertex][i] != INF && !vis[i])
dfs(graph, i); //进行dfs遍历
}
} void dfstraverse(MGraph graph)
{
int i;
for(i = ; i < graph.ver_num; ++i) //将所有结点标记为未访问过
vis[i] = ;
for(i = ; i < graph.ver_num; ++i) //从每个未被访问过的结点开始进行dfs遍历
if(!vis[i])
dfs(graph, i);
} typedef struct Queue{
int store[];
int front, rear;
}Queue; void InitQueue(Queue *queue)
{
queue->front = queue->rear = ;
}
void EnQueue(Queue *queue , int i)
{
if(queue->rear == )
puts("the queue is full!!");
else{
queue->store[queue->rear] = i;
queue->rear++;
}
} int DeQueue(Queue *queue)
{
if(queue->rear == queue->front)
printf("the queue is empty!!");
else{
queue->front++;
return queue->store[queue->front-];
}
} Queue queue; //定义队列
void bfs(MGraph graph, int vertex)
{
int i, tmp;
vis[vertex] = ;
printf("%d -> %c\t",vertex, graph.vertexs[vertex]);
EnQueue(&queue, vertex); while(queue.front != queue.rear)
{
tmp = DeQueue(&queue);
for(i = ; i < graph.ver_num; ++i)
{
if(graph.edge[tmp][i] > && graph.edge[tmp][i] != INF && !vis[i])
{
vis[i] = ;
printf("%d -> %c\t", i, graph.vertexs[i]);
EnQueue(&queue, i);
}
}
}
} void bfstraverse(MGraph graph)
{
int i;
InitQueue(&queue);
for(i = ; i < graph.ver_num; ++i)
vis[i] = ;
for(i = ; i < graph.ver_num; ++i)
if(!vis[i])
bfs(graph, i);
} int main()
{
MGraph graph;
scanf("%d %d", &(graph.ver_num), &(graph.edge_num));
create_graph(&graph);
print_graph(graph);
dfstraverse(graph);
puts("");
bfstraverse(graph);
return ;
}
/*
4 4
0 1 2 3
0 1 9
1 2 8
2 3 7
3 0 6
* */
基于邻接矩阵的 DFS & BFS
#include <stdio.h>
#include <string.h>
#include <stdlib.h> typedef struct ArcNode{
int adjVex; //邻接点域
struct ArcNode *next; //指针域
}ArcNode; //边表 typedef struct VertexNode{
int data; //顶点域
struct VertexNode *firstArc;//边表头指针
}VertexNode, AdjList[]; //顶点表 typedef struct {
AdjList adjlist;
int ArcNum, VertexNum; //图中的顶点数和边数
}AdjListGraph;
/*
* 创建的是无向图,采用头插法建立边表
* */
int create_graph(AdjListGraph *graph)
{
int i, j;
for(i = ; i < graph->VertexNum; ++i)
{
graph->adjlist[i].data = i;
graph->adjlist[i].firstArc = NULL;
}
int m, n, val;
for(i = ; i < graph->ArcNum; ++i)
{
puts("the every arc");
scanf("%d %d", &m, &n);
ArcNode *tmp1 = (ArcNode*)malloc(sizeof(ArcNode));
if(tmp1 == NULL)
return ;
tmp1->adjVex = n;
tmp1->next = graph->adjlist[m].firstArc;
graph->adjlist[m].firstArc = tmp1; ArcNode *tmp2 = (ArcNode*)malloc(sizeof(ArcNode));
if(tmp2 == NULL)
return ;
tmp2->adjVex = m;
tmp2->next = graph->adjlist[n].firstArc;
graph->adjlist[n].firstArc = tmp2;
}
return ;
}
int vis[];
void dfs(AdjListGraph *graph, int vertex)
{
ArcNode *p;
vis[vertex] = ;
printf("%d\t", graph->adjlist[vertex].data);
p = graph->adjlist[vertex].firstArc; while(p)
{
if(!vis[p->adjVex])
dfs(graph, p->adjVex);
p = p->next;
}
}
void dfstreverse(AdjListGraph *graph)
{
int i;
for(i = ; i < graph->VertexNum; ++i)
{
vis[i] = ;
}
for(i = ; i < graph->VertexNum; ++i)
{
if(!vis[i])
dfs(graph, i);
}
} typedef struct Queue{
int store[];
int front, rear;
}Queue; void InitQueue(Queue *queue)
{
queue->front = queue->rear=;
}
void EnQueue(Queue *queue , int i)
{
if(queue->rear == )
puts("the queue is full!!");
else{
queue->store[queue->rear] = i;
queue->rear++;
}
} int DeQueue(Queue *queue)
{
if(queue->rear == queue->front)
printf("the queue is empty!!");
else{
queue->front++;
return queue->store[queue->front-];
}
} Queue queue;
void bfs(AdjListGraph *graph, int vertex)
{
int i, tmp;
ArcNode *p;
vis[vertex] = ;
printf("%d\t", graph->adjlist[vertex].data); EnQueue(&queue, vertex);
while(queue.front != queue.rear)
{
tmp = DeQueue(&queue);
p = graph->adjlist[tmp].firstArc;
while(p)
{
if(!vis[p->adjVex])
{
vis[p->adjVex] = ;
printf("%d\t", graph->adjlist[p->adjVex].data);
EnQueue(&queue, p->adjVex);
}
p = p->next;
} }
} void bfstreverse(AdjListGraph *graph)
{ InitQueue(&queue);
int i;
for(i = ; i < graph->VertexNum; ++i)
vis[i] = ;
for(i = ; i < graph->VertexNum; ++i)
if(!vis[i])
bfs(graph, i);
}
int main()
{
AdjListGraph graph;
int arc_num, ver_num;
puts("input the arc_num & ver_num\n");
scanf("%d %d", &arc_num, &ver_num);
graph.ArcNum = arc_num;
graph.VertexNum = ver_num;
create_graph(&graph);
dfstreverse(&graph);
bfstreverse(&graph);
return ;
}
基于邻接表的 DFS & BFS
手写又不熟了,多多看,多多练!
图的基本遍历算法的实现(BFS & DFS)复习的更多相关文章
- 图的广度优先遍历算法(BFS)
在上一篇文章我们用java演示了图的数据结构以及图涉及到的深度优先遍历算法,本篇文章将继续演示图的广度优先遍历算法.广度优先遍历算法主要是采用了分层的思想进行数据搜索.其中也需要使用另外一种数据结构队 ...
- 图的创建和遍历(BFS/DFS)
图的表示方法主要有邻接矩阵和邻接表.其中邻接表最为常用,因此这里便以邻接表为例介绍一下图的创建及遍历方法. 创建图用到的结构有两种:顶点及弧 struct ArcNode { int vertexIn ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 图的深度优先遍历算法(DFS)
搜索算法有很多种,本次文章主要分享图(无向图)的深度优先算法.深度优先算法(DFS)主要是应用于搜索中,早期是在爬虫中使用.其主要的思想有如下: 1.先访问一个节点v,然后标记为已被访问过2.找到第一 ...
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
- 算法导论—无向图的遍历(BFS+DFS,MATLAB)
华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...
- 图的深度优先遍历DFS
图的深度优先遍历是树的前序遍历的应用,其实就是一个递归的过程,我们人为的规定一种条件,或者说一种继续遍历下去的判断条件,只要满足我们定义的这种条件,我们就遍历下去,当然,走过的节点必须记录下来,当条件 ...
- 图的深度优先遍历(DFS)—递归算法
实验环境:win10, DEV C++5.11 实验要求: 实现图的深度优先遍历 实验代码: #include <iostream> #define maxSize 255 #includ ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
随机推荐
- Java Native Interface 六JNI中的异常
本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 在这里只讨论调用JNI方法可能会出现的异常, ...
- ASP.NET获取客户端、服务器端的信息
ASP.NET获取客户端.服务器端基础信息 1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名: ...
- java模拟post方式提交表单实现图片上传【转】
转自:http://blog.csdn.net/5iasp/article/details/8669644 模拟表单html如下: <form action="up_result ...
- Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站
http://www.cnblogs.com/zhongweiv/archive/2013/01/07/https.html 配置环境 了解HTTPS 配置CA证书服务器 新建示例网站并发布在IIS ...
- Shell 获取指定行的内容
需求: 有一个文件,根据指定的字符串,得到该字符串上两行的内容. 文件内容如下: linux-56:# cat sys.ttconnect.ini # Copyright (C) 1999, 2006 ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- android初练二
android 之 Activity的启动方式 1.android的显示启动 显示启动一般用于在用自己的活动时进行页面跳转时常常使用到 public class MainActivity extend ...
- visual studio 2015连接到MySql相关问题
vs中使用服务器资源管理器连接到MySQL没有成功.按照网上提供的解决方法,相关插件已经安装: 1.控制面板中,MySQL Connector Net 6.9.9已经安装(原安装版本为6.9.8,后升 ...
- 【修改 UITextField 中 placeholder 的顏色】
第一种方法: [textfeild setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; ...
- scala eclipse plugin 插件安装
最近在看Apache Apollo 代码,其中有很多scala代码,没办法需要安装一个scala插件. 我试过zip 安装,直接下载的update-site.zip 不能直接安装到位.我又特别懒,不想 ...