#include #include #include #include #include #include using namespace std; unordered_map> G; // the adjacency list unordered_map visited; void Tokenize(string line, vector & tokens, string delimiters){ string token = ""; string OneCharString = " "; for (int i=0; i tokens; Tokenize(line, tokens, " \t"); if (tokens.size()!=2) continue; G[tokens[0]].push_back(tokens[1]); G[tokens[1]].push_back(tokens[0]); } inf.close(); } // assuming that nodes have unique string identifiers void DFS(string v, vector& ret){ visited[v] = true; ret.push_back(v); for (int i=0; i< G[v].size(); i++){ if (!visited[G[v][i]]) DFS(G[v][i], ret); } } vector BFS(string v){ vector ret; queue q; q.push(v); visited[v] = true; while (!q.empty()){ string u = q.front(); q.pop(); ret.push_back(u); for (int i=0; i< G[u].size(); i++){ if (visited[G[u][i]]) continue; q.push(G[u][i]); visited[G[u][i]] = true; } } return ret; } int main(int argc, char** argv){ readGreaph(argv[1]); cout << "DFS:" << endl; vector output; DFS(argv[2], output); for (int i=0; i