BFS DFS Prims
BFS DFS Prims
#include <bits/stdc++.h>
vector<int> res;
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
q.pop();
res.push_back(curr);
for (int i = 0; i < V; i++) {
visited[i] = true;
q.push(i);
return res;
int main() {
int V = 6;
{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}};
cout << "BFS Traversal starting from node " << src << ": ";
int n = ans.size();
return 0;
}
DFS
#include <iostream>
#include <stack>
#include <vector>
// DFS function
stack<int> st;
st.push(s);
visited[s] = true;
while (!st.empty()) {
st.pop();
res.push_back(curr);
visited[i] = true;
st.push(i);
}
int main() {
int V = 6;
int adj[6][6] = {
};
vector<int> dfs_result;
cout << "DFS Traversal starting from node " << src << ": ";
int n = dfs_result.size();
return 0;
}
Prims
#include <iostream>
#include <vector>
#include <climits>
key[0] = 0;
parent[0] = -1;
int u = -1;
u = v;
minKey = key[v];
}
mstSet[u] = true;
key[v] = adj[u][v];
parent[v] = u;
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;