Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a graph folder in C++ folder and added three algorithm in it wi… #368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions C++/graph/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Here’s how you can implement Breadth-First Search (BFS) in C++ using the graph structure

#include <iostream>
#include <unordered_map>
#include <list>
#include <queue>
#include <vector>

class Graph {
private:
// Adjacency list to store the graph
std::unordered_map<int, std::list<int>> 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<bool> visited(adjList.size(), false);
// Queue to help with the BFS traversal
std::queue<int> 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;
}
59 changes: 59 additions & 0 deletions C++/graph/DFS.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <unordered_map>
#include <list>
#include <vector>

class Graph {
private:
// Adjacency list to store the graph
std::unordered_map<int, std::list<int>> adjList;

// Helper function for DFS
void DFSUtil(int v, std::vector<bool>& 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<bool> 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;
}
12 changes: 12 additions & 0 deletions C++/graph/README.md
Original file line number Diff line number Diff line change
@@ -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:
48 changes: 48 additions & 0 deletions C++/graph/graph-implementation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//Here’s a simple implementation of a graph data structure in C++ using an adjacency list:

#include <iostream>
#include <unordered_map>
#include <list>

// Graph class representing an undirected graph
class Graph {
private:
// Adjacency list to store the graph
std::unordered_map<int, std::list<int>> 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;
}
Empty file added vertical_traversal.cpp
Empty file.