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.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<vector>
using namespace std;
const int maxn = ;
struct edge{
int to, cost;
edge(int t){
this->to = t; this->cost = ;
}
};
void addEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
edgelist.push_back(edge(to));
G[from].push_back(edgelist.size()-);
}
void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
addEdge(edgelist,G,from,to);
addEdge(edgelist,G,to,from);
}
bool isCyclic(vector<edge> edgelist, vector<vector<int> > G,vector<bool> vis, vector<bool> RecStack, int v){
for(int i=;i<G[v].size();++i){
edge e = edgelist[G[v][i]];
if(RecStack[e.to]) return true;
if(!vis[e.to]){
vis[e.to] = true; RecStack[e.to] = true;
if(isCyclic(edgelist,G,vis,RecStack,e.to)) return true;
RecStack[e.to] = false;
}
}
return false;
}
void isCyclicUtil(vector<edge> edgelist, vector<vector<int> > G){// find all cycles.
vector<bool> vis(G.size());
vector<bool> RecStack(G.size());
for(int i=;i<vis.size();++i) vis[i]=false;
for(int i=;i<RecStack.size();++i) RecStack[i]=false; for(int i=;i<G.size();++i){
if(!vis[i]){
vis[i] = true; RecStack[i] = true;
if(isCyclic(edgelist,G,vis,RecStack,i)){
cout<<i<<" starts a cycle"<<endl;
}
RecStack[i] = false;
}
}
}
bool dfs(vector<edge> edgelist, vector<vector<int> > G, vector<bool> vis, int from, int to){
if(from == to) return true;
for(int i=;i<G[from].size();++i){
edge e = edgelist[G[from][i]];
if(e.to == to) return true;
if(!vis[e.to]){
vis[e.to] = true;
if(dfs(edgelist, G, vis, e.to, to)) return true;
}
}
return false;
}
void isReachable(vector<edge> edgelist, vector<vector<int> > G, int from, int to){
vector<bool> vis(G.size());
for(int i=;i<vis.size();++i) vis[i] = false;
vis[from] = true;
if(dfs(edgelist, G, vis, from, to)) cout<<from<<" and "<<to<<" are reachable to each other"<<endl;
else cout<<from<<" and "<<to<<" are not reachable to each other"<<endl;
}
void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
}
int main(){
vector<edge> edgelist;
vector<vector<int> > G(maxn); buildMap(edgelist,G); isCyclicUtil(edgelist, G); isReachable(edgelist, G, , ); return ;
}

dataStructure@ Find if there is a path between two vertices in a directed graph的更多相关文章

  1. Directed Graph Loop detection and if not have, path to print all path.

    这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...

  2. dataStructure@ Check if a directed graph has cycles

    #include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...

  3. 《Cracking the Coding Interview》——第4章:树和图——题目2

    2014-03-19 03:32 题目:给定一个有向图,判断其中两点是否联通. 解法:DFS搜索解决,如果是无向图的话,就可以用并查集高效解决问题了. 代码: // 4.2 Write a progr ...

  4. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  5. HDU3631:Shortest Path(Floyd)

    Problem Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in ...

  6. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  7. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  8. Hdu5385 The path

    The path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  9. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

随机推荐

  1. spoj 665

    直接判  没什么算法  也没什么技巧  水水~~ #include <cstdio> #include <cstring> #include <algorithm> ...

  2. Samza文档翻译 : Architecture

    http://samza.incubator.apache.org/learn/documentation/0.7.0/introduction/architecture.html Samza由三层组 ...

  3. EL表达式对数组、集合操作

    el表达式是通过${key}的方式获取对象中的值.在el表达式中有如下几个隐含的对象,pageScope,requestSope,sessionScope,applicationScope,如果要取$ ...

  4. 李洪强iOS开发之代理

    如果A想让控制器B为他做事情 用代理的话 首先: 在A的.h文件中:  其次A的.m中 在控制器的.m文件中: 还是在控制器B的.m文件中 在A初始化的那一刻设置控制器B为A的代理 在B的.m中实现代 ...

  5. C语言不是C++的严格子集

    C语言是C++的子集吗?C++是在C语言的基础上扩展而来并包含所有C语言的内容吗? 回复: 从实用角度讲,C++属于C语言的一个超集,基本上兼容ANSI C.但是从编译角度上讲,C语言的有些特性在C+ ...

  6. ajax readyState的五种状态详解

    通过ajax的readyState的值,我们可以知道当前的这个http请求处于什么状态.对于web的调试是比较重要的. readyState 状态说明: (0)未初始化 此阶段确认XMLHttpReq ...

  7. C#字符串的常用操作

    一.string关键字与StringBuilder类 C# 字符串是使用 string 关键字声明的一个字符数组.字符串是使用引号声明的,如下例所示: string s = "Hello, ...

  8. 最短路径算法之三——Bellman-Ford算法

    Bellman-Ford算法 Dijkstra算法无法判断含负权边的图的最短路. 如果遇到负权,在没有负权回路存在时,即便有负权的边,也可以采用Bellman-Ford算法正确求出最短路径. PS:负 ...

  9. Android Editext监听光标位置

    因为项目需要,需要实时监听光标的位置变化,网上提出的用TextWatcher和onTouchListener中调用contentText.getSelectionStart()都是获取的上一次位置. ...

  10. bzoj3211,bzoj3038

    线段树的裸题: 但是操作很奇怪,开方是不能lazy tag的 看来只能暴力修改了 但注意,开放开到1的时候就不用开,立一个flag就可以了 这可以大大的优化: 其实我是来复习线段树的 ..] of i ...