|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <stack> |
| 4 | +#include <cassert> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief This class represents a simple undirected graph and provides a Depth-First Search (DFS) algorithm. |
| 10 | + * |
| 11 | + * The problem involves traversing an undirected graph using the Depth-First Search algorithm. Given a graph with |
| 12 | + * a certain number of vertices and edges, the DFS algorithm explores the vertices in a way that prioritizes |
| 13 | + * visiting unexplored neighbors, backtracking only when necessary. |
| 14 | + * |
| 15 | + * @details The Depth-First Search (DFS) algorithm is a recursive or iterative algorithm used to explore a graph's |
| 16 | + * vertices and edges. Starting from a specific vertex, the algorithm visits the vertex and then recursively explores |
| 17 | + * its unvisited neighbors, backtracking only when no further unexplored neighbors are present. This process ensures |
| 18 | + * that all vertices connected to the starting vertex are visited. |
| 19 | + */ |
| 20 | +class Graph { |
| 21 | +public: |
| 22 | + int vertices; |
| 23 | + vector<vector<int>> adjList; |
| 24 | + |
| 25 | + /** |
| 26 | + * @brief Construct a new Graph object with the given number of vertices. |
| 27 | + * |
| 28 | + * @param v Number of vertices in the graph. |
| 29 | + */ |
| 30 | + Graph(int v) : vertices(v), adjList(v) {} |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Add an edge between two vertices in the graph. |
| 34 | + * |
| 35 | + * @param v The first vertex. |
| 36 | + * @param w The second vertex. |
| 37 | + */ |
| 38 | + void addEdge(int v, int w) { |
| 39 | + adjList[v].push_back(w); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @brief Perform Depth-First Search (DFS) starting from the given vertex. |
| 44 | + * |
| 45 | + * @param start The starting vertex for DFS. |
| 46 | + * |
| 47 | + * @details The DFS algorithm starts from the specified vertex, explores each connected vertex, and recursively |
| 48 | + * traverses the unvisited neighbors. The algorithm maintains a stack to keep track of the vertices to visit, |
| 49 | + * ensuring a systematic exploration of the graph. |
| 50 | + */ |
| 51 | + void DFS(int start) { |
| 52 | + // Check the validity of the starting vertex |
| 53 | + if (start < 0 || start >= vertices) { |
| 54 | + cerr << "Invalid starting vertex" << endl; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + vector<bool> visited(vertices, false); |
| 59 | + stack<int> s; |
| 60 | + |
| 61 | + s.push(start); |
| 62 | + |
| 63 | + while (!s.empty()) { |
| 64 | + int current = s.top(); |
| 65 | + s.pop(); |
| 66 | + |
| 67 | + if (!visited[current]) { |
| 68 | + cout << current << " "; |
| 69 | + visited[current] = true; |
| 70 | + } |
| 71 | + |
| 72 | + for (int neighbor : adjList[current]) { |
| 73 | + if (!visited[neighbor]) { |
| 74 | + s.push(neighbor); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief Test cases for the DFS algorithm on an undirected graph. |
| 83 | + * |
| 84 | + * @details The testDFS function contains sample graph instances and tests the DFS algorithm's behavior in different scenarios, |
| 85 | + * including valid graph traversals and handling invalid starting vertices. |
| 86 | + */ |
| 87 | +void testDFS() { |
| 88 | + Graph g1(5); |
| 89 | + g1.addEdge(0, 1); |
| 90 | + g1.addEdge(0, 2); |
| 91 | + g1.addEdge(1, 3); |
| 92 | + g1.addEdge(2, 4); |
| 93 | + |
| 94 | + cout << "Test Case 1: Depth-First Traversal starting from vertex 0:\n"; |
| 95 | + g1.DFS(0); |
| 96 | + Graph g2(3); |
| 97 | + g2.addEdge(0, 1); |
| 98 | + g2.addEdge(1, 2); |
| 99 | + |
| 100 | + cout << "\nTest Case 2: Invalid starting vertex (expect error message):\n"; |
| 101 | + g2.DFS(5); |
| 102 | + Graph g3(0); |
| 103 | + |
| 104 | + cout << "\nTest Case 3: Depth-First Traversal on an empty graph (expect no output):\n"; |
| 105 | + g3.DFS(0); |
| 106 | + Graph g4(1); |
| 107 | + |
| 108 | + cout << "\nTest Case 4: Depth-First Traversal on a graph with a single vertex:\n"; |
| 109 | + g4.DFS(0); |
| 110 | + |
| 111 | + Graph g5(7); |
| 112 | + g5.addEdge(0, 1); |
| 113 | + g5.addEdge(0, 2); |
| 114 | + g5.addEdge(1, 3); |
| 115 | + g5.addEdge(2, 4); |
| 116 | + g5.addEdge(5, 6); |
| 117 | + |
| 118 | + cout << "\nTest Case 5: Depth-First Traversal on a graph with disconnected components starting from vertex 0:\n"; |
| 119 | + g5.DFS(0); |
| 120 | +} |
| 121 | + |
| 122 | +int main() { |
| 123 | + testDFS(); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
0 commit comments