zoukankan      html  css  js  c++  java
  • 算法 -图的深度优先遍历

    #ifndef COMPONENT_H
    #define COMPONENT_H
    #include <iostream>
    #include <cassert>
    using namespace std;
    
    //图的深度优先遍历
    template<typename Graph>
    class Component{
    private:
        Graph &G;
        bool *visited;
        int ccount;
        int *id;
    
        void dfs(int v){
            visited[v] = true;
            id[v] = ccount;
            typename Graph::adjIterator adj(G,v);
            for(int i =adj.begin();!adj.end();i= adj.next()){
                if(!visited[i])
                    dfs(i);
            }
        }
    public:
        Component(Graph &graph):G(graph){
            visited = new bool[G.V()];
            id = new int[G.V()];
            ccount=0;
            for(int i=0;i<G.V();i++){
                visited[i]= false;
                id[i] = -1;
            }
            for(int i =0;i<G.V();i++)
                if(!visited[i]){
                    dfs(i);
                    ccount++;
                    }
        }
    
        ~Component(){
            delete[] visited;
            delete[] id;
        }
    
        int count(){
            return ccount;
        }
        bool isConnected(int v,int w){
            assert(v>=0 && v<G.V());
            assert(w>=0 && w<G.V());
            return id[v] == id[w];
        }
    };
    
    
    
    #endif
  • 相关阅读:
    java properties
    js resource
    script Ruby / Rails / Arachni
    webServer / Apache / apache / apache http server / mod_cluster
    JAVA XML open protocol
    二维数组举例
    二维数组
    二维数组
    二维数组
    二维数组
  • 原文地址:https://www.cnblogs.com/Erick-L/p/12623062.html
Copyright © 2011-2022 走看看