1.5)看个荔枝——对给定图在DFS过程中添加背向边(backside)的过程

【2】source code + printing results
2.0)code specification:
我个人以为代码中的干货,一个当然是 DFS 的递归过程,另一个则是 添加背向边的代码,它的if 条件语句如下:
if(visited[adjVertex]) // judge whether the adjVertes was visited before
{
if(vertexIndex[vertex] > vertexIndex[adjVertex] && parent[vertex] != adjVertex)
{
parent[adjVertex] = vertex; // building back side, attention of condition of building back side above
// just for printing effect
for(i = 0; i < depth; i++)
printf(" ");
printf("vertex[%c]->vertex[%c] (backside) \n", flag[vertex], flag[adjVertex]);
}
}
- s1)对以上代码的分析: 当顶点vertex 的邻接顶点被访问过时,如果该邻接顶点 晚于 当前顶点vertex 访问, 且 该邻接顶点不是当前顶点vertex的 parent的话,那么就符合添加 背向边的条件了;(这里有点打脑筋,慢慢理解)
- s2)我们讲为什么要添加背向边? 因为我们的初衷是要让 深度优先搜索树的各个顶点的可达程度 和 给定的初始图的顶点可达情况类似或相同;你要想, 如果存在 (v1,v2) ,那必然有(v2,v1), 所以如果v1 是v2的parent,此时如果你又添加了 v2到 v1的背向边,那岂不是没有意义;我们再想, 如果 v1先于v2被访问, 你又添加了 v1到v2的背向边,那岂不是还是没有意义,请对照到上述代码中的 if 条件语句以便理解 为什么那样去添加背向边;
2.1)download source code: https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter9/p241_dfs_undirected_graph
2.2)source code at a glance:(for complete code , please click the given link above)
#include "dfs.h"
extern char flag[];
void dfs(Vertex vertex, int depth)
{
int i;
AdjTable temp;
Vertex adjVertex;
//printf("\n\t visited[%c] = 1 ", flag[vertex]);
visited[vertex] = 1; // update visited status of vertex
vertexIndex[vertex] = counter++; // number the vertex with counter
temp = adj[vertex];
while(temp->next)
{
adjVertex = temp->next->vertex;
if(visited[adjVertex]) // judge whether the adjVertes was visited before
{
if(vertexIndex[vertex] > vertexIndex[adjVertex] && parent[vertex] != adjVertex)
{
parent[adjVertex] = vertex; // building back side, attention of condition of building back side above
// just for printing effect
for(i = 0; i < depth; i++)
printf(" ");
printf("vertex[%c]->vertex[%c] (backside) \n", flag[vertex], flag[adjVertex]);
}
}
else
{
parent[adjVertex] = vertex;
// just for printing effect
for(i = 0; i < depth; i++)
printf(" ");
printf("vertex[%c]->vertex[%c] (building edge)\n", flag[vertex], flag[adjVertex]);
dfs(adjVertex, depth+1);
}
temp = temp->next;
}
}
2.3)printing results:

- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)
需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...
- (转)广度优先搜索BFS和深度优先搜索DFS
1. 广度优先搜索介绍 广度优先搜索算法(Breadth First Search),又称为"宽度优先搜索"或"横向优先搜索",简称BFS. 它的思想是:从图中 ...
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
- 深度优先搜索 DFS 学习笔记
深度优先搜索 学习笔记 引入 深度优先搜索 DFS 是图论中最基础,最重要的算法之一.DFS 是一种盲目搜寻法,也就是在每个点 \(u\) 上,任选一条边 DFS,直到回溯到 \(u\) 时才选择别的 ...
- 广度优先(bfs)和深度优先搜索(dfs)的应用实例
广度优先搜索应用举例:计算网络跳数 图结构在解决许多网络相关的问题时直到了重要的作用. 比如,用来确定在互联网中从一个结点到另一个结点(一个网络到其他网络的网关)的最佳路径.一种建模方法是采用无向图, ...
- 用深度优先搜索(DFS)解决多数图论问题
前言 本文大概是作者对图论大部分内容的分析和总结吧,\(\text{OI}\)和语文能力有限,且部分说明和推导可能有错误和不足,希望能指出. 创作本文是为了提供彼此学习交流的机会,也算是作者在忙碌的中 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
随机推荐
- stack栈和Queue队列
1.push将对象插入 System.Collections.Generic.Stack<T> 的顶部. Stack st = new Stack(); //栈是先进后出 st.Push( ...
- MySQL 中的 base64 函数
1. 5.6版本及之后的版本的base64 主要就是两个mysql内部函数to_base64和from_base64,使用也很简单,如下: 5.6之前不支持 mysql> select vers ...
- UIPanGestureRecognizer判断滑动的方向
.h文件 CGFloat const gestureMinimumTranslation = 20.0 ; typedef enum : NSInteger { kCameraMoveDirectio ...
- Intellij IDEA 14.x 菜单项中Compile、Make和Build的区别
Compile.Make和Build的区别 针对Java的开发工具,一般都有Compile.Make和Build三个菜单项,完成的功能的都差不多,但是又有区别. 编译,是将源代码转换为可执行代码的过程 ...
- App Distribution Guide (二)
Configuring Your Xcode Project for Distribution You can edit your project settings anytime, but som ...
- StringBuilder.append()与String的"+"的效率PK
如果String通过"+"来拼接,如果拼接的字符串是常量,则效率会非常高,因为会进行编译时优化,这个时候StringBuilder的append()是达不到的. 如果将String ...
- zabbix日志监控
一般情况下,日志最先反映出应用当前的问题,在海量日志里面找到我们异常记录,例如监控系统日志.nginx.Apache.业务日志,然后记录下来,并且根据情况报警. 1.日志监控项介绍 最主要的是监控日志 ...
- 2017.7.1 ftp文件服务器安装与配置(已验证可使用)
下载地址:http://learning.happymmall.com/ 1.点击exe文件 2.启动ftpserver 点击exe后,就出现如下画面:输入账户密码和勾选权限等. 并配置好对应的文件夹 ...
- [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...
- Gizmos 辅助线框
Gizmos are used to give visual debugging or setup aids in the scene view. Gizmos是用于在场景视图可视化调试或辅助设置. ...
|