zoukankan      html  css  js  c++  java
  • dataStructure@ 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 }
    View Code
  • 相关阅读:
    Consul常用命令
    ECharts 避免变窄
    TP3.2 日期默认格式
    新订单提示效果
    php 按照字典序排序 微信卡券签名算法用到
    td宽度自适应 窄的地方自动收缩
    git 删除本地分支,删除远程分支
    分页Model
    chrome表单自动填充如何取消
    tp3.2 如何比较两个字段
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4905497.html
Copyright © 2011-2022 走看看