Table Of ContentHabib Izadkhah
Problems
on Algorithms
A Comprehensive Exercise Book
for Students in Software Engineering
Problems on Algorithms
Habib Izadkhah
Problems on Algorithms
A Comprehensive Exercise Book for Students
in Software Engineering
HabibIzadkhah
FacultyofMathematics,Statistics,
andComputerScience
UniversityofTabriz
Tabriz,Iran
ISBN 978-3-031-17042-3 ISBN 978-3-031-17043-0 (eBook)
https://doi.org/10.1007/978-3-031-17043-0
©TheEditor(s)(ifapplicable)andTheAuthor(s),underexclusivelicensetoSpringerNature
SwitzerlandAG2022
Thisworkissubjecttocopyright.AllrightsaresolelyandexclusivelylicensedbythePublisher,whether
thewholeorpartofthematerialisconcerned,specificallytherightsoftranslation,reprinting,reuse
ofillustrations,recitation,broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,and
transmissionorinformationstorageandretrieval,electronicadaptation,computersoftware,orbysimilar
ordissimilarmethodologynowknownorhereafterdeveloped.
Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublication
doesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevant
protectivelawsandregulationsandthereforefreeforgeneraluse.
Thepublisher,theauthors,andtheeditorsaresafetoassumethattheadviceandinformationinthisbook
arebelievedtobetrueandaccurateatthedateofpublication.Neitherthepublishernortheauthorsor
theeditorsgiveawarranty,expressedorimplied,withrespecttothematerialcontainedhereinorforany
errorsoromissionsthatmayhavebeenmade.Thepublisherremainsneutralwithregardtojurisdictional
claimsinpublishedmapsandinstitutionalaffiliations.
ThisSpringerimprintispublishedbytheregisteredcompanySpringerNatureSwitzerlandAG
Theregisteredcompanyaddressis:Gewerbestrasse11,6330Cham,Switzerland
Preface
Learningisamysteriousprocess.Noonecansaywhatthepreciserulesoflearning
are. However, it is an agreed upon fact that the study of good examples plays a
fundamentalroleinlearning.Intheshortspanofasemester,itisdifficulttocover
enoughmaterialtogivestudentstheconfidencethattheyhavemasteredsomeportion
ofthesubject.Consequently,itiswellknownthatproblem-solvinghelpsoneacquire
routineskillsindesigning andanalyzing ofalgorithms.The salientfeaturesofthe
theoryarepresentedinclassalongwithafewexamples,andthenthestudentsare
expectedtoteachthemselvesthefineraspectsofthetheorythroughworkedexamples.
Thebookaimsatpresentingalotofproblems,aimingtoimprovethelearningprocess
ofstudents.
With approximately 2500 problems, this supplement provides a collection of
practical problems on the basic and advanced data structures, design, and analysis
ofalgorithms.Tomakethisbooksuitableforself-instruction,aboutone-thirdofthe
algorithms are supported by solutions, and some other are supported by hints and
comments. This book is intended for students wishing to deepen their knowledge
ofalgorithmdesigninanundergraduateorbeginninggraduateclassonalgorithms,
forthoseteachingcoursesinthisarea,forusebypracticingprogrammerswhowish
to hone and expand their skills, and as a self-study text for graduate students who
arepreparingforthequalifyingexaminationonalgorithmsforaPh.D.programin
ComputerScienceorComputerEngineering.Aboutall,it’sagoodsourceforexam
problems for those who teach algorithms and data structure. The format of each
chapterisjustalittlebitofinstructionfollowedbylotsofproblems.
Thisbookisintendedtoaugmenttheproblemsetsfoundinanystandardalgorithm
textbook.Inthisbook,threelevelsofdifficulty,simpleorrelativelysimple,moderate
levelorslightlydifficult.Thisbook,also,emphasizesthecreativeaspectsofalgorithm
design.
Thisbook
(cid:129)
beginswithfourchaptersonbackgroundmaterialthatmostalgorithminstructors
wouldliketheirstudentstohavemasteredbeforesettingfootinanalgorithmclass.
v
vi Preface
Theintroductorychaptersincludemathematicalinduction,complexitynotations,
recurrencerelations,andbasicalgorithmanalysismethods.
(cid:129)
provides many problems on basic and advanced data structures including basic
datastructures(arrays,stack,queue,andlinkedlist),hash,tree,search,andsorting
algorithms.
(cid:129)
provides many problems on algorithm design technique: divide and conquer,
dynamic programming, greedy algorithms, graph algorithms, and backtracking
algorithms.
(cid:129)
isroundedoutwithchapteronNP-completeness.
Tabriz,Iran HabibIzadkhah
Contents
1 MathematicalInduction ....................................... 1
1.1 LectureNotes ............................................ 1
1.2 Exercises ................................................ 5
1.2.1 Summations .................................... 5
1.2.2 Inequalities ..................................... 6
1.2.3 FloorsandCeilings .............................. 6
1.2.4 Divisibility ..................................... 7
1.2.5 PostageStamps .................................. 7
1.2.6 FibonacciNumbers .............................. 8
1.2.7 BinomialCoefficients ............................ 8
1.2.8 Miscellaneous ................................... 9
1.3 Solutions ................................................ 11
2 GrowthofFunctions ........................................... 29
2.1 LectureNotes ............................................ 29
2.1.1 OrdersofGrowth ................................ 31
2.1.2 Useful Theorems Involving the Asymptotic
Notations ....................................... 42
2.1.3 ApplyingLimitsforAnalyzingOrdersofGrowth .... 44
2.1.4 IteratedFunction ................................ 46
2.2 Exercises ................................................ 47
2.2.1 SizeofProblem ................................. 47
2.2.2 TrueorFalse? ................................... 48
2.2.3 RanktheFunctions .............................. 49
2.2.4 ProveUsingtheDefinitionofNotation .............. 52
2.2.5 FindNotations .................................. 56
2.2.6 PropertyofNotations ............................ 58
2.2.7 MoreExercises .................................. 61
2.3 Solutions ................................................ 70
vii
viii Contents
3 RecurrenceRelations .......................................... 89
3.1 LectureNotes ............................................ 89
3.1.1 CatalogofRecurrence ............................ 91
3.1.2 SolvingRecurrence .............................. 93
3.1.3 LinearHomogeneousRecurrences ................. 95
3.1.4 Nonhomogeneous ............................... 101
3.1.5 RecurrenceTree ................................. 104
3.1.6 MasterMethod .................................. 105
3.2 Exercises ................................................ 108
3.2.1 TheIterationMethod ............................. 108
3.2.2 Homogeneous Linear Recurrence Equation
withConstantCoefficients ........................ 110
3.2.3 Nonhomogeneous Recurrences Equation
withConstantCoefficients ........................ 112
3.2.4 GeneralFormula ................................. 114
3.2.5 ChangingVariablesinRecurrenceRelations ......... 115
3.2.6 MoreDifficultRecurrences ....................... 116
3.2.7 RecurrencewithFullHistory ...................... 118
3.2.8 RecurrencewithFloorsandCeilings ............... 119
3.2.9 TheMasterMethod .............................. 119
3.2.10 RecursionTreeMethod ........................... 123
3.2.11 Recurrence Relations with More Than One
Variable ........................................ 123
3.2.12 GeneratingFunctions ............................. 124
3.3 Solutions ................................................ 124
4 AlgorithmAnalysis ............................................ 135
4.1 LectureNotes ............................................ 135
4.2 Exercises ................................................ 136
4.2.1 IterativeAlgorithms .............................. 136
4.2.2 WhatisReturned? ............................... 152
4.2.3 RecursiveAlgorithm ............................. 159
4.2.4 RecurrenceRelationsforRecursiveFunctions ....... 165
4.3 Solutions ................................................ 166
5 BasicDataStructure .......................................... 171
5.1 LectureNotes ............................................ 171
5.1.1 Arrays ......................................... 171
5.1.2 Stack .......................................... 172
5.1.3 Queue .......................................... 172
5.1.4 LinkedList ..................................... 172
5.2 Exercises ................................................ 173
5.2.1 Arrays ......................................... 173
5.2.2 Stack .......................................... 184
5.2.3 Queue .......................................... 195
5.2.4 LinkedList ..................................... 197
5.3 Solutions ................................................ 201
Contents ix
6 Hash ......................................................... 219
6.1 LectureNotes ............................................ 219
6.2 Exercises ................................................ 220
6.2.1 Basic .......................................... 220
6.2.2 Applications .................................... 221
6.3 Solutions ................................................ 228
7 Tree .......................................................... 231
7.1 LectureNotes ............................................ 231
7.2 Exercises ................................................ 231
7.2.1 Tree ........................................... 231
7.2.2 BinaryTree ..................................... 232
7.2.3 BinarySearchTree .............................. 240
7.2.4 Heap ........................................... 250
7.2.5 Applications .................................... 255
7.3 Solutions ................................................ 258
8 Search ....................................................... 269
8.1 LectureNotes ............................................ 269
8.2 Exercises ................................................ 269
8.2.1 Preliminary ..................................... 269
8.2.2 LinearSearch ................................... 270
8.2.3 BinarySearch ................................... 271
8.2.4 TernarySearch .................................. 274
8.2.5 BinarySearchTree(BST) ........................ 275
8.2.6 FibonacciSearch ................................ 275
8.2.7 ExponentialSearch .............................. 276
8.2.8 InterpolationSearch .............................. 277
8.2.9 Applications .................................... 278
8.3 Solutions ................................................ 280
9 Sorting ....................................................... 289
9.1 LectureNotes ............................................ 289
9.2 Exercises ................................................ 290
9.2.1 Introduction .................................... 290
9.2.2 SelectionSort ................................... 291
9.2.3 BubbleSort ..................................... 293
9.2.4 InsertionSort ................................... 293
9.2.5 Heapsort ....................................... 297
9.2.6 ShellSort ....................................... 298
9.2.7 Introsort ........................................ 300
9.2.8 TimSort ........................................ 302
9.2.9 BinaryTreeSort ................................. 303
9.2.10 CountingSort ................................... 304
9.2.11 RadixSort ...................................... 307
9.2.12 Mergesort ...................................... 307
x Contents
9.2.13 QuickSort ...................................... 309
9.2.14 ShellSort ....................................... 312
9.2.15 CycleSort ...................................... 313
9.2.16 LibrarySort ..................................... 313
9.2.17 StrandSort ..................................... 315
9.2.18 CocktailSort .................................... 316
9.2.19 CombSort ...................................... 317
9.2.20 GnomeSort ..................................... 318
9.2.21 BogoSort ...................................... 319
9.2.22 SleepSort ...................................... 320
9.2.23 PigeonholeSort ................................. 321
9.2.24 BucketSort(UniformKeys) ....................... 322
9.2.25 BeadSort ....................................... 323
9.2.26 PancakeSort .................................... 324
9.2.27 Odd-EvenSort .................................. 325
9.2.28 StoogeSort ..................................... 326
9.2.29 PermutationSort ................................ 327
9.2.30 RecursiveBubbleSort ............................ 328
9.2.31 BinaryInsertionSort ............................. 329
9.2.32 RecursiveInsertionSort .......................... 330
9.2.33 TreeSort ....................................... 331
9.2.34 CartesianTreeSorting ............................ 332
9.2.35 3-WayQuicksort ................................ 333
9.2.36 3-WayMergesort ................................ 334
9.3 Solutions ................................................ 335
10 DivideandConquer ........................................... 351
10.1 LectureNotes ............................................ 351
10.2 Exercises ................................................ 352
10.2.1 Preliminary ..................................... 352
10.2.2 BinarySearch ................................... 353
10.2.3 FindingMinimumandMaximum .................. 358
10.2.4 GreatestCommonDivisor(gcd) ................... 362
10.2.5 Mergesort ...................................... 363
10.2.6 Quicksort ....................................... 368
10.2.7 FindingtheMedian .............................. 375
10.2.8 IntegerMultiplication ............................ 375
10.2.9 MatrixMultiplication ............................ 378
10.2.10 Application ..................................... 383
10.3 Solutions ................................................ 388
11 DynamicProgramming ........................................ 401
11.1 LectureNotes ............................................ 401
11.2 Exercises ................................................ 404
11.2.1 Preliminary ..................................... 404
11.2.2 MathematicsNumbers ............................ 404