ADA+Lab+All+Programs

download ADA+Lab+All+Programs

of 64

Transcript of ADA+Lab+All+Programs

  • 8/3/2019 ADA+Lab+All+Programs

    1/64

    Algorithms Lab Manual

    1. Implement Recursive Binary searchand Linear search and determine thetime taken to search an element.

    Repeat the experiment for differentvalues of n, the number of elements inthe list to be searched and plot a graphof the time taken versus n.

    /* Implementation of recursive binary

    search and sequential search */

    #include#include#include#include#define max 20

    int pos;int binsearch(int,int[],int,int,int);int linsearch(int,int[],int);

    void main(){

    int ch=1;

    double t;int n,i,a[max],k,op,low,high,pos;clock_t begin,end;clrscr();while(ch){

    Dept of MCA 2009 1

  • 8/3/2019 ADA+Lab+All+Programs

    2/64

    Algorithms Lab Manual

    printf("\n.....MENU.....\n 1.BinarySearch\n 2.Linear Search\n3.Exit\n");

    printf("\nEnter your choice\n");scanf("%d",&op);

    switch(op){

    case 1:printf("\nEnter the numberof elements \n");

    scanf("%d",&n);

    printf("\nEnter the elements ofan array in order\n");

    for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    3/64

    Algorithms Lab Manual

    break;case 2:printf("\nEnter the number

    of elements\n");

    scanf("%d",&n);printf("\nEnter the elements ofan array\n");

    for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    4/64

    Algorithms Lab Manual

    printf("\n Do you wish to run again(1/0) \n");

    scanf("%d",&ch);

    }getch();}

    int binsearch(int n,int a[],int k,intlow,int high){

    int mid;delay(1000);mid=(low+high)/2;if(low>high)return -1;

    if(k==a[mid])return(mid);

    elseif(k

  • 8/3/2019 ADA+Lab+All+Programs

    5/64

    Algorithms Lab Manual

    return -1;if(k==a[n-1])return(n-1);

    elsereturn linsearch(n-1,a,k);

    }

    OUTPUT

    Case 1

    .....MENU.....

    1.Binary Search

    2.Linear Search

    3.Exit

    Enter your choice

    1

    Enter the number of elements

    3

    Enter the elements of an array

    Dept of MCA 2009 5

  • 8/3/2019 ADA+Lab+All+Programs

    6/64

    Algorithms Lab Manual

    4

    8

    12

    Enter the elements to be searched

    12

    Element 12 is found at position 2

    Time taken is 1.978022 CPU1 cycles

    Case 2

    .....MENU.....

    1.Binary Search

    2.Linear Search

    3.Exit

    Enter your choice

    2

    Enter the number of elements

    4

    Enter the elements of an array

    3

    6

    9

    12

    Enter the elements to be searched

    9

    Dept of MCA 2009 6

  • 8/3/2019 ADA+Lab+All+Programs

    7/64

    Algorithms Lab Manual

    Element 9 is found at position 3

    Time taken is 3.021978 CPU cycles

    2. Sort a given set of elements using the Heap sort

    method and determine the time taken 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.

    #include#include#include

    Dept of MCA 2009 7

  • 8/3/2019 ADA+Lab+All+Programs

    8/64

    Algorithms Lab Manual

    void heapcom(int a[],int n){

    int i,j,k,item;for(i=1;ia[k]){

    a[j]=a[k];j=k;k=j/2;

    }a[j]=item;

    }}

    void adjust(int a[],int n){int item,i,j;j=1;item=a[j];i=2*j;while(i

  • 8/3/2019 ADA+Lab+All+Programs

    9/64

    Algorithms Lab Manual

    {a[j]=a[i];j=i;

    i=2*j;}elsebreak;

    }a[j]=item;

    }void heapsort(int a[],int n)

    {int i,temp;delay(1000);heapcom(a,n);for(i=n;i>=1;i--){

    temp=a[1];

    a[1]=a[i];a[i]=temp;adjust(a,i);

    }}void main()

    {int i,n,a[20],ch=1;

    clock_t start,end;clrscr();while(ch){printf("\n enter the number of

    elements to sort\n");

    Dept of MCA 2009 9

  • 8/3/2019 ADA+Lab+All+Programs

    10/64

    Algorithms Lab Manual

    scanf("%d",&n);printf("\n enter the elements to

    sort\n");

    for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    11/64

    Algorithms Lab Manual

    6

    3

    1

    the sorted list of elemnts is

    1

    3

    5

    6

    8

    Dept of MCA 2009 11

  • 8/3/2019 ADA+Lab+All+Programs

    12/64

    Algorithms Lab Manual

    3. Sort a given set of elements using Merge sort method

    and determine the time taken to sort the elements.

    Repeat the experiment for different values of n, the

    number of elements in the list to be sorted and plot agraph of the time taken versus n.

    #include#include

    #include#define max 20void mergesort(int a[],int low,inthigh);void merge(int a[],int low,int mid,inthigh);void main(){

    int n,i,a[max],ch=1;clock_t start,end;clrscr();while(ch){

    Dept of MCA 2009 12

  • 8/3/2019 ADA+Lab+All+Programs

    13/64

    Algorithms Lab Manual

    printf("\n\t enter the number ofelements\n");

    scanf("%d",&n);

    printf("\n\t enter theelements\n");for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    14/64

    Algorithms Lab Manual

    merge(a,low,mid,high);}

    }

    void merge(int a[],int low,int mid,inthigh){

    int i,j,k,t[max];i=low;j=mid+1;k=low;

    while((i

  • 8/3/2019 ADA+Lab+All+Programs

    15/64

    Algorithms Lab Manual

    Enter the elements

    6

    3

    41

    9

    The sorted array is

    1

    3

    4

    69

    time taken=0.824176

    Dept of MCA 2009 15

  • 8/3/2019 ADA+Lab+All+Programs

    16/64

    Algorithms Lab Manual

    4. Sort a given set of elements using Selection sort and

    hence find the time required to sort elements. Repeat

    the experiment for different values of n, the number ofelements in the list to be sorted and plot a graph of the

    time taken versus n.

    #include#include#include

    void main(){int i,n,j,min,k,a[20],ch=1;clock_t begin,end;clrscr();while(ch){

    printf("\n enter the number ofelements\n");scanf("%d",&n);printf("\n enter the elements to

    be sorted\n");for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    17/64

    Algorithms Lab Manual

    if(a[j]

  • 8/3/2019 ADA+Lab+All+Programs

    18/64

    Algorithms Lab Manual

    1

    9

    the sorted list of elements are:1 3 5 8 9

    time taken:0.824176

    Dept of MCA 2009 18

  • 8/3/2019 ADA+Lab+All+Programs

    19/64

    Algorithms Lab Manual

    5. a. Obtain the Topological ordering of vertices in a

    given digraph.

    #include#include#define max 20

    int a[max][max],n;void topological_sort();

    void main(){

    int i,j;clrscr();printf("\n enter the number of

    vertices\n");scanf("%d",&n);

    printf("\n enter the adjacencymatrix\n");for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    20/64

    Algorithms Lab Manual

    while(p

  • 8/3/2019 ADA+Lab+All+Programs

    21/64

    Algorithms Lab Manual

    }}printf("\n topological order

    obtained is...\n");for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    22/64

    Algorithms Lab Manual

    5 b. Implement All Pair Shortest paths problem using

    Floyd's

    algorithm.

    #include#include#includeint cost[10][10],a[10][10];void all_paths(int [10][10],int [10][10],int);int min1(int,int);

    void main(){

    int i,j,n;clrscr();printf("\n enter the number of

    vertices\n");

    scanf("%d",&n);printf("\n enter the adjacencymatrix\n");

    for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    23/64

    Algorithms Lab Manual

    }getch();

    }

    void all_paths(int cost[10][10],inta[10][10],int n){

    int i,j,k;for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    24/64

    Algorithms Lab Manual

    OUTPUT

    enter the number of vertices

    4

    enter the adjacency matrix

    999 999 3 999

    2 999 999 999

    999 7 999 1

    6 999 999 999

    the shortest path obtained is10 10 3 4

    2 12 5 6

    7 7 10 1

    6 16 9 10

    Dept of MCA 2009 24

  • 8/3/2019 ADA+Lab+All+Programs

    25/64

    Algorithms Lab Manual

    6. Implement 0/1 Knapsack problem using dynamic

    programming.

    #include#includeint v[20][20];int max1(int a,int b){

    return(a>b)?a:b;}void main(){

    int i,j,p[20],w[20],n,max;clrscr();

    Dept of MCA 2009 25

  • 8/3/2019 ADA+Lab+All+Programs

    26/64

    Algorithms Lab Manual

    printf("\n enter the number ofitems\n");

    scanf("%d",&n);

    for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    27/64

  • 8/3/2019 ADA+Lab+All+Programs

    28/64

    Algorithms Lab Manual

    enter the weight and profit of the item 4:2 15

    enter the capacity of the knapsack5

    The table is

    0 0 0 0 0 0

    0 0 12 12 12 12

    0 10 12 22 22 22

    0 10 12 22 30 32

    0 10 15 25 30 37

    The maximum profit is 37

    The most valuable subset is:{ item 4: item 2:

    item 1:}

    Dept of MCA 2009 28

  • 8/3/2019 ADA+Lab+All+Programs

    29/64

    Algorithms Lab Manual

    7. From a given vertex in a weighted connected graph,

    find shortest

    paths to other vertices using Dijkstra's algorithm.

    #includemain (){

    int n, cost[15][15], i, j, s[15], v,u, w, dist[15],

    num, min;clrscr();printf ("Enter the vertices

    please\n");scanf ("%d", &n);printf ("Enter the cost of the edges

    please\n");

    printf ("Enter 999 if the edgeis notpresent or for theself loop\n");

    for (i = 1; i

  • 8/3/2019 ADA+Lab+All+Programs

    30/64

    Algorithms Lab Manual

    }

    s[v] = 1;

    dist[v] = 0;

    for (num = 2; num

  • 8/3/2019 ADA+Lab+All+Programs

    31/64

    Algorithms Lab Manual

    printf (" %d\t %d\t\t %d\n",v, i, dist[i]);

    getch();

    }

    OUTPUT

    Enter the vertices pleasen = 5

    Enter the cost of the edges pleaseEnter 999 if the edge is not present orfor the self loopThe cost of the edges are :

    999 1 2 999 9991 999 3 4 999

    2 3 999 5 6999 4 5 999 6999 999 6 6 999

    Enter the Source vertex please : 1

    VERTEX DESTINATION COST1 1 0

    1 2 11 3 21 4 51 5 8

    Dept of MCA 2009 31

  • 8/3/2019 ADA+Lab+All+Programs

    32/64

    Algorithms Lab Manual

    8. Sort a given set of elements using Quick sort method

    and determine the time taken to sort the elements.

    Repeat the experiment for different values of n, the

    number of elements in the list to be sorted and plot agraph of the time taken versus n.

    #include#includevoid quicksort(int[],int,int);int partition(int[],int,int);

    void main(){

    int i,n,a[20],ch=1;clrscr();while(ch){

    printf("\n enter the number of

    elements\n");scanf("%d",&n);printf("\n enter the array

    elements\n");for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    33/64

    Algorithms Lab Manual

    }getch();

    }

    void quicksort(int a[],int low,int high){

    int mid;if(low

  • 8/3/2019 ADA+Lab+All+Programs

    34/64

    Algorithms Lab Manual

    else{

    k=a[j];

    a[j]=a[low];a[low]=k;}

    }return j;

    }

    OUTPUT

    enter the number of elements

    5

    enter the elements to be sorted

    8

    5

    2

    4

    1

    the sorted list of elements are:1 2 4 5 8

    time taken:0.824176

    Dept of MCA 2009 34

  • 8/3/2019 ADA+Lab+All+Programs

    35/64

    Algorithms Lab Manual

    9. Find Minimum Cost Spanning Tree of a given

    undirected graph

    using Kruskal's algorithm-

    #include#includeint root[10], flag = 0, count=0, temp,min;int a[20], cost[20][20], n, i, j, k,

    totalcost = 0, x, y;void find_min (), check_cycle (),update ();main (){

    clrscr();printf ("Enter the number of vertices

    please\n");scanf ("%d", &n);printf ("Enter the cost of the matrix

    please\n");for (i = 1; i

  • 8/3/2019 ADA+Lab+All+Programs

    36/64

    Algorithms Lab Manual

    printf ("%d ---> %d = %d\n",x, y,

    cost[x][y]);

    totalcost += cost[x][y];update ();count++;

    }cost[x][y] = cost[y][x] = 999;find_min ();

    }

    if (count < n - 2)printf ("The graph is not

    connected\n");elseprintf ("The graph is connected &

    the min cost is%d\n", totalcost);

    getch();}

    void check_cycle (){

    if ((root[x] == root[y]) &&(root[x] != 0))

    flag = 0;

    elseflag = 1;

    }

    void find_min (){

    Dept of MCA 2009 36

  • 8/3/2019 ADA+Lab+All+Programs

    37/64

    Algorithms Lab Manual

    min = 999;for (i = 1; i

  • 8/3/2019 ADA+Lab+All+Programs

    38/64

    Algorithms Lab Manual

    OUTPUT

    Enter the number of vertices please

    4

    Enter the cost of the matrix please

    999 1 5 21 999 999 999

    5 999 999 3

    2 999 3 999

    1 ---> 2 = 1

    1 ---> 4 = 2

    3 ---> 4 = 3

    The graph is connected & the min cost is 6

    Dept of MCA 2009 38

  • 8/3/2019 ADA+Lab+All+Programs

    39/64

    Algorithms Lab Manual

    10. a. Print all the nodes reachable from a given

    starting node in a digraph using Breadth First Search

    method.

    Dept of MCA 2009 39

  • 8/3/2019 ADA+Lab+All+Programs

    40/64

    Algorithms Lab Manual

    #include

    #include

    void distance(int,int);

    int a[10][10];void main()

    {

    int i,j,n;

    clrscr();

    printf("\n Enter the number of vertices in the

    diagraph:");

    scanf("%d",&n);

    printf("\n Enter the adjacency matrix\n");for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    41/64

    Algorithms Lab Manual

    visited[v]=1;

    do

    {

    i=queue[front++];for(j=1;j

  • 8/3/2019 ADA+Lab+All+Programs

    42/64

    Algorithms Lab Manual

    the vertex 1 to 2 is of distance=1

    the vertex 1 to 3 is of distance=1

    the vertex 1 to 4 is of distance=1

    press enter for other source vertex

    the starting vertex is 2

    the vertex 2 to 4 is of distance=1

    the vertex 2 to 3 is of distance=2

    press enter for other source vertex

    the starting vertex is 3

    press enter for other source vertex

    the starting vertex is 4

    the vertex 4 to 3 is of distance=1

    press enter for other source vertex

    Dept of MCA 2009 42

  • 8/3/2019 ADA+Lab+All+Programs

    43/64

    Algorithms Lab Manual

    10 b. Check whether a given graph is connected or not

    using DFS method.

    #include#includevoid dfs(int n,int cost[10][10],int

    u,int s[]){

    int v;s[u]=1;for(v=0;v

  • 8/3/2019 ADA+Lab+All+Programs

    44/64

    Algorithms Lab Manual

    printf("\n enter the adjacencymatrix\n");

    for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    45/64

    Algorithms Lab Manual

    }

    OUTPUT

    Case 1:

    enter the number of nodes

    4

    enter the adjacency matrix

    0 0 0 1

    0 0 0 0

    0 0 1 0

    0 0 0 1

    graph is not connected

    Case 2:

    enter the number of nodes

    4

    enter the adjacency matrix

    1 1 1 1

    1 1 1 1

    1 1 1 1

    Dept of MCA 2009 45

  • 8/3/2019 ADA+Lab+All+Programs

    46/64

    Algorithms Lab Manual

    1 1 1 1

    graph is connected

    11. Find a subset of a given set S == {sl,s2,......sn} of n

    positiveintegers whose sum is equal to a given positive integer

    d. For

    example, if S== (1, 2, 5, 6, 8} and d = 9 there are two

    solutions

    {1,2,6} and {1,8}. A suitable message is to be

    displayed if the given problem

    instance doesn't have a solution.

    #includeint s[10],d,n,set[10],count=0;void display(int);int flag = 0;

    void main(){

    int subset(int,int);int i;clrscr();

    Dept of MCA 2009 46

  • 8/3/2019 ADA+Lab+All+Programs

    47/64

    Algorithms Lab Manual

    printf("Enter the Number of elementsin the set\n");

    scanf("%d",&n);

    printf("enter the set values\n");for(i=0;id || i>=n) return;else{

    set[count]=s[i];

    count++;subset(sum+s[i],i+1);count--;subset(sum,i+1);

    }}

    Dept of MCA 2009 47

  • 8/3/2019 ADA+Lab+All+Programs

    48/64

    Algorithms Lab Manual

    void display(int count){int i;printf("{ ");for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    49/64

    Algorithms Lab Manual

    The Program Output is:

    { 1 2 6 }

    { 1 8 }

    Dept of MCA 2009 49

  • 8/3/2019 ADA+Lab+All+Programs

    50/64

    Algorithms Lab Manual

    12. a. Implement Horspool algorithm for String

    Matching,

    #include#includevoid main(){

    int table[126];char t[100],p[25];

    int n,i,k,j,m,flag=0;clrscr();printf("Enter the Text\n");gets(t);n=strlen(t);printf("Enter the Pattern\n");gets(p);

    m=strlen(p);for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    51/64

    Algorithms Lab Manual

    printf("The position of thepattern is

    %d\n",i-m+2);

    flag=1;break;}else

    i=i+table[t[i]];

    }

    if(!flag)printf("Pattern is not found in

    the giventext\n");

    getch();

    }

    OUTPUT

    Case 1:

    Enter the Text

    acharya_computer_science_engineering

    Enter the Pattern

    science

    The position of the pattern is 18

    Dept of MCA 2009 51

  • 8/3/2019 ADA+Lab+All+Programs

    52/64

    Algorithms Lab Manual

    Case 2:

    Enter the Text

    acharya_computer_sceince_engineering

    Enter the Pattern

    cj

    Pattern is not found in the given text

    Dept of MCA 2009 52

  • 8/3/2019 ADA+Lab+All+Programs

    53/64

    Algorithms Lab Manual

    12. b. Find the Binomial Co-efficient using Dynamic

    Programming.

    #include#includevoid main(){

    int i,j,k,n,c[50][50];clrscr();printf("\n enter the value of n &

    k\n");

    scanf("%d%d",&n,&k);for(i=0;i

  • 8/3/2019 ADA+Lab+All+Programs

    54/64

    Algorithms Lab Manual

    printf("\n\t the binomialcoefficient of C(%d,%d) is%d\n",n,k,c[n][k]);

    getch();}

    OUTPUT

    enter the value of n & k

    6 3

    the table for valuation is

    1

    1 1

    1 2 1

    1 3 3 1

    Dept of MCA 2009 54

  • 8/3/2019 ADA+Lab+All+Programs

    55/64

    Algorithms Lab Manual

    1 4 6 4

    1 5 10 10

    1 6 15 20

    the binomial coefficient of C(6,3) is 20

    Dept of MCA 2009 55

  • 8/3/2019 ADA+Lab+All+Programs

    56/64

    Algorithms Lab Manual

    13. Find Minimum Cost Spanning Tree of a given

    undirected graph

    using Prims algorithm.

    #include#includevoid main(){

    int cost[20][20],t[20][20],near1[20],a[20];

    inti,j,n,min,minimum,k,l,mincost,c,b;

    clrscr();printf("\n enter the number of

    nodes\n");scanf("%d",&n);printf("\n enter the adjacency

    matrix\n");for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    57/64

    Algorithms Lab Manual

    }mincost=minimum;t[1][1]=k;

    t[1][2]=l;for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    58/64

  • 8/3/2019 ADA+Lab+All+Programs

    59/64

    Algorithms Lab Manual

    the cost of minimum spanning tree is=6

    14. Compute the transitive closure of a given directed

    graph

    using Warshall's algorithm.

    #include#include

    int a[10][10];void main()

    { int i,j,k,n;clrscr();printf("\n enter the number of

    vertices\n");scanf("%d",&n);printf("\n enter the adjacency

    matrix\n");

    for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    60/64

    Algorithms Lab Manual

    for(j=1;j

  • 8/3/2019 ADA+Lab+All+Programs

    61/64

    Algorithms Lab Manual

    enter the number of vertices

    4

    enter the adjacency matrix0 1 0 0

    0 0 0 1

    1 0 1 0

    0 0 0 0

    the tranitive closure is

    0 1 0 1

    0 0 0 11 1 1 1

    0 0 0 0

    Dept of MCA 2009 61

  • 8/3/2019 ADA+Lab+All+Programs

    62/64

    Algorithms Lab Manual

    15. Implement N Queen's problem using Back Tracking

    #include

    #include#includeint x[20],count=1;void queens(int,int);int place(int,int);

    void main(){

    int n,k=1;clrscr();printf("\n enter the number of

    queens to be placed\n");scanf("%d",&n);queens(k,n);

    }

    void queens(int k,int n){int i,j;for(j=1;j

  • 8/3/2019 ADA+Lab+All+Programs

    63/64

    Algorithms Lab Manual

    printf("\n \t %d row %d

    column",i,x[i]);

    getch();}elsequeens(k+1,n);

    }}

    }int place(int k,int j)

    {int i;for(i=1;i

  • 8/3/2019 ADA+Lab+All+Programs

    64/64

    Algorithms Lab Manual

    1 row 3 column

    2 row 1 column

    3 row 4 column

    4 row 2 column