final ada lab manual

Upload: shivdev

Post on 05-Jul-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/15/2019 Final Ada Lab Manual

    1/62

    LAB MANUAL

    of 

    ANALYSIS AND DESIGN OF ALGORITHMS LABORATORY

    ( CSL52 )

    for

    V Semester

    De!"rtme#t of CSE

    S$%%"&"#&" I#st$t'te of Te#o*o&+

    T'm,'r 5-2./0

    1r"o% C 1

    Asst3 1rofessor

    De!t of CSE

    SIT T'm,'r 5-2./0

    T$s 4or, $s *$e#se% '#%er "

    Cre"t$e Commo#s Attr$'t$o#6No#Commer$"*6S"reA*$,e 03/ U#!orte% L$e#se3

    http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/

  • 8/15/2019 Final Ada Lab Manual

    2/62

    Question 1a

    Use divide and conquer method to recursively implement Binary Search

    /*******************************************************************************File : BinarySearch.c*Description : Program to perform Binary Search using Divide & Conuer*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : Friday )+ ,ovem"er )(%******************************************************************************/

    -includestdio.hint fnBinSearch0int !12 int 3 int i4o5int i6igh78

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    3/62

    return iGid8if03!1iGid27

    return fnBinSearch0!3i4o5iGidE(78if03!1iGid27

    return fnBinSearch0!3iGid(i6igh78else

    return E(8

    /******************************************************************************H'

  • 8/15/2019 Final Ada Lab Manual

    4/62

    Question 1a

    Use divide and conquer method to recursively implement Linear Search

    /*******************************************************************************File : 4inearSearch.c*Description : Program to perform 4inear Search*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : Friday (% Decem"er )(%******************************************************************************/-includestdio.hint fn4inSearch0int 12 int int78

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    5/62

    Question 1b

    Use divide and conquer method to recursively implement and to find the maximum

    and minimum in a given list of n elements.

    /*******************************************************************************File : GaLGin.c

    *Description : Program to find the maLimum and minimum in a given list* of n elements using divide and conuer.*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : (( ,ov )(%******************************************************************************/

    -include stdio.h-include stdli".h

    void fnecGaLGin0int 12 int int int* int*78/*******************************************************************************Function : main

    *9nput parameters:* int argc E no of commamd line arguments* char **argv E vector to store command line argumennts*;

  • 8/15/2019 Final Ada Lab Manual

    6/62

    *maL ? a1lo528*min ? a1high28

    else=

    *maL ? a1high28*min ? a1lo528

    else if0lo5 ?? high7=

    *min ? *maL ? a1lo528else if0lo5high7=

     mid?0lo5high7/)8fnecGaLGin0alo5mid&maL(&min(78fnecGaLGin0amid(high&maL)&min)78if0maL( maL)7

    *maL ? maL(8

    else *maL ? maL)8

    if0min( min)7*min ? min(8

    else*min ? min)8

    /******************************************************************************H'

  • 8/15/2019 Final Ada Lab Manual

    7/62

    Question 2

    Sort a given set of elements using the Merge sort method 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.

    The elements can be read from a file or can be generated using the random number

     generator./*******************************************************************************File : GergeSort.c*Description : Program to sort an array using Gerge Sort*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : (( ,ov )(%******************************************************************************/

    -include stdio.h-include stdli".h-include sys/time.h-include time.h

    void fnMenand9nput0int 12 int78void fnDisp!rray0 int 12 int78void fnGerge0int 12 int int int78void fnGergeSort0int 12 int int78

    /*******************************************************************************Function : main*9nput parameters:* int argc E no of commamd line arguments* char **argv E vector to store command line argumennts*;

  • 8/15/2019 Final Ada Lab Manual

    8/62

      fnGergeSort0ia!rriE(78

    gettimeofday0&tv,'4478d;nd ? tv.tvNsec 0tv.tvNusec/(.78

    fprintf0fp@dAtlfAn@id;ndEdStart78

    fclose0fp78

     printf0@AnData File generated and stored in file  GergePlot.dat .An 'se a plotting utilityAn@78

     "rea38

    case ): printf0@An;nter the num"er of elements to sortAn@78scanf0@d@&i,um78

     printf0@An'nsorted !rrayAn@78fnMenand9nput0ia!rri,um78

    fnDisp!rray0ia!rri,um78fnGergeSort0ia!rri,umE(78 printf0@AnSorted !rrayAn@78fnDisp!rray0ia!rri,um78

     "rea38

    case %:eLit078

    return 8

    /*******************************************************************************Function : fnGerge*Description : Function to merge t5o sorted arrays*9nput parameters:* int a12 E ia!rray to hold integers* int lo5 E start indeL of the su"ia!rray to "e sorted* int mid E mid indeL of the su"ia!rray to "e sorted* int right E end indeL of the su"ia!rray to "e sorted*;

  • 8/15/2019 Final Ada Lab Manual

    9/62

    for0i?lo58i38i7a1i2?"1i28

    /*******************************************************************************Function : fnGergeSort*Description : Function to sort elements in an ia!rray using uic3 Sort

    *9nput parameters:* int a12 E ia!rray to hold integers* int lo5 E start indeL of the array to "e sorted* int highE end indeL of the array to "e sorted*;

  • 8/15/2019 Final Ada Lab Manual

    10/62

    /******************************************************************************gnuplot plot @./GergePlot.tLt@gnuplot plot @./GergePlot.tLt@ 5ith linesgnuplot plot @./GergePlot.tLt@ 5ith linespoints******************************************************************************/

     GergePlot.gpl

    - Mnuplot script file for plotting data in file @GergePlot.dat@-

  • 8/15/2019 Final Ada Lab Manual

    11/62

    Question 3

    Sort a given set of elements using the Quick sort method 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.

    The elements can be read from a file or can be generated using the random number

     generator./*******************************************************************************File : uic3Sort.c*Description : Program to sort an array using uic3 Sort*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : (( ,ov )(%******************************************************************************/

    -include stdio.h-include stdli".h-include sys/time.h-include time.h

    void fnMenand9nput0int 12 int78void fnDisp!rray0 int 12 int78int fnPartition0int 12 int int 78void fnuic3Sort0int 12 int int 78inline void fnS5ap0int* int*78

    inline void fnS5ap0int *a int *"7=

    int t ? *a8 *a ? *"8 *" ? t8

    /*******************************************************************************Function : main*9nput parameters:* int argc E no of commamd line arguments* char **argv E vector to store command line argumennts*;

  • 8/15/2019 Final Ada Lab Manual

    12/62

      fnMenand9nput0ia!rri78

    gettimeofday0&tv,'4478dStart ? tv.tvNsec 0tv.tvNusec/(.78

    fnuic3Sort0ia!rriE(78

    gettimeofday0&tv,'4478d;nd ? tv.tvNsec 0tv.tvNusec/(.78

    fprintf0fp@dAtlfAn@id;ndEdStart78fclose0fp78

     printf0@AnData File generated and stored in file uic3Plot.dat .An 'se a plotting utilityAn@78

     "rea38

    case ): printf0@An;nter the num"er of elements to sortAn@78

    scanf0@d@&i,um78 printf0@An'nsorted !rrayAn@78fnMenand9nput0ia!rri,um78fnDisp!rray0ia!rri,um78fnuic3Sort0ia!rri,umE(78

     printf0@AnSorted !rrayAn@78fnDisp!rray0ia!rri,um78

     "rea38

    case %:eLit078

    return 8

    /*******************************************************************************Function : fnPartition*Description : Function to partition an ia!rray using First element asPivot*9nput parameters:* int a12 E ia!rray to hold integers* int l E start indeL of the su"ia!rray to "e sorted* int r E end indeL of the su"ia!rray to "e sorted*;

  • 8/15/2019 Final Ada Lab Manual

    13/62

    fnS5ap0&a1i2 &a1O278

     5hile 0iO78

    fnS5ap0&a1i2 &a1O278fnS5ap0&a1l2 &a1O278

    return O8

    /*******************************************************************************Function : fnuic3Sort*Description : Function to sort elements in an ia!rray using uic3 Sort*9nput parameters:* int a12 E ia!rray to hold integers* int l E start indeL of the su"ia!rray to "e sorted* int r E end indeL of the su"ia!rray to "e sorted*;

  • 8/15/2019 Final Ada Lab Manual

    14/62

    /******************************************************************************gnuplot plot @./uic3Plot.tLt@gnuplot plot @./uic3Plot.tLt@ 5ith lines******************************************************************************/

    uic3Plot.gpl

    - Mnuplot script file for plotting data in file @uic3Plot.dat@-

  • 8/15/2019 Final Ada Lab Manual

    15/62

    Question 4a

    Obtain the Topological ordering of vertices in a given digraph.

    /*******************************************************************************File :

  • 8/15/2019 Final Ada Lab Manual

    16/62

     5hile0(7=

    for 0i?8in8i7=

    if 0in1i2 ?? 7=

    stac31top2 ? i8

    in1i2 ? E(8

    if 0top ?? E(7 "rea38

    out132 ? stac31topEE28

    for 0i?8in8i7=

    if 0a1out13221i2 ?? (7in1i2EE8

    38

     printf0@

  • 8/15/2019 Final Ada Lab Manual

    17/62

    Question 4b

    Sort a given set of elements using Insertion sort method.

    /*******************************************************************************File : 9nsertionSort.c*Description : Program to sort an array using 9nsertion Sort*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : )) ,ov )(%******************************************************************************/

    -include stdio.h-include stdli".h-include sys/time.h-include time.h

    void fnMenand9nput0int 12 int78void fnDisp!rray0 int 12 int78void fn9nsertionSort0int 12 int78

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    18/62

      printf0@AnData File generated and stored in file 9nsertionPlot.dat .An 'se a plotting utilityAn@78

     "rea38

    case ): printf0@An;nter the num"er of elements to sortAn@78scanf0@d@&i,um78

     printf0@An'nsorted !rrayAn@78fnMenand9nput0ia!rri,um78fnDisp!rray0ia!rri,um78fn9nsertionSort0ia!rri,um78

     printf0@AnSorted !rrayAn@78fnDisp!rray0ia!rri,um78

     "rea38

    case %:eLit078

    return 8

    /*******************************************************************************Function : fn9nsertionSort*Description : Function to sort an array using 9nsertion Sort*9nput parameters:* int !12 E ia!rray to hold integers* int n E no of elements in the array*;

  • 8/15/2019 Final Ada Lab Manual

    19/62

    =Q1i2 ? rand07(8

    /*******************************************************************************Function : Disp!rray*Description : Function to display elements of an array

    *9nput parameters:* int Q12 E array to hold integers* int n E no of elements in the array*;

  • 8/15/2019 Final Ada Lab Manual

    20/62

    Question 5a

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

     Depth First Search method.

    /*******************************************************************************File : DFS.cpp

    *Description: Program to find all nodes reacha"le from a given node using DFS*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : Uednesday )J ,ovem"er )(%******************************************************************************/-include iostream-include cstdli"using namespace std8

    const int G!Q ? (8

    void fnDepthFirstSearch0int currentVerteL int v1G!Q2 int g1G!Q21G!Q2 int n78

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    21/62

      return 8

    /*******************************************************************************Function : fnDepthFirstSearch*Description : Function to perform DFS traversal and mar3 visited vertices*9nput parameters:

    * int currentVerteL E source verteL* int v12 E vector to store visited information* int g1212 E adOacency matriL of the graph* int n E no of vertices*;

  • 8/15/2019 Final Ada Lab Manual

    22/62

    ( ( ( ;nter the source verteL : )

     VerteL ( not reacha"le

     VerteL ) not reacha"le VerteL % reacha"le VerteL # reacha"le

    S!GP4; #

    ;nter the num"er of vertices : #;nter the adOacency matriL : ( ( (( ;nter the source verteL : )

     VerteL ( reacha"le VerteL ) reacha"le VerteL % reacha"le VerteL # reacha"le******************************************************************************/

    Prabodh C P, Dept of CSE, SIT, Tumkur 

  • 8/15/2019 Final Ada Lab Manual

    23/62

    Question 5b

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

     Breadth First Search method.

    /*******************************************************************************File : BFS.cpp

    *Description: Program to find all nodes reacha"le from a given node using BFS*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : Uednesday )J ,ovem"er )(%******************************************************************************/-include iostream-include cstdli"using namespace std8

    const int G!Q ? (8void fnBreadthFirstSearcheach0int verteL int g1G!Q21G!Q2 int v1G!Q2 int n78

    class ueue

    =  private:int cueue1G!Q28int front rear8

     pu"lic:ueue078Xueue078int enueue0int data78int deueue078int empty07 = return front ?? E( Y ( : 8 8

    8

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    24/62

    fnBreadthFirstSearcheach0startVertE(graphvisitednumVert78

    cout @Vertices 5hich can "e reached from verteL @ startVert @are :E@ endl8

    for 0i?8 inumVert8 i7if 0visited1i27

    cout i( @ @8

    cout endl8return 8/*Constructor*/ueue::ueue07=

    front ? rear ? E(8

    /*Destructor*/ueue::Xueue07=

    /*******************************************************************************Function : enueue*Description : Function to insert an element at the rear of a ueue*9nput parameters:*int data E element to "e inserted into the ueue*;

  • 8/15/2019 Final Ada Lab Manual

    25/62

    elsefront ? 0front(7G!Q8

    return data8

    /******************************************************************************

    *Function : fnBreadthFirstSearcheach*Description : Function to perform BFS traversal and mar3 visited vertices*9nput parameters:* int verteL E source verteL* int g1212 E adOacency matriL of the graph* int v12 E vector to store visited information* int n E no of vertices*;

  • 8/15/2019 Final Ada Lab Manual

    26/62

    ( ( ;nter the starting verteL : (

     Vertices 5hich can "e reached from verteL ( are :E( )

    S!GP4; %

    ;nter the num"er of vertices : #;nter the adOacency matriL : ( ( ( ;nter the starting verteL : )

     Vertices 5hich can "e reached from verteL ) are :E) % #

    S!GP4; #

    ;nter the num"er of vertices : #;nter the adOacency matriL : ( ( (( ;nter the starting verteL : )

     Vertices 5hich can "e reached from verteL ) are :E( ) % #

    ******************************************************************************/

    Prabodh C P, Dept of CSE, SIT, Tumkur 

  • 8/15/2019 Final Ada Lab Manual

    27/62

    Question 6

    Sort a given set of elements using the Heap sort method 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.

    The elements can be read from a file or can be generated using the random number

     generator./*******************************************************************************File : 6eapSort.c*Description : Program to sort an array using 6eap Sort*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : Friday )) ,ovem"er )(%******************************************************************************/

    -include stdio.h-include stdli".h-include sys/time.h-include time.h

    void fnMenand9nput0int 12 int78void fnDisp!rray0 int 12 int78void fnGaL6eapify0int12intint78void fnBuildGaL6eap0int12int78void fn6eapSort0int12int78

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    28/62

      gettimeofday0&tv,'4478d;nd ? tv.tvNsec 0tv.tvNusec/(.78

    fprintf0fp@dAtlfAn@id;ndEdStart78

    fclose0fp78

     printf0@AnData File generated and stored in file 6eapPlot.dat.An 'se a plotting utilityAn@78

     "rea38

    case ): printf0@An;nter the num"er of elements to sortAn@78scanf0@d@&i,um78

     printf0@An'nsorted !rrayAn@78fnMenand9nput0ia!rri,um78fnDisp!rray0ia!rri,um78

    fn6eapSort0ia!rri,um78 printf0@AnSorted !rrayAn@78

    fnDisp!rray0ia!rri,um78 "rea38

    case %:eLit078

    return 8

    /*******************************************************************************Function : fnGaL6eapify*Description : Function to recreate a maL heap*9nput parameters:* int a12 E !rray to hold integers* int i E start indeL of the !rray* int n E end indeL of the !rray*;

  • 8/15/2019 Final Ada Lab Manual

    29/62

    /*******************************************************************************Function : fnBuildGaL6eap*Description : Function to create a maL heap

    *9nput parameters:* int a12 E !rray to hold integers* int n E num"er of elements in the !rray*;

  • 8/15/2019 Final Ada Lab Manual

    30/62

    /*******************************************************************************Function : Disp!rray*Description : Function to display elements of an array*9nput parameters:* int Q12 E array to hold integers* int n E no of elements in the array*;

  • 8/15/2019 Final Ada Lab Manual

    31/62

    Question 7a

     Implement Horspool algorithm for String Matching.

    /*******************************************************************************File : 6orspool.c*Description: Program to find a matching su" string in a given teLt using

    6orspoolRs !lgorithm*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : )( ,ov )(%******************************************************************************/-include stdio.h-include string.h

    const int G!Q ? )I$8

    int fn6orspool0char string12 char pattern12int 1278void fnMenShift

  • 8/15/2019 Final Ada Lab Manual

    32/62

    t1i2?m8for0O?8 OmE(8 O7=

    t1p1O22 ? mE(EO8

    /*******************************************************************************Function : fn6orspool*Description : Function to search for a matching su"string for a given

     pattern in the teLt*9nput parameters:* char s12 E teLt string* char p12 E pattern to "e searched for* int t12 E shift ta"le containing shift values for each alpha"et*;

  • 8/15/2019 Final Ada Lab Manual

    33/62

    Question 7b

     For a given set of elements construct an AVL Tree and also display balance factor for

    each node.

    /*******************************************************************************File : !V4.c

    *Description: Program to construct an !V4

  • 8/15/2019 Final Ada Lab Manual

    34/62

     printf0@9norder

  • 8/15/2019 Final Ada Lab Manual

    35/62

     pptrE"alance ? (8 "rea38case (: /* 4eft heavy */aptr ? pptrElchild8if0aptrE"alance ?? (7=

     printf0@An otation performed during

    9nsertionAnAn@78  pptrElchild? aptrErchild8aptrErchild ? pptr8

     pptrE"alance ? 8aptrE"alance?8

     pptr ? aptr8else=

     printf0@An4 otation performed during9nsertionAnAn@78

     "ptr ? aptrErchild8aptrErchild ? "ptrElchild8

     "ptrElchild ? aptr8 pptrElchild ? "ptrErchild8 "ptrErchild ? pptr8if0"ptrE"alance ?? ( 7

     pptrE"alance ? E(8else

     pptrE"alance ? 8if0"ptrE"alance ?? E(7

    aptrE"alance ? (8else

    aptrE"alance ? 8 "ptrE"alance?8 pptr?"ptr8

    *htNinc ? false8

    if0info pptrEinfo7=

     pptrErchild ? insert0info pptrErchildhtNinc78if0*htNinc??true7=

    s5itch0pptrE"alance7=case (: /* 4eft heavy */

     pptrE"alance ? 8*htNinc ? false8

     "rea38case : /* Balanced */

     pptrE"alance ? E(8 "rea38

    case E(: /* ight heavy */aptr ? pptrErchild8if0aptrE"alance ?? E(7=

     printf0@An4 otation performed during 9nsertionAnAn@78 pptrErchild? aptrElchild8aptrElchild ? pptr8

     pptrE"alance ? 8aptrE"alance?8

    Prabodh C P, Dept of CSE, SIT, Tumkur 

  • 8/15/2019 Final Ada Lab Manual

    36/62

     pptr ? aptr8else=

     printf0@An4 otation performed during 9nsertionAnAn@78 "ptr ? aptrElchild8aptrElchild ? "ptrErchild8

     "ptrErchild ? aptr8 pptrErchild ? "ptrElchild8 "ptrElchild ? pptr8if0"ptrE"alance ?? E(7

     pptrE"alance ? (8else

     pptrE"alance ? 8if0"ptrE"alance ?? (7

    aptrE"alance ? E(8else

    aptrE"alance ? 8 "ptrE"alance?8 pptr ? "ptr8

    *htNinc ? false8

    return0pptr78

    /*******************************************************************************Function : inorder*Description : Function to display inorder traversal of !V4 tree 5ith* "alance factor at each node*9nput parameters:* ,HD;P

  • 8/15/2019 Final Ada Lab Manual

    37/62

    Question 8a

     Implement 0/1 Knapsack problem using dynamic programming.

    /*******************************************************************************File : 3napSac3.cpp*Description: Program to solve /( >napsac3 pro"lem using dynamic programming.*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : (( ,ov )(%******************************************************************************/-include iostream-include cstdli"using namespace std8

    const int G!Q ? (8

    inline int maL0int a int "78void fnProfit

  • 8/15/2019 Final Ada Lab Manual

    38/62

    fnProfit

  • 8/15/2019 Final Ada Lab Manual

    39/62

    /*******************************************************************************Function : fnSelect9tems*Description: Function to determine optimal su"set that fits into the 3napsac3*9nput parameters:

    * int n E no of items* int c E 3napsac3 capacity* int t1G!Q21G!Q2 E profit ta"le* int 51G!Q2 E 5eight vector* int l1G!Q2 E "it vector representing the optimal su"set*;

  • 8/15/2019 Final Ada Lab Manual

    40/62

  • 8/15/2019 Final Ada Lab Manual

    41/62

    =int iO38

    for 03?8 3numVert8 37=

    for 0i?8 inumVert8 i7=

    for 0O?8 OnumVert8 O7=if 0graph1i21O2 ZZ 0graph1i2132 && graph1321O277

    graph1i21O2 ? (8

    /****************************************************************************** UarshallRs

  • 8/15/2019 Final Ada Lab Manual

    42/62

    Question 9a

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

    /****************************************************************************File : Floyd.c*Description: Program to implement FloydRs !lgorithm*!uthor : Pra"odh C P*Compiler : gcc compiler '"untu ().#*Date : ) ,ovem"er )(%***************************************************************************/

    -include stdio.h-include stdli".h

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    43/62

    for0O?8Oi,8O7=

    iaFloyd1i21O2 ? 0iaFloyd1i21O2 0iaFloyd1i2132 iaFloyd1321O277Y 0iaFloyd1i21O27:0iaFloyd1i2132 iaFloyd1321O278

     printf0@An!ll Pair Shortest Path GatriLAn@78

    for0i?8ii,8i7=

    for0O?8Oi,8O7=

     printf0@dAt@iaFloyd1i21O278

     printf0@An@78

     printf0@An@78

    return 8

    /***************************************************************************Compiling instructions gcc filename.c Eo F4H[D.L;Lecution./F4H[D.L

    H'

  • 8/15/2019 Final Ada Lab Manual

    44/62

  • 8/15/2019 Final Ada Lab Manual

    45/62

    c1i21i2 ? (8

    for 0i?(8 i?n8 i7for 0O?(8 Oi8 O7

    c1i21O2 ? c1iE(21OE(2 c1iE(21O28

    cout @An

  • 8/15/2019 Final Ada Lab Manual

    46/62

    Question 10

     From a given vertex in a weighted connected graph, find shortest paths to other

     vertices using Dijkstra's algorithm.

    /*******************************************************************************File : DiO3stra.cpp

    *Description : Program to find shortest paths to other vertices* using DiO3straRs algorithm.*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : Friday )) ,ovem"er )(%******************************************************************************/-include iostream-include cstdiousing namespace std8

    const int G!Q,HD;S ? (9,F ? KKKK8

    void fnDiO3stra0int 121G!Q,HD;S2 int 12 int 12 int12 int int int78

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    47/62

      cout i @EE@8i ? path1i28

    5hile 0iW? source78cout i @ ? @ dist1dest2 endl8

    cout @Press ;nter to continue...@8

    return 8

    /*******************************************************************************Function : fnDiO3stra*Description : Function to find shortest paths to other vertices* using DiO3straRs algorithm.*9nput parameters:* int c1212 E cost adOacency matriL of the graph* int d12 E distance vector* int p12 E path vector

    * int s12 E vector to store visited information* int so E source verteL* int de E destination verteL* int n E no of vertices in the graph*;

  • 8/15/2019 Final Ada Lab Manual

    48/62

     for 0"?8"n8"7=

    if 0s1"2 ?? 7=

    if 0d1a2 c1a21"2 d1"27=

    d1"2 ? d1a2 c1a21"28 p1"2 ? a8

    /******************************************************************************;nter the num"er of nodesI;nter the Cost GatriL

    % KKKK J KKKK% # ) KKKKKKKK # I $J ) I #KKKK KKKK $ #

    //For Source VerteL : shortest path to other vertices//

    EE ?

    (EE ? %

    )EE(EE ? J

    %EE(EE ? I

    #EE%EE(EE ? KPress ;nter to continue...

    //For Source VerteL : ( shortest path to other vertices//

    EE( ? %

    (EE( ?

    )EE( ? #

    %EE( ? )

    #EE%EE( ? $Press ;nter to continue...

    //For Source VerteL : ) shortest path to other vertices//

    EE(EE) ? J

    (EE) ? #

    )EE) ?

    %EE) ? I

    Prabodh C P, Dept of CSE, SIT, Tumkur 

  • 8/15/2019 Final Ada Lab Manual

    49/62

     #EE) ? $Press ;nter to continue...

    //For Source VerteL : % shortest path to other vertices//

    EE(EE% ? I

    (EE% ? )

    )EE% ? I

    %EE% ?

    #EE% ? #Press ;nter to continue...

    //For Source VerteL : # shortest path to other vertices//

    EE(EE%EE# ? K

    (EE%EE# ? $

    )EE# ? $

    %EE# ? #

    #EE# ? Press ;nter to continue...******************************************************************************/

    Prabodh C P, Dept of CSE, SIT, Tumkur 

  • 8/15/2019 Final Ada Lab Manual

    50/62

    Question 11

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

    /*******************************************************************************File : Prim.cpp*Description : Program to find Ginimum Cost Spanning

  • 8/15/2019 Final Ada Lab Manual

    51/62

     min ? KKKK8source ? 8

    for0i?8 in8 i7=

    for0O?8 On8 O7=

    if0cost1i21O2 W? && cost1i21O2 ? min7= min ? cost1i21O28source ? i8

    for0i?8 in8 i7=

    d1i2 ? cost1source21i28s1i2 ? 8

     p1i2 ? source8

    s1source2 ? (8sum ? 83 ? 8count ? 8

     5hile 0count W? nE(7=

     min ? KKKK8u ? E(8for0O?8 On8 O7=

    if0s1O2 ?? 7=

    if0d1O2 ? min7=

     min ? d1O28u ? O8

    t13212 ? u8t1321(2 ? p1u2838count8sum ? cost1u21p1u228s1u2 ? (8

    for0v?8 vn8 v7=

    if0s1v2?? && cost1u21v2d1v27=

    d1v2 ? cost1u21v28 p1v2 ? u8

    if0sum ? KKKK7cout @AnSpanning tree does not eListAn@8

    Prabodh C P, Dept of CSE, SIT, Tumkur 

  • 8/15/2019 Final Ada Lab Manual

    52/62

    else=

    cout @An

  • 8/15/2019 Final Ada Lab Manual

    53/62

  • 8/15/2019 Final Ada Lab Manual

    54/62

    * int parent12 E parent vector*;

  • 8/15/2019 Final Ada Lab Manual

    55/62

    int i small pos8small ? 9,F8

     pos ? E(8

    for0i?8 in8 i7=

    if0e1i2.cost small7

    = small ? e1i2.cost8 pos ? i8

    return pos8

    void 3rus3al0int n edge e12 int m7=

    int i O count 3 sum u v t1G!Q,HD;S21)2 pos8int parent1G!Q,HD;S28

    count ? 83 ? 8sum ? 8

    for0i?8 in8 i7=

     parent1i2 ? i8

     5hile0count W? nE(7=

     pos ? fnMetGin;dge0em78if0pos ?? E(7=

     "rea38u ? e1pos2.u8v ? e1pos2.v8i ? fnFindParent0uparent78O ? fnFindParent0vparent78

    if0i W? O7=

    t13212 ? u8t1321(2 ? v838count8sum ? e1pos2.cost8fn'nionNiO0i O parent78

    e1pos2.cost ? 9,F8

    if0count ?? nE(7=

    cout @AnSpanning tree eLists@8cout @An

  • 8/15/2019 Final Ada Lab Manual

    56/62

    else

    cout @An

  • 8/15/2019 Final Ada Lab Manual

    57/62

    Question 13

     Find a subset of a given set S = {s1 ,s 2 ,.....,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 = 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.

    /*******************************************************************************File : Su"set.cpp*Description: Program to solve Su"set sum pro"lem.*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : (( ,ov )(%******************************************************************************/-include iostreamusing namespace std8

    // Constant definitionsconst int G!Q ? (8

    // class definitionsclass Su"Set=

    int st31G!Q2 set1G!Q28int sie top count8

     pu"lic:Su"Set07=

    top ? E(8count ? 8

    void get9nfo0void78void push0int data78

    void pop0void78void display0void78int fnFindSu"set0int pos int sum78

    8

    /*******************************************************************************Function : get9nfo*Description: Function to read input*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    58/62

    *int data E value to "e pushed on to the stac3*;

  • 8/15/2019 Final Ada Lab Manual

    59/62

    display078foundSoln ? (8

    return foundSoln8

    /*******************************************************************************Function : main

    *9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    60/62

    Question 14

     Implement N Queen's problem using Back Tracking.

    /*******************************************************************************File : nueens.cpp*Description: Program to solve , ueens pro"lem using "ac3trac3ing.*!uthor : Pra"odh C P*Compiler : gcc compiler #.$.% '"untu ().#*Date : (( ,ov )(%******************************************************************************/-include iostream-include cstdli"using namespace std8

    const int G!Q ? (8

    int SolnCount ?8

    void fnChessBoardSho50int n int ro51G!Q278 "ool fnChec3Place0int >thueen int Col,um int ro51G!Q278int ,ueen0int 3int n int ro51G!Q278

    /*******************************************************************************Function : main*9nput parameters: no parameters*;

  • 8/15/2019 Final Ada Lab Manual

    61/62

    =ro5132 ? i8if03 ?? nE(7=

    fnChessBoardSho50nro578SolnCount8flag ? (8

    return flag8 ,ueen03( n ro578

    return flag8

    /*******************************************************************************Function : fnChec3Place*Description: Function to chec3 5hether a 3th ueen can "e palced in a specific* column or not*9nput parameters:* int >thueen E 3th ueen

    * int Col,um E columnn num"er* int ro51G!Q2 E vector containing column num"ers of each ueen*;

  • 8/15/2019 Final Ada Lab Manual

    62/62