DESIGN AND ANALYSIS OF ALGORITHM (PRACTICAL FILE)

75
G. L. Bajaj Institute of Technology & Management Greater Noida Uttar Pradesh Technical University Lucknow

description

THIS IS COMPLETE PRACTICAL FILE OF DESIGN AND ANALYSIS OF ALGORITHM.NAME :- DESIGN AND ANALYSIS OF ALGORITHMCOURSE :- B.TECH ENGG.BRANCH :- COMPUTER SCIENCEYEAR :- 2ND (4TH SEM)

Transcript of DESIGN AND ANALYSIS OF ALGORITHM (PRACTICAL FILE)

DAA PRACTICAL FILE

G. L. Bajaj Institute of Technology & Management Greater Noida

Uttar Pradesh Technical UniversityLucknow

G.L. Bajaj Institute of Technology & Management

B.TechComputer Science & Engineering Department

(Session: 2014-15) ODD SEMESTERBatch (2011-15)THIRD YEAR (V SEMESTER)

DAA LAB FILE

Submitted to Submitted By Mr. B. DwivediBrij Bhushan Singh(Asst. Professor)(1219210901) CONTENTS

S.NO.NAME OF THE PROGRAM DATE REMARKS

1Write a program to implement the quick sort.

2Write a program to implement the merge sort.

3Wap to implement the strassens matrix multiplication

4Write a program to implement the 0-1 knapsack problem.

5Write a program to implement the fractional knapsack problem.

6Write a program to implement the job sequencing with deadlines while maximizing profits.

7Write a program to implement the optimal merge patterns.

8Write a program for creating minimum spanning tree from prim's algorithm.

9Write a Program for creating a minimum spanning tree from Kruskal's algorithm.

10Write a program to implement the n-queen's problem.

11Write a program to implement traveling salesman problem program.

12Write a program to implement the insertion sort.

13Write a program to implement heap sort.

14Write a program to implement the bubble sort method.

15Write a program to implement the sequential search.

16Write a program to implement the binary search [int array].

17Wap to find the kth smallest element in the given list of array elements.

G. L. Bajaj Institute of Technology & ManagementPlot no. 2, Knowledge Park III, Greater Noida, UP-201308DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

PROGRAM: - 1

WRITE A PROGRAM TO IMPLEMENT THE QUICK SORT.

#include (stdio.h)#include (conio.h)#define MAXSIZE 500voidquickSort (int elements [], int maxsize);void sort(int elements[], int left, int right);int elements[MAXSIZE];int main(){int i, maxsize;printf(\nHow many elements you want to sort: );scanf(%d,&maxsize);printf(\nEnter the values one by one: );for (i = 0; i < maxsize; i++){printf (\nEnter element %i :,i);scanf(%d,&elements[i]);}printf(\nArray before sorting:\n);for (i = 0; i < maxsize; i++)printf([%i], ,elements[i]);printf (\n);quickSort(elements, maxsize);printf(\nArray after sorting:\n);for (i = 0; i < maxsize; i++)printf([%i], , elements[i]);}void quickSort(int elements[], int maxsize){sort (elements, 0, maxsize - 1);}void sort (int elements[], int left, int right){int pivot, l, r;l = left;r = right;pivot = elements[left];while (left < right){while ((elements[right] >= pivot) && (left < right))if (left != right){elements[left] = elements[right];left++;}while ((elements[left] pivot)sort(elements, pivot + 1, right);}

PROGRAM: - 2

WRITE APROGRAM TO IMPLEMENT THE MERGE SORT.

#include #include #define MAXARRAY 10void mergesort(int a[], int low, int high);int main(void) {int array[MAXARRAY];int i = 0; /* load some random values into the array */for(i = 0; i < MAXARRAY; i++)array[i] = rand() % 100; /* array before mergesort */printf("Before :");for(i = 0; i < MAXARRAY; i++)printf(" %d", array[i]);printf("\n");mergesort(array, 0, MAXARRAY - 1); /* array after mergesort */printf("Mergesort :");for(i = 0; i < MAXARRAY; i++)printf(" %d", array[i]);printf("\n");return 0;}void mergesort(int a[], int low, int high) {int i = 0;int length = high - low + 1;int pivot = 0; int merge1 = 0; int merge2 = 0;int working[length];if(low == high)return;pivot = (low + high) / 2;mergesort(a, low, pivot);mergesort(a, pivot + 1, high); for(i = 0; i < length; i++)working[i] = a[low + i];merge1 = 0;merge2 = pivot - low + 1;for(i = 0; i < length; i++) {if(merge2 (m2, n2); double **M2 = Allocate2DArray< double >(m2, n2); double **M3 = Allocate2DArray< double >(m2, n2); double **M4 = Allocate2DArray< double >(m2, n2); double **M5 = Allocate2DArray< double >(m2, n2); double **M6 = Allocate2DArray< double >(m2, n2); double **M7 = Allocate2DArray< double >(m2, n2); double **A11 = new double*[m2]; double **A12 = new double*[m2]; double **A21 = new double*[m2]; double **A22 = new double*[m2];double **B11 = new double*[p2]; double **B12 = new double*[p2]; double **B21 = new double*[p2]; double **B22 = new double*[p2]; double **C11 = new double*[m2]; double **C12 = new double*[m2]; double **C21 = new double*[m2]; double **C22 = new double*[m2]; double **tAM1 = Allocate2DArray< double >(m2, p2); double **tBM1 = Allocate2DArray< double >(p2, n2); double **tAM2 = Allocate2DArray< double >(m2, p2); double **tBM3 = Allocate2DArray< double >(p2, n2); double **tBM4 = Allocate2DArray< double >(p2, n2); double **tAM5 = Allocate2DArray< double >(m2, p2); double **tAM6 = Allocate2DArray< double >(m2, p2); double **tBM6 = Allocate2DArray< double >(p2, n2); double **tAM7 = Allocate2DArray< double >(m2, p2); double **tBM7 = Allocate2DArray< double >(p2, n2); copyQtrMatrix(A11, m2, A, mf, pf); copyQtrMatrix(A12, m2, A, mf, p2); copyQtrMatrix(A21, m2, A, m2, pf); copyQtrMatrix(A22, m2, A, m2, p2); copyQtrMatrix(B11, p2, B, pf, nf); copyQtrMatrix(B12, p2, B, pf, n2); copyQtrMatrix(B21, p2, B, p2, nf); copyQtrMatrix(B22, p2, B, p2, n2); copyQtrMatrix(C11, m2, C, mf, nf); copyQtrMatrix(C12, m2, C, mf, n2); copyQtrMatrix(C21, m2, C, m2, nf); copyQtrMatrix(C22, m2, C, m2, n2); // M1 = (A11 + A22)*(B11 + B22) AddMatBlocks(tAM1, m2, p2, A11, A22); AddMatBlocks(tBM1, p2, n2, B11, B22); strassenMMult(0, m2, 0, n2, 0, p2, tAM1, tBM1, M1); //M2 = (A21 + A22)*B11 AddMatBlocks(tAM2, m2, p2, A21, A22); strassenMMult(0, m2, 0, n2, 0, p2, tAM2, B11, M2); //M3 = A11*(B12 - B22) SubMatBlocks(tBM3, p2, n2, B12, B22); strassenMMult(0, m2, 0, n2, 0, p2, A11, tBM3, M3); //M4 = A22*(B21 - B11) SubMatBlocks(tBM4, p2, n2, B21, B11); strassenMMult(0, m2, 0, n2, 0, p2, A22, tBM4, M4); //M5 = (A11 + A12)*B22 AddMatBlocks(tAM5, m2, p2, A11, A12); strassenMMult(0, m2, 0, n2, 0, p2, tAM5, B22, M5); //M6 = (A21 - A11)*(B11 + B12) SubMatBlocks(tAM6, m2, p2, A21, A11); AddMatBlocks(tBM6, p2, n2, B11, B12); strassenMMult(0, m2, 0, n2, 0, p2, tAM6, tBM6, M6); //M7 = (A12 - A22)*(B21 + B22) SubMatBlocks(tAM7, m2, p2, A12, A22); AddMatBlocks(tBM7, p2, n2, B21, B22); strassenMMult(0, m2, 0, n2, 0, p2, tAM7, tBM7, M7); for (int i = 0; i < m2; i++) for (int j = 0; j < n2; j++) { C11[i][j] = M1[i][j] + M4[i][j] - M5[i][j] + M7[i][j]; C12[i][j] = M3[i][j] + M5[i][j]; C21[i][j] = M2[i][j] + M4[i][j]; C22[i][j] = M1[i][j] - M2[i][j] + M3[i][j] + M6[i][j]; } Free2DArray< double >(M1); Free2DArray< double >(M2); Free2DArray< double >(M3); Free2DArray< double >(M4); Free2DArray< double >(M5); Free2DArray< double >(M6); Free2DArray< double >(M7); delete[] A11; delete[] A12; delete[] A21; delete[] A22; delete[] B11; delete[] B12; delete[] B21; delete[] B22; delete[] C11; delete[] C12; delete[] C21; delete[] C22; Free2DArray< double >(tAM1); Free2DArray< double >(tBM1); Free2DArray< double >(tAM2); Free2DArray< double >(tBM3); Free2DArray< double >(tBM4); Free2DArray< double >(tAM5); Free2DArray< double >(tAM6); Free2DArray< double >(tBM6); Free2DArray< double >(tAM7); Free2DArray< double >(tBM7); } } void matmultS(int m, int n, int p, double **A, double **B, double **C) { int i,j; for (i=0; i < m; i++) for (j=0; j < n; j++) C[i][j] = 0; strassenMMult(0, m, 0, n, 0, p, A, B, C); } int CheckResults(int m, int n, double **C, double **C1) { #define THRESHOLD 0.001 for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (abs(C[i][j] - C1[i][j]) > THRESHOLD ) { printf("%f %f\n", C[i][j], C1[i][j]); return 1; } }} return 0; } int main(int argc, char* argv[]) { clock_t before, after;

int M = atoi(argv[1]); int N = atoi(argv[2]); int P = atoi(argv[3]);

double **A = Allocate2DArray< double >(M, P); double **B = Allocate2DArray< double >(P, N); double **C = Allocate2DArray< double >(M, N); double **C4 = Allocate2DArray< double >(M, N);

int i, j;

for (i = 0; i < M; i++) { for (j = 0; j < P; j++) { A[i][j] = 5.0 - ((double)(rand()%100) / 10.0); } } for (i = 0; i < P; i++) { for (j = 0; j < N; j++) { B[i][j] = 5.0 - ((double)(rand()%100) / 10.0); }} printf("Execute Standard matmult\n\n"); before = clock(); seqMatMult(M, N, P, A, B, C); after = clock(); printf("Standard matrix function done in %7.2f secs\n\n\n",(float)(after - before)/ CLOCKS_PER_SEC); before = clock(); matmultS(M, N, P, A, B, C4); after = clock(); printf("Strassen matrix function done in %7.2f secs\n\n\n",(float)(after - before)/ CLOCKS_PER_SEC); if (CheckResults(M, N, C, C4)) printf("Error in matmultS\n\n"); else printf("OKAY\n\n"); Free2DArray(A); Free2DArray(B); Free2DArray(C); Free2DArray(C4); return 0; }

PROGRAM: - 4WRITE APROGRAM TO IMPLEMENT THE 0-1 KNAPSACK PROBLEM.#include #define MAXWEIGHT 100intn=3;intc[10]={8,6,4};intv[10]={16,10,7};intW=10;voidfill_sack(){inta[MAXWEIGHT];intlast_added[MAXWEIGHT];inti,j;intaux;for(i=0;ilink;q->link = tmp;if(q->link == NULL)tmp->link = NULL;}}struct edge *del_pque(){struct edge *tmp;tmp = front;printf("Edge processed is %d->%d %d\n",tmp->u,tmp->v,tmp->weight);front = front->link;return tmp;}

PROGRAM: - 10

WRITE APROGRAM TO IMPLEMENT THE N-QUEEN'S PROBLEM.

#include #include #include #include #include void check(int, int, char [100][100]);void print(char [100][100]);int no_of_queens, queen = 2, flagrow = 0, flagcol = 0;int count = 1;char ch, response_row, response_col;int main(void){int row, col, i;char board[100][100], response;clrscr ();Printf ("@@ This is n-queen problem.Enter the number ofqueens (say n) and watch how computer places them in (n x n) matrixsuch that none can meet another moving along horizontally, verticallyor digonally.");printf("Enter the number of queens : ");scanf("%d", &no_of_queens);if(no_of_queens > 23){Printf (" @@ Thought the program is OK for any queen value. Butdue the configuration of the output screen the output will betranketed (A very large queen number may cause the system stackoverflow).So it is highly recommended that you run the program withmaximum queen number 23...");printf("Want to continue(Y/N)?");fflush(stdin);scanf("%c", &response);if(toupper(response) == 'N')return (0);}else if(no_of_queens < 3){printf("The number of Queen must be greater than 3.");getch();return (0);}printf("Want a row number below the board(Y/N) : ");fflush(stdin);response_row = (char)getchar();if(toupper(response_row) == 'Y')flagrow = 1;printf("Want a column number below the board(Y/N) : ");fflush(stdin);response_col = (char)getchar();if(toupper(response_col) == 'Y')flagcol = 1;clrscr();printf("M/c in work ! Please Wait...");

// This for-loop is used for checking all the columns of row 0 only...setcursortype(_NOCURSOR);for(col = 0; col < no_of_queens; col++){memset(board, '-', sizeof(board));check( 0, col, board );}clrscr();printf("Thank you for seeing this program through.");getch();return (0);}

void check( int r, int c, char board[100][100] ){int i, j;

// Terminating condition for the recursion...if ( ( r == no_of_queens ) && ( c == 0 )) {clrscr();printf(" (%d-Queen) Set : %d", no_of_queens, count++);print( board );fflush(stdin);ch = (char)getch();clrscr();if(ch == 'e')exit (0);printf("M/c in work ! Please Wait...");}

// Vertical check...for(i = 0; i < r; i++){if ( board[i][c] == queen)return;}

// Horizontal check...for(j = 0; j < c; j++){if ( board[r][j] == queen)return;}

// Left-Diagonal check... i = r; j = c;do {if ( board[i][j] == queen )return;i--; j--; }while( i >= 0 && j >= 0 );

// Right-Diagonal check... i = r; j = c;do {if ( board[i][j] == queen )return;i--; j++; }while( i >= 0 && j < no_of_queens );

// Placing the queen if the ckecked position is OK...board[r][c] = queen;r++;

// This for-loop is used for checking all the columns for each row //starting from 1 upto the end...for(int p = 0; p < no_of_queens; p++)check(r, p, board);for(int h = 0; h < no_of_queens; h++)board[r - 1][h] = '-';

}

void print(char board[100][100]){

for(int i = 0; i < no_of_queens; i++){if(flagrow == 1)printf("%3d", i + 1);

for(int j = 0; j < no_of_queens; j++){if(board[i][j] == queen){textcolor(RED);cprintf("%3c", queen);}else{textcolor(8); //dark graycprintf("%3c", 22);}}printf(""); }textcolor(7);if(flagcol == 1) {if(flagrow)printf(" ");for(i = 0; i < no_of_queens; i++)printf("%3d", i + 1);}gotoxy(62, 1);printf("Press E to exit.");textcolor(7);}

PROGRAM: - 11WRITE APROGRAM TO IMPLEMENT Traveling Salesman problem program.

#include #include #include #include using namespace std;void createProfit(int profitMatrix[20][20]);void createRoute(int currentRoute[20]);int evaluateRoute(int currentRoute[20], const int profitMatrix[20][20]);void tryRoute(int currentRoute[20], int bestRoute[20], const int profitMatrix[20][20]);void swap(int &item1, int &item2);int main(){// variables usedint profitMatrix[20][20];int currentRoute[20];int bestRoute[20];int value=0; int max=0;int i=0;long int start;int kount=0;

// create a random environmentcreateRoute(currentRoute);createRoute(bestRoute);createProfit(profitMatrix);

// seed the rand number generatorsrand(time(0));

// loop for 60 CPU secondsstart=clock();while ((clock()-start) 10000) { kount=0; cout