Lab Manual: Meerut Institute of & Technology, Meerut
Lab Manual: Meerut Institute of & Technology, Meerut
Lab Manual
(RCS-552)
Employ various design strategies for problem solving and Implement various algorithms in ahigh
level language.
Synthesize divide-and-conqueralgorithms.
Identify the problem given and design the algorithm using various algorithm designtechniques.
PROGRAM:
#include <bits/stdc++.h>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
OUTPUT :
Sorted array:
1 5 7 8 9 10
EXPERIMENT- 2
PROGRAM :
using System;
class GFG {
countsort(arr);
OUTPUT
Sorted character array is cginnoorsttu
EXPERIMENT-3
Object: Write a program to implement Merge Sort on an unsortedarray
PROGRAM:
using System;
class MergeSort
{
void merge(int[] arr, int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
int i, j;
OUTPUT:
Given array is 23 45 6 42 8 3
Sorted array is 3 6 8 23 42 45
EXPERIMENT -4
Object: Write a program to implement Warshall algorithm
PROGRAM:
using System;
public class AllPairShortestPath
{
readonly static int INF = 99999, V = 4;
printSolution(dist);
}
OUTPUT :
Following matrix shows the shortest distances between every pair of vertices
0 5 8 9
INF 0 3 4
INF INF 0 1
INF INF INF 0
EXPERIMENT -5
Object: Write a program to Knapsack problem
PROGRAM:
using System ;
class GFG {
w = W;
for (i = n; i> 0 && res > 0; i--) {
OUTPUT:
220
20 30
EXPERIMENT-6
Object:Write a program to implement Shortest paths algorithm
PROGRAM:
using System;
using System.Collections.Generic;
class pathUnweighted{
addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
addEdge(adj, 1, 2);
addEdge(adj, 3, 4);
addEdge(adj, 3, 7);
addEdge(adj, 4, 5);
addEdge(adj, 4, 6);
addEdge(adj, 4, 7);
addEdge(adj, 5, 6);
addEdge(adj, 6, 7);
int source = 0, dest = 7;
printShortestDistance(adj, source,dest, v);
}
private static void addEdge(List<List<int>> adj,int i, int j)
{
adj[i].Add(j);
adj[j].Add(i);
}
private static void printShortestDistance(List<List<int>> adj,int s, int dest, int v)
{
int []pred = new int[v];
int []dist = new int[v];
Console.WriteLine("Path is ::");
for (int i = path.Count - 1;
i >= 0; i--)
{
Console.Write(path[i] + " ");
}
}
private static bool BFS(List<List<int>> adj,int src, int dest,int v, int []pred,int []dist)
{
List<int> queue = new List<int>();
bool []visited = new bool[v];
for (int i = 0; i < v; i++)
{
visited[i] = false;
dist[i] = int.MaxValue;
pred[i] = -1;
}
visited[src] = true;
dist[src] = 0;
queue.Add(src);
while (queue.Count != 0)
{
int u = queue[0];
queue.RemoveAt(0);
for (int i = 0;
i < adj[u].Count; i++)
{
if (visited[adj[u][i]] == false)
{
visited[adj[u][i]] = true;
dist[adj[u][i]] = dist[u] + 1;
pred[adj[u][i]] = u;
queue.Add(adj[u][i]);
if (adj[u][i] == dest)
return true;
}
}
}
return false;
}
}
EXPERIMENT-7
Object: Write a program to Minimum Cost Spanning Tree.
PROGRAM:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 5
int minKey(int key[], bool mstSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
return min_index;
}
int printMST(int parent[], int graph[V][V])
{
printf("Edge \tWeight\n");
for (int i = 1; i< V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
void primMST(int graph[V][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++) {
return 0;
}
OUTPUT:
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
EXPERIMENT-8
Object: Write a program to implement Tree Traversals.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
return (node);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
getchar();
return 0;
}
OUTPUT:
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231
EXPERIMENT-9
Object: Write a program to Sum of Subsets Problem
PROGRAM:
using System;
class GFG
{
static int subsetSum(int []a, int n, int sum)
{
if (n <= 0)
return 0;
if (tab[n - 1,sum] != -1)
return tab[n - 1,sum];
if (a[n - 1] > sum)
return tab[n - 1,sum]
= subsetSum(a, n - 1, sum);
else {
if (subsetSum(a, n - 1, sum) != 0
|| subsetSum(a, n - 1, sum - a[n - 1])!= 0) {
return tab[n - 1,sum] = 1;
}
else
return tab[n - 1,sum] = 0;
}
}
public static void Main(String[] args)
{
int n = 5;
int []a = { 1, 5, 3, 7, 4 };
int sum = 12;
if (subsetSum(a, n, sum) != 0) {
Console.Write("YES\n");
}
else
Console.Write("NO\n");
}
}
OUTPUT: Yes
EXPERIMENT-10
Object: Write a program to Travelling Sales Person Problem.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
intsmallest(int);
voidminimumcost(int);
inta[10][10], visited[10];
intn, cost=0;
voidmain()
{
inti, j;
printf("enter the no. of cities\n");
scanf("%d",&n);
printf("enter matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j>n;j++)
{
scanf("%d",&a[i][j]);
}
visited[i] = 0;
}
printf("the path is\n");
minimumcost(0);
printf("minimum cost = %d",cost);
}
voidminimumcost(intcity)
{
inti, nextcity;
visited[city] = 1;
printf("%d-->",city+1);
nextcity = smallest(city);
if(nextcity == 999)
{
nextcity = 0;
printf("%d",nextcity+1);
cost = cost + a[city][nextcity];
return;
}
minimumcost(nextcity);
}
intsmallest(intc)
{
inti, nc=999;
intminimum = 999, dmin;
for(i=0;i<n;i++)
{
if(a[c][i] != 0&& visited[i] == 0)
{
if(a[c][i] + a[i][c] < minimum)
{
minimum = a[i][0] + a[c][i];
dmin = a[c][i];
nc = i;
}
}
}
if(minimum != 999)
{
cost += dmin;
}
returnnc;
}
OUTPUT:
enter the no. of cities
4
enter matrix
0
1
5
7
4
0
6
5
7
8
0
1
7
6
3
0
the path is
1-->2-->4-->3-->1
minimum cost = 16
EXPERIMENT-11
Object: Write a program to All Pairs Shortest Paths.
PROGRAM:
using System;
class GFG{
OUTPUT: 20