From 856d88f127e749a547a90dbfa820a29ad58f4a8b Mon Sep 17 00:00:00 2001 From: vedantanasune Date: Sat, 19 Oct 2024 14:53:49 +0530 Subject: [PATCH] added a graph folder in C++ folder and added three algorithm in it with a README file --- C++/graph/BFS.cpp | 68 ++++++++++++++++++++++++++++++ C++/graph/DFS.cpp | 59 ++++++++++++++++++++++++++ C++/graph/README.md | 12 ++++++ C++/graph/graph-implementation.cpp | 48 +++++++++++++++++++++ vertical_traversal.cpp | 0 5 files changed, 187 insertions(+) create mode 100644 C++/graph/BFS.cpp create mode 100644 C++/graph/DFS.cpp create mode 100644 C++/graph/README.md create mode 100644 C++/graph/graph-implementation.cpp create mode 100644 vertical_traversal.cpp diff --git a/C++/graph/BFS.cpp b/C++/graph/BFS.cpp new file mode 100644 index 0000000..9785a5f --- /dev/null +++ b/C++/graph/BFS.cpp @@ -0,0 +1,68 @@ +// Here’s how you can implement Breadth-First Search (BFS) in C++ using the graph structure + +#include +#include +#include +#include +#include + +class Graph { +private: + // Adjacency list to store the graph + std::unordered_map> adjList; + +public: + // Function to add an edge between two vertices + void addEdge(int v1, int v2) { + adjList[v1].push_back(v2); + adjList[v2].push_back(v1); // Since the graph is undirected + } + + // Function to perform BFS traversal from a given source vertex + void BFS(int start) { + // Vector to track visited vertices + std::vector visited(adjList.size(), false); + // Queue to help with the BFS traversal + std::queue q; + + // Start from the given vertex + visited[start] = true; + q.push(start); + + // Continue the BFS until the queue is empty + while (!q.empty()) { + // Get the next vertex from the queue + int v = q.front(); + q.pop(); + + std::cout << v << " "; + + // Get all the adjacent vertices of the dequeued vertex + for (int neighbor : adjList[v]) { + if (!visited[neighbor]) { + // Mark neighbor as visited and enqueue it + visited[neighbor] = true; + q.push(neighbor); + } + } + } + } +}; + +int main() { + Graph g; + + // Adding edges + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(1, 3); + g.addEdge(2, 3); + g.addEdge(3, 4); + + // Perform BFS starting from vertex 0 + std::cout << "BFS starting from vertex 0:" << std::endl; + g.BFS(0); + + return 0; +} \ No newline at end of file diff --git a/C++/graph/DFS.cpp b/C++/graph/DFS.cpp new file mode 100644 index 0000000..4e901ee --- /dev/null +++ b/C++/graph/DFS.cpp @@ -0,0 +1,59 @@ +//Here’s how you can implement Depth-First Search (DFS) on the graph created earlier. DFS can be implemented recursively or iteratively; I’ll show you the recursive approach here. +#include +#include +#include +#include + +class Graph { +private: + // Adjacency list to store the graph + std::unordered_map> adjList; + + // Helper function for DFS + void DFSUtil(int v, std::vector& visited) { + // Mark the current node as visited + visited[v] = true; + std::cout << v << " "; + + // Recur for all vertices adjacent to this vertex + for (int neighbor : adjList[v]) { + if (!visited[neighbor]) { + DFSUtil(neighbor, visited); + } + } + } + +public: + // Function to add an edge between two vertices + void addEdge(int v1, int v2) { + adjList[v1].push_back(v2); + adjList[v2].push_back(v1); // Since the graph is undirected + } + + // Function to perform DFS traversal starting from vertex 'start' + void DFS(int start) { + // Keep track of visited vertices + std::vector visited(adjList.size(), false); + + // Call the recursive helper function to perform DFS + DFSUtil(start, visited); + } +}; + +int main() { + Graph g; + + // Adding edges + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(1, 3); + g.addEdge(2, 3); + g.addEdge(3, 4); + + // Perform DFS starting from vertex 0 + std::cout << "DFS starting from vertex 0:" << std::endl; + g.DFS(0); + + return 0; +} \ No newline at end of file diff --git a/C++/graph/README.md b/C++/graph/README.md new file mode 100644 index 0000000..eadd5e5 --- /dev/null +++ b/C++/graph/README.md @@ -0,0 +1,12 @@ +Explanation: +Graph class: This class uses an adjacency list (unordered map of lists) to store the graph. +addEdge function: Adds an edge between two vertices v1 and v2. Since it’s an undirected graph, the edge is bidirectional. +displayGraph function: Iterates through the adjacency list and prints out the graph structure. + +Explanation: +DFSUtil: A recursive helper function that does the actual DFS traversal. It takes the current vertex (v) and the visited vector as arguments. +It marks the current vertex as visited and prints it. +Then it recursively visits all unvisited neighbors. +DFS function: The function that initiates the DFS traversal. It creates a visited vector (initialized to false) to track which nodes have been visited. It then calls DFSUtil for the starting vertex. +Output: +If you run the code, the output will be a DFS traversal of the graph starting from vertex 0, such as: diff --git a/C++/graph/graph-implementation.cpp b/C++/graph/graph-implementation.cpp new file mode 100644 index 0000000..07a9bd5 --- /dev/null +++ b/C++/graph/graph-implementation.cpp @@ -0,0 +1,48 @@ +//Here’s a simple implementation of a graph data structure in C++ using an adjacency list: + +#include +#include +#include + +// Graph class representing an undirected graph +class Graph { +private: + // Adjacency list to store the graph + std::unordered_map> adjList; + +public: + // Function to add an edge between two vertices + void addEdge(int v1, int v2) { + adjList[v1].push_back(v2); // Add v2 to v1's adjacency list + adjList[v2].push_back(v1); // Add v1 to v2's adjacency list (undirected graph) + } + + // Function to display the graph + void displayGraph() { + for (const auto& vertex : adjList) { + std::cout << vertex.first << ": "; + for (const auto& neighbor : vertex.second) { + std::cout << neighbor << " "; + } + std::cout << std::endl; + } + } +}; + +int main() { + Graph g; + + // Adding edges + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(1, 3); + g.addEdge(2, 3); + g.addEdge(3, 4); + + // Displaying the graph + std::cout << "Graph Adjacency List:" << std::endl; + g.displayGraph(); + + return 0; +} \ No newline at end of file diff --git a/vertical_traversal.cpp b/vertical_traversal.cpp new file mode 100644 index 0000000..e69de29