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

BFS DFS Prims

The document contains implementations of three graph algorithms: Breadth-First Search (BFS), Depth-First Search (DFS), and Prim's Minimum Spanning Tree (MST). Each algorithm is implemented using an adjacency matrix for a graph with 6 vertices, and the main function demonstrates their usage by performing traversals and displaying results. The BFS and DFS functions utilize queues and stacks respectively, while Prim's algorithm constructs the MST by selecting edges with the minimum weight.
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 views9 pages

BFS DFS Prims

The document contains implementations of three graph algorithms: Breadth-First Search (BFS), Depth-First Search (DFS), and Prim's Minimum Spanning Tree (MST). Each algorithm is implemented using an adjacency matrix for a graph with 6 vertices, and the main function demonstrates their usage by performing traversals and displaying results. The BFS and DFS functions utilize queues and stacks respectively, while Prim's algorithm constructs the MST by selecting edges with the minimum weight.
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/ 9

BFS

#include <bits/stdc++.h>

using namespace std;

vector<int> bfsOfGraph(int adj[6][6], int V, int s) {

vector<int> res;

queue<int> q;

vector<bool> visited(V, false);

visited[s] = true;

q.push(s);

while (!q.empty()) {

int curr = q.front();

q.pop();

res.push_back(curr);
for (int i = 0; i < V; i++) {

if (adj[curr][i] == 1 && !visited[i]) {

visited[i] = true;

q.push(i);

return res;

int main() {

int V = 6;

int adj[6][6] = { {0, 1, 1, 1, 0, 0},

{1, 0, 0, 1, 0, 0},

{1, 0, 0, 0, 1, 0},

{1, 1, 0, 0, 0, 1},

{0, 0, 1, 0, 0, 0},

{0, 0, 0, 1, 0, 0}};

int src = 0; // Starting node for BFS

vector<int> ans = bfsOfGraph(adj, V, src);

cout << "BFS Traversal starting from node " << src << ": ";
int n = ans.size();

for (int i = 0; i < n; i++) {

cout << ans[i] << " ";

cout << endl;

return 0;

}
DFS
#include <iostream>

#include <stack>

#include <vector>

using namespace std;

// DFS function

void dfsOfGraph(int adj[6][6], int V, int s, vector<bool>& visited, vector<int>& res) {

stack<int> st;

st.push(s);

visited[s] = true;

while (!st.empty()) {

int curr = st.top();

st.pop();

res.push_back(curr);

// Explore the adjacent nodes

for (int i = 0; i < V; i++) {

if (adj[curr][i] == 1 && !visited[i]) {

visited[i] = true;

st.push(i);

}
int main() {

int V = 6;

// Adjacency matrix of the graph

int adj[6][6] = {

{0, 1, 1, 1, 0, 0}, // Node 0 is connected to 1, 2, 3

{1, 0, 0, 1, 0, 0}, // Node 1 is connected to 0, 3

{1, 0, 0, 0, 1, 0}, // Node 2 is connected to 0, 4

{1, 1, 0, 0, 0, 1}, // Node 3 is connected to 1, 5

{0, 0, 1, 0, 0, 0}, // Node 4 is connected to 2

{0, 0, 0, 1, 0, 0} // Node 5 is connected to 3

};

int src = 0; // Starting node for DFS

// Vector to store the DFS result

vector<int> dfs_result;

// Visited array to mark visited nodes

vector<bool> visited(V, false);

// Perform DFS starting from node src

dfsOfGraph(adj, V, src, visited, dfs_result);

// Output the DFS traversal result using index-based for loop

cout << "DFS Traversal starting from node " << src << ": ";

int n = dfs_result.size();

for (int i = 0; i < n; i++) { // Use index-based loop


cout << dfs_result[i] << " ";

cout << endl;

return 0;

}
Prims
#include <iostream>

#include <vector>

#include <climits>

using namespace std;

void primMST(int adj[6][6], int V) {

vector<int> parent(V, -1);

vector<int> key(V, INT_MAX);

vector<bool> mstSet(V, false);

key[0] = 0;

parent[0] = -1;

for (int count = 0; count < V - 1; count++) {

int u = -1;

int minKey = INT_MAX;

for (int v = 0; v < V; v++) {

if (!mstSet[v] && key[v] < minKey) {

u = v;

minKey = key[v];

}
mstSet[u] = true;

for (int v = 0; v < V; v++) {

if (adj[u][v] != 0 && !mstSet[v] && adj[u][v] < key[v]) {

key[v] = adj[u][v];

parent[v] = u;

cout << "Edge \tWeight\n";

for (int i = 1; i < V; i++) {

cout << parent[i] << " - " << i << "\t" << adj[i][parent[i]] << endl;

int main() {

int V = 6;

int adj[6][6] = {

{0, 1, 2, 3, 0, 0},

{1, 0, 0, 4, 0, 0},

{2, 0, 0, 0, 5, 0},

{3, 4, 0, 0, 0, 6},
{0, 0, 5, 0, 0, 0},

{0, 0, 0, 6, 0, 0}

};

primMST(adj, V);

return 0;

You might also like