簡體   English   中英

拓撲圖排序

[英]Topological Graph Sorting

我正在嘗試實現拓撲圖排序,並且我的程序正在編譯,但是我沒有得到填充的向量。 我試過運行一個調試器來跟蹤我的變量的去向,但我不太清楚為什么。 目前,我制作了一個圖形數據結構,其中包含一個頂點向量。 這是在我生成新圖時生成的(std:: vector vertices;)

我的圖是這樣的結構:

lass Graph {
    struct Edge{
        int dest = -1;
        Edge* next = nullptr;
        Edge(int dest, Edge* next) : dest(dest), next(next){};
        ~Edge() {delete next;}
    };

    struct vertex{
        int id =0;
        int degree = 0;
        int colour = 0;

        vertex* next  = nullptr;
        vertex* previous = nullptr;

    };

    Edge** edges = nullptr;
    std:: vector<vertex> vertices;
    std:: vector<vertex*> ordering;

所有這些都在圖形生成期間設置。

我真的很感激對這種排序的任何幫助。

void Graph::myOwnOrderingHelper(int v, bool *visited, std::stack<vector<vertex*>> &Stack) {

    vector<vertex*> hope;

    visited[v] = true;

    for (int i = 0; i < vertices[v].degree; i++) {
        int neighbour = vertices[v].id;

        if (!visited[neighbour]){
            myOwnOrderingHelper(i, visited, Stack);
            cout << vertices[v].next->id;
            hope.push_back(vertices[v].next);
        }
    }

    Stack.push(hope);
}

void Graph::myOwnOrdering() {
    std:: stack<vector<vertex*>> Stack;
    bool* visited = new bool[size];

    for(int i  = 0; i < size; i++){
        visited[i] = false;
    }

    for (int i = 0; i < size; i++){
        if (visited[i] == false){
            myOwnOrderingHelper(i, visited, Stack);
        }
    }

    while (Stack.empty() == false){
        std::vector<vertex*> temp = Stack.top();

        for(int i = 0; i < temp.size(); i++){
            cout << temp[i]->degree << endl;
            ordering.push_back(temp[i]);
        }

        Stack.pop();
    }

}

這里有不必要的復雜性,這使算法變得模糊。 考慮這個圖形表示:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

class Graph {
public:
  struct Vertex {
    std::vector<int> children;
  };

  // Add a vertex and return its index.
  int add_vertex() {
    int ix = vertices.size();
    vertices.push_back(Vertex());
    return ix;
  }

  // Add an edge from vertex a to b, both given by their indices.
  void add_edge(int a, int b) {
    vertices[a].children.push_back(b);
  }

  // Return a vector of vertex indices in topo order.
  std::vector<int> topo_sort() {
    std::set<int> visited;
    std::vector<int> result;

    // (1) The fun happens here. Return the reverse of a dfs post order visit.

    return result;
  }

private:
  std::vector<Vertex> vertices;

  // Recursive helper.
  void topo_sort(int i, std::set<int>& visited, std::vector<int>& result) {
    // (2) And yet more fun here.
  }
};

int main() {
  Graph g;
  int v0 = g.add_vertex();
  int v1 = g.add_vertex();
  int v2 = g.add_vertex();
  int v3 = g.add_vertex();
  int v4 = g.add_vertex();
  g.add_edge(v0, v1);
  g.add_edge(v0, v2);
  g.add_edge(v0, v4);
  g.add_edge(v1, v4);
  g.add_edge(v2, v4);

  std::vector<int> sorted = g.topo_sort();
  for (int i: sorted) std::cout << i << ' ';
  std::cout << std::endl;
  return 0;
}

這樣就可以通過添加 6 行來實現拓撲排序。

提示:當您在可以控制輸入數據的小程序中遇到意外行為時,添加跟蹤打印cerr <<...通常比設置調試器更省事,尤其是當您是編程新手時。

編輯

由於原始代碼現在應該可以工作,因此六行:

在 (1) 處,

    for (int i = 0; i < vertices.size(); ++i) topo_sort(i, visited, result);
    std::reverse(result.begin(), result.end());

在 (2) 處,它是

   if (visited.count(i)) return;
   visited.insert(i);
   for (int j: vertices[i].children) topo_sort(j, visited, result);
   result.push_back(i);

看看這段代碼:

for (int i = 0; i < vertices[v].degree; i++) {
    int neighbour = vertices[v].id;

    if (!visited[neighbour]){

每次通過這個 for 循環時, neighbour的值總是相同的。

無論您的 for 循環打算完成什么(您的代碼沒有記錄,因此不容易猜測)它實際上總是一遍又一遍地做完全相同的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM