class Graph {
int vertexNum;
list
public:
Graph(int _vertexNum) {
vertexNum = _vertexNum;
adjacents = new list
}
void findIndegree(int *indegree, int n);
bool topologicalSort();
void addEdge(int v, int w);
};
void Graph::addEdge(int v, int w) {
adjacents[v].push_back(w);
}
void Graph::findIndegree(int *indegree, int n) {
int v;
list
for(v = 0; v
indegree[*iter]++;
}
}
bool Graph::topologicalSort() {
int ver_count = 0;
stack
int *indegree = new int[vertexNum];
memset(indegree, 0, sizeof(int) * vertexNum);
findIndegree(indegree, vertexNum);
int v;
for (v = 0; v
m_stack.push(v);
while (!m_stack.empty()) {
v = m_stack.top();
m_stack.pop();
cout <
for (list
if (0 == --indegree[*iter])
m_stack.push(*iter);
}
}
cout <
return true;
}
int main(int argc, char *argv[]) {
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
if (g.topologicalSort())
cout <<"it is a topological graph" <
cout <<"it is not a topological graph" <
return 0;
}