找到有向图C ++的最短路径

时间:2015-05-07 14:29:30

标签: c++ graph graph-algorithm dijkstra shortest-path

在过去的一周里,我通过解析输入文件实现了Digraph。图表保证没有周期。我已经成功创建了图形,使用了返回顶点和边数的方法,并对图形进行了拓扑排序。该图由不同的主要课程及其先决条件组成。这是我的图表设置:

class vertex{
public:
    typedef std::pair<int, vertex*> ve;     
    std::vector<ve> adjacency;              
    std::string course;                     
    vertex(std::string c){
        course = c;
    }
};

class Digraph{
public:
    typedef std::map<std::string, vertex *> vmap;           
    vmap work;
    typedef std::unordered_set<vertex*> marksSet;           
    marksSet marks;
    typedef std::deque<vertex*> stack;                      
    stack topo;
    void dfs(vertex* vcur);                                 
    void addVertex(std::string&);                           
    void addEdge(std::string& from, std::string& to, int cost);     
    int getNumVertices();                                   
    int getNumEdges();                                      
    void getTopoSort();                                     

};

实施

//function to add vertex's to the graph
void Digraph::addVertex(std::string& course){
    vmap::iterator iter = work.begin();
    iter = work.find(course);
    if(iter == work.end()){
        vertex *v;
        v = new vertex(course);
        work[course] = v;
        return;
    }
}

//method to add edges to the graph
void Digraph::addEdge(std::string& from, std::string& to, int cost){
    vertex *f = (work.find(from)->second);
    vertex *t = (work.find(to)->second);
    std::pair<int, vertex *> edge = std::make_pair(cost, t);
    f->adjacency.push_back(edge);
}

//method to return the number of vertices in the graph
int Digraph::getNumVertices(){
    return work.size();
}

//method to return the number of edges in the graph
int Digraph::getNumEdges(){
    int count = 0;
    for (const auto & v : work) {
         count += v.second->adjacency.size();
     }
     return count;
}

//recursive function used by the topological sort method
void Digraph::dfs(vertex* vcur) {
  marks.insert(vcur);
  for (const auto & adj : vcur->adjacency) {
    vertex* suc = adj.second;
    if (marks.find(suc) == marks.end()) {
      this->dfs(suc);
    } 
  }
  topo.push_front(vcur);
}

//method to calculate and print out a topological sort of the graph
void Digraph::getTopoSort(){
    marks.clear();
    topo.clear();
    for (const auto & v : work) {
        if (marks.find(v.second) == marks.end()) {
            this->dfs(v.second);
        }
    }
    // Display it
   for (const auto v : topo) {
    std::cout << v->course << "\n";
  }
}

对于我实施的最后一部分,我一直在尝试做两件事。找到从第一个顶点到每个其他顶点的最短路径,并找到访问每个顶点并返回到第一个顶点的最短路径。我完全迷失在这个实现上。我从阅读中假设我需要使用Dijkstra算法来实现这一点。我一直在尝试过去3天无济于事。我是否以不好的方式设置了我的图表来实现这些步骤?任何指导都表示赞赏。

1 个答案:

答案 0 :(得分:2)

没有循环这一事实使问题变得更加简单。寻找最短的路径和最小的“盛大之旅”是O(n)。

实施Dijkstra并运行它,没有“目的地”节点;继续前进,直到所有节点都被访问过。一旦标记了每个节点(与根节点的距离),您就可以从任何节点开始,并按照最短(且唯一)的路径返回到根节点,始终踩到距离小于此距离的唯一邻居。如果需要,您可以非常轻松地构建这些路径,并使用完整路径将每个节点标记回根目录,但复制这些路径可以将成本推到O(n 2 )你不小心。

一旦标记了所有节点,您就可以构建一个最小的游览。从根开始;当你访问一个节点时,迭代它的未访问的邻居(即除你刚来的那个之外的所有邻居),访问每个,然后返回你来自的那个。 (如果你愿意的话,我可以用更严格的数学表达,或者给出一个例子。)

相关问题