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

Create Adjacency_list_representation.cpp #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
93 changes: 93 additions & 0 deletions graph/Adjacency_list_representation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <iostream>
#include <vector>
#include <list>
#include <queue>

class Graph {
private:
int V; // Number of vertices
std::vector<std::list<int>> adj; // Adjacency list representation

public:
Graph(int vertices) {
V = vertices;
adj.resize(V);
}

void addEdge(int u, int v) {
adj[u].push_back(v); // Add edge from u to v
adj[v].push_back(u); // Uncomment this line for undirected graph
}

void displayGraph() {
for (int i = 0; i < V; ++i) {
std::cout << "Vertex " << i << ": ";
for (int neighbor : adj[i]) {
std::cout << neighbor << " ";
}
std::cout << std::endl;
}
}

void bfs(int start) {
std::vector<bool> visited(V, false);
std::queue<int> q;

visited[start] = true;
q.push(start);

while (!q.empty()) {
int current = q.front();
q.pop();
std::cout << current << " ";

for (int neighbor : adj[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
std::cout << std::endl;
}

void dfsUtil(int v, std::vector<bool>& visited) {
visited[v] = true;
std::cout << v << " ";

for (int neighbor : adj[v]) {
if (!visited[neighbor]) {
dfsUtil(neighbor, visited);
}
}
}

void dfs(int start) {
std::vector<bool> visited(V, false);
dfsUtil(start, visited);
std::cout << std::endl;
}
};

int main() {
Graph g(5); // Create a graph with 5 vertices

g.addEdge(0, 1);
g.addEdge(0, 4);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 3);
g.addEdge(3, 4);

std::cout << "Graph adjacency list:" << std::endl;
g.displayGraph();

std::cout << "BFS starting from vertex 0:" << std::endl;
g.bfs(0);

std::cout << "DFS starting from vertex 0:" << std::endl;
g.dfs(0);

return 0;
}