zoukankan      html  css  js  c++  java
  • C++图的一条简单路径搜索

    /*
     * description:        图的一条简单路径搜索,不是最短的
     * writeby:            Nick
     * date:            2012-10-25 23:32
     *
     */
    
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    struct Edge
    {
        int v, w;
        Edge(int v=-1, int w=-1) : v(v), w(w) {}
    };
    
    class Graph
    {
        private:
            int vcount, ecount;        //记录顶点总数,边总数
            bool digraph;            //标记是否有向图
            vector <vector <bool> > adj;        //邻接矩阵数组
        public:
            Graph(int V, bool di = false) : adj(V), vcount(V), digraph(di)
            {
                for(int i=0; i<V; i++)
                    adj[i].assign(V, false);    // V * V 临接矩阵的大小 
            }
            //~Graph();
            int V() const {return vcount;}
    
            int E() const {return ecount;}
    
            bool directed() const { return digraph; }
    
            int insert(Edge e)
            {
                int v=e.v, w=e.w;
                if(adj[v][w] == false) ecount++;
                adj[v][w] = true;                        // v-w 做标记
                if(!digraph) adj[w][v] = true;            //无向图中 w-v 也做标记
            }
    
            int remove(Edge e)
            {
                int v=e.v, w=e.w;
                if(adj[v][w]==true) ecount--;
                adj[v][w] = false;
                if(!digraph) adj[w][v] = false;
            }
    
            bool edge(int v, int w) const { return adj[v][w]; }
    
            class adjIterator;
            friend class adjIterator;
    };
    
    class Graph::adjIterator        //临接矩阵表示的迭代器
    {
        private:
            const Graph &G;
            int i, v;
        public:
            adjIterator(const Graph& G, int v) : G(G), v(v), i(-1)
            {}
    
            int begin()
            {
                i=-1;
                return next();
            }
    
            int next()
            {
                for(i++; i<G.V(); i++)
                    if(G.adj[v][i] == true) return i;    //adj[v][0..v-1] 记录着 v 到 0..v 各点是否相连
                return -1;    //没有找到
            }
    
            int end()
            {
                return i>=G.V();
            }
    };
    
    class spath
    {
        private:
            const Graph &G;
            vector <bool> visited;
            bool found;
            bool searchR(int v, int w)
            {
                if(v == w) return true;
                visited[v] = true;
                Graph::adjIterator ite(G, v);        //图的遍历器
                for(int t=ite.begin(); !ite.end(); t=ite.next())    //遍历是从0..v-1的顺序, 不太科学
                    if(!visited[t])        //标记已访问
                        if(searchR(t, w))         //如果t 能到达 w
                        {
                            cout << v << " -> " << t << endl;        //输出经过的路径
                            return true;
                        }
                return false;
            }
        public:
            spath(const Graph& G, int v, int w) : G(G), visited(G.V(), false)
            {
                found = searchR(v, w);
            }
            bool exist()
            {
                return found;
            }
    };
    
    int main()
    {
        Graph g(7, false);
    
        g.insert(Edge(0, 1));
        g.insert(Edge(0, 2));
        g.insert(Edge(0, 5));
        g.insert(Edge(0, 6));
    
        g.insert(Edge(1, 2));
        g.insert(Edge(2, 3));
        g.insert(Edge(2, 4));
        g.insert(Edge(3, 4));
        g.insert(Edge(4, 5));
        g.insert(Edge(4, 6));
    
        spath sp(g, 1, 6);
        cout << sp.exist();        //输出结果 0 或 1
        
        //结果
        4 -> 6
        3 -> 4
        2 -> 3
        0 -> 2
        1 -> 0
        1
    
        return 0;
    }
  • 相关阅读:
    饿了么P7级前端工程师进入大厂的面试经验
    前端程序员面试的坑,简历写上这一条信息会被虐死!
    这次来分享前端的九条bug吧
    移动端开发必会出现的问题和解决方案
    创建一个dynamics 365 CRM online plugin (八)
    创建一个dynamics 365 CRM online plugin (七)
    创建一个dynamics 365 CRM online plugin (六)
    创建一个dynamics 365 CRM online plugin (五)
    使用User Primary Email作为GUID的问题
    怎样Debug Dynamics 365 CRM Plugin
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2740658.html
Copyright © 2011-2022 走看看