#include #include #include #include #include using namespace std; typedef vector< pair > > Graph; void visit(string s) { cout << s << " "; } int getIndex(const Graph & g, string s) { for(int i = 0; i < g.size(); i++) { if(g[i].first == s) { return i; } } return -1; } void bfs(const Graph & g) { //if the graph is empty, then there's noting to do if(g.empty()) { return; } //set up the queue to visit nodes, and add first node queue q; //keep track of nodes already visited set visited; for(int i = 0; i < g.size(); i++) { q.push(g[i].first); while(!q.empty()) { //get next node in the queue and visit it if haven't already string next = q.front(); q.pop(); if(visited.count(next) == 0) { visit(next); //add it to set of visited nodes so we don't visit it again visited.insert(next); //add its adjacent nodes to the queue, to visit later int index = getIndex(g, next); vector adjList = g[index].second; for (int i = 0; i < adjList.size(); i++) { if(visited.count(adjList[i]) == 0) { q.push(adjList[i]); } } } } } cout << endl; } void recursiveDFSHelper(const Graph & g, set & visited, string node) { if(visited.count(node) == 0) { visit(node); visited.insert(node); int index = getIndex(g, node); vector adjList = g[index].second; for (int i = 0; i < adjList.size(); i++) { if(visited.count(adjList[i]) == 0) { recursiveDFSHelper(g, visited, adjList[i]); } } } } void dfs(const Graph & g) { //empty visited set set visited; for(int i = 0; i < g.size(); i++) { string node = g[i].first; recursiveDFSHelper(g, visited, node); } cout << endl; } bool recursiveDFSCycle(const Graph & g, set & visited, string node, string parent) { bool hasCycle = false; if(visited.count(node) == 0) { visit(node); visited.insert(node); int index = getIndex(g, node); vector adjList = g[index].second; for (int i = 0; i < adjList.size(); i++) { if(visited.count(adjList[i]) != 0 && adjList[i] != parent) { hasCycle = true; } hasCycle |= recursiveDFSCycle(g, visited, adjList[i], node); } } return hasCycle; } bool dfs_cycle(const Graph & g) { //empty visited set set visited; bool hasCycle = false; for(int i = 0; i < g.size(); i++) { string node = g[i].first; //no parent node, since we've just started string parent = ""; hasCycle |= recursiveDFSCycle(g, visited, node, parent); } cout << endl; return hasCycle; } /* * simple graph test */ void test1 () { //just create the graph by hand Graph graph; //node 0 and its adjacency list graph.push_back(pair >()); graph[0].first = "A"; graph[0].second.push_back("B"); graph[0].second.push_back("C"); //node 1 and its adjacency list graph.push_back(pair >()); graph[1].first = "B"; graph[1].second.push_back("A"); graph[1].second.push_back("D"); graph[1].second.push_back("E"); //node 2 and its adjacency list graph.push_back(pair >()); graph[2].first = "C"; graph[2].second.push_back("A"); graph[2].second.push_back("E"); //node 3 and its adjacency list graph.push_back(pair >()); graph[3].first = "D"; graph[3].second.push_back("B"); //node 4 and its adjacency list graph.push_back(pair >()); graph[4].first = "E"; graph[4].second.push_back("B"); graph[4].second.push_back("C"); bfs(graph); dfs(graph); } /* * Simple tree, no cycles. */ void test2 () { //just create the graph by hand Graph graph; //node 0 and its adjacency list graph.push_back(pair >()); graph[0].first = "A"; graph[0].second.push_back("B"); graph[0].second.push_back("C"); //node 1 and its adjacency list graph.push_back(pair >()); graph[1].first = "B"; graph[1].second.push_back("A"); graph[1].second.push_back("D"); graph[1].second.push_back("E"); //node 2 and its adjacency list graph.push_back(pair >()); graph[2].first = "C"; graph[2].second.push_back("A"); //node 3 and its adjacency list graph.push_back(pair >()); graph[3].first = "D"; graph[3].second.push_back("B"); //node 4 and its adjacency list graph.push_back(pair >()); graph[4].first = "E"; graph[4].second.push_back("B"); cout << dfs_cycle(graph) << endl; } /* * unconnected graph test */ void test3 () { //just create the graph by hand Graph graph; //node 0 and its adjacency list graph.push_back(pair >()); graph[0].first = "A"; graph[0].second.push_back("B"); graph[0].second.push_back("C"); //node 1 and its adjacency list graph.push_back(pair >()); graph[1].first = "B"; graph[1].second.push_back("A"); graph[1].second.push_back("C"); //node 2 and its adjacency list graph.push_back(pair >()); graph[2].first = "C"; graph[2].second.push_back("A"); graph[2].second.push_back("B"); //node 3 and its adjacency list graph.push_back(pair >()); graph[3].first = "D"; graph[3].second.push_back("E"); graph[3].second.push_back("F"); //node 4 and its adjacency list graph.push_back(pair >()); graph[4].first = "E"; graph[4].second.push_back("D"); //node 5 and its adjacency list graph.push_back(pair >()); graph[5].first = "F"; graph[5].second.push_back("D"); bfs(graph); cout << dfs_cycle(graph) << endl; } int main() { test1(); }