0% found this document useful (0 votes)
38 views

Daa Practical File Prabhjot

This document contains source code for 8 programs related to graph algorithms and data structures. Program 1 implements quicksort to sort an array of elements. Program 2 implements parallelized merge sort. Program 3 prints all nodes reachable from a starting node using BFS. Program 4 checks if a graph is connected using DFS. Program 5 finds the minimum spanning tree of an undirected graph using Kruskal's algorithm. Program 6 finds the minimum spanning tree using Prim's algorithm. Program 7 finds shortest paths from a source vertex to other vertices using Dijkstra's algorithm. Program 8 implements the all-pairs shortest paths problem using Floyd's algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Daa Practical File Prabhjot

This document contains source code for 8 programs related to graph algorithms and data structures. Program 1 implements quicksort to sort an array of elements. Program 2 implements parallelized merge sort. Program 3 prints all nodes reachable from a starting node using BFS. Program 4 checks if a graph is connected using DFS. Program 5 finds the minimum spanning tree of an undirected graph using Kruskal's algorithm. Program 6 finds the minimum spanning tree using Prim's algorithm. Program 7 finds shortest paths from a source vertex to other vertices using Dijkstra's algorithm. Program 8 implements the all-pairs shortest paths problem using Floyd's algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

PRACTICAL FILE

OF
DESIGN ANALYSIS AND ALGORITHM

(Session : 2021-2025)

UNIVERSITY INSTITUTE OF ENGINEERING


AND TECHNOLOGY, KURUKSHETRA UNIVERSITY

SUBMITTED TO SUBMITTED BY
Mrs. Sonia Saini Prabhjot Singh
CSE dept. 252102107
UIET,KUK CSE-B
4th Sem.
PROGRAM 1
AIM: Sort a given set of elements using the Quick sort method and determine
the time required to sort the elements. Repeat the experiment for different
values of n, the number of elements in the list to be sorted and plot a graph of
the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
SOURCE CODE:
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void printArray(int array[], int size) {
int i;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}
void quickSort(int array[], int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}
int main() {
int data[] = {8, 7, 6, 1, 0, 9, 2};
int n = sizeof(data) / sizeof(data[0]);

cout << "Unsorted Array: \n";


printArray(data, n);
quickSort(data, 0, n - 1);
cout << "Sorted array in ascending order: \n";
printArray(data, n);
}
OUTPUT:
PROGRAM 2
AIM: Using Open, implement a parallelized Merge Sort algorithm to sort a
given set of elements and determine the time required to sort the elements.
Repeat the experiment for different values of n, the number of elements in the
list to be sorted and plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.
SOURCE CODE:
#include <iostream>
using namespace std;
void merge(int arr[], int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], M[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
int i, j, k;
i = 0;
j = 0;
k = p;
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = M[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Unsorted array: \n";
printArray(arr, size);
mergeSort(arr, 0, size - 1);
cout << "Sorted array: \n";
printArray(arr, size);
return 0;
}
Output:
PROGRAM 3
AIM: Print all the nodes reachable from a given starting node in a digraph using
BFS method.
SOURCE CODE:
#include <iostream>
#include <bits\stdc++.h>
using namespace std;
void BFS(int node, unordered_map<int, list<int>> &adjacency_matrix,
unordered_map<int, bool> &visited, vector<int> &ans)
{
queue<int> q;
visited[node] = true;
q.push(node);
while (!q.empty())
{
int front = q.front();
q.pop();
ans.push_back(front);
for (auto neighbour : adjacency_matrix[front])
{
if (!visited[neighbour])
{
q.push(neighbour);
visited[neighbour] = true;
}
}
}
}
int main()
{
int vertex, edges;
cout << "Enter no of vertices and edges : ";
cin >> vertex >> edges;
unordered_map<int, list<int>> adjacency_matrix;
for (int i = 0; i < edges; i++)
{
int m, n;
cin >> m >> n;
adjacency_matrix[m].push_back(n);
adjacency_matrix[n].push_back(m);
}
vector<int> ans;
unordered_map<int, bool> visited;
for (int i = 0; i < vertex; i++)
{
if (!visited[i])
BFS(i, adjacency_matrix, visited, ans);
}
for (auto i : ans)
cout << i << " ";
cout << endl;
}
Output:
PROGRAM 4
AIM: Check whether a given graph is connected or not using DFS method.
SOURCE CODE:
#include<iostream>
#include<list>
using namespace std;
class Graph{ int numver;
list<int>* adjLists; bool* visited; public:
Graph(int ver);
void addedge(int src,int dest); void dfs(int vertex);
};
Graph::Graph(int ver){ numver=ver;
adjLists=new list<int>[ver]; visited=new bool[ver];
}
void Graph::addedge(int src,int dest){ adjLists[src].push_back(dest);
adjLists[dest].push_back(src);
}
void Graph::dfs(int vertex){
visited[vertex]=true;
cout<<"Vertex visited"<<vertex<<endl;
list<int>::iterator i; for(i=adjLists[vertex].begin();i!=adjLists[vertex].end();++i){
if(!visited[*i]){ dfs(*i);
}
}
}
int main(){ Graph g(4);
g.addedge(0,2);
g.addedge(0,1);
g.addedge(2,3);
g.addedge(1,0);
g.addedge(1,3);
g.addedge(3,2);
g.dfs(2);
return 0;
}
Output:
PROGRAM-5
AIM: Find Minimum Cost Spanning Tree of a given undirected graph using
Krushkal’s algorithm.
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
#define edge pair<int, int>
class Graph {
private:
vector<pair<int, edge> > G;
vector<pair<int, edge> > T;
int *parent;
int V;
public:
Graph(int V);
void AddWeightedEdge(int u, int v, int w);
int find_set(int i);
void union_set(int u, int v);
void kruskal();
void print();
};
Graph::Graph(int V) {
parent = new int[V];
for (int i = 0; i < V; i++)
parent[i] = i;
G.clear();
T.clear();
}
void Graph::AddWeightedEdge(int u, int v, int w) {
G.push_back(make_pair(w, edge(u, v)));
}
int Graph::find_set(int i) {
if (i == parent[i])
return i;
else
return find_set(parent[i]);
}
void Graph::union_set(int u, int v) {
parent[u] = parent[v];
}
void Graph::kruskal() {
int i, uRep, vRep;
sort(G.begin(), G.end());
for (i = 0; i < G.size(); i++) {
uRep = find_set(G[i].second.first);
vRep = find_set(G[i].second.second);
if (uRep != vRep) {
T.push_back(G[i]);
union_set(uRep, vRep);
}
}
}
void Graph::print() {
cout << "Edge :"
<< " Weight" << endl;
int sum =0;
for (int i = 0; i < T.size(); i++) {
cout << T[i].second.first << " - " << T[i].second.second << " : "
<< T[i].first;
cout << endl;
sum = sum + T[i].first;
}
cout << "Total weight/cost of the MST is " << sum << endl;
}
int main() {
Graph g(6);
g.AddWeightedEdge(0, 1, 2);
g.AddWeightedEdge(1, 2, 3);
g.AddWeightedEdge(1, 3, 2);
g.AddWeightedEdge(1, 0, 4);
g.AddWeightedEdge(3, 0, 3);
g.AddWeightedEdge(2, 1, 5);
g.AddWeightedEdge(3, 2, 3);
g.AddWeightedEdge(4, 5, 1);
g.AddWeightedEdge(3, 4, 6);
g.AddWeightedEdge(4, 2, 1);
g.AddWeightedEdge(3, 5, 5);
g.AddWeightedEdge(4, 0, 4);
g.AddWeightedEdge(4, 3, 2);
g.AddWeightedEdge(5, 2, 4);
g.AddWeightedEdge(5, 4, 3);
g.kruskal();
g.print();
return 0;
}
Output:
PROGRAM-6
AIM: Find Minimum Cost Spanning Tree of a given undirected graph using
Prim’s algorithm.
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
#define V 5
int minKey(int key[], bool mstSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
void printMST(int parent[], int graph[V][V])
{
cout << "Edge \tWeight\n";
int sum =0;
for (int i = 1; i < V; i++){
cout << parent[i] << " - " << i << " \t"<< graph[i][parent[i]] << " \n";
sum = sum + graph[i][parent[i]];
}
cout << "Weight of MST is " << sum;
}
void primMST(int graph[V][V])
{
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == false
&& graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, graph);
}
int main()
{
int graph[V][V] = { { 1, 0, 3 , 5, 1 },
{ 0, 4, 6, 7, 4 },
{ 6, 0, 1, 1, 2 },
{ 5, 4, 8, 9, 0 },
{ 1, 4, 9, 1, 2 } };
primMST(graph);
return 0;
}
Output:
PROGRAM-7
AIM: From a given vertex in a weighted connected graph, find shortest paths
to other vertices using Dijkstra’s algorithm
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
void dijkstra(int src, int n, unordered_map<int,list<pair<int,int>>> &adj){
vector<int> dist(n,1e9);
dist[src] = 0;
set<pair<int,int>> s;
s.insert({0,src});
while(!s.empty()){
auto top = *(s.begin());
s.erase(top);
for(auto nbr : adj[top.second]){
if(dist[top.second] + nbr.second < dist[nbr.first]){
s.erase({dist[nbr.first], nbr.first});
dist[nbr.first] = dist[top.second] + nbr.second;
s.insert({dist[nbr.first],nbr.first});
}
}
}
for(auto i : dist){
cout<<i<<" ";
}
}
int main(){
cout<<"enter no of vertices and edges : ";
int V,E;
cin>>V>>E;
unordered_map<int,list<pair<int,int>>> adj;
for(int i=0; i<E; i++){
int u,v,w;
cout<<"enter edges and weights : ";
cin>>u>>v>>w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
int src;
cout<<"enter source node : ";
cin>>src;
dijkstra(src, V, adj);
}
Output:
PROGRAM-8
AIM: - Implement All-Pairs Shortest Paths Problem using Floyd's algorithm.
SOURCE CODE:
#include"bits/stdc++.h"
using namespace std;
int main(){
cout<<"enter no of vertives and edges : ";
int V,E;
cin>>V>>E;
vector<vector<int> > edges(E,vector<int>(E,1e9));
for(int i=0;i<E;i++){
int u,v,w;
cout<<"Enter edges and their weights : ";
cin>>u>>v>>w;
edges[u][v] = w; }
vector<vector<int> > dist = edges;
for(int k=0;k<V;k++){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}}}
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(i == j)
dist[i][j] = 0;
cout<<dist[i][j]<<" ";
}
cout<<endl;
}}
Output:
PROGRAM-9
AIM: - Obtain the Topological ordering of vertices in a given digraph.
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
void usingDFS(int node, unordered_map<int,list<int>>&adj,
unordered_map<int,bool>&visited, stack<int>&temp){
visited[node] = true;
for(auto neighbour : adj[node]){
if(!visited[neighbour]){
usingDFS(neighbour, adj, visited, temp);
}
}
temp.push(node);
}
int main(){
int V,E;
cout<<"Enter no of vertices and edges : ";
cin>>V >> E;
unordered_map<int,list<int> > adj;
for(int i=0;i<E;i++){
int m,n;
cout<<"Enter the required edges : ";
cin>>m>>n;
adj[m].push_back(n);
}
vector<int> ans;
unordered_map<int,bool> visited;
stack<int> temp;
for(int i=1;i<V;i++){
if(!visited[i])
usingDFS(i, adj, visited, temp);
}
while(!temp.empty()){
ans.push_back(temp.top());
temp.pop();
}
for(auto i : ans){
cout<<i<<" ";
}
}
Output:
PROGRAM-10
AIM: - Implement a program to calculate total number of strongly connected
components in a digraph.
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
void topo(int node, unordered_map<int,list<int>>&adj,
unordered_map<int,bool>&visited, stack<int>&temp){
visited[node] = true;
for(auto neighbour : adj[node]){
if(!visited[neighbour]){
topo(neighbour, adj, visited, temp);
}
}
temp.push(node);
}
void DFS(int node, unordered_map<int,list<int>>&adj,
unordered_map<int,bool>&visited){
visited[node] = true;
for(auto neighbour : adj[node]){
if(!visited[neighbour]){
DFS(neighbour, adj, visited);
}}}
int main(){
int V,E;
cout<<"Enter no of vertices and edges : ";
cin>>V >> E;
unordered_map<int,list<int> > adj;
for(int i=0;i<E;i++){
int m,n;
cout<<"Enter the required edges : ";
cin>>m>>n;
adj[m].push_back(n);
}
unordered_map<int,bool> visited;
stack<int> temp;
for(int i=0;i<V;i++){
if(!visited[i])
topo(i, adj, visited, temp);
}
unordered_map<int, list<int> > transpose;
for(int i=0;i<V;i++){
visited[i] = 0;
for(auto nbr: adj[i]){
transpose[nbr].push_back(i);
}
}
int cnt = 0;
while(!temp.empty()){
int top = temp.top();
temp.pop();
if(!visited[top]){
cnt++;
DFS(top, transpose, visited);
}
}
cout<<"Number of connected components are "<<cnt;
}
Output:
PROGRAM-11
AIM: - From a given vertex in a weighted connected graph, find shortest paths
to other vertices using Bellman Ford algorithm.
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
void bellman(int src, int n, vector<vector<int>> &edges){
vector<int> dist(n,1e9);
dist[src] = 0;
for(int i=0; i<n-1; i++){
for(int j=0; j<edges.size(); j++){
int u = edges[j][0];
int v = edges[j][1];
int w = edges[j][2];
dist[v] = min(dist[v], dist[u] + w);
}
}
for(auto i : dist){
cout<<i<<" ";
}cout<<endl;
}
int main(){
int V,E;
cout<<"enter no of vertives and edges : ";
cin>>V>>E;
vector<vector<int>> edges;
for(int i=0; i<E; i++){
cout<<"enter edges and their weights : ";
int u,v,w;
cin>>u>>v>>w;
edges.push_back({u,v,w});
}
int src;
cout<<"enter source node : ";
cin>>src;
bellman(src, V, edges);
}
Output:
PROGRAM-12
AIM: - Compute the transitive closure of a given directed graph using
Warshall's algorithm.
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"enter no of vertives and edges : ";
int V,E;
cin>>V>>E;
vector<vector<int> > edges(E,vector<int>(E,1e9));
for(int i=0;i<E;i++){
int u,v,w;
cout<<"Enter edges and their weights : ";
cin>>u>>v>>w;
edges[u][v] = w;
}
vector<vector<int> > dist = edges;
for(int k=0;k<V;k++){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(dist[i][j] != 1e9 || i == j){
cout<<1<<" ";
}
else{
cout<<0<<" ";
}
}
cout<<endl;
}
}
Output:

You might also like