0% found this document useful (0 votes)
2 views

Graph Program

The document contains a C++ implementation of a graph data structure with methods for adding edges, printing the graph, and performing depth-first search (DFS) and breadth-first search (BFS) traversals. It defines a Graph class that manages an adjacency list and includes a main function to demonstrate its functionality with a sample graph of 11 vertices. The implementation showcases the use of lists for adjacency representation and includes memory management for visited nodes.

Uploaded by

Monette Loy-a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Graph Program

The document contains a C++ implementation of a graph data structure with methods for adding edges, printing the graph, and performing depth-first search (DFS) and breadth-first search (BFS) traversals. It defines a Graph class that manages an adjacency list and includes a main function to demonstrate its functionality with a sample graph of 11 vertices. The implementation showcases the use of lists for adjacency representation and includes memory management for visited nodes.

Uploaded by

Monette Loy-a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Graph Program

#include <list>
#include <iostream>
using namespace std;
class Graph {
private:
int V;
list<int>* adj;
void DFSUtil(int v, bool visited[]);
public:
Graph(int);
void addEdge(int u, int v);
void printGraph();
void DFS(int v);
void BFS(int s);
};
Graph::Graph(int x)
{
V = x;
adj = new list<int>[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
}
void Graph::printGraph()
{
cout << "Adjacency List..." << endl;
for (int v = 0; v < V; ++v)
{
cout << "V[" << v << "]";
for (auto x : adj[v])
cout << " -> " << x;
cout << endl;
}
}
void Graph::DFS(int v)
{
// Mark all the vertices as not visited
bool* visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to print DFS traversal
DFSUtil(v, visited);
}
void Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
cout << v << " ";

// Recur for all the vertices adjacent to this vertex


list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}
// BFS traversal from source vertex `s`
void Graph::BFS(int s) {
bool* visited = new bool[V](); // Initialize visited array
list<int> queue; // Queue for BFS

visited[s] = true;
queue.push_back(s);

list<int>::iterator i;
while (!queue.empty()) {
s = queue.front();
cout << s << " ";
queue.pop_front();

for (i = adj[s].begin(); i != adj[s].end(); ++i) {


if (!visited[*i]) {
visited[*i] = true;
queue.push_back(*i);
}
}
}
cout << endl;
delete[] visited; // Free allocated memory
}
int main()
{
Graph g(11); // Creates a graph with 11 vertices (0 to 10)

g.addEdge(0, 1);
g.addEdge(0, 5);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(1, 5);
g.addEdge(2, 4);
g.addEdge(4, 3);
g.addEdge(5, 6);
g.addEdge(6, 8);
g.addEdge(7, 3);
g.addEdge(7, 8);
g.addEdge(8, 10);
g.addEdge(9, 4);
g.addEdge(9, 7);
g.addEdge(9, 10);

g.printGraph(); // Prints the adjacency list representation

cout << "BFS starting from node 0: ";


g.BFS(0);

cout << "DFS starting from node 0: ";


g.DFS(0);

system("pause"); // Pauses execution (Windows-specific)


}

You might also like