Home > Net >  Topological Graph Sorting
Topological Graph Sorting

Time:12-15

I am trying to implement a topological graph sort, and my program is compiling however I am not getting a populated vector. I have tried running a debugger to track where my variables are going and I am not too sure why. Currently, I have made a graph data structure, which contains a vector of vertices. This gets generated during when I generate a new graph (std:: vector vertices;)

My graph is structure like such:

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;

All of which get set during the graph generation.

I would really appreciate any help with this sorting.

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();
    }

}

CodePudding user response:

Look at this code:

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

    if (!visited[neighbour]){

Every time through this for loop the value of neighbour will always be the same.

Whatever your for loop is intended to accomplish ( your code is undocumented so it is not easy to guess ) it will in fact always do exactly the same over and over again.

CodePudding user response:

There's needless complexity here, which is obscuring the algorithm. Consider this graph representation:

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

    // 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) {
    // 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;
}

With this it's possible to implement topo sort by adding 6 lines.

Hint: When you get unexpected behavior in a small program where you have control of the input data, adding trace prints cerr << ... is often less trouble than setting up a debugger, especially when you're new to programming.

  • Related