-2

I have 2 functions and one of them is recursive. Here is the real code:

class Graph {
public:
    Graph(int v);
    virtual ~Graph();

    void addEdge(int v, int w);

    /**
     * main function that finds and checks SCC
     */
    bool checkSCC();

    /**
     * function that returns reverse (or transpose) of this graph
     */
    Graph getTranspose();
private:
    int V;
    std::list<int> *adj;
    std::vector<int> scc;

    /**
     * fills stack with vertices (in increasing order of finishing times).
     * the top element of stack has the maximum finishing time.
     */
    void fillOrder(int v, bool visited[], std::stack<int> &stack);

    /**
     * a recursive function to print DFS starting from v
     */
    void DFSUtil(int v, bool visited[]);
};

Graph::Graph(int v) {
    this->V = v;
    adj = new std::list<int>[v];
}

Graph::~Graph() {
}

void Graph::addEdge(int v, int w) {
    adj[v].push_back(w);
}

bool Graph::checkSCC() {
    std::stack<int> stack;

    bool *visited = new bool[V];
    for(int i=0; i<V; i++)
        visited[i]=false;

    for(int i=0; i<V; i++)
        if(visited[i]==false)
            fillOrder(i, visited, stack);

    Graph gr = getTranspose();

    for(int i=0; i<V; i++)
        visited[i]=false;

    while(stack.empty() == false){
        int v = stack.top();
        stack.pop();

        if(visited[v]==false){

            if(scc.size() > 1) { /*NOTE the problem is HERE !*/
                return true;
            }

            gr.DFSUtil(v, visited);
        }
    }

    return false;
}

Graph Graph::getTranspose() {
    Graph g(V);
    for(int v=0;v<V;v++){
        std::list<int>::iterator i;
        for(i=adj[v].begin();i!=adj[v].end();++i)
            g.adj[*i].push_back(v);
    }
    return g;
}

void Graph::fillOrder(int v, bool visited[], std::stack<int>& stack) {
    visited[v] = true;

    std::list<int>::iterator i;
    for(i = adj[v].begin(); i!= adj[v].end(); ++i)
        if(!visited[*i])
            fillOrder(*i, visited, stack);

    stack.push(v);
}

void Graph::DFSUtil(int v, bool visited[]) {
    visited[v] = true;
    scc.push_back(v); /*NOTE scc only works in this function !! */
    std::cout << v << " ";

    std::list<int>::iterator i;
    for(i = adj[v].begin(); i != adj[v].end(); ++i)
        if(!visited[*i])
            Graph::DFSUtil(*i, visited);
}

In this code, if i call Graph::checkSCC, scc preserve it's contents in scope of Graph::DFSUtil but NOT in Graph::checkSCC !! Why this happens ?

I appreciate any ideas and suggestions.

mrv90
  • 3
  • 4
  • Then why aren't you showing your actual code? Apparently it's different from this one, or else it would work just the same – UnholySheep Jul 25 '17 at 06:49
  • The code is basically the same.. The difference is too small to tell ... – mrv90 Jul 25 '17 at 06:51
  • 3
    It's apparently still relevant, otherwise it would work exactly the same way. Also in general it's pointless to provide a working code and asking why a different one (that's not shown) isn't working – UnholySheep Jul 25 '17 at 06:52
  • 1
    So basically are we supposed to guess what's wrong with the code we haven't seen by looking at the code that works perfectly? – CinCout Jul 25 '17 at 06:53
  • Dear @UnholySheep, the whole code is fairly complex ... I just thinking, there should be a way to prevent this problem .. Should i replace this snippet with actual code ?? – mrv90 Jul 25 '17 at 06:54
  • Ok guys. i replace the code with actual one... – mrv90 Jul 25 '17 at 06:56
  • 1
    @HamidMousaviNasab Yes please. You need to provide a minimal example that can be used to reporoduce the issue. A minimal example that doesn't reproduce the issue is useless. – Noel Widmer Jul 25 '17 at 06:57
  • Sorry my friends! hope this real code, makes clear the problem.. – mrv90 Jul 25 '17 at 07:08

1 Answers1

1

You're calling checkSCC on one object, and DFSUtil on another one (gr, which you create in checkSCC).

The scc member of one has no connection to the scc member of the other.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Dear @molbdnilo, thanks for your reply. If i want to preserve the contents of scc between functions of Graph class, how should i do this ? – mrv90 Jul 25 '17 at 07:35
  • The most obvious way is to pass `DFSUtil` a reference to it. – molbdnilo Jul 25 '17 at 07:46
  • Sorry, i don't understand, can you show me a link, or some sample ?? Similar to your solution ? – mrv90 Jul 25 '17 at 07:49
  • [This](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a good starting point. – molbdnilo Jul 25 '17 at 07:51
  • Problem solved. Thank you @molbdnilo, NoelWidmer, and 2 other fellows who decreased points of this question :) – mrv90 Jul 25 '17 at 08:34