CS3401- Algorithms
CS3401- Algorithms
1. Implement Linear Search. Determine the time required to search for an element.
Repeat the experiment for different values of n, the number of elements in the list
to be searched and plot a graph of the time taken versus n.
AIM:
To write a C program to implement Linear Search. Determine the time required to search
for an element. Repeat the experiment for different values of n, the number of elements in
the list to be searched and plot a graph of the time taken versus n
ALGORITHM:
Step 5: Go to Step 2
Step 6: Printf Element x Found at index i and go to step 8
Step 7: Printf element not found
Step 8: Exit
PROGRAM:
#include <stdio.h>
#include <time.h>
int linearSearch(int arr[], int n, int x)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
{
return i;
}
}
return -1;
}
int main()
{ int n, x, pos;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: "); scanf("%d", &x);
clock_t start = clock();
pos = linearSearch(arr, n, x); clock_t end = clock();
if (pos == -1) {
printf("Element not found.\n");
}
else
{
printf("Element found at position %d.\n", pos);
}
return 0;
}
OUTPUT:
c1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Enter the number of elements: 12
Enter 12 elements:
2
3
6
9
10
13
16
26
26
15
56
45
Enter the element to search: 56
Element found at position 10.
Time taken: 0.000005 seconds.
Time N
0.000005 5
0.000002 7
0.000003 8
0.000005 10
0.000005 12
Graph:
RESULT:
Thus the C program for linear search algorithm was executed successfully and the output
was verified.
2. Implement recursive Binary Search. Determine the time required to search an
element. Repeat the experiment for different values of n, the number of elements
in the list to be searched and plot a graph of the time taken versus n.
AIM :
To write a C program to implement recursive Binary Search. Determine the time required
to search an element. Repeat the experiment for different values of n, the number of
elements in the list to be searched and plot a graph of the time taken versus n.
ALGORITHM:
Step 1: Compare x with the middle element.
Step 2: If x matches with the middle element, we return the mid index.
Step 3: Else If x is greater than the mid element, then x can only lie in the right half subarray
after the mid element. So, we recur for the right half.
Step 4: Else (x is smaller) recur for the left half.
PROGRAM:
#include<stdio.h>
#include <time.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
{
return mid;
}
if (arr[mid] > x) {
return binarySearch(arr, l, mid - 1, x);
}
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main()
{
int n, x, pos;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements in sorted order:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &x);
clock_t start = clock();
pos = binarySearch(arr, 0, n - 1, x);
clock_t end = clock();
if (pos == -1) {
printf("Element not found.\n");
} else {
printf("Element found at position %d.\n", pos);
}
double time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time taken: %lf seconds.\n", time_taken);
return 0;
}
OUTPUT:
c1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Enter the number of elements: 6
Enter 6 elements in sorted order:
8
10
12
14
15
16
Enter the element to search: 15
Element found at position 4.
Time taken: 0.000001 seconds.
RESULT:
Thus the C program for binary search algorithm was executed successfully and the output
was verified.
3. Given a text txt [0...n-1] and a pattern pat [0...m-1], write a function search (char
pat [ ], char txt [ ]) that prints all occurrences of pat [ ] in txt [ ]. You may assume
that n > m.
AIM :
To write a C program to given a text txt [0...n-1] and a pattern pat [0...m-1], write a function
search (char pat [ ], char txt [ ]) that prints all occurrences of pat [ ] in txt [ ]. You may
assume that n > m.
ALGORITHM:
Step 1: We start the comparison of pat[j] with j = 0 with characters of the current window
of text.
Step 2: We keep matching characters txt[i] and pat[j] and keep incrementing i and j while
pat[j] and txt[i] keep matching.
Step 3: When we see a mismatch
1) We know that characters pat[0..j-1] match with txt[i-j…i-1] (Note that j starts with 0 and
increments it only when there is a match).
2) We also know (from the above definition) that lps[j-1] is the count of
characters of pat[0…j-1] that are both proper prefix and suffix.
3) From the above two points, we can conclude that we do not need to match these lps[j-1]
characters with txt[i-j…i-1] because we know that these characters will anyway match. Let
us consider the above example to understand this.
PROGRAM:
#include <stdio.h>
#include <string.h>
void search(char pat[], char txt[])
{
int m = strlen(pat);
int n = strlen(txt);
int main()
{
char txt[] = "AABAACAADAABAAABAA";
char pat[] = "AABA"; search(pat, txt);
return 0;
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Pattern found at position 0.
Pattern found at position 9.
Pattern found at position 13.
RESULT:
Thus, the C program for Pattern search algorithm was executed successfully and the
output was verified.
4. Sort a given set of elements using the Insertion sort and Heap sort methods 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.
AIM:
To write a c program to sort a given set of elements using the Insertion sort and Heap sort
methods 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.
*a = *b;
*b = temp;
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements in sorted order:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
clock_t start = clock();
heapSort(arr, N);
clock_t end = clock();
printf("Sorted array is\n");
printArray(arr, N);
double time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time taken: %lf seconds.\n", time_taken);
return 0;
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gedit heap.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc heap.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Enter the number of elements: 5
Enter 5 elements:
12
11
13
5
6
Sorted array is
5 6 7 11 12 13
Time taken: 0.000008 seconds.
Time N
0.000008 5
0.000008 7
0.000003 8
0.000002 10
0.000005 12
RESULT:
Thus, the c program for Insertion sort and Heap sort was executed successfully and the
output was verified.
GRAPH ALGORITHMS
5. Develop a program to implement graph traversal using Breadth First
Search
AIM :
To write a c program to develop a program to implement graph traversal using Breadth
First Search.
ALGORITHM:
PROGRAM:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50
Graph* Graph_create(int V)
{
Graph* g = malloc(sizeof(Graph)); g->V = V;
void Graph_destroy(Graph* g)
{
free(g);
}
int queue[MAX_VERTICES];
int main()
{
Graph* g = Graph_create(4); Graph_addEdge(g, 0, 1);
Graph_addEdge(g, 0, 2);
Graph_addEdge(g, 1, 2);
Graph_addEdge(g, 2, 0);
Graph_addEdge(g, 2, 3);
Graph_addEdge(g, 3, 3);
printf("Following is Breadth First Traversal " "(starting from vertex 2) \n");
Graph_BFS(g, 2);
Graph_destroy(g);
return 0;
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc bfs.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Following is Breadth First Traversal (starting from vertex 2)
2031
RESULT:
Thus the C program for breadth first search algorithm was executed successfully and
the output was verified.
6. Develop a program to implement graph traversal using Depth First Search
AIM :
To write a C program to develop a program to implement graph traversal using Depth
First Search.
ALGORITHM:
Step 1: Start by putting any one of the graph’s vertices at the back of the queue.
Step 2: Now take the front item of the queue and add it to the visited list.
Step 3: Create a list of that vertex's adjacent nodes. Add those which are not within
the visited list to the rear of the queue.
Step 4: Keep continuing steps two and three till the queue is empty.
Step 5: Stop the program
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int sourceV,Vertex,Edge,time,visited[10],Graph[10][10];
void DepthFirstSearch(int i)
{
int j; visited[i]=1;
printf(" %d->",i++);
for(j=0;j<Vertex;j++)
{
if(Graph[i][j]==1&&visited[j]==0)
DepthFirstSearch(j);
}
}
int main()
{
int i,j,vertex1,vertex2;
printf("\t\t\tGraphs\n");
printf("Enter no. of edges:");
scanf("%d",&Edge);
printf("Enter no. of vertices:");
scanf("%d",&Vertex); for(i=0;i<Vertex;i++)
{
for(j=0;j<Vertex;j++)
Graph[i][j]=0;
}
for(i=0;i<Edge;i++)
{
printf("Enter the edges in V1 V2 : ");
scanf("%d%d",&vertex1,&vertex2);
Graph[vertex1-1][vertex2-1]=1;
}
for(i=0;i<Vertex;i++)
{
for(j=0;j<Vertex;j++)
printf(" %d ",Graph[i][j]);
printf("\n");
}
printf("Enter source Vertex: ");
scanf("%d",&sourceV);
DepthFirstSearch(sourceV-1);
return 0;
}
OUTPUT:
c1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc dfs.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Graphs
Enter no. of edges:6
Enter no. of vertices:5
Enter the edges in V1 V2 : 1 2
Enter the edges in V1 V2 : 1 3
Enter the edges in V1 V2 : 2 5
Enter the edges in V1 V2 : 3 4
Enter the edges in V1 V2 : 2 4
Enter the edges in V1 V2 : 4 5
0 1 1 0 0
0 0 0 1 1
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0
Enter source Vertex: 1
0-> 3-> 4->
RESULT:
Thus the C program for depth first search algorithm was executed successfully and the
output was verified.
7. From a given vertex in a weighted connected graph, develop a program to find the
shortest paths to other vertices using Dijkstra’s algorithm.
AIM :
To write a c program from a given vertex in a weighted connected graph, develop a
program to find the shortest paths to other vertices using Dijkstra’s algorithm.
ALGORITHM:
Step 1: Mark the source node with a current distance of 0 and the rest with infinity.
Step 2: Set the non-visited node with the smallest current distance as the current node.
Step 3: For each neighbor, N of the current node adds the current distance of the
adjacent node with the weight of the edge connecting 0->1. If it is smaller than the
current distance of Node, set it as the new current distance of N.
Step 4: Mark the current node 1 as visited.
Step 5: Go to step 2 if there are any nodes are unvisited .
PROGRAM:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 9
printSolution(dist);
}
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc dj.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
RESULT:
Thus the C program for depth first search algorithm was executed successfully and the
output was verified.
8. Find the minimum cost spanning tree of a given undirected graph using Prim’s
algorithm.
AIM:
To write a C program to find the minimum cost spanning tree of a given undirected
graph using Prim’s algorithm.
ALGORITHM:
PROGRAM:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 5
return min_index;
}
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] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
}
OUTPUT:
RESULT:
Thus, the C program for depth first search algorithm was executed successfully and the
output was verified.
9. Implement Floyd’s algorithm for the All-Pairs- Shortest- Paths problem.
AIM :
To write a C program to Implement Floyd’s algorithm for the All-Pairs- Shortest-
Paths problem.
ALGORITHM:
Step 1: Initialize the solution matrix same as the input graph matrix as a first step.
Step 2: Then update the solution matrix by considering all vertices as an
intermediate vertex.
Step 3: The idea is to one by one pick all vertices and updates all shortest paths
which include the picked vertex as an intermediate vertex in the shortest path.
Step 4: When we pick vertex number k as an intermediate vertex, we already
have considered vertices {0, 1, 2, .. k-1} as intermediate vertices.
Step 5: For every pair (i, j) of the source and destination vertices respectively, there are
two possible cases.
• k is not an intermediate vertex in shortest path from i to j. We keep the
value of dist[i][j] as it is.
• k is an intermediate vertex in shortest path from i to j. We update the
value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] +
dist[k][j]
PROGRAM:
#include <stdio.h>
#define V 4
#define INF 99999
int main()
{
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
floydWarshall(graph);
return 0;
}
OUTPUT:
RESULT:
Thus, the C program for Floyd’s algorithm was executed successfully and the output
was verified.
10. Compute the transitive closure of a given directed graph using Warshall's
algorithm
AIM:
To write a C program to compute the transitive closure of a given directed graph using
Warshall's algorithm.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<math.h>
int max(int, int);
void warshal(int p[10][10], int n)
{
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
p[i][j] = max(p[i][j], p[i][k] && p[k][j]);
}
int max(int a, int b)
{
if (a > b)
return (a);
else
return (b);
}
void main()
{
int p[10][10] = { 0 }, n, e, u, v, i, j;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
printf("\n Enter the number of edges:");
scanf("%d", &e);
for (i = 1; i <= e; i++)
{
printf("\n Enter the end vertices of edge %d:", i);
scanf("%d%d", &u, &v);
p[u][v] = 1;
}
printf("\n Matrix of input data: \n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
printf("%d\t", p[i][j]);
printf("\n");
}
warshal(p, n);
printf("\n Transitive closure: \n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
printf("%d\t", p[i][j]);
printf("\n");
}
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc war.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Transitive closure:
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
RESULT:
Thus the C program for Warshall's algorithm was executed successfully and the output
was verified.
ALGORITHM DESIGN TECHNIQUES:
11. Develop a program to find out the maximum and minimum numbers in a
given list of n numbers using the divide and conquer technique.
AIM :
To write a C program to develop a program to find out the maximum and
minimum numbers in a given list of n numbers using the divide and conquer
technique.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<stdio.h>
int max, min;
int a[100];
int main ()
{
int i, num;
printf ("\nEnter the total number of numbers : ");
scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=1;i<=num;i++)
scanf ("%d",&a[i]);
max = a[0];
min = a[0];
maxmin(1, num);
printf ("Minimum element in an array : %d\n", min);
printf ("Maximum element in an array : %d\n", max);
return 0;
}
OUTPUT:
c1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc divide.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
RESULT:
Thus, the C program for finding maximum and minimum numbers in a given list of n
numbers using the divide and conquer technique was executed successfully and the
output was verified.
12. Implement Merge sort and Quick sort methods to sort an array of elements
and determine the time required to sort. 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
AIM:
To write a C program to implement Merge sort and Quick sort methods to sort an array
of elements and determine the time required to sort. 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.
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left >
right return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1,
right) merge(array, left, mid,
right)
step 4: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[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 A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
clock_t start = clock();
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
clock_t end = clock();
double time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time taken: %lf seconds.\n", time_taken);
return 0;
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc merge.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Enter the number of elements: 6
Enter 6 elements:
12
11
13
5
6
7
Sorted array is
5 6 7 11 12 13
Time taken: 0.000006 seconds.
Time N
0.000006 6
0.000008 7
0.000003 8
0.000002 10
0.000005 12
ALGORITHM (QUICK SORT):
Step 3: Recursively call the quicksort for the left and the right part of the pi.
#include <stdio.h>
#include <time.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
clock_t start = clock();
quickSort(arr, 0, N - 1);
printf("Sorted array: \n");
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
clock_t end = clock();
double time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\nTime taken: %lf seconds.\n", time_taken);
return 0;
}
OUTPUT:
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc quick.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
Enter the number of elements: 6
Enter 6 elements:
8
9
10
5
1
7
Sorted array:
1 5 7 8 9 10
Time taken: 0.000005 seconds.
Time N
0.000005 6
0.000008 7
0.000003 8
0.000004 10
0.000005 12
RESULT:
Thus, the C program Merge sort and Quick sort methods for was executed
successfully and the output was verified.
STATE SPACE SEARCH ALGORITHMS:
13. Implement N Queens problem using Backtracking
AIM:
To write a C program to implement N Queens problem using Backtracking.
ALGORITHM:
PROGRAM:
#define N 8
#include <stdbool.h>
#include <stdio.h>
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false)
{
printf("Solution does not exist"); return false;
}
printSolution(board); return true;
}
int main()
{
solveNQ();
return 0;
}
OUTPUT:
c1-24@cc124-HP-280-G4-MT-Business-PC:~$ gcc nqueen.c
cc1-24@cc124-HP-280-G4-MT-Business-PC:~$ ./a.out
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
RESULT:
Thus, the C program n queens problem was executed successfully and the output
was verified.
APPROXIMATION ALGORITHMS RANDOMIZED ALGORITHMS
14. Implement any scheme to find the optimal solution for the Traveling
Salesperson problem and then solve the same problem instance using any
approximation algorithm and determine the error in the approximation
AIM:
To write a C program to implement any scheme to find the optimal solution for the
Traveling Salesperson problem and then solve the same problem instance using
any approximation algorithm and determine the error in the approximation .
ALGORITHM:
int least_opt(int c)
{
int i,nc=999;
int min=999,kmin=999;
for(i=1;i<=n;i++)
{
if((a[c][i]!=0)&&(visit[i]==0))
if(a[c][i]<min)
{
min=a[i][1]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost_opt+=kmin;
return nc;
}
int least_apr(int c)
{
int i,nc=999;
int min=999,kmin=999;
for(i=1;i<=n;i++)
{
if((a[c][i]!=0)&&(visit[i]==0))
if(a[c][i]<kmin)
{
min=a[i][1]+a[c][i]; kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost_apr+=kmin;
return nc;
}
void main()
{
int i,j;
printf("Enter No. of cities:\n");
scanf("%d",&n);
0 1 3 6
1 0 2 3
3 2 0 1
6 3 1 0
Optimal Solution :
The path is :
1-->2-->4-->3-->1
Minimum cost:8
Approximated Solution :
The path is :
1-->2-->3-->4-->1
Minimum cost:10
RESULT:
Thus, the C program to find the optimal solution for the Traveling Salesperson problem
to find the optimal solution for the Traveling Salesperson problem was executed successfully
and the output was verified
15. Implement randomized algorithms for finding the kth smallest number
AIM:
To write a C program to implement randomized algorithms for finding the kth
smallest number.
ALGORITHM:
Step 1: find the k'th element in A using 'selection algorithm', let it be 'z'
Step 2: initialize an empty list 'L'
Step 3: initialize counter<-0
Step 4: for each element in
A:
4.1 : if element < z:
4.1.1 : counter<-counter + 1;
L.add(element)
Step 5: for each element in A:
5.1 : if element == z AND count < k:
5.1.1 : counter<-counter + 1;
L.add(element)
Step 6: return L
PROGRAM:
#include <limits.h>
#include <stdio.h>
return INT_MAX;
}
swap(&arr[i], &arr[r]);
return i;
}
int main()
{
RESULT:
Thus, the C program to implement randomized algorithms for finding the kth
smallest number was executed successfully and the output was verified.