[email protected] Find if there is a path between two vertices in a directed graph

Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second. For example, in the following graph, there is a path from vertex 1 to 3. As another example, there is no path from 3 to 0.

We can either use Breadth First Search (BFS) or Depth First Search (DFS) to find path between two vertices. Take the first vertex as source in BFS (or DFS), follow the standard BFS (or DFS). If we see the second vertex in our traversal, then return true. Else return false.

Following is C++ code that uses BFS for finding reachability of second vertex from first vertex.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<limits>
 5 #include<vector>
 6 using namespace std;
 7 const int maxn = 4;
 8 struct edge{
 9     int to, cost;
10     edge(int t){
11         this->to = t; this->cost = 0;
12     }
13 };
14 void addEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
15     edgelist.push_back(edge(to));
16     G[from].push_back(edgelist.size()-1);
17 }
18 void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
19     addEdge(edgelist,G,from,to);
20     addEdge(edgelist,G,to,from);
21 }
22 bool isCyclic(vector<edge> edgelist, vector<vector<int> > G,vector<bool> vis, vector<bool> RecStack, int v){
23     for(int i=0;i<G[v].size();++i){
24         edge e = edgelist[G[v][i]];
25         if(RecStack[e.to]) return true;
26         if(!vis[e.to]){
27             vis[e.to] = true; RecStack[e.to] = true;
28             if(isCyclic(edgelist,G,vis,RecStack,e.to)) return true;
29             RecStack[e.to] = false;
30         }
31     }
32     return false;
33 }
34 void isCyclicUtil(vector<edge> edgelist, vector<vector<int> > G){// find all cycles.
35     vector<bool> vis(G.size());
36     vector<bool> RecStack(G.size());
37     for(int i=0;i<vis.size();++i) vis[i]=false;
38     for(int i=0;i<RecStack.size();++i) RecStack[i]=false;
39
40     for(int i=0;i<G.size();++i){
41         if(!vis[i]){
42             vis[i] = true; RecStack[i] = true;
43             if(isCyclic(edgelist,G,vis,RecStack,i)){
44                 cout<<i<<" starts a cycle"<<endl;
45             }
46             RecStack[i] = false;
47         }
48     }
49 }
50 bool dfs(vector<edge> edgelist, vector<vector<int> > G, vector<bool> vis, int from, int to){
51     if(from == to) return true;
52     for(int i=0;i<G[from].size();++i){
53         edge e = edgelist[G[from][i]];
54         if(e.to == to) return true;
55         if(!vis[e.to]){
56             vis[e.to] = true;
57             if(dfs(edgelist, G, vis, e.to, to)) return true;
58         }
59     }
60     return false;
61 }
62 void isReachable(vector<edge> edgelist, vector<vector<int> > G, int from, int to){
63     vector<bool> vis(G.size());
64     for(int i=0;i<vis.size();++i) vis[i] = false;
65     vis[from] = true;
66     if(dfs(edgelist, G, vis, from, to)) cout<<from<<" and "<<to<<" are reachable to each other"<<endl;
67     else cout<<from<<" and "<<to<<" are not reachable to each other"<<endl;
68 }
69 void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){
70     addEdge(edgelist,G,0,1);
71     addEdge(edgelist,G,0,2);
72     addEdge(edgelist,G,2,0);
73     addEdge(edgelist,G,1,2);
74     addEdge(edgelist,G,2,3);
75     addEdge(edgelist,G,3,3);
76 }
77 int main(){
78     vector<edge> edgelist;
79     vector<vector<int> > G(maxn);
80
81     buildMap(edgelist,G);
82
83     isCyclicUtil(edgelist, G);
84
85     isReachable(edgelist, G, 1, 1);
86
87     return 0;
88 }

时间: 2024-12-28 01:08:17

[email protected] Find if there is a path between two vertices in a directed graph的相关文章

mac安装beego工具bee报错 go: github.com/derekparker/[email&#160;protected]: parsing go.mod: unexpected module path &quot;github.com/go-delve/delve&quot;

主要参考URL: http://www.678fly.cn/d/3-go-mod-bee 一.创建一个 go mod 下的文件夹 mkdir test cd test go mod init test 二.在 go.mod 内把 bee 的源替换掉,如下所示: github.com/realguan/bee 是我 fork 了 github.com/beego/bee 的源码,进行了源代码更改: module test replace github.com/beego/bee v1.10.0 =

[email&#160;protected] $location.path(&#39;/login&#39;)-$location服务用法示例

$httpProvider interceptor .factory('auth403', ['$rootScope', '$q', '$location', function auth403($rootScope, $q, $location) { return { request: function (config) { console.log(config); var start = new Date(); return config; }, response: function (res

JSON Extractor/[email&#160;protected] - JSON Path Extractor 举例2

测试描述 使用json返回结果做校验 测试步骤 1.配置http请求 2.根据结果树返回的json,取值 { "status_code":200, "message":"success", "data": { "current_page":1, "data": [ { "id":"69", "title":"Zlife

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(六)

无书面许可请勿转载 高级playbook Finding files with variables All modules can take variables as part of their arguments by dereferencing them with {{ and }} . You can use this to load a particular file based on a variable. For example, you might want to select a

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(七)

如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. This approach will work for simple infrastructures, or when using Ansible as a simple deployment mechanism. However, if you have a large and complicated

git笔记[email&#160;protected]

之前安装了git,用了不久就升级系统了,发现又忘记了步骤,虽然网上有很多教程,但寻找需要浪费太多的时间,由于github连接比较慢,所以使用了开源中国的托管http://git.oschina.net/,安装流程写在oneNote里. 1.首先需要安装git<a href="http://git-scm.com/download/">Git官网</a>,安装好之后会又Git GUI和Git Bash,就用命令行吧~~ 打开Git Bash,简单的配置下: 先建立

Intellij IDEA使用[email&#160;protected] 推送本地代码到 git

1. 安装git for windows 首先安装git for windows 推荐使用这个:http://msysgit.github.io/ 可以在任何目录 右键--git bash 弹出对应路径的 git 命令行窗口 而且启动速度比较快 在Intellij中Settings--Version Control--Git--Path to Git executable 找到安装git  bin目录下的git.exe 2. 适用于已有项目 先在[email protected]上创建仓库  拿

Shell特殊变量:Shell $0, $#, $*, [email&#160;protected], $?, $$和命令行参数

变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid,看下面的代码: $echo $$ 运行结果 29949 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $# 传递给脚本或函数的参数个数. $* 传递给脚本或函数的所有参数. [email protected] 传递给脚本或函数的所有参数.被

======= 利用源码安装的tar包,免安装--MySQL-5.6.23 for [email&#160;protected]

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/44836547 一.在源码安装的基础上:我打了一个tar包:cd /u01/myss06tar cvzf my3306.tar . 二.关闭防火墙chkconfig iptables off service iptables stop vi /etc/selinux/configSELINUX=disabled 三.检查操作系统上是否安装了MySQL[[email protected] b