算法-图的路径查询-深度优先遍历

#include <cassert>
#include <vector>

template<typename Graph>
class Path
{
private:
    Graph &G;
    int s; //某一个点
    bool* visited;
    int* from;//路径

    void dfs(int v){
        visited[v] = true;
        typename Graph::adjIterator adj(G,v);
        for(int i =adj.begin();!adj.end();i= adj.next()){
            if(!visited[i]){
                from[i] =v;
                dfs(i);
            }
        }
    }
public:
    Path(Graph &graph,int s):G(graph){
        //算法初始化
        assert(s>=0 && s < G.V());
        visited = new bool[G.V()];
        from = new int[G.V()];
        for(int i =0;i<G.V();i++){
            visited[i] = false;
            from[i] = -1;
        }
        this->s = s;

        //寻路算法
        dfs(s);

    };
    ~Path(){
        delete[] visited;
        delete[] from;
    }
    //从s到w是否有路径
    bool hasPath(int w){
        assert(w>=0 && w<G.V());
        return visited[w];
    }

    //从s到w路径是多少
    void path(int w,vector<int> &vec){
        stack<int> s;
        int p = w;
        while (p!=-1){
            s.push(p);
            p = from[p];
        }
        vec.clear();
        while (!s.empty()){
            vec.push_back(s.top());
            s.pop();
        }

    }
    //把路径打印出来
    void showPath(int w){
        vector<int> vec;
        path(w,vec);
        for(int i=0;i<vec.size();i++){
            cout<<vec[i];
            if(i==vec.size()-1)
                cout<<endl;
            else
                cout<<" --> ";
        }

    }

};

原文地址:https://www.cnblogs.com/Erick-L/p/12623248.html

时间: 2024-08-30 09:57:09

算法-图的路径查询-深度优先遍历的相关文章

算法-图的路径查询-广度优先遍历

#include <cassert> #include <vector> template<typename Graph> class ShortestPath { private: Graph &G; int s; //某一个点 bool* visited; int* from;//路径 int *ord;//最短距离 public: ShortestPath(Graph &graph,int s):G(graph){ //算法初始化 assert(s

图的邻接表+深度优先遍历+广度优先遍历

1 /** 2 无向图的邻接表存储 3 深度优先遍历递归 4 广度优先遍历递归+非递归 5 */ 6 #include <stdio.h> 7 #include <string.h> 8 #include <malloc.h> 9 #define N 5 10 #define MAX 50 11 typedef struct A{ 12 int adjvex; 13 struct A* nextArc; 14 }Arc; 15 typedef struct node{

图的广度、深度优先遍历 C语言

以下是老师作为数据结构课的作业的要求,没有什么实际用处和可以探讨和总结的的地方,所以简单代码直接展示. 宽度优先遍历: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 6 #define _clr(x, y) memset(x, y, sizeof(x)) 7 #define N 1010 8 9 int head[N], tot; 10 struc

python数据结构与算法——图的广度优先和深度优先的算法

根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 每次循环从队列弹出一个结点 将该节点的所有相连结点放入队列,并标记已被发现 通过队列,将迷宫路口所有的门打开,从一个门进去继续打开里面的门,然后返回前一个门处 1 """ 2 procedure BFS(G,v) is 3 let Q be a queue 4 Q.enqueue(v) 5 label v as discovered 6 while Q is not empty 7

数据结构与算法-图的遍历

#include<iostream> #include<string> #include<queue> using namespace std; #define ERROR 1 #define MAX_VERTEX_NUM 100 typedef struct ArcNode{ int adjvex; struct ArcNode *nextarc; string info; }ArcNode; typedef struct VNode{ char date; ArcN

邻接矩阵的深度优先遍历

摘要:对<大话数据结构>P240——邻接矩阵的深度优先遍历,进行了自己的理解并完善了代码.Qt Creator测试通过. 举个简单的无序图例子,为了节省时间传手稿. 首先用邻接矩阵的存储结构创建该图,再进行深度优先遍历.代码和解释如下: #include <iostream> #include <stdlib.h> using namespace std; typedef struct//图的邻接矩阵存储结构 { char vexs[5]; int arc[5][5];

邻接表的深度优先遍历

转载请注明出处http://www.cnblogs.com/hslzju 对<大话数据结构>P241——邻接表的深度优先遍历,进行了自己的理解并完善了代码. 邻接矩阵的深度优先遍历见http://www.cnblogs.com/hslzju/p/5399249.html 举个简单的无序图例子,为了节省时间传手稿. 首先用邻接表的存储结构创建该图,再进行深度优先遍历.代码和解释如下(Qt Creator测试通过): 1 #include <iostream> 2 #include &

算法题——图的深度优先遍历

原理和方法可以参考: 图的深度优先遍历 教科书上的C代码,递归: 1 //教科书方法,邻接表 2 bool visited[MAX]; 3 void visitFunc(int v); 4 5 void dfsTraverse(Graph G) 6 { 7 for(v = 0; v < G.vexnum; ++v) //初始化访问标识为false 8 visited[v] = false; 9 for(v = 0; v < G.vexnum; ++v) //深搜 10 if(!visited[

算法学习 - 图的深度优先遍历(DFS) (C++)

深度优先遍历 在图的遍历中,其中深度优先遍历和广度优先遍历是最常见,也最简单的两种遍历方法. 深度优先遍历的思想就是一直向下找,找到尽头之后再去其他分支查找. 在上一篇博客中我已经写了广度优先遍历(BFS). 想看的传送门:图的广度优先遍历 代码实现 这里实现和BFS的差别在于,在BFS中,我们使用的容器是队列(queue),是先进先出的, 而在DFS中我们需要使用的是栈(stack)一个先进后出的容器. 其他基本原理相同. // // main.cpp // DFS // // Created