Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //graph adjacency corresponds to the index where it is assigned
- class DFS {
- public:
- int vertices;
- std::vector<std::vector<int>> graph;
- std::vector<int> visitedFlags;
- bool checkGraphConnected() {
- int startIndex = 0;
- visitedFlags = std::vector<int>(vertices, false);
- dfs(startIndex);
- return (std::find(visitedFlags.begin(), visitedFlags.end(), 0) == visitedFlags.end());
- }
- void dfs(int i) {
- if (!visitedFlags[i]) {
- visitedFlags[i] = true;
- for (int j = 0; j < graph[i].size(); j++) {
- dfs(graph[i][j]);
- }
- }
- }
- };
Add Comment
Please, Sign In to add comment