Professional Documents
Culture Documents
Assignment 9
Assignment 9
Nilesh Kumar
06116403222
Btech CSE
USICT
CODE:
#include <iostream>
#include <queue>
#include <vector>
struct Node {
int data;
vector<Node*> neighbors;
bool visited;
Node(int value) : data(value), visited(false) {}
};
queue<Node*> q;
start->visited = true;
q.push(start);
while (!q.empty()) {
q.pop();
// Process the current node
cout << current->data << " ";
// If the neighbor is not visited, mark it as visited and push it to the queue
if (!neighbor->visited) {
neighbor->visited = true;
q.push(neighbor);
int main() {
node1->neighbors.push_back(node3);
node2->neighbors.push_back(node4);
node3->neighbors.push_back(node4);
node3->neighbors.push_back(node5);
node4->neighbors.push_back(node5);
BFS(node1);
// Freeing memory
delete node1;
delete node2;
delete node3;
delete node4;
delete node5;
return 0;
OUTPUT:
Question-2 WAP to implement DFS traversal.
CODE:
#include <iostream>
#include <vector>
#include <stack>
vector<Node*> neighbors;
bool visited;
start->visited = true;
s.push(start);
s.pop();
// If the neighbor is not visited, mark it as visited and push it to the stack
if (!neighbor->visited) {
neighbor->visited = true;
s.push(neighbor);
}
}
}
}
int main() {
return 0;
}
OUTPUT:
CODE:
#include<iostream>
{
int min = 999, min_index; // 999 represents an Infinite value
min_index = v;
}
}
return min_index;
}
{
int minCost=0;
cout<<"Edge \tWeight\n";
for (int i = 1; i< V; i++) {
cout<<parent[i]<<" - "<<i<<" \t"<<cost[i][parent[i]]<<" \n";
minCost+=cost[i][parent[i]];
}
cout<<"Total cost is"<<minCost;
}
{
int parent[V], key[V];
bool visited[V];
{
// cost[u][v] is non zero only for adjacent vertices of u //
visited[v] is false for vertices not yet included in MST // key[]
gets updated only if cost[u][v] is smaller than key[v] if
(cost[u][v]!=0 && visited[v] == false && cost[u][v] < key[v]) {
parent[v] = u;
key[v] = cost[u][v];
}
}
}
// print the final MST
print_MST(parent, cost);
}
// main function
int main()
{
int cost[V][V];
cout<<"Enter the vertices for a graph with 6 vetices";
for (int i=0;i<V;i++)
{
for(int j=0;j<V;j++)
{
cin>>cost[i][j];
}
find_MST(cost);
return 0;
OUTPUT:
* Time Complexity for Prim's algorithm
Prim's has a Time Complexity of O(V^2) , V being the number if
vertices and it can be improved upto O(ElogV) using Fibonacci Heaps.
* Krushkal algorithm
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
};
while (i != id[i]) {
i = id[i];
}
return i;
id[i] = j;
int count = 0;
int i = 0;
while (count < V - 1 && i < edges.size()) {
int src = edges[i].src;
int dest = edges[i].dest;
MST.push_back(edges[i]);
unionSet(src, dest, id);
count++;
}
i++;
return MST;
int main() {
int V = 6; // number of vertices in the graph
vector<Edge> edges = {
{0, 1, 4},
{0, 2, 3},
{1, 2, 1},
{1, 3, 2},
{2, 3, 4},
{3, 4, 2}
};
displayMST(MST);
return 0;
}
OUTPUT:
* Time Complexity for Krushkal algorithm
The time complexity of Kruskal's Algorithm is O(ElogV), where E is the
number of edges in the graph. This complexity is because the
algorithm uses a priority queue with a time complexity of O(logE).
However, the space complexity of the algorithm is O(E), which is
relatively high.
* Prim's Algorithm