Department of Cse - Artificial Intelligence & Machine Learning
Department of Cse - Artificial Intelligence & Machine Learning
Course Objective:
To develop efficient coding for the algorithms with various inputs and algorithms
Course Outcomes:
List of Experiments:
1. Develop a program and measure the running time for Binary Search with Divide and Conquer
2. Develop a program and measure the running time for Merge Sort with Divide and Conquer
3. Develop a program and measure the running time for Quick Sort with Divide and Conquer
4. Develop a program and measure the running time for estimating minimum-cost spanning
Trees with Greedy Method
5. Develop a program and measure the running time for estimating Single Source Shortest Paths
with Greedy Method
6. Develop a program and measure the running time for optimal Binary search trees with
Dynamic Programming
7. Develop a program and measure the running time for identifying solution for traveling
salesperson problem with Dynamic Programming
8. Develop a program and measure the running time for identifying solution for 8-Queens
problem with Backtracking
9. Develop a program and measure the running time for Graph Coloring with Backtracking
11. Develop a program and measure the running time running time to generate solution of
Knapsack problem with Backtracking
Time complexity:O(log n)
// Binary Search in C
#include <stdio.h>
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
// Merge sort in C
#include <stdio.h>
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Driver program
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
Time Complexity
Best O(n*log n)
Worst O(n*log n)
Average O(n*log n)
Stability Yes
// Quick sort in C
#include <stdio.h>
// main function
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
printf("Unsorted Array\n");
printArray(data, n);
Quicksort Complexity
Time Complexity
Best O(n*log n)
Worst O(n2)
Average O(n*log n)
Stability No
// Kruskal's algorithm in C
#include <stdio.h>
#define MAX 30
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist;
void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();
sort();
spanlist.n = 0;
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}
// Sorting algo
void sort() {
int i, j;
int main() {
int i, j, total_cost;
n = 6;
Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;
kruskalAlgo();
print();
}
Prim's algorithm is another popular minimum spanning tree algorithm that uses a different logic
to find the MST of a graph. Instead of starting from an edge, Prim's algorithm starts from a
vertex and keeps adding lowest-weight edges which aren't in the tree, until all vertices have been
covered.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
return min_index;
}
// driver's code
int main()
{
/* Let us create the example graph discussed above */
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 } };
// Function call
dijkstra(graph, 0);
return 0;
}
#include <stdio.h>
#include <limits.h>
// freq[i] to freq[j]
// Base cases
return 0;
return freq[i];
min = cost;
// accordingly.
// freq[i] to freq[j]
int s = 0;
s += freq[k];
return s;
int n = sizeof(keys)/sizeof(keys[0]);
return 0;
Output
Cost of Optimal BST is 142
#include <stdio.h>
completed[city] = 1;
if (ncity == 999) {
ncity = 0;
printf("%d", ncity + 1);
cost += ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c) {
int i, nc = 999;
int min = 999, kmin;
if (min != 999)
cost += kmin;
return nc;
}
int main() {
int n = 4;
ary[0][0] = 0;
ary[0][1] = 4;
ary[0][2] = 1;
ary[1][0] = 4;
ary[1][1] = 0;
ary[1][2] = 2;
ary[2][0] = 1;
ary[2][1] = 2;
ary[2][1] = 0;
int i;
for(i = 0 ; i < n; i++){
completed[i] = 0;
}
mincost(0);
printf("%d", cost);
return 0;
}
#include<stdio.h>
#include<math.h>
void print(int p)
{
int i,j;
printf("\n\nThis is Solution no. %d:\n\n",++count);
for(i=1;i<=p;++i)
for(i=1;i<=p;++i){
printf("\n\n%d",i);
for(j=1;j<=p;++j){
if(chess[i]==j)
printf("\tQ");
else
printf("\t-");
}
}
printf("\n\n\nThere are total 92 solutions for 8-queens problem.");
}
Result
#include <stdbool.h>
#include <stdio.h>
return false;
return true;
int color[V])
if (i == V) {
// if coloring is safe
if (isSafe(graph, color)) {
printSolution(color);
return true;
return false;
color[i] = j;
if (graphColoring(graph, m, i + 1, color))
return true;
color[i] = 0;
return false;
printf("Solution Exists:"
printf("\n");
// Driver code
int main()
(3)---(2)
| / |
|/ |
(0)---(1)
*/
bool graph[V][V] = {
{ 0, 1, 1, 1 },
{ 1, 0, 1, 0 },
{ 1, 1, 0, 1 },
{ 1, 0, 1, 0 },
};
int color[V];
color[i] = 0;
// Function call
if (!graphColoring(graph, m, 0, color))
return 0;
Output
Solution Exists: Following are the assigned colors
1 2 3 2
Time Complexity: O(mV). There is a total O(m V) combination of colors
Auxiliary Space: O(V). Recursive Stack of graph coloring(…) function will require O(V)
space.
using backtracking */
#include<stdio.h>
#define V 5
in 'path[]') */
added vertex. */
if (graph [ path[pos-1] ][ v ] == 0)
return false;
return false;
return true;
if (pos == V)
// first vertex
return true;
return false;
path[pos] = v;
return true;
then remove it */
path[pos] = -1;
return false;
prints the path. Please note that there may be more than one solutions,
path[i] = -1;
a Hamiltonian Cycle, then the path can be started from any point
path[0] = 0;
return false;
return true;
// Let us print the first vertex again to show the complete cycle
printf("\n");
int main()
(0)--(1)--(2)
| /\ |
| / \ |
|/ \|
(3)-------(4) */
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
hamCycle(graph1);
(0)--(1)--(2)
| /\ |
| / \ |
|/ \|
(3) (4) */
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
hamCycle(graph2);
return 0;
Output:
Solution Exists: Following is one Hamiltonian Cycle
0 1 2 4 3 0
#include <stdio.h>
// Base Case
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
else
return max(
val[n - 1]
// Driver code
int main()
int W = 50;
return 0;
Output
220
Time Complexity: O(2N)
Auxiliary Space: O(N), Stack space required for recursion