### Problem when writing Kosaraju’s algorithm on Mac

I ran some C++ codes in order to realize Kosaraju’s algorithm on my mac. I want to output the largest 5 SCCs in terms of their number of nodes. I have to write a class of graph. I don't know whether my class of graph is OK. The code is as follows:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139`` ``````#include #include #include #include #include #include using namespace std; class Graph { int V; // No. of vertices list * adj; public: Graph(); Graph(int v); Graph getTrans(); // reverse graph int DFSlen (int v, bool visited[]); // return the length of the SCC of v void fillOrder (int v, bool visited[], stack &Stack); vector top5SCC(); void parseFile(const string filename); }; Graph::Graph(int v){ this->V = v; adj = new list[v]; } Graph Graph::getTrans(){ Graph g(V); for (int i = 0; i < V; ++i){ list::iterator j; for(j = adj[i].begin(); j != adj[i].end(); ++j) g.adj[*j].push_back(i); } return g; } int Graph::DFSlen (int v, bool visited[]) { visited[v] = true; static int len = 0; len += 1; list::iterator i; for(i = adj[v].begin(); i != adj[v].end(); ++i) { if (!visited[*i]) DFSlen (*i, visited); } return len; } void Graph::fillOrder (int v, bool visited[], stack &Stack){ visited[v] = true; list::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) { if (!visited[*i]) fillOrder(*i, visited, Stack); } Stack.push(v); } int getNodeCount(const string filename){ ifstream graphFile(filename); int n = 0; string str; while(getline(graphFile, str, '\n')){ ++n; } return n; } // parse file into graph void Graph::parseFile(const string filename){ this->V = getNodeCount(filename); ifstream graphFile(filename); int nodeIndex; int outIndex; while (graphFile) { graphFile >> nodeIndex; graphFile >> outIndex; adj[nodeIndex - 1].push_back(outIndex - 1); } } void top5array(vector& ar, int a){ int temp = 0; int i = 0; for(; i < 5; ++i) { if(a > ar[i]) { temp = ar[i]; ar[i] = a; break; } } if(i<4){ ar[i+1] = temp; for(int j = 4; j>i+1; --j) ar[j] = ar[j-1]; } } vector Graph::top5SCC(){ stack Stack; vector vec(5, 0); 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]) fillOrder(i, visited, Stack); Graph gr = getTrans(); for (int i = 0; i < V; ++i) visited[i] = false; while (!Stack.empty()) { int v = Stack.top(); Stack.pop(); if (!visited[v]) { top5array(vec, gr.DFSlen(v, visited)); } } return vec; } int main() { Graph g(5); g.parseFile("1.txt"); vector a = g.top5SCC(); for(int i = 0; i < a.size(); ++i) cout << a[i] << endl; return 0; } ``````

Here `"1.txt"` is a file
 ``1234567891011`` ``````1 4 2 8 3 6 4 7 5 2 6 9 7 1 8 5 8 6 9 7 9 3``````

Then I just got the following error massage from the terminal:

 ``1234567891011`` ``````Undefined symbols for architecture x86_64: "std::__1::__vector_base_common::__throw_length_error() const", referenced from: std::__1::vector >::allocate(unsigned long) in kosaraju-a4f23c.o "std::__1::locale::has_facet(std::__1::locale::id&) const", referenced from: std::__1::basic_filebuf >::basic_filebuf() in kosaraju-a4f23c.o "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from: std::__1::basic_ostream >& std::__1::endl >(std::__1::basic_ostream >&) in kosaraju-a4f23c.o std::__1::basic_filebuf >::imbue(std::__1::locale const&) in kosaraju-a4f23c.o std::__1::basic_filebuf >::basic_filebuf() in kosaraju-a4f23c.o "std::__1::ios_base::getloc() const", referenced from: ...``````

The message is quite long and I just post a piece of it.

Anyone knows what is the problem? Many thanks ahead.
Last edited on
¿is that a build error? If that's the case, show your build command and the full error message.

iirc in 1.txt you say that there are going to be 11 nodes (maybe is an upper limit), but your graph is initialized for just 5 nodes `Graph g(5);`
By the way, you don't have a single `delete[]`