Docsity

Running Time Analysis and Stacks - Data Structures and Algorithms | CS 25100, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS๎˜ƒ251๎˜ƒ
Spring๎˜ƒ2009๎˜ƒ
Voicu๎˜ƒPopescu๎˜ƒ
Assignment๎˜ƒ3๎˜ƒ
Due๎˜ƒFriday๎˜ƒFebruary๎˜ƒ6th,๎˜ƒ11:59PM๎˜ƒ
Running๎˜ƒTime๎˜ƒAnalysis๎˜ƒand๎˜ƒStacks๎˜ƒ
๎˜ƒ
1. Write๎˜ƒa๎˜ƒrecursive๎˜ƒfunction๎˜ƒthat๎˜ƒgiven๎˜ƒa๎˜ƒpositive๎˜ƒinteger๎˜ƒn๎˜ƒprints๎˜ƒout๎˜ƒthe๎˜ƒpermutations๎˜ƒof๎˜ƒ
the๎˜ƒnumbers๎˜ƒ{1,๎˜ƒ2,๎˜ƒโ€ฆ,๎˜ƒn}.๎˜ƒFor๎˜ƒn=3,๎˜ƒthe๎˜ƒpermutations๎˜ƒare๎˜ƒ{1,๎˜ƒ2,๎˜ƒ3},๎˜ƒ{1,๎˜ƒ3,๎˜ƒ2},๎˜ƒ{2,๎˜ƒ1,๎˜ƒ3},๎˜ƒ{2,๎˜ƒ3,๎˜ƒ
1},๎˜ƒ{3,๎˜ƒ1,๎˜ƒ2},๎˜ƒ{3,๎˜ƒ2,๎˜ƒ1}.๎˜ƒThe๎˜ƒorder๎˜ƒin๎˜ƒwhich๎˜ƒthe๎˜ƒpermutations๎˜ƒare๎˜ƒprinted๎˜ƒis๎˜ƒnot๎˜ƒimportant.๎˜ƒ
What๎˜ƒis๎˜ƒthe๎˜ƒBigโ€Oh๎˜ƒrunning๎˜ƒtime๎˜ƒof๎˜ƒthe๎˜ƒfunction?๎˜ƒWarning:๎˜ƒpay๎˜ƒvery๎˜ƒclose๎˜ƒattention๎˜ƒto๎˜ƒ
the๎˜ƒrequired๎˜ƒfile๎˜ƒname,๎˜ƒinput๎˜ƒand๎˜ƒoutput๎˜ƒformat๎˜ƒas๎˜ƒan๎˜ƒerror๎˜ƒhere๎˜ƒcan๎˜ƒbe๎˜ƒcostly.๎˜ƒ
๎˜ƒ
2. Implement๎˜ƒa๎˜ƒC++๎˜ƒclass๎˜ƒthat๎˜ƒmodels๎˜ƒthe๎˜ƒstack๎˜ƒADT.๎˜ƒThe๎˜ƒelements๎˜ƒare๎˜ƒintegers๎˜ƒand๎˜ƒare๎˜ƒ
stored๎˜ƒin๎˜ƒan๎˜ƒarray.๎˜ƒThe๎˜ƒarray๎˜ƒshould๎˜ƒincrease๎˜ƒin๎˜ƒlength๎˜ƒby๎˜ƒ50%๎˜ƒwhen๎˜ƒa๎˜ƒpush๎˜ƒoperation๎˜ƒ
does๎˜ƒnot๎˜ƒhave๎˜ƒroom๎˜ƒto๎˜ƒstore๎˜ƒthe๎˜ƒnew๎˜ƒelement๎˜ƒ(take๎˜ƒthe๎˜ƒfloor๎˜ƒwhen๎˜ƒthe๎˜ƒarray๎˜ƒlength๎˜ƒis๎˜ƒ
odd,๎˜ƒi.e.๎˜ƒ9๎˜ƒbecomes๎˜ƒ13).๎˜ƒInitial๎˜ƒarray๎˜ƒlength๎˜ƒis๎˜ƒ2.๎˜ƒYou๎˜ƒshould๎˜ƒimplement๎˜ƒthe๎˜ƒoperations๎˜ƒin๎˜ƒ
โ€œstack.hโ€๎˜ƒwith๎˜ƒthe๎˜ƒgiven๎˜ƒsignatures๎˜ƒand๎˜ƒexplanations.๎˜ƒWarning:๎˜ƒpay๎˜ƒvery๎˜ƒclose๎˜ƒattention๎˜ƒ
to๎˜ƒthe๎˜ƒrequired๎˜ƒfile๎˜ƒname,๎˜ƒinput๎˜ƒand๎˜ƒoutput๎˜ƒformat๎˜ƒas๎˜ƒan๎˜ƒerror๎˜ƒhere๎˜ƒcan๎˜ƒbe๎˜ƒcostly.๎˜ƒ
a. What๎˜ƒis๎˜ƒthe๎˜ƒBigโ€Oh๎˜ƒamortized๎˜ƒcost๎˜ƒof๎˜ƒa๎˜ƒpush๎˜ƒoperation?๎˜ƒ
b. Use๎˜ƒthe๎˜ƒstack๎˜ƒclass๎˜ƒin๎˜ƒa๎˜ƒprogram๎˜ƒthat๎˜ƒchecks๎˜ƒwhether๎˜ƒa๎˜ƒstring๎˜ƒof๎˜ƒcharacters๎˜ƒis๎˜ƒ
parenthetically๎˜ƒcorrect,๎˜ƒmeaning๎˜ƒthat๎˜ƒthe๎˜ƒparenthetic๎˜ƒsymbols๎˜ƒ(),๎˜ƒ{},๎˜ƒand๎˜ƒ[]๎˜ƒare๎˜ƒ
balanced๎˜ƒand๎˜ƒproperly๎˜ƒnested.๎˜ƒThe๎˜ƒstring๎˜ƒends๎˜ƒwith๎˜ƒthe๎˜ƒcharacter๎˜ƒโ€˜zโ€™๎˜ƒand๎˜ƒdoes๎˜ƒ
not๎˜ƒcontain๎˜ƒany๎˜ƒother๎˜ƒโ€˜zโ€™๎˜ƒcharacters.๎˜ƒWarning:๎˜ƒpay๎˜ƒvery๎˜ƒclose๎˜ƒattention๎˜ƒto๎˜ƒthe๎˜ƒ
required๎˜ƒfile๎˜ƒname,๎˜ƒinput๎˜ƒand๎˜ƒoutput๎˜ƒformat๎˜ƒas๎˜ƒan๎˜ƒerror๎˜ƒhere๎˜ƒcan๎˜ƒbe๎˜ƒcostly.๎˜ƒ
๎˜ƒ
๎˜ƒ
3. Extraโ€credit๎˜ƒ(3%)๎˜ƒWrite๎˜ƒa๎˜ƒprogram๎˜ƒthat๎˜ƒsolves๎˜ƒthe๎˜ƒTowers๎˜ƒof๎˜ƒHanoi๎˜ƒpuzzle๎˜ƒ(see๎˜ƒproblem๎˜ƒ
Cโ€4.6๎˜ƒat๎˜ƒpage๎˜ƒ198๎˜ƒof๎˜ƒtextbook)๎˜ƒfor๎˜ƒn๎˜ƒ=๎˜ƒ5๎˜ƒusing๎˜ƒyour๎˜ƒstack๎˜ƒclass.๎˜ƒ
๎˜ƒ
4. Turn๎˜ƒin๎˜ƒinstructions๎˜ƒ
a. Question๎˜ƒ1:๎˜ƒName๎˜ƒyour๎˜ƒprogram๎˜ƒfile๎˜ƒas๎˜ƒโ€œpermutation.cppโ€.๎˜ƒโ€œpermutation.cppโ€๎˜ƒ
will๎˜ƒhave๎˜ƒa๎˜ƒmain๎˜ƒfunction๎˜ƒthat๎˜ƒtakes๎˜ƒthe๎˜ƒinput๎˜ƒn๎˜ƒas๎˜ƒan๎˜ƒargument๎˜ƒto๎˜ƒthe๎˜ƒ
executable.๎˜ƒSo,๎˜ƒyou๎˜ƒshould๎˜ƒrun๎˜ƒwith๎˜ƒn=4๎˜ƒas๎˜ƒfollows:๎˜ƒ
g++๎˜ƒโ€o๎˜ƒpermutation๎˜ƒpermutation.cpp๎˜ƒ
./permutation๎˜ƒ4๎˜ƒ
๎˜ƒ
The๎˜ƒmain๎˜ƒfunction๎˜ƒcalls๎˜ƒthe๎˜ƒrecursive๎˜ƒfunction๎˜ƒwhich๎˜ƒprints๎˜ƒout๎˜ƒall๎˜ƒpermutations๎˜ƒ
to๎˜ƒthe๎˜ƒstandard๎˜ƒoutput.๎˜ƒPrint๎˜ƒone๎˜ƒpermutation๎˜ƒterminated๎˜ƒby๎˜ƒa๎˜ƒnewline๎˜ƒat๎˜ƒeach๎˜ƒ
line๎˜ƒ(The๎˜ƒintegers๎˜ƒin๎˜ƒa๎˜ƒpermutation๎˜ƒare๎˜ƒone๎˜ƒafter๎˜ƒanother,๎˜ƒi.e.๎˜ƒNOT๎˜ƒseparated๎˜ƒby๎˜ƒ
the๎˜ƒspace๎˜ƒcharacter).๎˜ƒ
๎˜ƒ
An๎˜ƒexample๎˜ƒoutput๎˜ƒfor๎˜ƒn=4๎˜ƒis๎˜ƒprovided๎˜ƒas๎˜ƒa๎˜ƒfile๎˜ƒโ€œpermutationโ€sampleoutputโ€.๎˜ƒ
You๎˜ƒshould๎˜ƒprint๎˜ƒall๎˜ƒpossible๎˜ƒpermutations.๎˜ƒHowever,๎˜ƒthe๎˜ƒorder๎˜ƒof๎˜ƒthe๎˜ƒ
permutations๎˜ƒmay๎˜ƒbe๎˜ƒdifferent๎˜ƒthan๎˜ƒthis๎˜ƒoutput๎˜ƒ(This๎˜ƒfile๎˜ƒis๎˜ƒgiven๎˜ƒto๎˜ƒshow๎˜ƒthe๎˜ƒ
output๎˜ƒformat,๎˜ƒyou๎˜ƒwill๎˜ƒprint๎˜ƒout๎˜ƒto๎˜ƒstandard๎˜ƒoutput๎˜ƒnot๎˜ƒto๎˜ƒa๎˜ƒfile).๎˜ƒ
๎˜ƒ
pf3

Partial preview of the text

Download Running Time Analysis and Stacks - Data Structures and Algorithms | CS 25100 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS 251

Spring 2009 Voicu Popescu Assignment 3 Due Friday February 6th, 11:59PM Running Time Analysis and Stacks

  1. Write a recursive function that given a positive integer n prints out the permutations of the numbers {1, 2, โ€ฆ, n }. For n =3, the permutations are {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}. The order in which the permutations are printed is not important. What is the Bigโ€Oh running time of the function? Warning: pay very close attention to the required file name, input and output format as an error here can be costly.
  2. Implement a C++ class that models the stack ADT. The elements are integers and are stored in an array. The array should increase in length by 50% when a push operation does not have room to store the new element (take the floor when the array length is odd, i.e. 9 becomes 13). Initial array length is 2. You should implement the operations in โ€œstack.hโ€ with the given signatures and explanations. Warning: pay very close attention to the required file name, input and output format as an error here can be costly. a. What is the Bigโ€Oh amortized cost of a push operation? b. Use the stack class in a program that checks whether a string of characters is parenthetically correct, meaning that the parenthetic symbols (), {}, and [] are balanced and properly nested. The string ends with the character โ€˜zโ€™ and does not contain any other โ€˜zโ€™ characters. Warning: pay very close attention to the required file name, input and output format as an error here can be costly.
  3. Extraโ€credit (3%) Write a program that solves the Towers of Hanoi puzzle (see problem Cโ€4.6 at page 198 of textbook) for n = 5 using your stack class.
  4. Turn in instructions a. Question 1: Name your program file as โ€œpermutation.cppโ€. โ€œpermutation.cppโ€ will have a main function that takes the input n as an argument to the executable. So, you should run with n=4 as follows: g++ โ€o permutation permutation.cpp ./permutation 4 The main function calls the recursive function which prints out all permutations to the standard output. Print one permutation terminated by a newline at each line (The integers in a permutation are one after another, i.e. NOT separated by the space character). An example output for n=4 is provided as a file โ€œpermutationโ€sampleoutputโ€. You should print all possible permutations. However, the order of the permutations may be different than this output (This file is given to show the output format, you will print out to standard output not to a file).

Of course once you get n=4 case to work you should devise additional cases with different n values to ensure your program truly works. b. Question 2: Name your program file as โ€œstack.cppโ€. โ€œstack.cppโ€ should implement the operations explained in โ€œstack.hโ€. A sample test file โ€œstackTest.cppโ€ is also provided. Examine and run that program to see what is expected. You can play with that file and add more test as you like. You will not turnin that file. Question 2 part b: Name your program file as โ€œparenthetic.cppโ€. โ€œparenthetic.cppโ€ should read the input strings from file โ€œparentheticโ€inputโ€. The input file has one string per line. The strings are read from the file. The

program must print the answer as yes or no to the standard output per line

corresponding to the each input line. A sample โ€œparentheticโ€inputโ€ file is

provided. The corresponding output is provided as a file โ€œ parentheticโ€

output โ€ (This file is given to show the output format, you will print out to

standard output not to a file). (You do not have to evaluate the expressions) c. Questions 1 and 2: Turn in a PDF document named as โ€œa3.pdfโ€ with your answers to the questions. Read General turnin instructions parts E, F and G very carefully, on the website. d. Question 3 (Extra): Name your program file as โ€œhanoi.cppโ€. โ€œhanoi.cppโ€ should solve Towers of Hanoi puzzle for n = 5 using your stack class. You need to solve the puzzle in the minimum number of moves. You are supposed to move all the disks from peg a to peg c. Your program should print out the elements in the three stacks from bottom to top at the beginning and after each move to the standard output. Assume that you have the elements 5,4,3,2,1 at peg a from bottom to top at the beginning. The format for output is given as files for n= (โ€œhanoi2โ€outputโ€) and n=3 (โ€œhanoi3โ€outputโ€). You can run your program with n=3, direct the output to a temporary file and diff your output with the given โ€œhanoi3โ€outputโ€ file. Assuming that your executable is called โ€œhanoi" which solves n=3: ./hanoi > myhanoi3โ€output diff myhanoi3โ€output hanoi3โ€output Then, make your program solve n=5, and check your output for correctness by yourself. e. Your codes MUST compile on lore.cs.purdue.edu for it to be graded. f. All of your documents (permutation.cpp, stack.cpp, parenthetic.cpp, a3.pdf, hanoi.cpp (OPTIONAL)) MUST be in the directory A3. In the directory above A3, run the command on lore.cs.purdue.edu