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

#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>=0 && s < G.V());

        visited = new bool[G.V()];
        from = new int[G.V()];
        ord = new int[G.V()];

        for(int i =0;i<G.V();i++){
            visited[i] = false;
            from[i] = -1;
            ord[i] = -1;
        }
        this->s = s;

        queue<int> q;
        //广度优先无向图最短路径算法
        q.push(s);
        visited[s] = true;
        ord[s]=0;
        while (!q.empty())
        {
            int v = q.front();
            q.pop();
            typename Graph::adjIterator adj(G,v);
            for(int i =adj.begin();!adj.end();i= adj.next()){
                if(!visited[i]){
                    q.push(i);
                    visited[i] =true;
                    from[i]=v;
                    ord[i] = ord[v] +1
                }
            }
        }

    };
    ~Path(){
        delete[] visited;
        delete[] from;
        delete[] ord;
    }
    //从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<<" --> ";
        }

    }
    int lenth(int w){
        assert(w>=0 && w<G.V());
        return ord[w];
    }

};

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

时间: 2024-08-30 17:27:24

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

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

#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(

【数据结构-图】图的建立以及广度优先遍历算法

本文利用邻接表的方法将图进行了表示,并且利用广度优先遍历方法对图进行遍历 下面是一个图的示例: 代码如下: #include<iostream> using namespace std; typedef int VexType; typedef struct Arcnode{ VexType data; struct Arcnode *nextarc; }ArcNode; typedef struct Vexnode { VexType data; ArcNode *firstarc; }Vn

图的邻接表存储表示,图的深度优先和广度优先遍历

1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define MAX_VERTAX_SIZE 20 5 #define OK 1 6 #define ERROR 0 7 8 typedef int Status; 9 typedef char ElemType; 10 11 typedef struct EageNode{ 12 int adjacentVertax; 13 struct EageNode* nextAdjacentVer

Java实现图的深度和广度优先遍历算法

概述: 近期要学习写网络爬虫.所以把图的深度和广度搜索都再温习一下. 图结构展示: 实现过程: 首先,我们来看看图结构在代码中的实现.有三块逻辑: 1.图中的节点: public class GraphNode { public List<GraphEdge> edgeList = null; private String label = ""; public GraphNode(String label) { this.label = label; if (edgeLis

图的邻接表(广度优先遍历,深度优先遍历,最小生成树(Kruskal算法))

main.h: #include <iostream> #include <queue> #define DefaultSize 10 #define maxWeight -1 using namespace std; template<typename T,typename E> struct Edge { int dest; E cost; Edge<T,E> *link; Edge(int d=0,int c=0):dest(d),cost(c),li

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

广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不懂的人.请先学下图的存储方法.在我的之前博客里. 传送门:图表示方法 然后我们如果有一个图例如以下: 节点1->3->NULL 节点2->NULL 节点3->2->4->NULL 节点4->1->2->NULL 这样我们已经知道这是一个什么图了. 如果我们

图---广度优先遍历

1.解决问题:判断一幅图中 从顶点S到顶点V之间是否有一条路径---Dijkstar算法的基础(计算出S--V点中最短路径) 1.使用队列(FIFO)先进先出原则 存储与顶点S相距 N条边的点,直到遍历到V点上  不同于深度优先遍历 不需要进行递归 因此 BFS无法判断图中是否有环的 判断出一个有向图是否有环的是:a.最短路径有向图最短路径Dijkstra不一定能够判断有环,除非Visted数字记录0(false),1(已经访问)2(再一次重复访问): b.括扑排序:通过入度0之后记录顶点数与原

数据结构(C实现)------- 图的广度优先遍历

[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020] 算法描述: 设图G的初始状态是所有顶点均未被访问过,在G中的任选一顶点vi为初始出发点,则广度优先遍历 可定义如下:首先,访问初始出发点vi,接着依次访问vi的所有邻接点w1,w2,...,wk;然后,依次访问w1,w2,...,wk 的邻接的所有未被访问过的顶点,依次类推,直到图中所有的和初始点vi有路径相通的顶点都被访问过为止. 算法实现: (1) 访问初始顶点vi (

python实现图广度优先遍历、深度优先遍历

一.广度优先遍历-bfs 顾名思义,bfs总是先访问完同一层的结点,然后才继续访问下一层结点,它最有用的性质是可以遍历一次就生成中心结点到所遍历结点的最短路径,这一点在求无权图的最短路径时非常有用.广度优先遍历的核心思想非常简单,用python实现起来也就十来行代码.下面就是超精简的实现,用来理解核心思想足够了: 1 import queue 2 3 def bfs(adj, start): 4 visited = set() 5 q = queue.Queue() 6 q.put(start)