0% found this document useful (0 votes)
10 views4 pages

2251012055 (1)

This document contains C++ code for implementing breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. It defines a Graph class with methods to add edges and perform BFS and DFS. A readGraphFromFile function parses an input file and builds the graph. The main function reads a graph file, performs a BFS or DFS from a source vertex, and prints the traversal order.

Uploaded by

tien.phamvan2103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

2251012055 (1)

This document contains C++ code for implementing breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. It defines a Graph class with methods to add edges and perform BFS and DFS. A readGraphFromFile function parses an input file and builds the graph. The main function reads a graph file, performs a BFS or DFS from a source vertex, and prints the traversal order.

Uploaded by

tien.phamvan2103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BFS

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <sstream>
#include <stdexcept>
using namespace std;
class Graph {
unordered_map<int, vector<int>>
adjList;
public:
void addEdge(int start, int end)
{

adjList[start].push_back(end);

adjList[end].push_back(start);
}
void BFS(int source) {
queue<int> q;
q.push(source);
unordered_set<int> visited;
visited.insert(source);
while (!q.empty()) {
int curr = q.front();
q.pop();
cout << curr << " ";
for (int neighbor :
adjList[curr]) {
if
(visited.find(neighbor) ==visited.end()) {

q.push(neighbor);

visited.insert(neighbor);
}
}
}
}
};
void readGraphFromFile(const string&
filename, Graph& graph) {
ifstream file(filename);
if (!file.is_open()) {
throw runtime_error("Failed to open file : " + filename);
}
string line;
if (!getline(file, line)) {
throw runtime_error("Invalid file format : " + filename);
}
istringstream iss(line);
int vertexCount;
if (!(iss >> vertexCount)) {
throw runtime_error("Invalid file format : " + filename);
}
for (int i = 0; i < vertexCount;
i++) {
int vertex;
if (!(file >> vertex)) {
throw
runtime_error("Invalid file format: " + filename);
}
}
int start, end;
while (file >> start >> end) {
graph.addEdge(start, end);
}
}
int main() {
Graph g;
try {

readGraphFromFile("graph.txt", g);
int sourceVertex = 0;
cout << "BFS traversal from source vertex" << sourceVertex
<<":\n";
g.BFS(sourceVertex);
}
catch (const exception& e) {
cerr << "Error: " <<
e.what() << endl;
return 1;
}
return 0;
}

DFS

#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <stdexcept>
using namespace std;
class Graph {
unordered_map<int, vector<int>>
adjList;
public:
void addEdge(int start, int end)
{

adjList[start].push_back(end);

adjList[end].push_back(start);
}
void DFS(int source) {
unordered_set<int> visited;
DFSHelper(source, visited);
}
private:
void DFSHelper(int vertex,
unordered_set<int>& visited) {
visited.insert(vertex);
cout << vertex << " ";
for (int neighbor :
adjList[vertex]) {
if
(visited.find(neighbor) ==visited.end()) {
DFSHelper(neighbor,
visited);
}
}
}
};
void readGraphFromFile(const string&
filename, Graph& graph) {
ifstream file(filename);
if (!file.is_open()) {
throw runtime_error("Failed to open file : " + filename);
}
string line;
if (!getline(file, line)) {
throw runtime_error("Invalid file format : " + filename);
}
istringstream iss(line);
int vertexCount;
if (!(iss >> vertexCount)) {
throw runtime_error("Invalid file format : " + filename);
}
for (int i = 0; i < vertexCount;
i++) {
int vertex;
if (!(file >> vertex)) {
throw
runtime_error("Invalid file format: " + filename);
}
}
int start, end;
while (file >> start >> end) {
graph.addEdge(start, end);
}
}
int main() {
Graph g;
try {

readGraphFromFile("graph.txt", g);
int sourceVertex = 0;
cout << "DFS traversal from source vertex " << sourceVertex <<
":\n";
g.DFS(sourceVertex);
}
catch (const exception& e) {
cerr << "Error: " <<
e.what() << endl;
return 1;
}
return 0;
}

You might also like