5semada_new(pgms with op)

Upload: yahoorahul

Post on 10-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 5semada_new(Pgms With Op)

    1/31

    ALGORITHMS LABORATORY

    Subject Code: 06CSL58 I.A. Marks : 25

    Hours/Week : 03 Exam Hours: 03

    Total Hours : 42 Exam Marks: 50

    Implement the following using C/C++ language.

    1. Implement Recursive Binary search and Linear search and 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.

    2. Sort a given set of elements using the Heapsort method and determine the timerequired 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 takenversus n.

    3. Sort a given set of elements using Merge sort method and determine the time

    required to sort the elements. Repeat the experiment for different values of n, thenumber of elements in the list to be sorted and plot a graph of the time taken

    versus n.

    4. Sort a given set of elements using Selection sort and determine the time required

    to sort 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.

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

    b. Implement All Pair Shortest paths problem using Floyd's algorithm.

    6. Implement 0/1 Knapsack problem using dynamic programming.

    7. From a given vertex in a weighted connected graph, find shortest paths to othervertices using Dijkstra's algorithm.

    8. Sort a given set of elements using Quick sort method and determine the time

    required sort the elements. Repeat the experiment for different values of n, thenumber of elements in the list to be sorted and plot a graph of the time taken

    versus n.

    9. Find Minimum Cost Spanning Tree of a given undirected graph using

    Kruskal's algorithm.

  • 8/8/2019 5semada_new(Pgms With Op)

    2/31

    10. a. Print all the nodes reachable from a given starting node in a digraph using BFS

    method.

    b. Check whether a given graph is connected or not using DFS method.

    11. Find a subset of a given set S = {sl,s2,.....,sn} of n positive integers whose sum is

    equal to a given positive integer d. For example, if S= {1, 2, 5, 6, 8} and d = 9there 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.

    12. a. Implement Horspool algorithm for String Matching.

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

    13. Find Minimum Cost Spanning Tree of a given undirected graph using Prims algorithm.

    14. a. Implement Floyds algorithm for the All-Pairs- Shortest-Paths problem.

    b. Compute the transitive closure of a given directed graph using Warshall's algorithm.

    15. Implement N Queen's problem using Back Tracking.

    Note: In the examination questions must be given based on ots.

    /* Program1.PERFORM RECURSIVE BINARY SEARCH AND LINEAR SEARCH AND HENCEFIND THE TIME REQUIRED TO SEARCH AN ELEMENT */

  • 8/8/2019 5semada_new(Pgms With Op)

    3/31

    #include#include#include

    int binsearch(int a[],int key,int first,int last){int mid;if(first > last)

    return -1;else{mid=(first+last)/2;if(key < a[mid])

    return binsearch(a,key,first,mid-1);else if(key > a[mid])

    return binsearch(a,key,mid+1,last);else

    return mid;}

    }

    int linsearch(int a[10],int i,int n,int key){if(i= 3)exit(0);else{

    printf("\n Enter the number of elements : ");scanf("%d",&n);printf("\n Enter the elements \n");for(i=0;i

  • 8/8/2019 5semada_new(Pgms With Op)

    4/31

    start=clock();pos=binsearch(a,key,0,n-1);delay(100);end=clock();duration=(end-start)/CLK_TCK;printf("\n Time taken is %f\n",duration);if(pos==-1)printf("\n key is not found\n");

    elseprintf("\n key is found\n");

    break;case 2:

    start=clock();pos=linsearch(a,0,n,key);delay(100);end=clock();duration=(end-start)/CLK_TCK;printf("\n Time taken is %f",duration);if(pos==-1)

    printf("\n key not found\n");

    elseprintf("\n Key is found\n");

    break;

    }getch();}

    }}Output :

    1.Binary search2.Linear search3.exit

    Enter your choice :1

    Enter the number of elements : 5

    Enter the elements1122334455

    Enter the key to be searched : 33

    Time taken is 0.109890

    key is found1.Binary search2.Linear search3.exit

  • 8/8/2019 5semada_new(Pgms With Op)

    5/31

    Enter your choice :2

    Enter the number of elements : 4

    Enter the elements1638

    Enter the key to be searched : 8

    Time taken is 0.109890Key is found

    1.Binary search2.Linear search3.exit

    Enter your choice :1

    Enter the number of elements : 4

    Enter the elements23456778

    Enter the key to be searched : 11

    Time taken is 0.109890key is not found1.Binary search2.Linear search3.exit

    Enter your choice :3

    /* Program2. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING THE HEAPSORT*/#include#includevoid heapcon(int a[],int n){int i,j,k,item;for(i=2;i a[k])

  • 8/8/2019 5semada_new(Pgms With Op)

    6/31

    {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/8/2019 5semada_new(Pgms With Op)

    7/31

    getch();}

    Output :

    Enter the number of elements : 8Enter the elements138643385423863The sorted array is8132338

    43546386

    /* Program3. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING MERGE SORTMETHOD */

    #include#include

    void merge(int a[],int low,int mid,int high){int i,j,k,b[15];i=low;j=mid+1;k=low;while((i

  • 8/8/2019 5semada_new(Pgms With Op)

    8/31

  • 8/8/2019 5semada_new(Pgms With Op)

    9/31

  • 8/8/2019 5semada_new(Pgms With Op)

    10/31

    Enter the paths (0 if edge is not present,1 if present0 1 1 01 0 0 01 0 0 10 0 1 0

    Enter the source vertex :2

    The graph is connected

    Enter the number of nodes : 4

    Enter the paths (0 if edge is not present,1 if present0 1 1 01 0 0 01 0 0 00 0 0 0

    Enter the source vertex :1

    The graph is not connected

    Enter the number of nodes : 3

    Enter the paths (0 if edge is not present,1 if present0 1 10 0 00 0 0

    Enter the source vertex :3

    The graph is not connected

    /* Program4. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING SELECTIONSORT AND HENCE FIND THE TIME REQUIRED TO SORT ELEMENTS */#include#include#includeint min(int a[],int k,int n){

    int loc,j,min;min=a[k];loc=k;for(j=k+1;j a[j]){min=a[j];loc=j;}

  • 8/8/2019 5semada_new(Pgms With Op)

    11/31

    }return loc;

    }void main(){int i,a[20],n,k,pos=0,temp;clock_t start,end;float duration;clrscr();printf("\n Enter the number of elemnts : ");scanf("%d",&n);printf("\n Enter the elements of the array :\n");for(i=0;i

  • 8/8/2019 5semada_new(Pgms With Op)

    12/31

    int temp[10],k=0;void topo(int n,int indegree[10],int a[10][10]){ int i,j;

    for(i=1;i

  • 8/8/2019 5semada_new(Pgms With Op)

    13/31

    /* Program5b. SORT A GIVEN SET OF ELEMENTS USING INSERTION SORT */

    #include#include

    void insertsort(int a[],int n){int i,j,item;for(i=1; i=0 && item < a[j];j--){a[j+1]=a[j];

    }a[j+1]=item;

    }

    }

    void main(){int i,a[20],n;clrscr();printf("\n\n Enter the number of elements : ");scanf("%d",&n);printf("\n\n Enter the elments : \n");for(i=0;i

  • 8/8/2019 5semada_new(Pgms With Op)

    14/31

    int max(int a,int b){

    return a>b?a:b;}

    int knap(int i,int m){

    if (i==n) return w[i]>m?0:p[i];if (w[i]>m) return knap(i+1,m);returnmax(knap(i+1,m),knap(i+1,m-w[i])+p[i]);

    }

    void main(){

    int m,i,max_profit;clrscr();printf ("\n Enter the number of objects : ");scanf ("%d",&n);

    printf ("\n Enter the knapsack capacity:");scanf("%d",&m);printf ("\n Enter profit followed by weight:\n");for (i=1;i

  • 8/8/2019 5semada_new(Pgms With Op)

    15/31

    {distance[i]=cost[source][i];visited[i]=0;

    }visited[source]=1;for(i=1;i

  • 8/8/2019 5semada_new(Pgms With Op)

    16/31

    SHORTEST DISTANCE FROM 1 TO 1 IS 0

    SHORTEST DISTANCE FROM 1 TO 2 IS 5

    SHORTEST DISTANCE FROM 1 TO 3 IS 12

    SHORTEST DISTANCE FROM 1 TO 4 IS 13

    SHORTEST DISTANCE FROM 1 TO 5 IS 12

    /* Program8. PROGRAM TO SORT A GIVEN SET OF ELEMENTS USING QUICK SORT */#include#includeint partition(int a[],int low,int high)

    {int i,j,temp,key;key=a[low];i=low+1;j=high;while(1){while(i=a[i])i++;while(key

  • 8/8/2019 5semada_new(Pgms With Op)

    17/31

    void main(){int i,n,a[20];clrscr();printf("\n Enter the number of elements : ");scanf("%d",&n);printf("\n Enter the elements \n");for(i=0;i

  • 8/8/2019 5semada_new(Pgms With Op)

    18/31

  • 8/8/2019 5semada_new(Pgms With Op)

    19/31

    Enter the number of vertices : 4

    Enter the cost adjaceny matrix 0- for self edge and 999-if no edge0 20 2 99920 0 15 52 15 0 25999 5 25 999

    cost of spanning tree is 22

    Edges of spanning tree are1->32->42->3

    Enter the number of vertices : 7

    Enter the cost adjaceny matrix 0 - for self egde and 999 if no edge0 28 999 999 999 10 999

    28 0 16 999 999 999 14999 16 999 12 999 999 999999 999 12 0 22 999 18999 999 999 22 0 25 2410 999 999 999 25 0 999999 14 999 18 24 999 0

    cost of spanning tree is 99Edges of spanning tree are1->63->42->72->34->55->6

    /* Program10a. PRINT ALL THE NODES REACHABLE FROM A GIVEN STARTINGNODE IN A DIAGRAPH USING BREADTH FIRST SEARCH METHOD */

    #include#includeint visited[10];

    void bfs(int n,int a[10][10],int source){

    int i,q[10],u;int front=1,rear=1;visited[source]=1;q[rear]=source;while(front

  • 8/8/2019 5semada_new(Pgms With Op)

    20/31

    front=front+1;for(i=1;i

  • 8/8/2019 5semada_new(Pgms With Op)

    21/31

    0 1 1 00 0 0 00 0 0 00 1 0 0

    Enter the source :1

    The node 1 is reachableThe node 2 is reachableThe node 3 is reachableThe node 4 is not reacable

    /* Program10b.IMPLEMENT ALL PAIR SHORTEST PATHS PROBLEM USING FLOYD'SALGORITHM */

    #include

    #include#define INFINITY 999int min(int a,int b){return a

  • 8/8/2019 5semada_new(Pgms With Op)

    22/31

    output :Enter the number of vertices: 4

    Enter the cost matrix 0 - self loop and 999 for no edge0 999 3 9992 0 999 999999 7 0 16 999 999 0

    Shortest path matrix0 10 3 42 0 5 67 7 0 16 16 9 0

    /* program11. FIND A SUBSET OF A GIVEN SET S={s1,S2,Sn} OF N POSITIVEINTEGERS WHOSE SUM IS EQUAL TO A GIVEN POSITIVE INTEGER d.FOR EXAMPLE

    S={1,2,5,6,8} AND d=9 THERE ARE TWO SOLUTIONS {1,2,6} AND {1,8}. A SUITABLEMESSAGE IS TO BE DISPLAYED IF THE GIVEN PROBLEM INSTANCE DOESNT HAVE ASOLUTION. */#include#include#define MAX 10int s[MAX],x[MAX];int d;void sumofsub(int p, int k, int r)

    {int i;x[k]=1;if ((p + s[k]) == d){for(i=1; i

  • 8/8/2019 5semada_new(Pgms With Op)

    23/31

    printf("\n Enter the max. subset value : ");scanf("%d",&d);for(i=1;i d)printf("\n No subset possible");

    elsesumofsub(0,1,sum);getch();

    }

    Output :

    Enter max. number : 5Enter the set in increasing order :1 2 5 6 8

    Enter the max. subset value : 91 2 61 8

    /* program12a. IMPLEMENT HORSPOOL ALGORITHM FOR STRING MATCHING */#include#include#include#define MAX 256int t[MAX];void shifttable(char pat[]){int i,j,m;m=strlen(pat);for(i=0;i

  • 8/8/2019 5semada_new(Pgms With Op)

    24/31

    {char src[100],pat[10];int pos;clrscr();printf("\n Enter the main source string\n");gets(src);printf("\n Enter the pattern to be searched\n");gets(pat);shifttable(pat);pos=horspool(src,pat);if(pos>=0)printf("\n Found at %d position ",pos+1);elseprintf("\n String match failed");getch();

    }

    Output :

    Enter the main source string

    This is ada lab programEnter the pattern to be searchedlabFound at 13 position

    /* Program12b.FIND THE BINOMIAL COEFFICIENT USING DYNAMIC PROGRAMMING */

    #include#includeint binomial(int n,int k){int i,j,a[10][10];for(i=0;i

  • 8/8/2019 5semada_new(Pgms With Op)

    25/31

    printf("\n Enter the value for k : ");scanf("%d",&k);res=binomial(n,k);printf("\n The binomial co-efficient is %d\n",res);getch();

    }

    output :

    Enter the value for n : 4Enter the value for k : 2The binomial co-efficient is 6

    /* program13. FIND MINIMUM COST SPANNING TREE OF A GIVEN UNDIRECTEDGRAPH USING PRIMS ALGORITHM */#include

    #include#define INFINITY 999

    int prim (int cost[10][10],int source,int n){

    int i,j,sum=0,visited[10],cmp[10],vertex[10];int min,u,v;for (i=1;i

  • 8/8/2019 5semada_new(Pgms With Op)

    26/31

    {int a[10][10],n,i,j,m,source;clrscr();printf("\nEnter the number of vertices :");scanf("%d",&n);printf("\nEnter the cost matrix: 0 self loop & 999 - no edge\n");for (i=1;i 2 sum = 303 - > 4 sum = 70

    Cost = 70

    Enter the number of vertices : 5Enter the cost matrix: 0 self loop & 999 - no edge0 11 9 7 811 0 15 14 139 15 0 12 147 14 12 0 68 13 14 6 0

    Enter the source : 1

    1 - > 4 sum=74 - > 5 sum=131 - > 3 sum=22

    1 - > 2 sum=33

    Cost=33

    /* Program14a.PRINT ALL THE NODES REACHABLE FROM A GIVEN STARTING NODEIN A GIVEN DIAGRAPH USING DFS METHOD */

    #include#include

  • 8/8/2019 5semada_new(Pgms With Op)

    27/31

    int visited[10];

    void dfs(int n,int a[10][10],int source){int i;visited[source]=1;for(i=1;i

  • 8/8/2019 5semada_new(Pgms With Op)

    28/31

    1 0 0 10 0 0 0

    Enter the source vertex :2

    The node 1 is not reacableThe node 2 is reachableThe node 3 is not reacableThe node 4 is reachable

    /* Program14b. COMPUTE THE TRANSITIVE CLOSURE OF A GIVEN DIRECTED GRAPHUSING WARSHALL'S ALGORITHM */

    #include#include

    void warsh(int p[10][10],int n){

    int i,j,k;

    for (k=1;k

  • 8/8/2019 5semada_new(Pgms With Op)

    29/31

    0 0 1 01 0 0 10 0 0 0

    The resultant Path matrix is1 1 1 11 1 1 11 1 1 10 0 0 0

    /* Program15. IMPLEMENT N QUENN'S PROBLEM USING BACK TRACKING */

    #include #include #include

    #define MAX 50

    int can_place (int c[], int r){

    int i;

    /* Two queens attack if: they are in the same row/column, or* if they are diagonally opposite to each other. */

    for (i=0; i

  • 8/8/2019 5semada_new(Pgms With Op)

    30/31

    int r; /* Current row. */int c[MAX]; /* Stores the queen's column position for each row. */

    c[0] = -1; /* Initially no column is found. */r = 0; /* Start from row 1. */

    /* If row < 0, no solution possible. */while (r >= 0){c[r]++; /* Start from the col. before which we stopped. */

    /* Move ahead until we get a non-attacking column. */while (c[r] < n && !can_place (c, r))

    c[r]++;

    /* Did we get a column at all? */if (c[r] < n){

    /* Was this the column for the last row? Then we're done. */if (r == n - 1){display (c, n);printf(\n\n);}/* No? Good, now find the column for the next row. */else{r++;c[r] = -1;}

    }/* No such column found. Backtrack and find another column. */else

    r--;}

    }

    void main(){

    int n; /* Number of queens. */clrscr();printf ("\n Enter the number of queens :");scanf("%d",&n);n_queens(n);getch();

    }

    output :

    Enter the number of queens : 4

    - Q - -- - - QQ - - -- - Q -

  • 8/8/2019 5semada_new(Pgms With Op)

    31/31

    - - Q -Q - - -- - - Q- Q - -