Download as pdf or txt
Download as pdf or txt
You are on page 1of 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