Copy of Data Structures and Algorithms - Shubham Gupta (1)
Copy of Data Structures and Algorithms - Shubham Gupta (1)
SHUBHAM GUPTA
Gupta
Data structures are specialized formats for organizing, storing, and manipulating
data in computer programs. There are several types of data structures, including
arrays, linked lists, stacks, queues, and trees. Algorithms are a set of instructions or
procedures that are designed to solve a specific computation problem.
About the Author In today’s fast-paced digital age, efficient data management is more critical than
ever before. As the amount of data being generated and processed increases
exponentially, the need for powerful algorithms and data structures becomes
increasingly pressing. Data structures and algorithms are important concepts in
computer science, and their understanding is crucial for building efficient and
scalable software systems. A good understanding of data structures and algorithms
can help software developers to design efficient programs, optimize code perfor-
mance and solve complex computational problems.
The discussion of the book starts with the fundamentals of data structures and
algorithms, followed by the classification of algorithms used in data structures. Also,
Shubham Gupta is a highly skilled software the concept of arrays and sets used in data structures and the selection of
engineer with over seven years of experience in algorithms are described in detail in this book. The fundamental concept of two very
important data structures, stacks, and queues, is also explained in detail. Another
DATA STRUCTURES
on numerous projects involving the development process or flows for the selection of a suitable algorithm. The fifth chapter describes
of web-based applications and services. In the concept of stacks and queues in data structures, along with the implementation
addition to his technical skills, Shubham is also a of these concepts using arrays and linked lists. The sixth chapter deals with the
strong communicator and team player. He enjoys concepts of trees, basic binary operations, and the efficiency of binary trees. The
collaborating with other professionals to develop
seventh chapter is about search algorithms that are used in the data structure to
AND ALGORITHMS
solutions that are both technically sound and
user-friendly. Shubham is passionate about his
find an element inside the structure, and the eighth chapter is about the gover-
work and is always looking for ways to improve nance of algorithms and the limitations of governance options.
his skills and stay up-to-date with the latest This book has been written specifically for students and scholars to meet their needs
industry trends and technologies. His commit- in terms of knowledge and to provide them with a broad understanding of data
ment to excellence has earned him a reputation structures and algorithms. We aim for this book to be a resource for academics in
as a trusted and reliable software engineer. many subjects since we believe it will provide clarity and insight for its readers.
ISBN 978-1-77956-178-7
TAP
00000
TAP
TAP
9 781779 561787
Toronto Academic Press
DATA STRUCTURES
AND ALGORITHMS
Shubham Gupta
TAP
Toronto Academic Press
Data Structures and Algorithms
Shubham Gupta
© 2024
ISBN: 978-1-77956-178-7 (e-book)
This book contains information obtained from highly regarded resources. Reprinted material sources are indicated
and copyright remains with the original owners. Copyright for images and other graphics remains with the original
owners as indicated. A Wide variety of references are listed. Reasonable efforts have been made to publish reliable
data. Authors or Editors or Publishers are not responsible for the accuracy of the information in the published
chapters or consequences of their use. The publisher assumes no responsibility for any damage or grievance to the
persons or property arising out of the use of any materials, instructions, methods or thoughts in the book. The
authors or editors and the publisher have attempted to trace the copyright holders of all material reproduced in this
publication and apologize to copyright holders if permission has not been obtained. If any copyright holder has not
been acknowledged, please write to us so we may rectify.
Notice: Registered trademark of products or corporate names are used only for explanation and identification
without intent of infringement.
Toronto Academic Press publishes wide variety of books and eBooks. For more information about Toronto
Academic Press and its products, visit our website at www.tap-books.com.
ABOUT THE AUTHOR
Shubham Gupta is a highly skilled software engineer with over seven years of experience in the
field. He holds a Master’s degree in Software Engineering from KIET Group of Institutes, where he
gained a deep understanding of software development methodologies and best practices. Throughout
his career, Shubham has worked with a variety of clients, from small startups to large corporations.
He is dedicated to delivering high-quality software solutions that meet the unique needs of his clients
and help them achieve their business goals. Shubham is well-versed in a wide range of programming
languages, frameworks, and technologies. He has a particular expertise in web development, and has
worked on numerous projects involving the development of web-based applications and services. In
addition to his technical skills, Shubham is also a strong communicator and team player. He enjoys
collaborating with other professionals to develop solutions that are both technically sound and user-
friendly. Shubham is passionate about his work and is always looking for ways to improve his skills and
stay up-to-date with the latest industry trends and technologies. His commitment to excellence has
earned him a reputation as a trusted and reliable software engineer.
Contents
Preface xv
List of Figures ix
List of Tables xi
List of Abbreviations xiii
1 Fundamentals of Data
Structures and 2 Classification of
Algorithms 31
Algorithms 1 Unit Introduction 31
2.1. Deterministic and Randomized
Unit Introduction 1 Algorithms 34
1.1. Issues Solved by Algorithms 4 2.2. Online Vs. Offline Algorithms 35
1.2. Data Structures 7 2.3. Exact, Approximate, Heuristic, and
1.2.1. Data Structure’s Need 8 Operational Algorithms 36
1.2.2. Execution Time Cases 8 2.4. Classification According to
1.2.3. Basic Terminologies 9 Main Concept 37
4 Algorithm Selection
Multiple Choice Questions 126
75 References 127
Unit Introduction
4.1. Ordered Arrays
75
77
6 Trees 133
4.2. Searching an Ordered Array 79 Unit Introduction 133
4.3. Binary Search 80 6.1. Tree Terminology 136
4.4. Binary Search Vs. Linear Search 83 6.1.1. Path 136
Summary 87 6.1.2. Root 136
Review Questions 87 6.1.3. Parent 137
Multiple Choice Questions 87 6.1.4. Child 137
References 88 6.1.5. Leaf 137
91 6.1.7. Visiting
6.1.8. Traversing
137
138
6.1.9. Levels 138
Unit Introduction 91
6.1.10. Keys 138
5.1. understanding stacks 94
6.1.11. Binary Trees 138
5.1.1. The Stack Workshop Applet 95
6.2. Tree Analogy in Computer 139
5.1.2 Stack Implementation in C++ 98
6.3. Basic Binary Tree Operations 139
5.1.3. StackX Class Member Functions 100
6.3.1. The Tree Workshop Applet 140
5.1.4. Error Handling 101
6.3.2. Illustrating the Tree in C++ Code 142
5.1.5. Delimiter Matching 102
6.4. Finding A Node 145
5.1.6 Utilizing the Stack as an Analytical Tool 108
6.4.1. Locating a Node with the Workshop
5.1.7. Efficiency of Stacks 108 Applet 145
5.2. Queues 108 6.4.2. C++ Code for Finding a Node 147
5.2.1. The Applet Queue Workshop 108
vi
6.4.3. Unable to Locate the Node 148 7.6. Graph Grep 180
6.4.4. Found the Node 148 7.7. Searching in Trees 182
6.4.5. Efficiency of the Find Operation 148 7.8. Searching in Temporal Probabilistic
6.5. Inserting A Node 148 Object Data Model 184
Governance of
8
6.7.1. Inorder Traversal 152
6.7.2. C++ Code for Traversing 153
6.7.3. Moving Through a 3-Node Tree 153 Algorithms in Data
6.7.4. Using the Workshop Applet to Traverse 155 Structures 197
6.7.5. Postorder and Preorder Traversals 157
6.8. The Efficiency of Binary Trees 159 Unit Introduction 197
Summary 161 8.1. Analytical Framework 201
Review Questions 161 8.2. Governance Options By Risks 203
Multiple Choice Questions 161 8.2.1. Potential of Market Solutions and
References 162 Governance by Design 204
8.2.2. Options for the Industry:
vii
List of Figures
Figure 1.1. Illustration of algorithms’ basic structure Figure 4.2. Graph of performance comparison
and overview (Source: Tim Roughgarden, Creative between linear and binary search (Source: Brain
Commons License). Macdonald, Creative Commons License).
Figure 1.2. Flowchart of algorithm application Figure 5.1. Image of how a queue works (Source:
(Source: JT Newsome, Creative Commons License). Jeff Durham, Creative Commons License).
Figure 2.1. Major types of data structure algorithms Figure 5.2. Image of a stack of letters (Source:
(Source: Code Kul, Creative Commons License). Robert Lafore, Creative Commons License).
Figure 2.2. Illustration of deterministic and Figure 5.3. Illustration of the stack workshop
randomized algorithms (Source: Shinta Budiaman, applet (Source: Tonya Simpson, Creative Commons
Creative Commons License). License).
Figure 2.3. Offline Evaluation of Online Reinforcement Figure 5.4. Operation of the StackX class member
Learning Algorithms (Source: Travis Mandel, Creative functions (Source: Robert Lafore, Creative Commons
Commons License). License).
Figure 2.4. Comparison of iterative and recursive Figure 5.5. Image of the queue workshop applet
approaches (Source: Beau Cranes, Creative (Source: Mike Henry, Creative Commons License).
Commons License). Figure 5.6. Operation of the queue class member
Figure 2.5. Representation of divide and conquer functions (Source: Dan Schref, Creative Commons
algorithm (Source: Khan Academy, Creative Commons License).
License). Figure 5.7. Representing a queue with some items
Figure 2.6. Depiction of dynamic programming removed (Source: Tonya Simpson, Creative Commons
algorithm (Source: Khan Academy, Creative License).
Commons License). Figure 5.8. Representation of rear arrow at the
Figure 2.7. Representation of backtracking algorithm end of array (Source: Brian Gill, Creative Commons
(Source: Programiz, Creative Commons License). License).
Figure 2.8. Numerical representation of a greedy Figure 5.9. Illustration of letters in a priority queue
algorithm (Source: Margaret Rouse, Creative (Source: Mike Henry, Creative Commons License).
Commons License). Figure 5.10. Illustration of the priority queue workshop
Figure 2.9. Graphical form of brute force algorithm applet (Source: Richard Wright, Creative Commons
(Source: Margaret Rouse, Creative Commons License).
License). Figure 5.11. Operation of the priority queue class
Figure 2.10. Depiction of sequential branch-and- member functions (Source: Mike Henry, Creative
bound method (Source: Malika Mehdi, Creative Commons License).
Commons License). Figure 6.1. Illustration of tree with more than two
Figure 4.1. Illustration of game for binary search children (Source: Narasimha Karumanchi, Creative
(Source: Jay Wengrow, Creative Commons License). Commons License).
Figure 6.2. Image of tree terms (Source: Sobhan, Figure 7.3. Illustration of binary search algorithm
Creative Commons License). with an example (Source: Alyssa Walker, Creative
Figure 6.3. Representation of a non-tree (Source: Commons License).
Vamshi Krishna, Creative Commons License). Figure 7.4. Illustration of (a)A structured database
Figure 6.4. Image of a binary search tree (Source: tree and (b)A query containing wildcards (Source:
Kalyani Tummala, Creative Commons License). R.Giugno, Creative Commons License).
Figure 6.5. Illustration of applet from binary tree Figure 7.5. Instances of isomorphic graphs (Source:
workshop (Source: Narasimha Karumanchi, Creative Bruce Schneier, Creative Commons License).
Commons License). Figure 7.6. Representation of (a)graph (GRep) with
Figure 6.6. Representation of an out balanced tree 6 vertices and 8 edges; (b,c, and d) possible cliques
without balanced sub tree (Source: Sobhan, Creative of GRep is D1={VR1, VR2, VR5}, D2={VR2, VR3,
Commons License). VR4,VR5}, and D3={VR6, VR5, VR4} (Source: Soft
Computing, Creative Commons License).
Figure 6.7. Illustration of finding the node number 57
(Source: Ram Mohan, Creative Commons License). Figure 7.7. Attributes of binary search tree (Source:
Alyssa Walker, Creative Commons License).
Figure 6.8. Representation of adding a node (Source:
Vamshi Krishna, Creative Commons License). Figure 7.8. Illustration of temporal persistence
modeling for object search (Source: Russel Toris,
Figure 6.9. Flowchart of applying a 3-node tree’s
Creative Commons License).
inorder () member method (Source: Kalyani Tummala,
Creative Commons License). Figure 8.1. Theoretical model of variables measuring
the significance of algorithmic governance in everyday
Figure 6.10. Image of traversing a tree in order
life (Source: Beau Cranes, Creative Commons
(Source: Kiran, Creative Commons License).
License).
Figure 6.11. Image of an algebraic expression
Figure 8.2. Framework of the data analysis algorithms
represented by a tree (Source: Laxmi, Creative
(Source: Ke Yan, Creative Commons License)
Commons License).
Figure 8.3. Illustration of algorithmic decision systems
Figure 7.1. Categorization of search algorithms
(Source: David Restrepo, Creative Commons License)
(Source: geeksforgeeks, Creative Commons License).
Figure 7.2. Explanation of linear search (Source:
Karuna Sehgal, Creative Commons License).
List of Tables
Table 1.1. Benefits and drawbacks of different data structures (Source: Robert Lafore, Creative Commons
License)
Table 1.2. Illustration of insertion sort for different times and cost (Source: Tim Roughgarden, Creative Commons
License)
Table 5.1. Stack contents matching delimiter (Source: Robert Lafore, Creative Commons License)
Table 6.1. Tabular data of workshop applet traversal (Source: Laxmi, Creative Commons License)
Table 6.2. Levels for a described number of nodes (Source: Narasmiha, Creative Commons License)
Table 8.1. Illustration of different algorithm types and their examples (Source: David Restrepo, Creative Commons
License)
List of Abbreviations
B2C Business-to-Consumer
EU European Union
LIFO Last-in-First-Out
Data structures are specialized formats for organizing, storing, and manipulating data in computer
programs. There are several types of data structures, including arrays, linked lists, stacks, queues,
and trees. Algorithms are a set of instructions or procedures that are designed to solve a specific
computation problem.
In today’s fast-paced digital age, efficient data management is more critical than ever before. As
the amount of data being generated and processed increases exponentially, the need for powerful
algorithms and data structures becomes increasingly pressing. Data structures and algorithms are
important concepts in computer science, and their understanding is crucial for building efficient and
scalable software systems. A good understanding of data structures and algorithms can help software
developers to design efficient programs, optimize code performance and solve complex computational
problems.
The discussion of the book starts with the fundamentals of data structures and algorithms, followed by
the classification of algorithms used in data structures. Also, the concept of arrays and sets used in data
structures and the selection of algorithms are described in detail in this book. The fundamental concept
of two very important data structures, stacks, and queues, is also explained in detail. Another important
data structure known as a tree, which consists of nodes connected by edges, is described in the book.
The fundamental search algorithms that are used to locate an element within the structure are also part
of this book. Lastly, the governance of algorithms and limitations of governance options are described
in detail at the end of this book.
This book comprises eight chapters; the first chapter discusses the fundamentals of data structures
and algorithms with emphasis on the need for data structures, issues addressed by algorithms, and
the benefits and drawbacks of data structures. The second chapter deals with the classification of
algorithms and provides insight into the differences between them.
The third chapter is about the analysis of two important data structures, arrays, and sets, and describes
the temporal complexity of various arrays and set operations. The fourth chapter discusses the process
or flows for the selection of a suitable algorithm.
The fifth chapter describes the concept of stacks and queues in data structures, along with the
implementation of these concepts using arrays and linked lists. The sixth chapter deals with the concepts
of trees, basic binary operations, and the efficiency of binary trees. The seventh chapter is about search
algorithms that are used in the data structure to find an element inside the structure, and the eighth
chapter is about the governance of algorithms and the limitations of governance options.
This book has been written specifically for students and scholars to meet their needs in terms of knowledge
and to provide them with a broad understanding of data structures and algorithms. We aim for this book to be a
resource for academics in many subjects since we believe it will provide clarity and insight for its readers.
—Author
CHAPTER 1
FUNDAMENTALS OF DATA
STRUCTURES AND ALGORITHMS
UNIT INTRODUCTION
An algorithm is a well-described computational process that accepts every value, or set
of values, as input and outputs any value or set of values. As a result, an algorithm is
a set of computing procedures that changes any input into an output. A further way to
think about an algorithm is as a tool for solving a well-defined computing issue (Aggarwal
& Vitter, 1988; Agrawal et al., 2004). Generally, the issue description presupposes the
intended relationship between outputs and inputs, but the algorithm specifies a specific
computing procedure for attaining that relationship.
Sorting is considered a basic step in computer science since it is helpful as an
intermediary step in many applications. As a result, access to a huge variety of effective
sorting algorithms is available (Ahuja & Orlin, 1989; Ahuja et al., 1989). The best algorithm
for any specific application is determined by various factors, including the different items
to be organized, the degree whereby the items are slightly organized, the computer
architecture, potential item value limitations, and the kind of storage devices to be utilized:
disks, main memory, or tapes.
When an algorithm halts with the appropriate output for each input example, it is
thought to be appropriate. A proper algorithm solves the stated computing issue. An
erroneous algorithm, on either side, cannot halt at all on certain input examples, or it
can halt with an inaccurate response (Ahuja et al., 1989; Courcoubetis et al., 1992). In
contrast to assumptions, erroneous algorithms may occasionally be advantageous if the
rate of mistake may be controlled. Therefore, only accurate algorithms will be of concern
(Szymanski & Van Wyk, 1983; King, 1995; Didier, 2009) (Figure 1.1).
2 Data Structures and Algorithms
The structure of data is a methodical strategy to arrange data so that it can be used
effectively. Fundamental terms of the data structure are as follows.
Interface: Each data structure has a respective interface. An interface shows the
set of operations a data structure can perform, and the data structure is capable of
performing. An interface specifies the supported operations, parameter types, and return
types for each supported operation.
Implementation: The implementation provides the inner representation of a data
structure. The implementation also describes the algorithms used for the data structure
operations.
Learning Objectives
Readers will have the chance to understand the following by the chapter’s conclusion:
• Understand the fundamental concepts of data structures and algorithms.
• Learn about the issues solved by algorithms and the need for data structures.
• Advantages and disadvantages of different data structures.
• Learn about analyzing algorithms and data structures
Key Terms
1. Algorithms
2. Array
3. Data structures
4. Data Item
5. Entity
6. Field
7. File
8. Group Item
9. Linked list
10. Queues
11. Stacks
12. Trees
CHAPTER
1
4 Data Structures and Algorithms
CHAPTER
1
Fundamentals of Data Structures and Algorithms 5
CHAPTER
1
10 Data Structures and Algorithms
Ordered array Quicker than an unsorted array Fixed-size, slow deletion, an insertion.
for searching.
Stack Access is last-in, first-out. Accessing other objects slowly.
Queue Gives access on a first-in, first- Accessing other objects slowly.
out basis.
Linked list Quick addition and deletion. Search slowly.
Binary tree Quick insertion, search, and Complex deletion algorithm.
deletion (if the tree remains
balanced).
Hash table Fast access if the key is known. Slow deletion, sluggish access if the
Speedy insertion. key is unknown, and inefficient memory
consumption.
Heap Access to the largest item and Accessing other objects slowly.
quick insertion and deletion.
the fact that you have been tasked with developing an effective
algorithm for any NP-complete issue, it is likely that you would
waste a significant amount of time in a unproductive pursuit. If on
either side, you may demonstrate that the issue is NP-complete,
you may devote your attention instead to inventing an proficient
algorithm that delivers a decent, but the inefficient solution (Leighton,
1996; Roura, 2001; Drmota & Szpankowski, 2013).
Consider the case of a delivery company with a main depot,
which is already in existence. Throughout the day, its delivery
vehicles are loaded at the depot and then dispatched to different
locations to deliver items to customers. Every truck is expected
to return to the depot before the end of the day so that it may
be prepared for loading the following day’s loads (Bentley et al.,
1980; Chan, 2000; Yap, 2011). Specifically, the corporation desires
to pick a series of delivery stops that results in the shortest total
distance traveled by every vehicle to reduce prices. In this issue,
which is known as the “traveling-salesman problem,” the NP-
completeness of the solution has been established. It doesn’t have
KEYWORD
a fast algorithm to work with. Furthermore, given some specific
assumptions, we may identify effective algorithms that provide Traveling
an average distance that is not more than the shortest feasible salesman problem
distance (Shen & Marston, 1995; Verma, 1997). (TSP) is an
algorithmic problem
tasked with finding
1.2.6. Parallelism the shortest route
between a set
Over many years, we had been able to depend upon the speed of points and
of processor clock increasing at a consistent pace. Even though locations that must
physical restrictions provide an eventual impediment to ever- be visited.
increasing clock speeds, the following is true: as with the clock’s
speed, power density increases super-linearly; as a result, chips
are at risk of melting once their clock speeds reach a certain
threshold (Meijer & Akl, 1987, 1988). We build circuits with many
processing cores to conduct more computations per second and
so achieve higher throughput. Such multicore computers may be
compared to a large number of sequential computers on a single
chip, or we may refer to them as a form of “parallel computer,”
depending upon your perspective. To get the maximum potential
performance out of multicore machines, we should design algorithms
with parallelism in mind while developing them. Algorithms that
use several cores, like “multithreaded” algorithms, are particularly
advantageous. From a theoretical standpoint, this sort of model has
significant advantages, and as a result, it serves as the foundation
for a large number of successful computer systems (Wilf, 1984).
CHAPTER
1
12 Data Structures and Algorithms
CHAPTER
1
Fundamentals of Data Structures and Algorithms 17
CHAPTER
1
18 Data Structures and Algorithms
∑
n
6 i=2 j
t c5 while A[i]>key and i>0
∑ (t )
n
7 j= 2 j
−1 c6 A[i+1]=A[i]
∑ (t )
n
8 j= 2 j
−1 c7 i =i-1
9 n-1 c8 A[i+1]=key
=j 2=j 2
( )
=j 2
( )
T(n) = c1 n + c 2 (n − 1) + c 4 (n − 1) + c 5 ∑ t j + c 6 ∑ t j − 1 + c7 ∑ t j − 1 + c 8 (n − 1)
CHAPTER
1
Fundamentals of Data Structures and Algorithms 19
and
n
n(n − 1)
∑ ( j − 1) =
j= 2 2
CHAPTER
1
20 Data Structures and Algorithms
CHAPTER
1
Fundamentals of Data Structures and Algorithms 21
SUMMARY
Data structures are used to organize and store data in a computer program, allowing for
efficient retrieval and manipulation of the data. Common data structures include arrays,
linked lists, stacks, queues, trees and graphs. Algorithms on the other hand are step-by-
step procedures used to solve a particular problem. Algorithms can be applied to data
structures to perform operations such as searching, sorting and traversing the data. Some
common algorithms include binary search, bubble sort and quick sort. Choosing the right
data structure and algorithm for a particular problem can greatly impact the efficiency
and effectiveness of a program. It’s important for computer scientists and programmers
to have a strong understanding of these fundamental concepts in order to create efficient
and effective software solutions.
REVIEW QUESTIONS
1. What is a data structure?
2. What are the common types of data structures?
3. Why is it important to choose the right data structure for a particular problem?
4. What is an algorithm and what are some common algorithms used in programming?
5. How can you implement a data structure or algorithm in a programming language?
CHAPTER
1
22 Data Structures and Algorithms
REFERENCES
1. Abramowitz, M., & Stegun, I. A., (1965). Handbook of Mathematical Functions with
Formulas, Graphs, and Mathematical Table (Vol. 2172, pp. 1–23). New York: Dover.
2. Acar, U. A., (2009). Self-adjusting computation:(an overview). In: Proceedings of the
2009 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation
(pp. 1–6). ACM.
3. Ahuja, R. K., & Orlin, J. B., (1989). A fast and simple algorithm for the maximum
flow problem. Operations Research, 37(5), 748–759.
4. Ahuja, R. K., Orlin, J. B., & Tarjan, R. E., (1989). Improved time bounds for the
maximum flow problem. SIAM Journal on Computing, 18(5), 939–954.
5. Ajtai, M., Megiddo, N., & Waarts, O., (2001). Improved algorithms and analysis for
secretary problems and generalizations. SIAM Journal on Discrete Mathematics,
14(1), 1–27.
6. Aki, S. G., (1989). The Design and Analysis of Parallel Algorithms, 1, 10.
7. Akra, M., & Bazzi, L., (1998). On the solution of linear recurrence equations.
Computational Optimization and Applications, 10(2), 195–210.
8. Alon, N., (1990). Generating pseudo-random permutations and maximum flow
algorithms. Information Processing Letters, 35(4), 201–204.
9. Amir, A., Aumann, Y., Benson, G., Levy, A., Lipsky, O., Porat, E., & Vishne, U., (2006).
Pattern matching with address errors: Rearrangement distances. In: Proceedings of
the Seventeenth Annual ACM-SIAM Symposium on Discrete Algorithm (Vol. 1, pp.
1221–1229). Society for Industrial and Applied Mathematics.
CHAPTER
1
Fundamentals of Data Structures and Algorithms 23
10. Amtoft, T., Consel, C., Danvy, O., & Malmkjær, K., (2002). The abstraction and
instantiation of string-matching programs. In: The Essence of Computation (pp.
332–357). Springer, Berlin, Heidelberg.
11. Andersson, A. A., & Thorup, M., (2000). Tight(er) worst-case bounds on dynamic
searching and priority queues. In: Proceedings of the Thirty-Second Annual ACM
Symposium on Theory of Computing (Vol. 1, pp. 335–342). ACM.
12. Arge, L., (2001). External memory data structures. In: European Symposium on
Algorithms (pp. 1–29). Springer, Berlin, Heidelberg.
13. Arora, S., & Lund, C., (1996). Hardness of approximations. In: Approximation
Algorithms for NP-Hard Problems (pp. 399–446). PWS Publishing Co.
14. Arora, S., (1994). Probabilistic Checking of Proofs and Hardness of Approximation
Problems. Doctoral dissertation, Princeton University, Department of Computer
Science.
15. Arora, S., Lund, C., Motwani, R., Sudan, M., & Szegedy, M., (1998). Proof verification
and the hardness of approximation problems. Journal of the ACM (JACM), 45(3),
501–555.
16. Aslam, J. A., (2001). A Simple Bound on the Expected Height of a Randomly Built
Binary Search Tree,1, 20-35.
17. Ausiello, G., Crescenzi, P., Gambosi, G., Kann, V., Marchetti-Spaccamela, A., &
Protasi, M., (2012). Complexity and Approximation: Combinatorial Optimization
Problems and Their Approximability Properties. Springer Science & Business Media.
18. Avidan, S., & Shamir, A., (2007). Seam carving for content-aware image resizing.
In: ACM Transactions on Graphics (TOG) (Vol. 26, No. 3, p. 10). ACM.
19. Babaioff, M., Immorlica, N., Kempe, D., & Kleinberg, R., (2007). A knapsack secretary
problem with applications. In: Approximation, Randomization, and Combinatorial
Optimization: Algorithms and Techniques (pp. 16–28). Springer, Berlin, Heidelberg.
20. Bach, E., & Shallit, J. O., (1996). Algorithmic Number Theory: Efficient Algorithms
(Vol. 1). MIT press.
21. Bach, E., (1990). Number-theoretic algorithms. Annual Review of Computer Science,
4(1), 119–172.
22. Bailey, D. H., Lee, K., & Simon, H. D., (1991). Using Strassen’s algorithm to accelerate
the solution of linear systems. The Journal of Supercomputing, 4(4), 357–371.
23. Bakker, M., Riezebos, J., & Teunter, R. H., (2012). Review of inventory systems
with deterioration since 2001. European Journal of Operational Research, 221(2),
275–284.
24. Baswana, S., Hariharan, R., & Sen, S., (2002). Improved decremental algorithms
for maintaining transitive closure and all-pairs shortest paths. In: Proceedings of the
34th Annual ACM Symposium on Theory of Computing (pp. 117–123). ACM.
25. Bateni, M., Hajiaghayi, M., & Zadimoghaddam, M., (2010). Submodular secretary
problem and extensions. In: Approximation, Randomization, and Combinatorial
CHAPTER
1
24 Data Structures and Algorithms
CHAPTER
1
Fundamentals of Data Structures and Algorithms 25
42. Bloom, N., & Van, R. J., (2002). Patents, real options and firm performance. The
Economic Journal, 112(478).
43. Blumofe, R. D., & Leiserson, C. E., (1999). Scheduling multithreaded computations
by work stealing. Journal of the ACM (JACM), 46(5), 720–748.
44. Bollerslev, T., Engle, R. F., & Nelson, D. B., (1994). ARCH models. Handbook of
Econometrics, 4, 2959–3038.
45. Brassard, G., & Bratley, P., (1996). Fundamentals of Algorithmics (Vol. 33). Englewood
Cliffs: Prentice Hall.
46. Brent, R. P., (1974). The parallel evaluation of general arithmetic expressions. Journal
of the ACM (JACM), 21(2), 201–206.
47. Brodnik, A., Miltersen, P. B., & Munro, J. I., (1997). Trans-dichotomous algorithms
without multiplication—Some upper and lower bounds. In: Workshop on Algorithms
and Data Structures (pp. 426–439). Springer, Berlin, Heidelberg.
48. Buchbinder, N., Jain, K., & Singh, M., (2010). Secretary problems via linear
programming. In: International Conference on Integer Programming and Combinatorial
Optimization (pp. 163–176). Springer, Berlin, Heidelberg.
49. Buhler, J. P., Lenstra, H. W., & Pomerance, C., (1993). Factoring integers with the
number field sieve. In: The Development of the Number Field Sieve (pp. 50–94).
Springer, Berlin, Heidelberg.
50. Chan, T. H., (2000). Homfly polynomials of some generalized Hopf links. Journal of
Knot Theory and Its Ramifications, 9(07), 865–883.
51. Chen, L. T., & Davis, L. S., (1990). A parallel algorithm for list ranking image curves
in O (log N) time. In: DARPA Image Understanding Workshop (pp. 805–815).
52. Cheriyan, J., & Hagerup, T., (1989). A randomized maximum-flow algorithm. In:
Foundations of Computer Science, 1989, 30th Annual Symposium (pp. 118–123). IEEE.
53. Cheriyan, J., & Hagerup, T., (1995). A randomized maximum-flow algorithm. SIAM
Journal on Computing, 24(2), 203–226.
54. Cheriyan, J., Hagerup, T., & Mehlhorn, K., (1990). Can a maximum flow be computed in
o (nm) time? In: International Colloquium on Automata, Languages, and Programming
(pp. 235–248). Springer, Berlin, Heidelberg.
55. Cheriyan, J., Hagerup, T., & Mehlhorn, K., (1996). An o(n^3)-time maximum-flow
algorithm. SIAM Journal on Computing, 25(6), 1144–1170.
56. Ciurea, E., & Ciupala, L., (2001). Algorithms for minimum flows. Computer Science
Journal of Moldova, 9(3), 27.
57. Conforti, M., Wolsey, L. A., & Zambelli, G., (2010). Projecting an extended formulation
for mixed-integer covers on bipartite graphs. Mathematics of Operations Research,
35(3), 603–623.
58. Courcoubetis, C., Vardi, M., Wolper, P., & Yannakakis, M., (1992). Memory-efficient
algorithms for the verification of temporal properties. Formal Methods in System
Design, 1(2, 3), 275–288.
CHAPTER
1
26 Data Structures and Algorithms
59. D’Aristotile, A., Diaconis, P., & Newman, C. M., (2003). Brownian motion and the
classical groups. Lecture Notes-Monograph Series, 97–116.
60. Damgård, I., Landrock, P., & Pomerance, C., (1993). Average case error estimates
for the strong probable prime test. Mathematics of Computation, 61(203), 177–194.
61. Dehling, H., & Philipp, W., (2002). Empirical process techniques for dependent
data. In: Empirical Process Techniques for Dependent Data (pp. 3–113). Birkhäuser,
Boston, MA.
62. Dengiz, B., Altiparmak, F., & Smith, A. E., (1997). Efficient optimization of all-terminal
reliable networks, using an evolutionary approach. IEEE Transactions on Reliability,
46(1), 18–26.
63. Dickerson, M., Eppstein, D., Goodrich, M. T., & Meng, J. Y., (2003). Confluent drawings:
Visualizing non-planar diagrams in a planar way. In: International Symposium on
Graph Drawing (pp. 1–12). Springer, Berlin, Heidelberg.
64. Didier, F., (2009). Efficient Erasure Decoding of Reed-solomon Codes (Vol. 1, pp.
1–22). arXiv preprint arXiv:0901.1886.
65. Drmota, M., & Szpankowski, W., (2013). A master theorem for discrete divide and
conquer recurrences. Journal of the ACM (JACM), 60(3), 16.
66. Du, W., & Atallah, M. J., (2001). Protocols for secure remote database access with
approximate matching. In: E-Commerce Security and Privacy (pp. 87–111). Springer,
Boston, MA.
67. Duda, R. O., Hart, P. E., & Stork, D. G., (1973). Pattern Classification (Vol. 2). New
York: Wiley.
68. Eppstein, D., Goodrich, M. T., & Sun, J. Z., (2005). The skip quadtree: A simple
dynamic data structure for multidimensional data. In: Proceedings of the Twenty-First
Annual Symposium on Computational Geometry (pp. 296–305). ACM.
69. Eppstein, D., Goodrich, M. T., & Sun, J. Z., (2008). Skip quadtrees: Dynamic data
structures for multidimensional point sets. International Journal of Computational
Geometry & Applications, 18(1, 2), 131–160.
70. Faenza, Y., & Sanità, L., (2015). On the existence of compact ε-approximated
formulations for knapsack in the original space. Operations Research Letters, 43(3),
339–342.
71. Festa, P., & Resende, M. G., (2002). GRASP: An annotated bibliography. In: Essays
and Surveys in Metaheuristics (Vol. 1, pp. 325–367). Springer, Boston, MA.
72. Fourier, J. B. J., (1890).Second extrait: Oeuvres de Fourier, 1(2), 38–42.
73. Fourier, J. B. J., (1973). Second extrait: Oeuvres de Fourier, Paris 1890, 1, 50-72.
74. Fredman, M. L., & Willard, D. E., (1993). Surpassing the information theoretic bound
with fusion trees. Journal of Computer and System Sciences, 47(3), 424–436.
75. Friedl, K., & Sudan, M., (1995). Some improvements to total degree tests. In:
Theory of Computing and Systems, 1995; Proceedings, Third Israel Symposium
(pp. 190–198). IEEE.
CHAPTER
1
Fundamentals of Data Structures and Algorithms 27
76. Frigo, M., Leiserson, C. E., & Randall, K. H., (1998). The implementation of the
cilk-5 multithreaded language. ACM Sigplan Notices, 33(5), 212–223.
77. Fussenegger, F., & Gabow, H. N., (1979). A counting approach to lower bounds for
selection problems. Journal of the ACM (JACM), 26(2), 227–238.
78. Glasser, K. S., & Austin, B. R. H., (1983). The d choice secretary problem. Sequential
Analysis, 2(3), 177–199.
79. Goodrich, M. T., Atallah, M. J., & Tamassia, R., (2005). Indexing information for
data forensics. In: International Conference on Applied Cryptography and Network
Security (pp. 206–221). Springer, Berlin, Heidelberg.
80. Graefe, G., (2011). Modern B-tree techniques. Foundations and Trends® in Databases,
3(4), 203–402.
81. Hall, M., Frank, E., Holmes, G., Pfahringer, Badel’son-Vel’skii, G. M., & Landis, E.
M., (1962). Partial Differential Equations of Elliptic Type (Vol. 1, pp. 1–22). Springer,
Berlin.
82. Herr, D. G., (1980). On the history of the use of geometry in the general linear
model. The American Statistician, 34(1), 43–47.
83. Icking, C., Klein, R., & Ottmann, T., (1987). Priority search trees in secondary memory.
In: International Workshop on Graph-Theoretic Concepts in Computer Science (pp.
84–93). Springer, Berlin, Heidelberg.
84. Igarashi, Y., Sado, K., & Saga, K., (1987). Fast parallel sorts on a practical sized
mesh-connected processor array. IEICE Transactions (1976–1990), 70(1), 56–64.
85. John, J. W., (1988). A new lower bound for the set-partitioning problem. SIAM Journal
on Computing, 17(4), 640–647.
86. Kim, S. H., & Pomerance, C., (1989). The probability that a random probable prime
is composite. Mathematics of Computation, 53(188), 721–741.
87. King, D. J., (1995). Functional binomial queues. In: Functional Programming, Glasgow
1994 (Vol. 1, pp. 141–150). Springer, London.
88. Kirkpatrick, D. G., (1981). A unified lower bound for selection and set partitioning
problems. Journal of the ACM (JACM), 28(1), 150–165.
89. Kwong, Y. S., & Wood, D., (1982). A new method for concurrency in B-trees. IEEE
Transactions on Software Engineering, (3), 211–222.
90. Leighton, T., (1996). Notes on Better Master Theorems for Divide-and-Conquer
Recurrences. Manuscript. Massachusetts Institute of Technology.
91. Maurer, S. B., (1985). The lessons of Williamstown. In: New Directions in Two-Year
College Mathematics (Vol. 1, pp. 255–270). Springer, New York, NY.
92. Meijer, H., & Akl, S. G., (1987). Optimal computation of prefix sums on a binary
tree of processors. International Journal of Parallel Programming, 16(2), 127–136.
93. Meijer, H., & Akl, S. G., (1988). Bit serial addition trees and their applications.
Computing, 40(1), 9–17.
CHAPTER
1
28 Data Structures and Algorithms
94. Monier, L., (1980). Evaluation and comparison of two efficient probabilistic primality
testing algorithms. Theoretical Computer Science, 12(1), 97–108.
95. Morain, F., (1988). Implementation of the Atkin-Goldwasser-Kilian Primality Testing
Algorithm. Doctoral dissertation, INRIA.
96. Munro, J. I., & Poblete, P. V., (1982). A Lower Bound for Determining the Median.
Faculty of Mathematics, University of Waterloo.
97. Nelson, D. B., & Foster, D. P., (1992). Filtering and Forecasting with Misspecified
ARCH Models II: Making the Right Forecast with the Wrong Model, 67(2), 303-335.
98. Nievergelt, J., (1974). Binary search trees and file organization. ACM Computing
Surveys (CSUR), 6(3), 195–207.
99. Park, T. G., & Oldfield, J. V., (1993). Minimum spanning tree generation with content-
addressable memory. Electronics Letters, 29(11), 1037–1039.
100. Phillips, S., & Westbrook, J., (1993). Online load balancing and network flow. In:
Proceedings of the Twenty-Fifth Annual ACM Symposium on Theory of Computing
(pp. 402–411). ACM.
101. Polishchuk, A., & Spielman, D. A., (1994). Nearly-linear size holographic proofs. In:
Proceedings of the Twenty-Sixth Annual ACM Symposium on Theory of Computing
(pp. 194–203). ACM.
102. Prechelt, L., (1993). Measurements of MasPar MP-1216A Communication Operations.
Univ., Fak. für Informatik.
103. Price, G. B., (1973). Telescoping sums and the summation of sequences. The Two-
Year College Mathematics Journal, 4(2), 16–29.
104. Ramadge, P. J., & Wonham, W. M., (1989). The control of discrete event systems.
Proceedings of the IEEE, 77(1), 81–98.
105. Raman, R., (1996). Priority queues: Small, monotone and trans-dichotomous. In:
European Symposium on Algorithms (pp. 121–137). Springer, Berlin, Heidelberg.
106. Regli, W. C., (1992). A Survey of Automated Feature Recognition Techniques, 1,
1–22.
107. Roura, S., (2001). Improved master theorems for divide-and-conquer recurrences.
Journal of the ACM (JACM), 48(2), 170–205.
108. Sardelis, D. A., & Valahas, T. M., (1999). Decision making: A golden rule. The
American Mathematical Monthly, 106(3), 215–226.
109. Schönhage, A., Paterson, M., & Pippenger, N., (1976). Finding the median. Journal
of Computer and System Sciences, 13(2), 184–199.
110. Shen, Z., & Marston, C. M., (1995). A study of a dice problem. Applied Mathematics
and Computation, 73(2, 3), 231–247.
111. Smith, R. S., (1986). Rolle over Lagrange—Another shot at the mean value theorem.
The College Mathematics Journal, 17(5), 403–406.
112. Snyder, L., (1984). Parallel Programming and the Poker Programming Environment
CHAPTER
1
Fundamentals of Data Structures and Algorithms 29
(No. TR-84-04-02, pp. 1–30). Washington Univ. Seattle Dept of Computer Science.
113. Stock, J. H., & Watson, M. W., (2001). Vector autoregressions. Journal of Economic
Perspectives, 15(4), 101–115.
114. Sudan, M., (1992). Efficient checking of polynomials and proofs and the hardness
of approximation problems. Lecture Notes in Computer Science, 1001.
115. Szymanski, T. G., & Van, W. C. J., (1983). Space efficient algorithms for VLSI
artwork analysis. In: Proceedings of the 20th Design Automation Conference (pp.
734–739). IEEE Press.
116. Szymanski, T. G., (1975). A Special Case of the Maximal Common Subsequence
Problem. Technical Report TR-170, Computer Science Laboratory, Princeton University.
117. Thorup, M., (1997). Faster Deterministic Sorting and Priority Queues in Linear Space
(pp. 550–555). Max-Planck-Institut für Informatik.
118. Vanderbei, R. J., (1980). The optimal choice of a subset of a population. Mathematics
of Operations Research, 5(4), 481–486.
119. Verma, R. M., (1994). A general method and a master theorem for divide-and-conquer
recurrences with applications. Journal of Algorithms, 16(1), 67–79.
120. Verma, R. M., (1997). General techniques for analyzing recursive algorithms with
applications. SIAM Journal on Computing, 26(2), 568–581.
121. Wallace, L., Keil, M., & Rai, A., (2004). Understanding software project risk: A cluster
analysis. Information & Management, 42(1), 115–125.
122. Wang, Y., (2008). Topology control for wireless sensor networks. In: Wireless Sensor
Networks and Applications (Vol. 1, pp. 113–147). Springer, Boston, MA.
123. Wilf, H. S., (1984). A bijection in the theory of derangements. Mathematics Magazine,
57(1), 37–40.
124. Williamson, J., (2002). Probability logic. In: Studies in Logic and Practical Reasoning
(Vol. 1, pp. 397–424). Elsevier.
125. Wilson, J. G., (1991). Optimal choice and assignment of the best m of n randomly
arriving items. Stochastic Processes and Their Applications, 39(2), 325–343.
126. Winograd, S., (1970). On the algebraic complexity of functions. In: Actes du Congres
International Des Mathématiciens (Vol. 3, pp. 283–288).
127. Wunderlich, M. C., (1983). A performance analysis of a simple prime-testing algorithm.
Mathematics of Computation, 40(162), 709–714.
128. Xiaodong, W., & Qingxiang, F., (1996). A frame for general divide-and-conquer
recurrences. Information Processing Letters, 59(1), 45–51.
129. Yap, C., (2011). A real elementary approach to the master recurrence and
generalizations. In: International Conference on Theory and Applications of Models
of Computation (pp. 14–26). Springer, Berlin, Heidelberg.
130. Zhu, X., & Wilhelm, W. E., (2006). Scheduling and lot sizing with sequence-dependent
setup: A literature review. IIE Transactions, 38(11), 987–1007.
CHAPTER
1
CHAPTER 2
CLASSIFICATION OF
ALGORITHMS
UNIT INTRODUCTION
An algorithm is a technique or group of methods for completing a problem-solving activity.
The term algorithm, which comes from Medieval Latin, refers to more than only computer
programming. There are many different sorts of algorithms for various issues. The belief
that there are only a finite number of algorithms and that one must learn all of them is a
fallacy that leads many potential programmers to turn to lawn maintenance as a source
of revenue to cover their expenses (Rabin, 1977; Cook, 1983; 1987).
When an issue develops throughout the course of developing the complete software,
an algorithm is created. A variety of classification schemes for algorithms are discussed in
detail in this chapter. There is no single “correct” classification that applies to all situations.
When comparing the tasks of classifying algorithms and attributing them, it is important to
remember that the former is more difficult (Karp, 1986; Virrer & Simons, 1986). Following
a discussion of the labeling procedure for a given algorithm (e.g., the divide-and-conquer
algorithm), we will examine the various methods for examining algorithms in greater depth.
Frequently, the labels with which the algorithms are classified are quite useful and assist
in the selection of the most appropriate type of analysis to perform (Cormen & Leiserson,
1989; Stockmeyer & Meyer, 2002) (Figure 2.1).
The number of fundamental jobs that an algorithm completes is used to determine
its speed or efficiency. Consider the case of an algorithm whose input is N and which
executes a large number of operations.
32 Data Structures and Algorithms
CHAPTER
2
Classification of Algorithms 33
Key Terms
1. Algorithms
2. Approximation Algorithms
3. Dynamic Programming
4. Divide and Conquer
5. Las Vegas
6. Monte Carlo
7. Online
8. Offline
CHAPTER
2
34 Data Structures and Algorithms
CHAPTER
2
Classification of Algorithms 35
• Divide-and-conquer algorithm
• Dynamic programming algorithm
• Backtracking algorithm
• Greedy algorithm
• Brute force algorithm
• Branch-and-bound algorithm
CHAPTER
2
38 Data Structures and Algorithms
Figure 2.4.
Comparison of
iterative and
recursive approaches
(Source: Beau
Cranes, Creative
Commons License).
CHAPTER
2
Classification of Algorithms 39
Figure 2.5.
Representation of
divide and conquer
algorithm (Source:
Khan Academy,
Creative Commons
License).
CHAPTER
2
40 Data Structures and Algorithms
CHAPTER
2
42 Data Structures and Algorithms
Figure 2.7.
Representation
of backtracking
algorithm (Source:
Programiz, Creative
Commons License).
CHAPTER
2
44 Data Structures and Algorithms
• a bill for $5
• a one-dollar note, for a total of six dollars
• 6.25 dollars with a 25-cent coin
• To make 6.35 dollars, you’ll need a ten-cent coin.
• To make $6.39, you’ll need four one-cent coins.
The greedy algorithm always finds the best answer for the
number of dollars.
CHAPTER
2
Classification of Algorithms 45
Figure 2.9.
Graphical form
of brute force
algorithm (Source:
Margaret Rouse,
Creative Commons
License).
CHAPTER
2
Classification of Algorithms 47
SUMMARY
In data structures, classification of algorithms refers to categorizing algorithms based on
their time and space complexity, as well as their general approach to problem-solving.
The time complexity of an algorithm refers to the amount of time it takes to execute,
whereas the space complexity refers to the amount of memory required by the algorithm
to solve the problem.
Algorithms can be classified based on their approach to problem-solving such as
divide and conquer algorithms break a problem down into smaller subproblems and solve
each subproblem separately, while dynamic programming algorithms solve a problem by
breaking it down into smaller overlapping subproblems and storing the results of each
subproblem to avoid redundant computation. Some common classification of algorithms in
data structures include dynamic programming algorithms, divide and conquer algorithms,
greedy algorithms and sorting algorithms.
REVIEW QUESTIONS
1. How are algorithms classified in data structures based on their approach to
problem-solving?
2. What are the differences between deterministic and randomized algorithms?
3. What are online and offline algorithms and how do they differ in their approach
to processing data?
4. What are the characteristics of divide and conquer algorithms?
5. What are dynamic programming algorithms and how do they work?
CHAPTER
2
48 Data Structures and Algorithms
REFERENCES
1. Aydin, M. E., & Fogarty, T. C., (2004). A distributed evolutionary simulated annealing
algorithm for combinatorial optimization problems. Journal of Heuristics, 10(3),
269–292.
2. Aydin, M. E., & Fogarty, T. C., (2004). A simulated annealing algorithm for multi-agent
systems: A job-shop scheduling application. Journal of Intelligent Manufacturing,
15(6), 805–814.
3. Aydin, M. E., & Yigit, V., (2005). 12 parallel simulated annealing. Parallel Metaheuristics:
A New Class of Algorithms, 47, 267.
4. Battiti, R., & Tecchiolli, G., (1994). The reactive tabu search. ORSA Journal on
Computing, 6(2), 126–140.
5. Chazelle, B., Edelsbrunner, H., Guibas, L., & Sharir, M., (1989). Lines in space-
combinators, algorithms and applications. In: Proceedings of the Twenty-First Annual
ACM Symposium on Theory of Computing (pp. 382–393). ACM.
6. Codenotti, B., & Simon, J., (1998). On the Complexity of the Discrete Fourier
Transform and Related Linear Transforms Preliminary Version, 1(2), 22-34.
CHAPTER
2
Classification of Algorithms 49
26. Hirschberg, D. S., & Wong, C. K., (1976). A polynomial-time algorithm for the
knapsack problem with two variables. Journal of the ACM (JACM), 23(1), 147–154.
27. Hu, X., Eberhart, R. C., & Shi, Y., (2003). Particle swarm with extended memory
for multiobjective optimization. In: Swarm Intelligence Symposium, 2003. SIS’03;
Proceedings of the 2003 IEEE (pp. 193–197). IEEE.
28. Kågström, B., & Van, L. C., (1998). Algorithm 784: GEMM-based level 3 BLAS:
Portability and optimization issues. ACM Transactions on Mathematical Software
(TOMS), 24(3), 303–316.
29. Kannan, R., (1980). A polynomial algorithm for the two-variable integer programming
problem. Journal of the ACM (JACM), 27(1), 118–122.
30. Karp, R. M., (1986). Combinatorics, complexity, and randomness. Commun. ACM,
29(2), 97–109.
31. Kennedy, J., & Mendes, R., (2002). Population structure and particle swarm
performance. In: Evolutionary Computation, 2002: CEC’02; Proceedings of the 2002
Congress (Vol. 2, pp. 1671–1676). IEEE.
32. Kennedy, J., & Mendes, R., (2006). Neighborhood topologies in fully informed and
best-of-neighborhood particle swarms. IEEE Transactions on Systems, Man, and
Cybernetics, Part C (Applications and Reviews), 36(4), 515–519.
33. Kennedy, J., Eberhart, R. C., & Shi, Y., (2001). Swarm Intelligence. Morgan Kaufmann
Publishers. Inc., San Francisco, CA.
34. Kröse, B., Krose, B., Van, D. S. P., & Smagt, P., (1993). An Introduction to Neural
Networks, 1, 12-19.
35. Kukuk, M., (1997). Kompakte Antwortdatenbasen Fiur die LIOSUNG Von Geometrischen
Anfrageproblemen Durch Abtastung. Doctoral dissertation, Diplomarbeit, Informatik
VII, universitiat Dortmund.
36. Li, J., (2004). Peer Streaming: A Practical Receiver-Driven Peer-to-Peer Media
Streaming System. Microsoft Research MSR-TR-2004-101, Tech. Rep.
37. Liu, H., Luo, P., & Zeng, Z., (2007). A structured hierarchical P2P model based on
a rigorous binary tree code algorithm. Future Generation Computer Systems, 23(2),
201–208.
38. Nisan, N., & Wigderson, A., (1995). On the complexity of bilinear forms: Dedicated to
the memory of jacques morgenstern. In: Proceedings of the Twenty-Seventh Annual
ACM Symposium on Theory of Computing (pp. 723–732). ACM.
39. Panda, P. R., Nakamura, H., & Dutt, N. D., (1997). Tiling and data alignment. Solving
Irregularly Structured Problems in Parallel Lecture Notes in Computer Science.
40. Pereira, M., (2009). Peer-to-peer computing. In: Encyclopedia of Information Science
and Technology, (2nd edn., pp. 3047–3052). IGI Global.
41. Pham, D., & Karaboga, D., (2012). Intelligent Optimization Techniques: Genetic
Algorithms, Tabu Search, Simulated Annealing and Neural Networks. Springer
Science & Business Media.
CHAPTER
2
Classification of Algorithms 51
CHAPTER
2
CHAPTER 3
ANALYSIS OF ARRAYS
AND SETS
UNIT INTRODUCTION
After writing a few words of computer code, it is realized that data is the heart of
programming. All computer programs do is receive, modify, and output data. The software
depends on data, whether it’s a straightforward application that adds two numbers or
enterprise applications that manage entire businesses (Lin et al., 2001).
Data is a general phrase that covers all kinds of information, including the most
elementary strings and numbers. The string “Hello World!” seems a data item in the
straightforward yet iconic “Hello World!” program (Aho & Hopcroft, 1974). Even the most
complex data sets are typically decomposed into a collection of numbers and strings.
Data organization is referred to as data structures. Examine the code below (Gu et
al., 1997):
x = “Hello!”
y = “How are you” z = “today?”
Print x + y + z
Three pieces of data are handled by a straightforward program, generating three strings
into a single, coherent message. Characterizing the data structure used in a program
54 Data Structures and Algorithms
made it clear that it consists of three separate strings, each referenced by a different
variable. This book clarifies that data organization is important for more than just keeping
things in order; and how quickly codes are executed. Software may run quite faster or
slower depending on how data is arranged (Heintze, 1994). Additionally, the chosen data
structures could determine whether the software works or crashes because the load of
developing a program cannot be managed and must deal with a lot of data or a web
application used mostly by thousands of users at once.
The ability to design swift, elegant code which might guarantee that software runs
quickly and without hiccups will substantially improve having a firm understanding of the
various datatypes and how each one affects the speed of writing a program (Morris et
al., 2008).
The analysis of sets and arrays is made clearer in this chapter. Despite the two data
structures’ apparent similarity, how to examine each decision’s effects on performance is
explained.
LEARNING OBJECTIVES
After the chapter, students will be able to understand the following:
• The basic idea and characteristics of arrays as a data structure
• Sets’ fundamental concept as a data structure.
• Recognize many operations that may be carried out on sets and arrays.
• Recognize the temporal complexity of various arrays and set operations.
Key Terms
1. Array
2. Data Structure
3. Delete
4. Insert
5. Memory Address
6. Read
7. Search
8. Set
9. Operations
10. Time Complexity
CHAPTER
3
Analysis of Arrays and Sets 55
3.1.1. Reading
Reading is the first operation to determine the value at a specific
array index.
CHAPTER
3
Analysis of Arrays and Sets 57
CHAPTER
3
58 Data Structures and Algorithms
KEYWORD
Memory address
is a reference to
a specific memory
location used at
various levels
by software and
hardware.
The shopping list array, along with its indexes and memory
addresses, can be shown in the following diagram (Wiebe et al.,
2014):
3.1.2. Searching
Searching an array entails determining whether a specific value
is present and, if so, at which index. How many steps would be
needed to look for “dates” in an array?
The “dates” are immediately visible as the user quickly scans
the grocery list and counts, thinking it’s at index 3 (Virtanen et
al., 2020). On the other hand, a computer lacks sight and must
proceed cautiously across the array.
The computer begins at index 0 when looking for a value in
an array, checks the value, and moves on to the next index if it
doesn’t find what it’s looking for. It continues doing this until it
discovers the value it seeks.
Such illustrations show how the computer search for “dates”
within our array of grocery lists:
The machine checks index 0 first (Vigna, 2008):
CHAPTER
3
60 Data Structures and Algorithms
3.1.3. Insertion
Inserting a new data item into an array determines how efficiently
one can do so.
Including “figs” as the final item on a shopping list must be
done once because the computer knows the memory address
where the array starts. The computer can now determine which
memory address to add a new element too because it knows how
many items the array currently includes and can do so in a single
step. View the diagram below (Aurenhammer, 1991):
CHAPTER
3
62 Data Structures and Algorithms
CHAPTER
3
Analysis of Arrays and Sets 63
Step #4: Last but not least, we may add “figs” to index 2: KEYWORD
Array is a data
structure consisting
of a collection of
elements (values
or variables), of
same memory size,
each identified by
There are four steps in the given case. Three stages involved at least one array
moving data to the right, and step four involved adding the new index or key.
value.
Inserting data at the top of the array is the worst-case scenario
and the circumstance where insertion requires the most steps
(Reynolds, 2002). This is because all other values in one cell
move to the right when inserted at the start of the array.
Hence, in the worst case, an array with N elements insertion
could require close to N + 1 steps. The worst situation involves
adding a value at the start of the array, which involves N shifts
(per array data element) and 1 insertion.
The last operation is deletion, which is essentially just insertion
done backward.
3.1.4. Deletion
Deleting is removing the values at a specific index out of an array.
Recalling the example array started, remove the values at
index 2. which are “cucumbers” in the existing scenario.
Step #1: “Cucumbers” are eliminated from the array (McKinney,
2010):
CHAPTER
3
64 Data Structures and Algorithms
empty and not permitted for arrays, and all of the other elements
would need to be shifted to the left to fill the void (Goodrich et
al., 2013).
One step is taken to remove the first element from a five-
element array, and another four steps to shift the other four
elements. One step is taken to delete the first element from an
array of 500 elements, and 499 steps to relocate the remaining
data (McKinney, 2011). Hence, the maximum number of steps
required for deletion for an array with N elements is N.
Though knowing the evaluation of data structure’s time
complexity, how various data structures perform in different ways
is determined. This is crucial because picking the proper data
structure for the program can significantly impact how efficient
and accurate the code will be. Did you Know?
The next data structure, the set, is so close to the array that Sets are a
mathematical idea
they may appear identical at first glance. Yet, the efficiency of
that has been
operations done on arrays and sets differs.
investigated for
centuries; the ancient
Greeks were the
3.2. EFFICIENCY AFFECTED IN SETS BY SINGLE first to investigate its
RULE properties.
contacts the Zirkinds to inform them of the mistake, the user’s wife
answers the phone since the user accidentally dialed his number.
(Ok, that last part doesn’t ever occur.) If only the computer that
generated the telephone directory had employed a set.
In any scenario, a set is just an array with the restriction of
duplicates not permitted. Yet, this restriction alters the efficiency
of one of the four basic operations for the set. Let’s examine
reading, searching, insertion, and deletion within the framework
of an array-based set.
Reading from one set is identical to reading from an array;
the computer needs only one step to determine what is needed
within a certain index. The system may jump to almost any index
between the sets (Vaidya & Clifton, 2005). After all, it knows where
the set begins in memory.
It takes almost N steps to determine whether a value exists
inside a set, the same as the number of steps required to determine
if a value appears within an array. And deletion is equivalent
between a set or an array; it requires up to N steps to remove a
value and shift data to the left to fill in the gap (Urban & Quilter,
2005). Arrays and sets differ, however, with respect to insertion.
Putting a number at the end of a set was the ideal example for
an array. While using an array, the computer can insert a value
at the end of an array in a single step computer can add a value
at the end of an array in one step while using an array.
Although this is exactly what sets are intended to achieve,
the computer must first confirm that this value doesn’t yet exist
within the set: They prevent data duplication. Thus, each insert
requires an initial search. No one wants to purchase the same
item twice, so having a defined grocery list would be a good idea.
If the current set is [“apples,” “bananas,” “cucumbers,” “dates,”
and “elderberries”] and “figs,” needs to add to this listing search
is required and follow the procedures below:
Step #1: Index 0 search for “figs” (Kulik et al., 1985):
CHAPTER
3
Analysis of Arrays and Sets 67
CHAPTER
3
68 Data Structures and Algorithms
Step #6: At the end of the set, add “figs.” (Bernard et al., 2012).
ACTIVITY 3.1
Adding a value at the end of a set is the best case, but still
Examine the had to go through 6 stages for a set with five items at first. This
effectiveness of means a search through all five items is needed before the last
various operations insertion step.
on arrays & sets,
including adding, In other words: In the best situation, N elements will require N
removing, looking for, + 1 steps for insertion into a set. This is so that the value can be
and iterating through inserted in one step after N searching steps to ensure it doesn’t
the elements. already exist in the set (Rosenschein & Zlotkin, 1994).
In the worst case, the computer would have to check N cells
to ensure the set doesn’t already include the value entered at the
beginning, take a further N step to move all of the data toward
the right, and then take a final step to add the new value. The
total steps are 2N plus 1.
Does this imply that sets should be avoided since insertion is
slower than normal arrays? Without a doubt, ensure there are no
duplicate pieces of data. Sets are crucial. If this is not required,
an array might be better because insertions of arrays are quicker
than insertions of sets. The best data format for the application
will depend on analyzing its requirements.
CHAPTER
3
Analysis of Arrays and Sets 69
SUMMARY
Understanding the efficiency of data structures starts with looking at how many steps an
operation requires. The proper data structure for software will determine whether it can
carry the load or break beneath it. How to apply this analysis and to determine if an array
or set could be the better option for a particular application is described in this chapter.
Use the same evaluation to assess competing algorithms (even those operating on the
same data structure), ensuring the final speed and performance of code now consider
the computation time of data structures.
REVIEW QUESTIONS
1. What are data structures, and why are they important in computer programming?
2. What is an array, and what are its characteristics?
3. How is a set different from other data structures?
4. How does the time complexity of operations differ between arrays and sets?
5. Give examples of algorithms or applications where arrays or sets are used.
CHAPTER
3
70 Data Structures and Algorithms
REFERENCES
1. Aho, A. V., & Hopcroft, J. E., (1974). The design and analysis of computer algorithms
(Vol. 1, pp. 2–5). Pearson Education India.
2. Amza, C., Cox, A. L., Dwarkadas, S., Keleher, P., Lu, H., Rajamony, R., & Zwaenepoel,
W., (1996). Treadmarks: Shared memory computing on networks of workstations.
Computer, 29(2), 18–28.
3. Aurenhammer, F., (1991). Voronoi diagrams—A survey of a fundamental geometric
data structure. ACM Computing Surveys (CSUR), 23(3), 345–405.
4. Belady, L. A., (1966). A study of replacement algorithms for a virtual-storage computer.
IBM Systems Journal, 5(2), 78–101.
5. Bernard, P. E., Moës, N., & Chevaugeon, N., (2012). Damage growth modeling using
the thick level set (TLS) approach: Efficient discretization for quasi-static loadings.
Computer Methods in Applied Mechanics and Engineering, 233(1), 11–27.
6. Breen, E. J., Joss, G. H., & Williams, K. L., (1992). Dynamic arrays for fast, efficient,
data manipulation during image analysis: A new software tool for exploratory data
analysis. Computer Methods and Programs in Biomedicine, 37(2), 85–92.
7. Burlinson, D., Mehedint, M., Grafer, C., Subramanian, K., Payton, J., Goolkasian,
P., & Kosara, R., (2016). BRIDGES: A system to enable creation of engaging data
structures assignments with real-world data and visualizations. In: Proceedings of
the 47th ACM Technical Symposium on Computing Science Education (Vol. 1, pp.
18–23).
8. Charles, P., Grothoff, C., Saraswat, V., Donawa, C., Kielstra, A., Ebcioglu, K., &
Sarkar, V., (2005). X10: An object-oriented approach to non-uniform cluster computing.
CHAPTER
3
Analysis of Arrays and Sets 71
24. Manber, U., & Myers, G., (1993). Suffix arrays: A new method for on-line string
searches. SIAM Journal on Computing, 22(5), 935–948.
25. McKinney, W., (2010). Data structures for statistical computing in python. In:
Proceedings of the 9th Python in Science Conference (Vol. 445, No. 1, pp. 51–56).
26. McKinney, W., (2011). Pandas: A foundational python library for data analysis and
statistics. Python for High Performance and Scientific Computing, 14(9), 1–9.
27. Mehlhorn, K., (2013). Data Structures and Algorithms 1: Sorting and Searching (Vol.
1, pp. 4–8). Springer Science & Business Media.
28. Millman, K. J., & Aivazis, M., (2011). Python for scientists and engineers. Computing
in Science & Engineering, 13(2), 9–12.
29. Mitzenmacher, M., (2001). Compressed bloom filters. In: Proceedings of the Twentieth
Annual ACM Symposium on Principles of Distributed Computing (Vol. 1, pp. 144–150).
30. Morris, M. D., Moore, L. M., & McKay, M. D., (2008). Using orthogonal arrays in the
sensitivity analysis of computer models. Technometrics, 50(2), 205–215.
31. Papazafiropulos, N., Fanucci, L., Leporini, B., Pelagatti, S., & Roncella, R., (2016).
Haptic models of arrays through 3D printing for computer science education. In:
Computers Helping People with Special Needs: 15th International Conference, ICCHP
2016, Linz, Austria, July 13–15, 2016, Proceedings, Part I 15 (Vol. 1, pp. 491–498).
Springer International Publishing.
32. Reynolds, J. C., (2002). Separation logic: A logic for shared mutable data structures.
In: Proceedings 17th Annual IEEE Symposium on Logic in Computer Science (Vol.
1, pp. 55–74). IEEE.
33. Rosenschein, J. S., & Zlotkin, G., (1994). Rules of Encounter: Designing Conventions
for Automated Negotiation Among Computers (Vol. 1, pp. 3–6). MIT press.
34. Samet, H., (1984). The quadtree and related hierarchical data structures. ACM
Computing Surveys (CSUR), 16(2), 187–260.
35. Urban, J. M., & Quilter, L., (2005). Efficient process or chilling effects-takedown notices
under section 512 of the digital millennium copyright act. Santa Clara Computer &
High Tech. LJ, 22(1), 621.
36. Vaidya, J., & Clifton, C., (2005). Secure set intersection cardinality with application
to association rule mining. Journal of Computer Security, 13(4), 593–622.
37. Van, D. W. S., Colbert, S. C., & Varoquaux, G., (2011). The NumPy array: A structure
for efficient numerical computation. Computing in Science & Engineering, 13(2), 22–30.
38. Verma, N., Jia, H., Valavi, H., Tang, Y., Ozatay, M., Chen, L. Y., & Deaville, P.,
(2019). In-memory computing: Advances and prospects. IEEE Solid-State Circuits
Magazine, 11(3), 43–55.
39. Vigna, S., (2008). Broadword implementation of rank/select queries. In: Experimental
Algorithms: 7th International Workshop, WEA 2008 Provincetown, MA, USA, May
30-June 1, 2008 Proceedings 7 (Vol. 1, pp. 154–168). Springer Berlin Heidelberg.
CHAPTER
3
Analysis of Arrays and Sets 73
40. Virtanen, P., Gommers, R., Oliphant, T. E., Haberland, M., Reddy, T., Cournapeau,
D., & Van, M. P., (2020). SciPy 1.0: Fundamental algorithms for scientific computing
in python. Nature Methods, 17(3), 261–272.
41. Wang, Y., Huang, J., & Lei, J., (2011). The formal design models of a universal
array (UA) and its implementation. International Journal of Software Science and
Computational Intelligence (IJSSCI), 3(3), 69–89.
42. Wiebe, M., Rocklin, M., Alumbaugh, T. J., & Terrel, A., (2014). Blaze: Building a
foundation for array-oriented computing in python. In: Proceedings of the 13th Python
in Science Conference (Vol. 1, pp. 99–102).
CHAPTER
3
CHAPTER 4
ALGORITHM SELECTION
UNIT INTRODUCTION
The effectiveness of code can be considerably impacted by the data structure. Even
seemingly unrelated data structures, like the set and array, may harm or make a program
under excessive stress (Raschka, 2018).
After choosing a specific data structure, there is still another crucial variable that can
have a significant impact on the effectiveness of code: selecting the right algorithm to
employ.
Although the term algorithm implies complexity, the process is quite simple. An algorithm
is a specific method for addressing a problem (Kerschke et al., 2019). For instance, an
algorithm might be used to prepare a cereal bowl. The algorithm for making cereal follows
these four steps:
i. Take a bowl.
ii. Fill the bowl with cereal.
iii. Fill the bowl with milk.
iv. Place a spoon inside the bowl (Rice, 1976).
An algorithm is a procedure for carrying out a specific operation in computing terms.
Deletion, searching, insertion, and reading are the four main processes examined previously.
This chapter explains multiple ways to approach a task. In other words, various algorithms
can carry out a specific operation.
76 Data Structures and Algorithms
To learn how to make the code writing fast or slow—even to the point at which it
breaks down under extreme stress. A brand-new data structure is examined for this
purpose, called an ordered array (Leyton-Brown et al., 2003). Many available algorithms
are searched for various orders and learn how to pick the best one.
Learning Objectives
After the chapter, readers will be able to comprehend the following:
• Recognize the significance of algorithms for data structures.
• A conceptual basis for ordered arrays.
• Method for looking for a value in ordered arrays.
• The fundamental concept of binary search or comparison to linear search.
Key Terms
1. Algorithms
2. Binary Search
3. Data Structures
4. Linear Search
5. Ordered arrays
6. Sorting
7. Searching
CHAPTER
4
Algorithm Selection 77
CHAPTER
4
78 Data Structures and Algorithms
CHAPTER
4
Algorithm Selection 79
CHAPTER
4
80 Data Structures and Algorithms
CHAPTER
4
Algorithm Selection 81
Let’s assume that one wants to look inside this ordered array
for the value 7. A binary search will help do this:
Step #1: Start looking in the center cell. Since the array’s
length is known, one can divide it by two to get the correct memory
address and jump to this cell. Look at that cell’s value:
CHAPTER
4
82 Data Structures and Algorithms
One can infer the position of the seven is to its left because
the revealed value is a 9. Effectively deleted all cells to the proper
(right) side of the nine from the array, which is half of the array.
(and the 9 itself) (Liu et al., 2022):
Step #2: Look at the cells’ middle value to the left of number
9. Given that there are two center values, we arbitrarily select the
left one (Crawford et al., 2017):
to the value in the last cell. This would require 100 steps for an
array of size 100.
But when utilizing binary search, every guess cuts down the
number of cells that potentially scan by half. To eliminate a startling
fifty cells in the first guess
Take a second look and will notice a pattern:
The binary search method requires two steps to find anything
in a list of size 3, which is the maximum number of steps possible
(Bentley, 1979).
KEYWORD There are seven cells in total to twice the present amount of
Binary search cells on an array of cells, and then add one more cell so that
is an efficient the total number remains odd to keep things simple. Using binary
algorithm for search, the number of steps that may be taken to find something
finding an item in such an array is three.
from a sorted list
of items. If double it once more (and add one), then the greatest number
of steps required to locate something utilizing binary search is
four. This is the case even if the sorted array has 15 members.
The pattern appears that the number of steps required for binary
search will rise by just one each time the number of objects in
the arranged array is doubled. This pattern develops when double
the number of items in the array.
This pattern is exceptionally useful: The binary search technique
will add an excess of a single additional step for each time that
doubles the amount of data (Das & Khilar, 2013).
Compare this to the method of linear search. Having three
items, the process could take up to three steps. No one can have
more than seven stages for any element. Up to 100 steps are
required to reach the number 100. A linear search will have the
same stages as an item within the database. When using a linear
search, the number of necessary steps will increase by a factor
of two if the same factor increases the number of elements in an
array (Balogun, 2020). In binary search, increase the number of
steps by one if the number of elements in the array increases is
increased by a factor of two.
This graph enables everyone to see how the performance of
binary search compares to that of linear search (Kumari et al.,
2012):
CHAPTER
4
Algorithm Selection 85
Now see how this works out for arrays that are even more
massive. When searching through an array with 10,000 elements,
a linear approach could take as many as 10,000 steps, while a
binary search could take as few as 13 steps. A linear search may
take as many as one million steps for an array with a size of 1
Did you Know?
million, whereas a binary search could take as few as twenty steps Ada Lovelace, a
for the same size array (Sreelaja, 2021). British mathematician,
and author who lived
Once more, it is important to consider that structured arrays are in the nineteenth
not always faster in all circumstances. Inserting data into ordered century created one
arrays takes significantly longer than into regular arrays. The trade- of the first computer
off, however, is as follows: While utilizing an ordered array, the programs.
insertion process is slightly slower, but the search process is a
lot quicker. Always evaluating applications is required to determine
what would be a better fit (Bajwa et al., 2015).
And this is the point where talking about algorithms. There
are frequently several ways to accomplish a specific computing
task and the algorithm that might significantly impact how quickly
the code runs.
It’s also critical to understand that, in most cases, there isn’t a
single data structure or method that is ideal in every circumstance
(Gambhir et al., 2016). Always utilize sorted arrays just because
they provide binary search. Standard arrays might be preferable
when expecting to explore the data frequently but merely add data,
as their insertion is quicker. Counting each competing algorithm’s
steps that each competing algorithm takes is the best way to
compare them.
CHAPTER
4
86 Data Structures and Algorithms
ACTIVITY 4.1
Use a binary search technique to look up a certain element within an ordered array.
Plot the runtimes of the linear search and binary search methods for big arrays to
compare how well they work.
CHAPTER
4
Algorithm Selection 87
SUMMARY
Because they govern how data may be handled, modified, and searched, algorithms are
important in data structures. Binary search is one typical algorithm that is used to order
arrays. A data structure known as an ordered collection comprises a group of components
sorted in either a descending or ascending order. An ordered array can be effectively
searched for a particular element using the binary search technique. With ordered arrays,
binary search dramatically lowers the number of comparisons required for locating an
element. Overall, algorithms like binary search are critical for data structures like ordered
arrays. They can greatly increase the effectiveness of activities like searching, resulting
in speedier and more productive systems.
REVIEW QUESTIONS
1. Why algorithms are important in data structures.
2. What effect do algorithms have on how well data structures perform?
3. Describe an ordered array & how data structures use it.
4. How binary search boosts the effectiveness of looking through ordered arrays.
5. How may algorithm selection impact the effectiveness of data structures?
CHAPTER
4
88 Data Structures and Algorithms
REFERENCES
1. Alhroob, A., Alzyadat, W., Imam, A. T., & Jaradat, G. M., (2020). The genetic algorithm
and binary search technique in the program path coverage for improving software
testing using big data. Intelligent Automation & Soft Computing, 26(4), 3–9.
2. Amestoy, P. R., Davis, T. A., & Duff, I. S., (2004). Algorithm 837: AMD, an approximate
minimum degree ordering algorithm. ACM Transactions on Mathematical Software
(TOMS), 30(3), 381–388.
3. Baase, S., (2009). Computer Algorithms: Introduction to Design and Analysis (Vol.
1, pp. 2–5). Pearson Education India.
4. Bajwa, M. S., Agarwal, A. P., & Manchanda, S., (2015). Ternary search algorithm:
Improvement of binary search. In: 2015 2nd International Conference on Computing
for Sustainable Global Development (INDIACom) (Vol. 1, pp. 1723–1725). IEEE.
5. Balogun, G. B., (2020). A Comparative Analysis of the Efficiencies of Binary and
Linear Search Algorithms (Vol. 1, pp. 5–8).
6. Bentley, J. L., & Sedgewick, R., (1997). Fast algorithms for sorting and searching
strings. In: Proceedings of the Eighth Annual ACM-SIAM Symposium on Discrete
Algorithms (Vol. 1, pp. 360–369).
7. Bentley, J. L., (1975). Multidimensional binary search trees used for associative
searching. Communications of the ACM, 18(9), 509–517.
8. Bentley, J. L., (1979). Multidimensional binary search trees in database applications.
IEEE Transactions on Software Engineering, 3(4), 333–340.
CHAPTER
4
Algorithm Selection 89
9. Berman, G., & Collin, A. W., (1974). A modified list technique allowing binary
search. Journal of the ACM (JACM), 21(2), 227–232.
10. Chen, W., (2001). New algorithm for ordered tree-to-tree correction problem. Journal
of Algorithms, 40(2), 135–158.
11. Christou, M., Crochemore, M., Flouri, T., Iliopoulos, C. S., Janoušek, J., Melichar, B.,
& Pissis, S. P., (2012). Computing all subtree repeats in ordered trees. Information
Processing Letters, 112(24), 958–962.
12. Crawford, B., Soto, R., Astorga, G., García, J., Castro, C., & Paredes, F., (2017).
Putting continuous metaheuristics to work in binary search spaces. Complexity, 1, 3–7.
13. Das, P., & Khilar, P. M., (2013). A randomized searching algorithm and its performance
analysis with binary search and linear search algorithms. International Journal of
Computer Science & Applications (TIJCSA), 1(11), 2–7.
14. Gambhir, A., Vijarania, M., & Gupta, S., (2016). Implementation and application of
binary search in 2-D array. Int. J. Inst. Ind. Res., 2(1), 30, 31.
15. Jennison, B. K., Allebach, J. P., & Sweeney, D. W., (1991). Efficient design of
direct-binary-search computer-generated holograms. JOSA A, 8(4), 652–660.
16. Kerschke, P., Hoos, H. H., Neumann, F., & Trautmann, H., (2019). Automated
algorithm selection: Survey and perspectives. Evolutionary Computation, 27(1), 3–45.
17. Kumar, P., (2013). Quadratic search: A new and fast searching algorithm (an
extension of classical binary search strategy). International Journal of Computer
Applications, 65(14), 1–10.
18. Kumari, A., Tripathi, R., Pal, M., & Chakraborty, S., (2012). Linear search versus
binary search: A statistical comparison for binomial inputs. International Journal of
Computer Science, Engineering and Applications, 2(2), 29–30.
19. Leyton-Brown, K., Nudelman, E., Andrew, G., McFadden, J., & Shoham, Y., (2003).
A portfolio approach to algorithm selection. In: IJCAI (Vol. 3, pp. 1542–1543).
20. Lieberman, D. J., & Allebach, J. P., (1997). Efficient model based halftoning
using direct binary search. In: Proceedings of International Conference on Image
Processing (Vol. 1, pp. 775–778). IEEE.
21. Liu, J. P., & Tsai, C. M., (2022). Binary computer-generated holograms by simulated-
annealing binary search. In: Photonics (Vol. 9, No. 8, p. 581). MDPI.
22. Liu, J. P., Yu, C. Q., & Tsang, P. W., (2019). Enhanced direct binary search algorithm
for binary computer-generated Fresnel holograms. Applied Optics, 58(14), 3735–3741.
23. Manber, U., & Myers, G., (1993). Suffix arrays: A new method for on-line string
searches. SIAM Journal on Computing, 22(5), 935–948.
24. Mehlhorn, K., (2013). Data Structures and Algorithms 1: Sorting and Searching
(Vol. 1, pp. 4–8). Springer Science & Business Media.
25. Mohammed, A. S., Amrahov, Ş. E., & Çelebi, F. V., (2021). Interpolated binary
search: An efficient hybrid search algorithm on ordered datasets. Engineering
Science and Technology, an International Journal, 24(5), 1072–1079.
CHAPTER
4
90 Data Structures and Algorithms
26. Mozes, S., Onak, K., & Weimann, O., (2008). Finding an optimal tree searching
strategy in linear time. In: SODA (Vol. 8, pp. 1096–1105).
27. Nowak, R., (2009). Noisy generalized binary search. Advances in Neural Information
Processing Systems, 22(1), 3–6.
28. Parmar, V. P., & Kumbharana, C. K., (2015). Comparing linear search and binary
search algorithms to search an element from a linear list implemented through static
array, dynamic array and linked list. International Journal of Computer Applications,
121(3), 1–6.
29. Puglisi, S. J., Smyth, W. F., & Turpin, A. H., (2007). A taxonomy of suffix array
construction algorithms. ACM Computing Surveys (CSUR), 39(2), 4–9.
30. Raschka, S., (2018). Model Evaluation, Model Selection, and Algorithm Selection in
Machine Learning, 1, 4–10.
31. Rice, J. R., (1976). The algorithm selection problem. In: Advances in Computers
(Vol. 15, pp. 65–118). Elsevier.
32. Sreelaja, N. K., (2021). Ant colony optimization based light weight binary search for
efficient signature matching to filter ransomware. Applied Soft Computing, 111(1),
107–635.
33. Swarztrauber, P. N., (1984). FFT algorithms for vector computers. Parallel Computing,
1(1), 45–63.
34. Zhang, D., Wei, L., Leung, S. C., & Chen, Q., (2013). A binary search heuristic
algorithm based on randomized local search for the rectangular strip-packing problem.
INFORMS Journal on Computing, 25(2), 332–345.
CHAPTER
4
CHAPTER 5
UNIT INTRODUCTION
A stack only provides access to the most recently inserted data item. One can see
the next-to-last item introduced, if the item is removed etc. This ability is beneficial in
numerous programming scenarios (Brandenburg, 1988). This section investigates the use
of stacking to determine whether the parentheses and commas in the source file of a
computer program are uniformly distributed. Stacking is also indispensable for decoding
the weakest (analyzing) equations like 3*(4+5).
Stacking is helpful when programmers use complex data structure algorithms. Most
microprocessors, including those found in computers, utilize a stack-based architecture.
The returned address and arguments are placed on a stack before the stack is cleared
when an associated function is called. Stacking operations are incorporated within the
microprocessor (Shavit & Taubenfeld, 2016).
Earlier portable calculators utilized a stack-based architecture. Instead of using
parentheses to input arithmetic expressions it pushed the intermediate outcomes onto a
stack.
A line is a British term for the procession (the type in which individuals wait). To queue
up in UK means creating a line. In computing, a queue has evolved into an identical
data structure to a stack, except that the first item added to a backlog is eliminated
first. (FIFO). The last item added to a pile is the first to be eliminated. (LIFO) (Pierson
& Rodger, 1998).
92 Data Structures and Algorithms
A queue works identically to the ticketing line: the first person who arrives at the
queue’s rear is the first to reach the front to purchase a ticket. Final in line appears to be
the only one who purchased a ticket (or, if the event is sold out, the final individual who
did not purchase a ticket). Figure 5.1 depicts how this appears (Park & Ahmed, 2017).
Figure 5.1. Image of how a queue works (Source: Jeff Durham, Creative Commons License).
Learning Objectives
At the chapter’s end, students will get the ability to comprehend the following:
• Understand the concepts of stacks and queues and their applications in computer
science
• Difference between LIFO (last in, first out) and FIFO (first in, first out) data
structures
• Basic operations of stacks and queues, including push, pop, enqueue and dequeue
• Understand how to implement stacks and queues using arrays and linked list
• Fundamental concept of priority queues and their implementation
Key Terms
1. Array Implementation
2. Dequeue
3. Enqueue
CHAPTER
5
Algorithm Selection 93
4. Function Calls
5. FIFO
6. LIFO
7. Linked List
8. Overflow
9. Push
10. Pop
11. Task Scheduling
12. Underflow
CHAPTER
5
94 Data Structures and Algorithms
CHAPTER
5
96 Data Structures and Algorithms
5.1.1.1. New
Four data items are already entered into the stack at the beginning
of the Workshop applet. The New button will start a brand-new
stack off empty if that’s what is required (Case et al., 2010). The
three buttons after that perform the key stack operations.
5.1.1.2. Push
To add an object to the stack, click the Push icon. After once
tapping this button, enter the key’s contents to push the object.
A few more keystrokes will move the entered item to the peak of
the stack after it has been entered into the text field.
The topmost part of the stack, or the most recent item inserted,
is always shown by a red arrow (Celinski et al., 2017). The Top
arrow is incremented (moved up) throughout the insertion procedure
in one step (button click), and the data item is inserted into the
cell in the following phase. Reversing the order would replace the
current item at the top. Maintaining these two phases correctly
while writing the code for implementing a stack is crucial.
Pushing a new item when the stack of items is full results in
a message that says, “Can’t insert: stacking is full.” (The array
that implements an ADT stack fills up even though an ADT stack
shouldn’t logically do so.)
CHAPTER
5
Algorithm Selection 97
5.1.1.3. Pop
Use the Pop icon to eliminate data from the highest part of the
KEYWORD
stack. When a pop() routine returns a result, the popped value Stack is an
displays in the Numbers text field (Boyapati & Darga, 2007). abstract data type
(ADT), that is
Again, note the two necessary steps: The item is first taken popularly used in
out of the cell that Top points to, after which Top decreases to most programming
indicate the greatest occupied cell. The order utilized in the initial languages. It
push operation is reversed in this. is named stack
because it has the
When an item is eliminated from the array using the pop
similar operations
operation, the cell color turns gray to indicate it has done so.
as the real-world
This slightly misrepresents how computer memory works because
stacks, for example
deleted objects stay in an array until new data is written over them – a pack of cards
(Nikander & Virrantaus, 2007). However, they are theoretically gone, or a pile of plates,
the applet illustrates, because they can’t be accessible once the etc.
Top marker goes below their position.
The top arrows indicate –1, below the lowest cell, when the
last item on the stack has been removed. This shows that there
is nothing on the stack. When the stack is empty, the message
“Can’t pop: Stacking is blank” tries to pop an item.
5.1.1.4. Peek
The two most important stack operations are push and pop (Rodger,
2002). However, reading values at the highest point of the stacks
with no deleting can sometimes be helpful. This is done using
the peek operation. Press a Peek button several times to copy
the category value of the items at the peak of the stack into the
Numeric text box. Nonetheless, the object doesn’t disappear from
the stack; it stays there.
Look at the top item briefly. The stacked user cannot see any
additional parts by design.
CHAPTER
5
98 Data Structures and Algorithms
After reviewing what stacks are used for, examine how they are
implemented in C++ (Wiener & Pinson, 2000).
}
double peek()
{
return stackVect[top];
}
bool isEmpty()
{
return (top == -1);
}
bool isFull()
{
return (top == maxSize-1);
}
};
int main()
{
StackX theStack(10);
theStack.push(20);
theStack.push(40);
theStack.push(60);
theStack.push(80);
while( !theStack.isEmpty() )
{
double value = theStack.pop();
cout << value << “ “;
}
cout << endl;
CHAPTER
5
100 Data Structures and Algorithms
return 0;
} //end main()
The main() function builds a stack with a capacity of 10, adds
four items to this stack, and then pops each item off the stack one
at a time until the stack is empty (Baumgartner & Russo, 1995).
Here is the result:
80 60 40 20
Observe how the data are presented in reverse order. The
80 shows up first in the output since the final item was pushed
& was the first one popped.
KEYWORD
The data members in this iteration within StackX class are of
Data item is a type double. This can be altered to anything else, such as object
container of data types.
that is registered
with the server.
The set of data 5.1.3. StackX Class Member Functions
items registered
with the server Like in earlier applications, a vector is the class’s data storage
comprises the method. It is known as stackVect here.
server's data store.
The function Object() [native code] creates a brand-new stack
of whatever size is specified in its argument. The variables hold
the stack’s maximum size (the vectors). The data members of the
vector are the vector itself, with the variable at the top, which stores
an index of the item at the top of the stack (Drocco et al., 2020).
To store a data item, the push() function within the member
moves top-up so that the points area is immediately above the
previous top. Keeping in mind that before inserting the object, the
top is increased (Hogan, 2014).
The member function pop() returns the top-most value and then
decreases the top (Dubois-Pelerin & Zimmermann, 1993). However,
the value is still available in the vector even though the item is
effectively rendered unreachable and removed from the stack.
A member’s peek() method returns the value at the peak of the
stacks without changing it. Stack being blank or full, the member
method isEmpty(), and full () returns true correspondingly. If a
stack is empty, the peak variable is at -1; if the stacking is full, it
is at maxSize-1. Push() and pop() are seen in Figure 5.4 (Blunk
& Fischer, 2014).
CHAPTER
5
Algorithm Selection 101
Figure 5.4.
Operation of the
StackX class
member functions
(Source: Robert
Lafore, Creative
Commons License).
else
cout << “Can’t insert, stack is full”;
This code portion is left aside from the main() procedure in the
interests of simplicity (and, in any case, in this small application,
KEYWORD recognizing the condition of the stack is not complete due to its
recent initialization). Test the empty stack when pop() is used in
Stack class main().
represents a last-
in-first-out (LIFO) Internal tests for these issues are provided by several stack
stack of objects. classes in the member functions push() and pop() (Serebryany
et al., 2018). Throwing an exception that can be collected and
handled by the class user is a good approach for a stack class
in C++ when such issues are found.
After learning the basics of programming a stack, let’s examine
several applications that use stacks to address issues.
CHAPTER
5
Algorithm Selection 103
CHAPTER
5
104 Data Structures and Algorithms
CHAPTER
5
Algorithm Selection 105
bool isEmpty()
{ return (top == -1); }
};
class BracketChecker
{
private:
string input;
public:
BracketChecker(string in) : input(in)
{ }
void check()
{
int stackSize = input.length();
StackX theStack(stackSize);
bool isError = false;
for(int j=0; j<input.length(); j++)
{
char ch = input[j];
switch(ch)
{
case ‘{‘:
case ‘[‘:
case ‘(‘:
theStack.push(ch);
break;
case ‘}’:
case ‘]’:
CHAPTER
5
106 Data Structures and Algorithms
case ‘)’:
if( !theStack.isEmpty() )
{
char chx = theStack.pop();
if( (ch==‘}’ && chx!=‘{‘) ||
(ch==‘]’ && chx!=‘[‘) ||
(ch==‘)’ && chx!=‘(‘) )
{
isError = true;
cout << “Mismatched delimeter:. “
<< ch << “ at “ << j << endl;
}
}
else
{
isError = true;
cout << “Misplaced delimiter: “
<< ch << “ at “ << j << endl;
}
break;
default:
}
}
if( !theStack.isEmpty() )
cout << “Missing right delimiter” << endl;
else if( !isError )
cout << “OK” << endl;
CHAPTER
5
Algorithm Selection 107
}
};
int main()
{
string input;
while(true)
{
cout << “Enter string containing delimiters “
KEYWORD
<< “(no whitespace): “;
Object-oriented
cin >> input; programming
is a computer
if( input.length() == 1 )
programming model
break; that organizes
software design
BracketChecker theChecker(input); around data, or
objects, rather than
theChecker.check();
functions and logic
}
return 0;
}
StackX class in previous program is used by the check()
procedure. Be mindful of how simple it is to use this class. The
code required is all in one location. One benefit of object-oriented
programming is this (Bonfanti et al., 2020).
The check() procedure on this BracketChecker structure is
invoked when This text string is used as input to generate a
BracketChecker object created by the main() function after it has
repeatedly received a line of data from the user. The checker()
member function shows any problems it discovers; otherwise, it
determines that the delimiters’ syntax is correct (Fülöp et al., 2022).
The check() method returns the wrong character detected there
and the character number (beginning at 0 on the left) where it
identified the issue.
a{b(c]d}e
CHAPTER
5
108 Data Structures and Algorithms
Figure 5.5. Image of the queue workshop applet (Source: Mike Henry,
Creative Commons License).
Figure 5.6.
Operation of
the queue class
member functions
(Source: Dan
Schref, Creative
Commons License).
CHAPTER
5
Algorithm Selection 111
Figure 5.7.
Representing a
queue with some
items removed
(Source: Tonya
Simpson, Creative
Commons License).
CHAPTER
5
112 Data Structures and Algorithms
Figure 5.8.
Representation of
rear arrow at the
end of array (Source:
Brian Gill, Creative
Commons License).
Moving the front and the back of the queue forward presents
a problem since. Eventually, the back of the queue will be after
the conclusion of the list (the highest index). Even though Rem
deleted the vacant cells at the start of the array, it is unable to
CHAPTER
5
Algorithm Selection 113
add an item because Rear is at its limit. But can it? Figure 5.8
depicts this circumstance.
Remember
5.2.3. C++ Code for a Queue Queues are used
in operating
The Queue class in the queue.cpp program has member procedures systems to
insert(), delete(), size(), peak(), isFull(), and isEmpty() (Demaine manage tasks
et al., 2007). that need to be
executed in a
The main() method creates the five-cell queue, which then specific order.
adds four things, takes away three, and finally adds four more.
The wraparound feature is activated after the sixth insertion. Then,
everything is taken out and put on the show. The result appears
as follows:
40 50 60 70 80
The program Queue.cpp is shown in the code below (Herlihy
et al., 2003).
THE Queue.cpp PROGRAM
#include <iostream>
#include <vector>
using namespace std;
class Queue
{
private:
int maxSize;
vector<int> queVect;
int front;
int rear;
int nItems;
public:
Queue(int s) : maxSize(s), front(0), rear(-1), nItems(0)
{ queVect.resize(maxSize); }
void insert(int j) //put item at rear of queue
CHAPTER
5
114 Data Structures and Algorithms
{
if(rear == maxSize-1)
rear = -1;
queVect[++rear] = j;
nItems++;
}
int remove()
{
int temp = queVect[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
int peekFront()
{ return queVect[front]; }
bool isEmpty()
{ return (nItems==0); }
bool isFull()
{ return (nItems==maxSize); }
int size()
{ return nItems; }
};
int main()
{
Queue theQueue(5);
theQueue.insert(10);
CHAPTER
5
Algorithm Selection 115
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while( !theQueue.isEmpty() )
{
int n = theQueue.remove();
cout << n << “ “;
}
cout << endl;
return 0;
KEYWORD
}
Queue is defined
A method in which the nItems data member of the Queue as a linear data
class includes both front and back and the present total number structure that is
of items in the queue. All queue versions lack this data component open at both ends
(Brodal & Okasaki, 1996). and the operations
are performed in
First In First Out
5.2.4. Insert() Constituent Function (FIFO) order.
The queue is not regarded as complete by the insert() method.
Despite not being illustrated by main() is frequently used only once,
invoking isFull() to get a false return value (Rihani et al., 2015).
(Including the fullness check within the insert() method and raising
an exception if a try is made to insert information onto a full line
is preferable.) Typically, insertion includes incrementing the rear
while inserting at the present location of the rear. During maxSize-1,
CHAPTER
5
116 Data Structures and Algorithms
the back is seen at the highest point of the array (vector), but
it has to shift to the lowest point before the increment. Rear is
set to -1 for the rear to become 0, the bottom of the array. Then
nItems is increased.
CHAPTER
5
Algorithm Selection 119
CHAPTER
5
Algorithm Selection 121
CHAPTER
5
122 Data Structures and Algorithms
CHAPTER
5
Algorithm Selection 123
CHAPTER
5
124 Data Structures and Algorithms
break;
}
queVect[j+1] = item;
nItems++;
}
}
double remove()
{ return queVect[--nItems]; }
double peekMin()
{ return queVect[nItems-1]; }
bool isEmpty()
{ return (nItems==0); }
bool isFull()
{ return (nItems == maxSize); }
};
int main()
{
PriorityQ thePQ(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);
while( !thePQ.isEmpty() )
{
double item = thePQ.remove();
cout << item << “ “;
CHAPTER
5
Algorithm Selection 125
}
cout << endl;
return 0;
}
Five objects are randomly placed into the main() function,
removed, and then shown. Always start by deleting the smallest
component to determine the outcome (Jain et al., 2015).
10, 20, 30, 40, 50
A member’s insert() function verifies whether any items exist;
if none exist, add them at position 0. If not, the function begins
at the head of the enumeration & moves current items up until
it finds the right spot for the new product. The items are then
incremented when a new item has been added (Rönngren & Ayani,
1997). Note that before executing insert, utilize isFull() if there’s
a risk if the priority list has become full. ().
Since the front remains nItems-1 and the rear is usually 0,
rear and front data components are no longer necessary in the
Queue class.
The remove() method, which lowers nItems or removes the first
item in the list, is the definition of simplicity. The peekMin() member
method is comparable, except that nItems is not decremented.
isEmpty() and isFull() verify whether nItems is equal to 0 or
maxSize, respectively.
CHAPTER
5
126 Data Structures and Algorithms
ACTIVITY 5.1
Write a code to implement a stack using an array data structure. Then tests its
implementation with different inputs to see if it behaves as expected.
Write a code to implement a queue using a linked list data structure. Then tests its
implementation with different inputs to see if it behaves as expected.
SUMMARY
Stacks and queues are two important data structures in computer science and programming.
They are both abstract data types that allow elements to be added and removed in a
specific order. A stack is a Last In, First Out (LIFO) data structure and it operates like
a stack of plates, where the most recent item added to the stack is the first one to be
removed. Elements can only be added or removed from the top of the stack. Common
operations on a stack include push and pop. A queue is a First In, First Out (FIFO)
data structure and it operates like a line of people waiting for a ticket, where the first
person in line is the first to be served. Elements can only be added to the back of the
queue and removed from the front. Common operations on a queue include enqueue and
dequeue. Both stacks and queues have many practical applications in computer science
and programming, such as in algorithms for parsing expressions, searching and sorting
data, and processing jobs in parallel.
REVIEW QUESTIONS
1. What is a stack and what is Last In, First Out (LIFO) property?
2. What are the common operations performed on a stack?
3. What is a queue and what are the common operations performed on a queue?
4. What are the practical applications of stacks and queues in computer science
and programming?
5. What is a priority queue and how is it different from a regular queue?
REFERENCES
1. Adiri, I., & Yechiali, U., (1974). Optimal priority-purchasing and pricing decisions in
nonmonopoly and monopoly queues. Operations Research, 22(5), 1051–1066.
2. Alexander, M., MacLaren, A., O’Gorman, K., & White, C., (2012). Priority queues:
Where social justice and equity collide. Tourism Management, 33(4), 875–884.
3. Batty, M., Dodds, M., & Gotsman, A., (2013). Library abstraction for C/C++ concurrency.
ACM SIGPLAN Notices, 48(1), 235–248.
4. Baumgartner, G., & Russo, V. F., (1995). Signatures: A language extension for
improving type abstraction and subtype polymorphism in C++. Software: Practice
and Experience, 25(8), 863–889.
CHAPTER
5
128 Data Structures and Algorithms
5. Blunk, A., & Fischer, J., (2014). A highly efficient simulation core in C++. In: Proceedings
of the Symposium on Theory of Modeling & Simulation-DEVS Integrative (Vol. 1,
pp. 1–8).
6. Bonfanti, S., Gargantini, A., & Mashkoor, A., (2020). Design and validation of a C++
code generator from abstract state machines specifications. Journal of Software:
Evolution and Process, 32(2), e2205.
7. Boyapati, C., & Darga, P., (2007). Efficient software model checking of data structure
properties. In: Dagstuhl Seminar Proceedings; Schloss Dagstuhl-Leibniz-Zentrum für
Informatik, (Vol. 1, pp. 2–9).
8. Brandenburg, F. J., (1988). On the intersection of stacks and queues. Theoretical
Computer Science, 58(1–3), 69–80.
9. Breen, E. J., & Monro, D. H., (1994). An evaluation of priority queues for mathematical
morphology. Mathematical Morphology and its Applications to Image Processing, 1,
249–256.
10. Brodal, G. S., & Okasaki, C., (1996). Optimal purely functional priority queues.
Journal of Functional Programming, 6(6), 839–857.
11. Brodal, G. S., (2013). A Survey on Priority Queues (Vol. 1, pp. 150–163). Space-
Efficient Data Structures, Streams, and Algorithms: Papers in Honor of J. Ian Munro
on the Occasion of His 66th Birthday.
12. Campoli, L., Oblapenko, G. P., & Kustova, E. V., (2019). KAPPA: Kinetic approach to
physical processes in atmospheres library in C++. Computer Physics Communications,
236, 244–267.
13. Carriero, N., Gelernter, D., & Leichter, J., (1986). Distributed data structures in
Linda. In: Proceedings of the 13th ACM SIGACT-SIGPLAN Symposium on Principles
of Programming Languages (Vol. 1, pp. 236–242).
14. Case, A., Marziale, L., & Richard, III. G. G., (2010). Dynamic recreation of kernel
data structures for live forensics. Digital Investigation, 7(1), S32–S40.
15. Celinski, T. M., Dijkstra, B. A., Ribeiro, L. G., Souza, M. A., & Celinski, V. G., (2017).
Development of learning objects and their application in teaching and learning data
structures and their algorithms. Iberoamerican Journal of Applied Computing, 7(2),
6–8.
16. Choi, B. D., & Chang, Y., (1999). Single server retrial queues with priority calls.
Mathematical and Computer Modeling, 30(3, 4), 7–32.
17. Demaine, E. D., Iacono, J., & Langerman, S., (2007). Retroactive data structures.
ACM Transactions on Algorithms (TALG), 3(2), 13–15.
18. Di Benedetto, M., Corsini, M., & Scopigno, R., (2011). SpiderGL: A graphics library
for 3D web applications. International Archives of the Photogrammetry, Remote
Sensing and Spatial Information Sciences, 38(5W16) (Vol. 1, 467–474).
19. Drocco, M., Castellana, V. G., & Minutoli, M., (2020). Practical distributed programming
in C++. In: Proceedings of the 29th International Symposium on High-Performance
CHAPTER
5
Algorithm Selection 129
CHAPTER
5
130 Data Structures and Algorithms
37. Langsam, Y., Augenstein, M. J., & Tenenbaum, A. M., (1996). Data Structures Using
C and C++ (Vol. 2, No. 1, pp. 2–6). Prentice Hall Press.
38. Maertens, T., Walraevens, J., & Bruneel, H., (2006). On priority queues with priority
jumps. Performance Evaluation, 63(12), 1235–1252.
39. Martin, B., (1990). The separation of interface and implementation in C++. Hewlett-
Packard Laboratories, 3(1), 8–10.
40. Mendelson, R., Tarjan, R. E., Thorup, M., & Zwick, U., (2006). Melding priority
queues. ACM Transactions on Algorithms (TALG), 2(4), 535–556.
41. Moir, M., Nussbaum, D., Shalev, O., & Shavit, N., (2005). Using elimination to
implement scalable and lock-free FIFO queues. In: Proceedings of the Seventeenth
Annual ACM Symposium on Parallelism in Algorithms and Architectures (Vol. 1, pp.
253–262).
42. Muzakkari, B. A., Mohamed, M. A., Kadir, M. F., & Mamat, M., (2020). Queue
and priority-aware adaptive duty cycle scheme for energy efficient wireless sensor
networks. IEEE Access, 8(1), 17231–17242.
43. Nageshwara, R. V., & Kumar, V., (1988). Concurrent access of priority queues. IEEE
Transactions on Computers, 37(12), 1657–1665.
44. Nikander, J., & Virrantaus, K., (2007). Learning environment of spatial data algorithms.
In: International Cartographic Conference ICC, Moscow, ICC (Vol. 1, pp. 1–5).
45. Park, B., & Ahmed, D. T., (2017). Abstracting learning methods for stack and queue
data structures in video games. In: 2017 International Conference on Computational
Science and Computational Intelligence (CSCI) (Vol. 1, pp. 1051–1054). IEEE.
46. Payer, H., Roeck, H., Kirsch, C. M., & Sokolova, A., (2011). Scalability versus
semantics of concurrent FIFO queues. In: Proceedings of the 30th Annual ACM
SIGACT-SIGOPS Symposium on Principles of Distributed Computing (Vol. 1, pp.
331, 332).
47. Paznikov, A. A., & Anenkov, A. D., (2019). Implementation and analysis of distributed
relaxed concurrent queues in remote memory access model. Procedia Computer
Science, 150(1), 654–662.
48. Pierson, W. C., & Rodger, S. H., (1998). Web-based animation of data structures
using JAWAA. ACM SIGCSE Bulletin, 30(1), 267–271.
49. Prakash, S., Lee, Y. H., & Johnson, T., (1994). A nonblocking algorithm for shared
queues using compare-and-swap. IEEE Transactions on Computers, 43(5), 548–559.
50. Rihani, H., Sanders, P., & Dementiev, R., (2015). Multiqueues: Simple relaxed
concurrent priority queues. In: Proceedings of the 27th ACM Symposium on Parallelism
in Algorithms and Architectures (Vol. 1, pp. 80–82).
51. Ritha, W., & Robert, L., (2010). Fuzzy queues with priority discipline. Applied
Mathematical Sciences, 4(12), 575–582.
52. Rodger, S. H., (2002). Using hands-on visualizations to teach computer science
from beginning courses to advanced courses. In: Second Program Visualization
CHAPTER
5
Algorithm Selection 131
CHAPTER
5
CHAPTER 6
TREES
UNIT INTRODUCTION
A tree is made up of nodes and edges. Figure 6.1 depicts a tree (Snyder, 1982). The
nodes and edges of a tree are shown in this illustration as circles and lines, respectively.
A great deal of theoretical knowledge about trees has been examined in great detail as
abstract mathematical entities (Culler et al., 2004).
Nodes are the standard items in any type of data structure, including persons, cars,
airplane bookings, etc. Nodes are frequently used to represent data items in computer
applications. Objects express entities from the real world in object-oriented programming
(OOP) languages like C++ (Raymond, 1989). Such data elements had previously been
kept in lists and arrays.
The lines (edges) linking the nodes display the connections between them. Generally
speaking, the lines represent comfort: A line connecting two nodes enables the software
to traverse the nodes quickly and easily (Plaisant et al., 2002). Only a path that crosses
through the lines allows one to move from node to node. Typically, movement occurs
from the bottom downward along the margins. When a program is written in C++, pointers
express edges.
A tree typically begins with a single node at the very top, with lines joining to additional
nodes on another row, and so on (Dung et al., 2007). As a result, trees have tiny crowns
and massive bases. Programs usually start operations from the little end of a tree, even
though this could seem backward compared to physical trees. Moving the tree upward,
like reading literature, may be easier to think about.
134 Data Structures and Algorithms
Figure 6.1. Illustration of tree with more than two children (Source:
Narasimha Karumanchi, Creative Commons License).
Learning Objectives
At the chapter’s end, students will get the ability to comprehend
the following:
• The basic concept of trees and important tree terminologies
• Understanding basic binary tree operations
• The process of finding, inserting and deleting a node in
the tree
• The concept of traversing the tree
• The efficiency of binary trees
Key Terms
1. Binary Search Trees
2. Deleting
3. Decision Trees
4. Edges
5. Graph Algorithms
CHAPTER
6
Trees 135
6. Insertion
7. In-order Traversal
8. Leaves
9. Nodes
10. Pre-order Traversal
11. Searching
12. Trees
CHAPTER
6
136 Data Structures and Algorithms
6.1.1. Path
Think about moving between nodes by using the edges that link
them. A path is a name given to the generated collection of parts
(Hayes, 1976).
6.1.2. Root
Root of a tree is the highest node. In a tree, there’s only one
root. Only if one path connects each node to the root across the
edges and nodes is it considered a tree. A non-tree is depicted
in Figure 6.3 (Bolognesi & Brinksma, 1987). It breaks this rule.
CHAPTER
6
Trees 137
Figure 6.3.
Representation of
a non-tree (Source:
Vamshi Krishna,
Creative Commons
License).
6.1.3. Parent
Every node (apart from the root) has one edge going up to another
node (Joshi, 1987). The parent node is the node that is located
above it.
6.1.4. Child
A line or multiple lines connecting a node to another node can run
downward. Children nodes are directly beneath a specific node
(Parhami, 2006).
6.1.5. Leaf
Leaf nodes, or just leaves, are nodes with no children. A tree can
only have one root, although it can have many leaves.
6.1.6. Sub-tree
The root of a subtree can be any node that has children, children
of children, and so forth (Singh, 2009). Thinking of a node as a
family, its sub-tree will contain all its offspring.
6.1.7. Visiting
The program’s control reaches a node as visited to perform some
operation on it, such as displaying it or evaluating the value of a
single data member (Grama et al., 2003). A node doesn’t seem to
have been visited if a user passes over it while traveling between
nodes.
CHAPTER
6
138 Data Structures and Algorithms
6.1.8. Traversing
To move through a tree is to go through each node in a particular
order (Schuld et al., 2015). For instance, going over each node
in the order of increasing key value. In the following hour, it is
observed that there are additional ways to climb a tree.
6.1.9. Levels
A node’s level describes how many generations away it is from
the root. Assuming the root has level zero, its offspring will be
level one, their grandchildren level 2, and so on.
6.1.10. Keys
A key value often refers to one data item within an object
(Shneiderman, 1992). This value is utilized while looking for the
object or working with it in various ways. Typically, the most
significant value of an item of information is depicted in the circle
KEYWORD symbolizing the node (this will be depicted in numerous figures
below).
Binary tree is a
rooted tree that is
also an ordered 6.1.11. Binary Trees
tree in which every
node has at most A binary tree is one in which each node can have a maximum
two children. of two offspring (Luellen et al., 2005). Due to their simplicity,
popularity, and widespread application, binary trees are the main
topic of this hour’s discussion.
Figure 6.2 shows how each node’s left and right children in a
binary structure correlate to their locations when a picture of the
tree is displayed. In a binary tree, a node doesn’t have a maximum
number of children; it could have just one left child, one right child,
or none (making it a leaf) (Sudkamp, 2007).
The formal name for this particular binary tree is the binary
search tree. The left child of this tree needs to possess a key
smaller than its parent, and the right child must have a key equal
to or larger than its parent. The binary search tree can be seen
in Figure 6.4.
CHAPTER
6
Trees 139
Figure 6.5.
Illustration of
applet from binary
tree workshop
(Source: Narasimha
Karumanchi, Creative
Commons License).
iii. To create a new tree, enter the number and click Fill
twice. Theories can be tested by making trees with various
amounts of nodes.
because the applet’s tree can only grow up to five levels deep; a
real tree wouldn’t be a problem.
Unbalanced trees may not be a major issue for larger trees if
a tree is constructed from information elements whose key values
appear in random order because it is unlikely that a long run of
consecutive numbers will occur. However, significant values may
be presented in a certain sequence, such as when a data entry
worker stacks the personnel files in ascending order of employee
numbers prior to entering the data. This is an example of how
important values may be presented. When this occurs, trees’
productive capacity place, the productive capacity of trees may
be considerably reduced.
pRightChild(NULL)
{ }
//
void displayNode() //display ourself: {75, 7.5}
{cout << ‘{‘ << iData << ,” “ << dData << “} “;
}}; //class Node ends
KEYWORD
Some programmers may include a pointer to the parent of
a node. It complicates some processes while simplifying others, Member functions
so it is not included. The code of this object’s member function are operators and
displayNode(), which shows the node’s data, is irrelevant to this functions that
are declared as
topic even though it exists (Sleator & Tarjan, 1983).
members of a
The class Node can be created using additional methods. class.
Instead of just including the information items in the node, one
could also provide a pointer to an object that uniquely identifies
the data item (Davoodi et al., 2017):
class Node
{Person* p1; //pointer for Person object
Node* pLeftChild; //pointer for left child
Node* pRightChild; //pointer for right child};
This makes it more conceptually clear the node & data items it
carries are not the same, but it leads to slightly more sophisticated
code, so stick with the first way.
private:
Node* pRoot; //first node of tree
public:
//-------------------------------------------------------------
Tree() : pRoot(NULL) //constructor
{ }
//-------------------------------------------------------------
Node* find(int key) //find node with given key
{ /*body not shown*/ }
//-------------------------------------------------------------
void insert(int id, double dd) //insert new node
{ /*body not shown*/ }
//-------------------------------------------------------------
void traverse(int traverseType)
{ /*body not shown*/ }
//-------------------------------------------------------------
void displayTree()
{ /*body not shown*/ }
//-------------------------------------------------------------
}; //end class Tree
theTree.insert(25, 1.7);
theTree.insert(75, 1.9);
Node* found = theTree.find(25); //find node with key 25
if(found != NULL)
cout << “Found the node with key 25” << endl;
else
cout << “Could not find node with key 25” << endl;
return 0;
} // end main()
The main() procedure in Listing 6.1 offers a basic interface to
insert data, search for items, and carry out other tasks using the
keyboard (Miao et al., 2006).
Specific tree operations are examined, such as identifying and
adding a node. The issue of removing a node is also discussed.
CHAPTER
6
146 Data Structures and Algorithms
CHAPTER
6
Trees 147
CHAPTER
6
148 Data Structures and Algorithms
ii. Write the name corresponding to the node’s value for the
key for a node that needs to be placed. A brand-new node
having a value of 45 might be added. Put this in the text
box.
iii. Continue to press the Ins button. The newly created node
will be attached when the red arrow drops to the insertion
site (Shivaratri et al., 1992).
The program searches for the appropriate location to insert
a node as its initial phase. Figure 6.8a depicts how it appears.
Figure 6.8.
Representation
of adding a node
(Source: Vamshi
Krishna, Creative
Commons License).
Since 45 is greater than 40, but below 60, one can reach
node 50. The pLeftChild data component is Empty since 45 is
now smaller than 50, heading left, but 50 lacks a child to its left.
The insertion function of the node understands where to attach
the new node when it comes across this NULL (Dakin, 1965). As
seen in Figure 6.8b, the Workshop applet accomplishes this by
constructing an additional node containing the value 45 and linking
it to the child node to the left of 50.
is that it runs into a NULL node when looking for a node, and
the node seeking doesn’t exist (Yen, 1972). If necessary, create
a node while attempting to insert one before returning.
The data item supplied as an input id is the value that needs
to be looked for. The while loop treats a node with an identical
KEYWORD key value with the assumption that it is simply over the key value
and uses true of the state because Locating a node whose id
Return statement value matches another one. Discuss duplicate nodes once more.
ends the execution
of a function, and In genuine trees, a site to add a node is continually found
returns control to unless memory is exhausted; Using a return statement, the while
the calling function. loop is completed after adding the new node.
Code insert() function’s is shown below (Nardelli et al., 2003):
void insert(int id, double dd) //insert new node
{
Node* pNewNode = new Node; //make new node
pNewNode->iData = id; //insert data
pNewNode->dData = dd;
if(pRoot==NULL) //no node in root
pRoot = pNewNode;
else //root occupied
{
Node* pCurrent = pRoot; //start at root
Node* pParent;
while(true) //(exits internally)
{
pParent = pCurrent;
if(id < pCurrent->iData) //go left?
{
pCurrent = pCurrent->pLeftChild;
if(pCurrent == NULL) //if end of the line,
{ //insert on left
CHAPTER
6
Trees 151
pParent->pLeftChild = pNewNode;
return;
}
} //end if go left
else //or go right?
{
pCurrent = pCurrent->pRightChild;
if(pCurrent == NULL) //if end of the line
{ //insert on right
pParent->pRightChild = pNewNode;
return;
}
} //end else go right
} //end while
} //end else not root
} //end insert()
To keep track of the most recent non-NULL node across, create
a new variable called pParent . (50 in the figure). This is required
because upon learning that the previous value of pCurrent lacked
an appropriate child and was set to NULL (Ishida et al., 1995).
Without saving pParent, the Current location track would be lost.
The previous non-NULL node determined, pParent, should
have the proper child pointer changed to the new node to insert
the new node. If the left grandchild of pParent cannot be found,
attach the newly generated nodes towards the left child; if the
right child is not found, join the new node child. The left offspring
of 50 and 45 are shown attached in Figure 6.8.
observe how certain nodes are removed in various ways. Any node
with no offspring can be deleted quickly and simply by removing
it. A node is eliminated by linking its child with its parent if it has
just one child (Lewis & Yannakakis, 1980). However, deleting a
node with two children is challenging.
Some programs label a node as “deleted” to avoid the
KEYWORD complicated process. Despite not being erased, algorithms may
choose to leave it.
Traversing is a
process in which
each element of 6.7. TRAVERSING TREE
a data structure
is accessed. A tree can be traversed by going through every node in a specific
Accessing an order (Soule, 1977). Finding, adding, and removing nodes are more
element of data frequent operations than this one. The fact that traversing is not
structure means extremely quick is one explanation for this. Theoretically, however,
visiting every exploring a tree may have unexpected practical implications.
element at least
once A tree can be crossed in three easy ways. , postorder, inorder
and Preorder are their names. Let’s first look at the order in which
binary search trees use most frequently.
CHAPTER
6
154 Data Structures and Algorithms
CHAPTER
6
156 Data Structures and Algorithms
The routine visits each node travels its left subtree, and then
visits its right subtree for each node. This may not be immediately
apparent. For example, this occurs in phases 2, 7, and 8 for node
30 (Karapinar et al., 2012).
CHAPTER
6
Trees 157
CHAPTER
6
158 Data Structures and Algorithms
CHAPTER
6
Trees 159
CHAPTER
6
160 Data Structures and Algorithms
CHAPTER
6
Trees 161
SUMMARY
In computer science and programming, a tree is a widely used data structure that is used
to represent hierarchical relationships between elements. Trees consist of nodes, which
are connected by edges, and have a root node that serves as the starting point for the
tree. Each node in a tree can have one or more child nodes, which are connected to
it by edges. A node that has no children is called a leaf node, while a node with one
or more children is called an internal node. Trees are used in a variety of applications
such as file systems, compiler construction and graph algorithms. Some common types
of trees used in programming include binary trees, binary search trees, balanced trees,
heap and trie.
REVIEW QUESTIONS
1. What is a tree and how is it different from a linear data structure?
2. What is the root node of a tree and what is its significance?
3. What is a binary tree and what are its properties?
4. What is a leaf node in a tree and how is it different from an internal node?
5. How are trees used in real-world applications, such as file systems and databases?
CHAPTER
6
162 Data Structures and Algorithms
REFERENCES
1. Alakeel, A. M., (2010). A guide to dynamic load balancing in distributed computer
systems. International Journal of Computer Science and Information Security, 10(6),
153–160.
2. Baeza-Yates, R., Cunto, W., Manber, U., & Wu, S., (1994). Proximity matching using
fixed-queries trees. In: CPM (Vol. 94, pp. 198–212).
3. Barringer, R., & Akenine-Möller, T., (2013). Dynamic stackless binary tree traversal.
Journal of Computer Graphics Techniques, 2(1), 38–49.
4. Benoit, D., Demaine, E. D., Munro, J. I., Raman, R., Raman, V., & Rao, S. S.,
(2005). Representing trees of higher degree. Algorithmica, 43(1), 275–292.
5. Berman, P., Karpinski, M., & Nekrich, Y., (2007). Optimal trade-off for Merkle tree
traversal. Theoretical Computer Science, 372(1), 26–36.
6. Bholowalia, P., & Kumar, A., (2014). EBK-means: A clustering technique based on
elbow method and k-means in WSN. International Journal of Computer Applications,
105(9), 2–6.
7. Bolognesi, T., & Brinksma, E., (1987). Introduction to the ISO specification language
LOTOS. Computer Networks and ISDN Systems, 14(1), 25–59.
8. Brinck, K., (1985). The expected performance of traversal algorithms in binary trees.
The Computer Journal, 28(4), 426–432.
9. Cano, A., Gémez-Olmedo, M., & Moral, S., (2011). Approximate inference in Bayesian
networks using binary probability trees. International Journal of Approximate Reasoning,
52(1), 49–62.
CHAPTER
6
Trees 163
10. Chawra, V. K., & Gupta, G. P., (2020). Load balanced node clustering scheme using
improved memetic algorithm based meta-heuristic technique for wireless sensor
network. Procedia Computer Science, 167, 468–476.
11. Chen, C. C. Y., & Das, S. K., (1992). Breadth-first traversal of trees and integer
sorting in parallel. Information Processing Letters, 41(1), 39–49.
12. Chen, M. S., Park, J. S., & Yu, P. S., (1996). Data mining for path traversal patterns
in a web environment. In: Proceedings of 16th International Conference on Distributed
Computing Systems (Vol. 1, pp. 385–392). IEEE.
13. Culler, D., Estrin, D., & Srivastava, M., (2004). Guest editors’ introduction: Overview
of sensor networks. Computer, 37(08), 41–49.
14. Dakin, R. J., (1965). A tree-search algorithm for mixed integer programming problems.
The Computer Journal, 8(3), 250–255.
15. Davoodi, P., Raman, R., & Satti, S. R., (2017). On succinct representations of binary
trees. Mathematics in Computer Science, 11(1), 177–189.
16. Dung, P. M., Mancarella, P., & Toni, F., (2007). Computing ideal sceptical argumentation.
Artificial Intelligence, 171(10–15), 642–674.
17. Fei, B., & Liu, J., (2006). Binary tree of SVM: A new fast multiclass training and
classification algorithm. IEEE Transactions on Neural Networks, 17(3), 696–704.
18. Fortune, S., Hopcroft, J., & Wyllie, J., (1980). The directed subgraph homeomorphism
problem. Theoretical Computer Science, 10(2), 111–121.
19. Friedman, S. J., & Supowit, K. J., (1987). Finding the optimal variable ordering for
binary decision diagrams. In: Proceedings of the 24th ACM/IEEE Design Automation
Conference (Vol. 1, pp. 348–356).
20. Frisken, S. F., & Perry, R. N., (2002). Simple and efficient traversal methods for
quadtrees and octrees. Journal of Graphics Tools, 7(3), 1–11.
21. George, A., & Liu, J. W., (1979). An implementation of a pseudoperipheral node
finder. ACM Transactions on Mathematical Software (TOMS), 5(3), 284–295.
22. Grama, A., Kumar, V., Gupta, A., & Karypis, G., (2003). Introduction to Parallel
Computing (Vol. 1, pp. 2–7). Pearson Education.
23. Guha, S., & Khuller, S., (1998). Approximation algorithms for connected dominating
sets. Algorithmica, 20(1), 374–387.
24. Gupta, S. K., Kuila, P., & Jana, P. K., (2016). Genetic algorithm approach for
k-coverage and m-connected node placement in target based wireless sensor
networks. Computers & Electrical Engineering, 56(1), 544–556.
25. Gurwitz, C., (1995). Achieving a uniform interface for binary tree implementations.
In: Proceedings of the Twenty-Sixth SIGCSE Technical Symposium on Computer
Science Education (Vol. 1, pp. 66–70).
26. Halasz, F., & Moran, T. P., (1982). Analogy considered harmful. In: Proceedings of
the 1982 Conference on Human Factors in Computing Systems (Vol. 1, pp. 383–386).
CHAPTER
6
164 Data Structures and Algorithms
27. Hamouda, S., Edwards, S. H., Elmongui, H. G., Ernst, J. V., & Shaffer, C. A., (2020).
BTRecurTutor: A tutorial for practicing recursion in binary trees. Computer Science
Education, 30(2), 216–248.
28. Hapala, M., & Havran, V., (2011). Kd‐tree traversal algorithms for ray tracing. In:
Computer Graphics Forum (Vol. 30, No. 1, pp. 199–213). Oxford, UK: Blackwell
Publishing Ltd.
29. Hassan, M., (1986). A fault-tolerant modular architecture for binary trees. IEEE
Transactions on Computers, 100(4), 356–361.
30. Hayes, J. P., (1976). A graph model for fault-tolerant computing systems. IEEE
Transactions on Computers, 25(09), 875–884.
31. Ishida, K., Kakuda, Y., & Kikuno, T., (1995). A routing protocol for finding two node-
disjoint paths in computer networks. In: Proceedings of International Conference on
Network Protocols (Vol. 1, pp. 340–347). IEEE.
32. Jakobsson, M., Leighton, T., Micali, S., & Szydlo, M., (2003). Fractal Merkle tree
representation and traversal. In: CT-RSA (Vol. 2612, pp. 314–326).
33. Johnsson, S. L., (1987). Communication efficient basic linear algebra computations
on hypercube architectures. Journal of Parallel and Distributed Computing, 4(2),
133–172.
34. Joshi, A. K., (1987). An introduction to tree adjoining grammars. Mathematics of
Language, 1, 87–115.
35. Karapinar, Z., Senturk, A., Zavrak, S., Kara, R., & Erdogmus, P., (2012). Binary
apple tree: A game approach to tree traversal algorithms. In: 2012 International
Conference on Information Technology Based Higher Education and Training (ITHET)
(Vol. 1, pp. 1–3). IEEE.
36. Klein, P., & Ravi, R. J. J. A., (1995). A nearly best-possible approximation algorithm
for node-weighted Steiner trees. Journal of Algorithms, 19(1), 104–115.
37. Lewis, J. M., & Yannakakis, M., (1980). The node-deletion problem for hereditary
properties is NP-complete. Journal of Computer and System Sciences, 20(2), 219–230.
38. Li, T., Wang, G., & Chen, J., (2010). A modified binary tree codification of drainage
networks to support complex hydrological models. Computers & Geosciences, 36(11),
1427–1435.
39. Lucas, J. M., (1987). The rotation graph of binary trees is Hamiltonian. Journal of
Algorithms, 8(4), 503–535.
40. Luellen, J. K., Shadish, W. R., & Clark, M. H., (2005). Propensity scores: An
introduction and experimental test. Evaluation Review, 29(6), 530–558.
41. McLennan, G., Ferguson, J. S., Thomas, K., Delsing, A. S., Cook-Granroth, J., &
Hoffman, E. A., (2007). The use of MDCT-based computer-aided pathway finding for
mediastinal and perihilar lymph node biopsy: A randomized controlled prospective
trial. Respiration, 74(4), 423–431.
42. Miao, L., Qi, H., Ramanath, R., & Snyder, W. E., (2006). Binary tree-based generic
CHAPTER
6
Trees 165
59. Singh, A., (2009). An artificial bee colony algorithm for the leaf-constrained minimum
spanning tree problem. Applied Soft Computing, 9(2), 625–631.
60. Sleator, D. D., & Tarjan, R. E., (1983). Self-adjusting binary trees. In: Proceedings of
the Fifteenth Annual ACM Symposium on Theory of Computing (Vol. 1, pp. 235–245).
61. Smalheiser, N. R., Torvik, V. I., & Zhou, W., (2009). Arrowsmith two-node search
interface: A tutorial on finding meaningful links between two disparate sets of articles
in MEDLINE. Computer Methods and Programs in Biomedicine, 94(2), 190–197.
62. Snyder, L., (1982). Introduction to the configurable, highly parallel computer. Computer,
15(01), 47–56.
63. Soule, S., (1977). A note on the nonrecursive traversal of binary trees. The Computer
Journal, 20(4), 350–352.
64. Sudkamp, T. A., (2007). Languages and Machines: An Introduction to the Theory of
Computer Science, 3/E (Vol. 1, pp. 3–7). Pearson Education India.
65. Tang, J., Hao, B., & Sen, A., (2006). Relay node placement in large scale wireless
sensor networks. Computer Communications, 29(4), 490–501.
66. Tueno, A., & Janneck, J., (2021). A method for securely comparing integers using
binary trees. Cryptology ePrint Archive, 2(1), 3–9.
67. Wald, I., Ize, T., Kensler, A., Knoll, A., & Parker, S. G., (2006). Ray tracing animated
scenes using coherent grid traversal. In: ACM SIGGRAPH 2006 Papers (Vol. 1, pp.
485–493).
68. Weiser, M., (1999). The computer for the 21st century. ACM SIGMOBILE Mobile
Computing and Communications Review, 3(3), 3–11.
69. Wilkov, R., (1972). Analysis and design of reliable computer networks. IEEE
Transactions on Communications, 20(3), 660–678.
70. Wilson, L., Vaughn, N., & Krasny, R., (2021). A GPU-accelerated fast multipole
method based on barycentric Lagrange interpolation and dual tree traversal. Computer
Physics Communications, 265(1), 108017.
71. Wu, H., Zeng, Y., Feng, J., & Gu, Y., (2012). Binary tree slotted ALOHA for passive
RFID tag anticollision. IEEE Transactions on Parallel and Distributed Systems, 24(1),
19–31.
72. Yen, J. Y., (1972). Finding the lengths of all shortest paths in n-node nonnegative-
distance complete networks using ½ N3 additions and N3 comparisons. Journal of
the ACM (JACM), 19(3), 423, 424.
73. Yi, J., & Tan, G., (2016). Binary-tree encryption strategy for optical multiple-image
encryption. Applied Optics, 55(20), 5280–5291.
74. Zhou, J., Zhang, Y., & Cheng, J., (2014). Preference-based mining of top-K influential
nodes in social networks. Future Generation Computer Systems, 31(1), 40–47.
CHAPTER
6
CHAPTER 7
SEARCH ALGORITHMS IN
DATA STRUCTURES
UNIT INTRODUCTION
Relational databases have evolved as a powerful technical tool for data transfer and
manipulation over the last few years. Rapid improvements in data technology and science
have had a substantial influence on how data is represented in current years (Abiteboul
et al., 1995; 1997; 1999). In database technology, a new issue has emerged that restricts
the effective representations of the data using classical tables. Data are shown as graphs
and trees in several database systems. Certain applications, on either side, need a
specific database system to manage time and uncertainty. To overcome the issues faced
in database systems, significant research is being done (Adalı & Pigaty, 2003; Shaw et
al., 2016).
At the moment, the researchers are looking at the possibility of developing models for
“next-generation database systems,” or databases that may reflect new data kinds and
provide unique manipulation capabilities while still enabling regular search operations. Next-
generation databases effectively handle structured documents, Web, network directories,
and XML by modeling data in the format of trees and graphs (Altınel & Franklin, 2000;
Almohamad & Duffuaa, 1993) (Figure 7.1).
In addition, modern database system utilizes tree or graph models for the representation
of data in a variety of applications, including picture databases, molecular databases,
and commercial databases. Due to the enormous importance of tree and graph database
systems, several methods for tree and graph querying are now being developed (Altman,
1968; Amer-Yahia et al., 2001, 2002). Certain applications, including databases for
commercial package delivery, meteorological databases, and financial databases, require
168 Data Structures and Algorithms
CHAPTER
7
Search Algorithms in Data Structures 169
(i.e., n), an array of items (i.e., A), and the key-value pairs to be used in locating the
objects (i.e., x). This explains the many kinds of search algorithms that are available
(Boncz et al., 1998, 1999).
LEARNING OBJECTIVES
At the chapter’s end, students will get the ability to comprehend the following:
• The basic concept of search algorithms in data structures
• Understanding the concept of ordered and unordered linear search
• Learn about binary search in data structures
• The process of searching in trees and graphs
KEY TERMS
1. Artificial Intelligence
2. Binary Search
3. Linear Search
4. Graph Search
5. Machine Learning
6. Search Algorithms
7. Tree Search
CHAPTER
7
170 Data Structures and Algorithms
CHAPTER
7
Search Algorithms in Data Structures 171
CHAPTER
7
174 Data Structures and Algorithms
CHAPTER
7
Search Algorithms in Data Structures 175
iv. If no, compare the value of x to the final element of the 1st
half to check if the value of x is larger than that element.
v. If yes, x must be in the 2nd part of the equation. The 2nd
half of the array should now be handled as a new array
to run the Binary Search on it.
vi. If no, then x should be placed in the 1st half. The 1st half
of the array should now be handled as a new array, and
Binary Search should be run on it.
vii. If x isn’t discovered, display a notice that says “x not
found.”
Consider the following depicted array once more:
KEYWORD
i I II III IV V VI VII VIII
B 8 9 17 23 26 34 35 49 Biological
database is a
If we require to find x = 26. We shall compare x to every large, organized
element (23, 34) two times (such as one for “=“ and another for body of persistent
“>“), followed by a single comparison to element (26) for “=.” data, usually
Ultimately, at the fifth place, we discover x. There are a total number associated with
of comparisons: 2 x 2 + 1 = 5. T (n) = 2log2n in the worst-case computerized
situation (Cook & Holder, 1993; Cole, et al., 1999). software designed
to update, query,
and retrieve
7.5. SEARCHING IN GRAPHS components of the
data stored within
Due to their broad, powerful, and flexible form, graphs are commonly the system.
employed to describe data in a variety of applications. A graph
is made up of a collection of vertices and edges that connect
the pairs of vertices. Generally, edges are utilized to depict the
relationships between various data variables, and vertices are
utilized to depict the data itself (such as anything which needs a
description). Based upon the level of abstraction used to describe
the information (data), a graph illustrates the data either semantically
or syntactically (Figure 7.4).
Let’s look at a biological database system (e.g., proteins).
Commonly, proteins are described utilizing labeled graphs, in which
the edges reflect the links between distinct atoms and the vertices
show specific atoms. Proteins are often classified depending upon
their shared structural characteristics. Estimating the functioning of
a novel protein fragment is one of the uses of these classifications
(such as synthesized or discovered). The functioning of the novel
fragments may be deduced by looking for structural similarities
between the novel fragments and known proteins. Furthermore,
CHAPTER
7
176 Data Structures and Algorithms
CHAPTER
7
Search Algorithms in Data Structures 179
Figure 7.6.
Representation of
(a)graph (GRep)
with 6 vertices and
eight edges; (b,
c, and d) possible
cliques of GRep
is D1={VR1, VR2,
VR5}, D2={VR2,
VR3, VR4,VR5},
and D3={VR6, VR5,
VR4} (Source: Soft
Computing, Creative
Commons License).
way for estimating the distance between related coins (Luks, 1982;
Xu et al., 1999). Figure 7.7 depicts a partial features tree for a
generic coin. The arrangement of a tree-like structure is mostly
based upon a detailed examination performed by an information
technologist in collaboration with an archeologist specialist. The
more selective traits are associated with the higher-level nodes of
a tree, according to a standard rule. This criterion, although, can
be overridden if it will result in incompetence in dealing with the
resulting tree structure (Filotti & Mayer, 1980; Babai et al., 1982).
CHAPTER
7
Search Algorithms in Data Structures 185
CHAPTER
7
Search Algorithms in Data Structures 187
SUMMARY
In data structures, search algorithms are used to locate an element within the structure.
The choice of search algorithms depends on the type of data structure being and the
properties of the data. The linear search algorithms are used to search elements in arrays
and lists. It is easy and simple to implement but can be slow for large targets. The binary
search algorithm is used for searching elements in sorted arrays or lists. It is faster than
linear search but requires that the data structure be sorted. The hashing algorithm is used
for searching elements in hash tables. Hashing can be very fast for large structures, but
can also suffer from collisions that require additional processing. The search algorithms
can be combined with other data structures, such as trees and hash tables, to provide
efficient search capabilities. The choice of algorithm and data structure depends on the
properties of the data being searched such as size, sparsity and access patterns.
REVIEW QUESTIONS
1. What are search algorithms and why are they important in computer science?
2. What are the different types of search algorithms and how do they differ from
one another?
3. What is linear search and what types of data structures it is used for?
4. What factors should be considered when choosing a search algorithm for a
particular data structure?
5. What are some real-world applications of search algorithms in computer science
and other fields?
CHAPTER
7
188 Data Structures and Algorithms
REFERENCES
1. Abiteboul, S., & Vianu, V., (1999). Regular path queries with constraints. Journal of
Computer and System Sciences, 58(3), 428–452.
2. Abiteboul, S., Hull, R., & Vianu, V., (1995). Foundations of Databases: The Logical
Level. Addison-Wesley Longman Publishing Co., Inc.
3. Abiteboul, S., Quass, D., McHugh, J., Widom, J., & Wiener, J. L., (1997). The Lorel
query language for semistructured data. International Journal on Digital Libraries,
1(1), 68–88.
4. Adalı, S., & Pigaty, L., (2003). The DARPA advanced logistics project. Annals of
Mathematics and Artificial Intelligence, 37(4), 409–452.
5. Aho, A. V., & Ganapathi, M., (1985). Efficient tree pattern matching (extended
abstract): An aid to code generation. In: Proceedings of the 12th ACM SIGACT-
SIGPLAN Symposium on Principles of Programming Languages (pp. 334–340). ACM.
6. Aho, A. V., Ganapathi, M., & Tjiang, S. W., (1989). Code generation using tree
CHAPTER
7
Search Algorithms in Data Structures 189
22. Barrow, H. G., & Burstall, R. M., (1976). Subgraph isomorphism, matching relational
structures and maximal cliques. Information Processing Letters, 4(4), 83–84.
23. Boag, S., Chamberlin, D., Fernández, M. F., Florescu, D., Robie, J., Siméon, J., &
Stefanescu, M., (2002). XQuery 1.0: An XML Query Language, 7, 4-18.
24. Bomze, I. M., Budinich, M., Pardalos, P. M., & Pelillo, M., (1999). The maximum
clique problem. In: Handbook of Combinatorial Optimization (pp. 1–74). Springer,
Boston, MA.
25. Boncz, P., Wilshut, A. N., & Kersten, M. L., (1998). Flattening an object algebra to
provide performance. In: Data Engineering, 1998; Proceedings, 14th International
Conference on (pp. 568–577). IEEE.
26. Boole, G., (1916). The Laws of Thought (Vol. 2, pp. 1–20). Open Court Publishing
Company.
27. Bowersox, D. J., Closs, D. J., & Cooper, M. B., (2002). Supply Chain Logistics
Management (Vol. 2, pp. 5–16). New York, NY: McGraw-Hill.
28. Bozkaya, T., & Ozsoyoglu, M., (1999). Indexing large metric spaces for similarity
search queries. ACM Transactions on Database Systems (TODS), 24(3), 361–404.
29. Brin, S., (1995). Near Neighbor Search in Large Metric Spaces, 1, 1–22.
30. Brusoni, V., Console, L., Terenziani, P., & Pernici, B., (1995). Extending temporal
relational databases to deal with imprecise and qualitative temporal information. In:
Recent Advances in Temporal Databases (Vol. 1, pp. 3–22). Springer, London.
31. Buneman, P., Fan, W., & Weinstein, S., (1998). Path constraints on semistructured
and structured data. In: Proceedings of the Seventeenth ACM SIGACT-SIGMOD-
SIGART Symposium on Principles of Database Systems (Vol. 7, pp. 129–138). ACM.
32. Burkowski, F. J., (1992). An algebra for hierarchically organized text-dominated
databases. Information Processing & Management, 28(3), 333–348.
33. Burns, J. B., & Riseman, E. M., (1992). Matching complex images to multiple 3D objects
using view description networks. In: Computer Vision and Pattern Recognition, 1992;
Proceedings CVPR’92, 1992 IEEE Computer Society Conference (pp. 328–334). IEEE.
34. Cai, J., Paige, R., & Tarjan, R., (1992). More efficient bottom-up multi-pattern matching
in trees. Theoretical Computer Science, 106(1), 21–60.
35. Caouette, J. B., Altman, E. I., & Narayanan, P., (1998). Managing Credit Risk: The
Next Great Financial Challenge (Vol. 2, pp. 1–20). John Wiley & Sons.
36. Cesarini, F., Lastri, M., Marinai, S., & Soda, G., (2001). Page classification for meta-
data extraction from digital collections. In: International Conference on Database
and Expert Systems Applications (Vol. 1, pp. 82–91). Springer, Berlin, Heidelberg.
37. Chase, D. R., (1987). An improvement to bottom-up tree pattern matching. In:
Proceedings of the 14 th ACM SIGACT-SIGPLAN Symposium on Principles of
Programming Languages (Vol. 14, pp. 168–177). ACM.
38. Chiueh, T. C., (1994). Content-based image indexing. In: VLDB (Vol. 94, pp. 582–593).
CHAPTER
7
Search Algorithms in Data Structures 191
39. Christmas, W. J., Kittler, J., & Petrou, M., (1995). Structural matching in computer
vision using probabilistic relaxation. IEEE Transactions on Pattern Analysis and
Machine Intelligence, 17(8), 749–764.
40. Chung, C. W., Min, J. K., & Shim, K., (2002). APEX: An adaptive path index for
XML data. In: Proceedings of the 2002 ACM SIGMOD International Conference on
Management of Data (Vol. 1, pp. 121–132). ACM.
41. Ciaccia, P., Patella, M., & Zezula, P., (1997). DEIS-CSITE-CNR. In: Proceedings
of the International Conference on Very Large Data Bases (Vol. 23, pp. 426–435).
42. Clarke, C. L., Cormack, G. V., & Burkowski, F. J., (1995). An algebra for structured
text search and a framework for its implementation. The Computer Journal, 38(1),
43–56.
43. Cole, R., & Hariharan, R., (1997). Tree pattern matching and subset matching in
randomized O (nlog3m) time. In: Proceedings of the Twenty-Ninth Annual ACM
Symposium on Theory of Computing (Vol. 1, pp. 66–75). ACM.
44. Cole, R., Hariharan, R., & Indyk, P., (1999). Tree pattern matching and subset
matching in deterministic O (n log super (3) n)-time. In: The 1999 10th Annual ACM-
SIAM Symposium on Discrete Algorithms (Vol. 10, pp. 245–254).
45. Consens, M. P., & Mendelzon, A. O., (1990). GraphLog: A visual formalism for real life
recursion. In: Proceedings of the Ninth ACM SIGACT-SIGMOD-SIGART Symposium
on Principles of Database Systems (Vol. 1, pp. 404–416). ACM.
46. Console, L., Brusoni, V., Pernici, B., & Terenziani, P., (1995). Extending Temporal
Relational Databases to Deal with Imprecise and Qualitative Temporal Information,
1, 1–20.
47. Conte, D., Foggia, P., Sansone, C., & Vento, M., (2004). Thirty years of graph
matching in pattern recognition. International Journal of Pattern Recognition and
Artificial Intelligence, 18(03), 265–298.
48. Cook, D. J., & Holder, L. B., (1993). Substructure discovery using minimum description
length and background knowledge. Journal of Artificial Intelligence Research, 1,
231–255.
49. Cooper, B. F., Sample, N., Franklin, M. J., Hjaltason, G. R., & Shadmon, M., (2001).
A fast index for semistructured data. In: VLDB (Vol. 1, pp. 341–350).
50. Corneil, D. G., & Gotlieb, C. C., (1970). An efficient algorithm for graph isomorphism.
Journal of the ACM (JACM), 17(1), 51–64.
51. Cour, T., Srinivasan, P., & Shi, J., (2007). Balanced graph matching. In: Advances
in Neural Information Processing Systems (Vol. 1, pp. 313–320).
52. Day, Y. F., Dagtas, S., Iino, M., Khokhar, A., & Ghafoor, A., (1995). Object-oriented
conceptual modeling of video data. In: Data Engineering, 1995; Proceedings of the
Eleventh International Conference (Vol. 1, pp. 401–408). IEEE.
53. Dehaspe, L., Toivonen, H., & King, R. D., (1998). Finding frequent substructures in
chemical compounds. In: KDD (Vol. 98, p. 1998).
CHAPTER
7
192 Data Structures and Algorithms
54. Dekhtyar, A., Ross, R., & Subrahmanian, V. S., (2001). Probabilistic temporal
databases, I: Algebra. ACM Transactions on Database Systems (TODS), 26(1), 41–95.
55. Deutsch, A., Fernandez, M., Florescu, D., Levy, A., & Suciu, D., (1999). A query
language for XML. Computer Networks, 31(11–16), 1155–1169.
56. DeWitt, D. J., Kabra, N., & Luo, J., (1994). Client-server paradise. In: Patel, J.
M., & Yu, J., (eds.), Proceedings of the International Conference on Very Large
Databases (VLDB), 1, 1–22.
57. Dinitz, Y., Itai, A., & Rodeh, M., (1999). On an algorithm of zemlyachenko for subtree
isomorphism. Information Processing Letters, 70(3), 141–146.
58. Djoko, S., Cook, D. J., & Holder, L. B., (1997). An empirical study of domain knowledge
and its benefits to substructure discovery. IEEE Transactions on Knowledge and
Data Engineering, 9(4), 575–586.
59. Dolog, P., Stuckenschmidt, H., Wache, H., & Diederich, J., (2009). Relaxing RDF
queries based on user and domain preferences. Journal of Intelligent Information
Systems, 33(3), 239.
60. Drira, K., & Rodriguez, I. B., (2009). A Demonstration of an Efficient Tool for Graph
Matching and Transformation, 1, 1–20.
61. Dubiner, M., Galil, Z., & Magen, E., (1994). Faster tree pattern matching. Journal
of the ACM (JACM), 41(2), 205–213.
62. Dublish, P., (1990). Some comments on the subtree isomorphism problem for ordered
trees. Information Processing Letters, 36(5), 273–275.
63. Dubois, D., & Prade, H., (1989). Processing fuzzy temporal knowledge. IEEE
Transactions on Systems, Man, and Cybernetics, 19(4), 729–744.
64. Dutta, S., (1989). Generalized events in temporal databases. In: Data Engineering,
1989; Proceedings Fifth International Conference on (Vol. 5, pp. 118–125). IEEE.
65. Dyreson, C. E., & Snodgrass, R. T., (1998). Supporting valid-time indeterminacy.
ACM Transactions on Database Systems (TODS), 23(1), 1–57.
66. Eiter, T., Lukasiewicz, T., & Walter, M., (2001). A data model and algebra for
probabilistic complex values. Annals of Mathematics and Artificial Intelligence, 33(2–4),
205–252.
67. Engels, G., Lewerentz, C., Nagl, M., Schäfer, W., & Schürr, A., (1992). Building
integrated software development environments. Part I: Tool specification. ACM
Transactions on Software Engineering and Methodology (TOSEM), 1(2), 135–167.
68. Eshera, M. A., & Fu, K. S., (1984). A graph distance measure for image analysis.
IEEE Transactions on Systems, man, and Cybernetics, (3), 398–408.
69. Fernández, M. L., & Valiente, G., (2001). A graph distance metric combining maximum
common subgraph and minimum common supergraph. Pattern Recognition Letters,
22(6, 7), 753–758.
70. Fernandez, M., Florescu, D., Kang, J., Levy, A., & Suciu, D., (1998). Catching
the boat with strudel: Experiences with a web-site management system. In: ACM
CHAPTER
7
Search Algorithms in Data Structures 193
86. Grossi, R., (1993). On finding common subtrees. Theoretical Computer Science,
108(2), 345–356.
87. Gupta, A., & Nishimura, N., (1995). Finding smallest supertrees. In: International
Symposium on Algorithms and Computation (pp. 112–121). Springer, Berlin, Heidelberg.
88. Gupta, A., & Nishimura, N., (1998). Finding largest subtrees and smallest supertrees.
Algorithmica, 21(2), 183–210.
89. Güting, R. H., (1994). GraphDB: Modeling and querying graphs in databases. In:
VLDB (Vol. 94, pp. 12–15).
90. Gyssens, M., Paredaens, J., & Van, G. D., (1989). A Grammar-Based Approach
Towards Unifying Hierarchical Data Models (Vol. 18, No. 2, pp. 263–272). ACM.
91. Hirata, K., & Kato, T., (1992). Query by visual example. In: International Conference
on Extending Database Technology (Vol. 1, pp. 56–71). Springer, Berlin, Heidelberg.
92. Hirschberg, D. S., & Wong, C. K., (1976). A polynomial-time algorithm for the
knapsack problem with two variables. Journal of the ACM (JACM), 23(1), 147–154.
93. Hlaoui, A., & Wang, S., (2002). A new algorithm for inexact graph matching. In:
Pattern Recognition, 2002; Proceedings 16th International Conference (Vol. 4, pp.
180–183). IEEE.
94. Hlaoui, A., & Wang, S., (2004). A node-mapping-based algorithm for graph matching.
J. Discrete Algorithms, 1, 1–22.
95. Hoffmann, C. M., & O’Donnell, M. J., (1982). Pattern matching in trees. Journal of
the ACM (JACM), 29(1), 68–95.
96. Hong, P., & Huang, T. S., (2001). Spatial pattern discovering by learning the isomorphic
subgraph from multiple attributed relational graphs. Electronic Notes in Theoretical
Computer Science, 46, 113–132.
97. Hong, P., & Huang, T. S., (2004). Spatial pattern discovery by learning a probabilistic
parametric model from multiple attributed relational graphs. Discrete Applied
Mathematics, 139(1–3), 113–135.
98. Hong, P., Wang, R., & Huang, T., (2000). Learning patterns from images by combining
soft decisions and hard decisions. In: Computer Vision and Pattern Recognition,
2000; Proceedings IEEE Conference (Vol. 1, pp. 78–83). IEEE.
99. Hopcroft, J. E., & Wong, J. K., (1974). Linear time algorithm for isomorphism of planar
graphs (preliminary report). In: Proceedings of the Sixth Annual ACM Symposium
on Theory of Computing (Vol. 1, pp. 172–184). ACM.
100. James, C. A., Weininger, D., & Delany, J., (2000). Daylight Theory Manual 4.71,
Daylight Chemical Information Systems. Inc., Irvine, CA.
101. Jensen, C. S., & Snodgrass, R. T., (1999). Temporal data management. IEEE
Transactions on Knowledge and Data Engineering, 11(1), 36–44.
102. Jensen, C. S., Dyreson, C. E., Böhlen, M., Clifford, J., Elmasri, R., Gadia, S. K., &
Kline, N., (1998). The consensus glossary of temporal database concepts—February
CHAPTER
7
Search Algorithms in Data Structures 195
1998 version. In: Temporal Databases: Research and Practice (pp. 367–405).
Springer, Berlin, Heidelberg.
103. Kannan, R., (1980). A polynomial algorithm for the two-variable integer programming
problem. Journal of the ACM (JACM), 27(1), 118–122.
104. Kanza, Y., & Sagiv, Y., (2001). Flexible queries over semistructured data. In:
Proceedings of the Twentieth ACM SIGMOD-SIGACT-SIGART Symposium on
Principles of Database Systems (Vol. 1, pp. 40–51). ACM.
105. Kao, M. Y., Lam, T. W., Sung, W. K., & Ting, H. F., (1999). A decomposition theorem
for maximum weight bipartite matchings with applications to evolutionary trees. In:
European Symposium on Algorithms (Vol. 1, pp. 438–449). Springer, Berlin, Heidelberg.
106. Karray, A., Ogier, J. M., Kanoun, S., & Alimi, M. A., (2007). An ancient graphic
documents indexing method based on spatial similarity. In: International Workshop
on Graphics Recognition (Vol. 1, pp. 126–134). Springer, Berlin, Heidelberg.
107. Kato, T., Kurita, T., Otsu, N., & Hirata, K., (1992). A sketch retrieval method for
full color image database-query by visual example. In: Pattern Recognition, 1992;
Conference A: Computer Vision and Applications, Proceedings., 11th IAPR International
Conference (Vol. 1, pp. 530–533). IEEE.
108. Kaushik, R., Bohannon, P., Naughton, J. F., & Korth, H. F., (2002). Covering indexes
for branching path queries. In: Proceedings of the 2002 ACM SIGMOD International
Conference on Management of Data (pp. 133–144). ACM.
109. Kilpeläinen, P., & Mannila, H., (1993). Retrieval from hierarchical texts by partial
patterns. In: Proceedings of the 16th Annual International ACM SIGIR Conference
on Research and Development in Information Retrieval (Vol. 1, pp. 214–222). ACM.
110. Kilpeläinen, P., & Mannila, H., (1994). Query primitives for tree-structured data.
In: Annual Symposium on Combinatorial Pattern Matching (Vol. 1, pp. 213–225).
Springer, Berlin, Heidelberg.
111. Kilpeläinen, P., (1992). Tree Matching Problems with Applications to Structured Text
Databases, 1, 1–20.
112. Leordeanu, M., & Hebert, M., (2005). A spectral technique for correspondence
problems using pairwise constraints. In: Computer Vision, 2005. ICCV 2005; Tenth
IEEE International Conference (Vol. 2, pp. 1482–1489). IEEE.
113. Leordeanu, M., Hebert, M., & Sukthankar, R., (2009). An integer projected fixed point
method for graph matching and map inference. In: Advances in Neural Information
Processing Systems (pp. 1114–1122).
114. Luccio, F., & Pagli, L., (1991). Simple solutions for approximate tree matching
problems. In: Colloquium on Trees in Algebra and Programming (pp. 193–201).
Springer, Berlin, Heidelberg.
115. Lueker, G. S., & Booth, K. S., (1979). A linear time algorithm for deciding interval
graph isomorphism. Journal of the ACM (JACM), 26(2), 183–195.
116. Luks, E. M., (1982). Isomorphism of graphs of bounded valence can be tested in
polynomial time. Journal of Computer and System Sciences, 25(1), 42–65.
CHAPTER
7
196 Data Structures and Algorithms
117. Luo, B., & Hancock, E. R., (2001). Structural graph matching using the EM algorithm
and singular value decomposition. IEEE Transactions on Pattern Analysis and Machine
Intelligence, 23(10), 1120–1136.
118. Macleod, I. A., (1991). A query language for retrieving information from hierarchic
text structures. The Computer Journal, 34(3), 254–264.
119. McHugh, J., Abiteboul, S., Goldman, R., Quass, D., & Widom, J., (1997). Lore: A
database management system for semistructured data. SIGMOD Record, 26(3), 54–66.
120. Milo, T., & Suciu, D., (1999). Index structures for path expressions. In: International
Conference on Database Theory (pp. 277–295). Springer, Berlin, Heidelberg.
121. Navarro, G., & Baeza-yates, R., (1995). Expressive power of a new model for
structured text databases. In: In Proc. PANEL’95 (pp. 1–20).
122. Nishimura, N., Ragde, P., & Thilikos, D. M., (2000). Finding smallest supertrees
under minor containment. International Journal of Foundations of Computer Science,
11(03), 445–465.
123. Pelegri-Llopart, E., & Graham, S. L., (1988). Optimal code generation for expression
trees: An application BURS theory. In: Proceedings of the 15th ACM SIGPLAN-
SIGACT Symposium on Principles of Programming Languages (pp. 294–308). ACM.
124. Salminen, A., & Tompa, F. W., (1992). PAT expressions: An algebra for text search.
Acta Linguistica Hungarica, 41(1, 4), 277–306.
125. Schlieder, T., (2002). Schema-driven evaluation of approximate tree-pattern queries.
In: International Conference on Extending Database Technology (Vol. 1, pp. 514–532).
Springer, Berlin, Heidelberg.
126. Shaw, S., Vermeulen, A. F., Gupta, A., & Kjerrumgaard, D., (2016). Querying semi-
structured data. In: Practical Hive (pp. 115–131). A Press, Berkeley, CA.
127. Sikora, F., (2012). An (Almost Complete) State of the Art Around the Graph Motif
Problem (Vol. 1, pp. 1–22). Université Paris-Est Technical reports.
128. Tague, J., Salminen, A., & McClellan, C., (1991). Complete formal model for information
retrieval systems. In: Proceedings of the 14th Annual International ACM SIGIR
Conference on Research and Development in Information Retrieval (pp. 14–20). ACM.
129. Umeyama, S., (1988). An eigen decomposition approach to weighted graph matching
problems. IEEE Transactions on Pattern Analysis and Machine Intelligence, 10(5),
695–703.
130. Verma, R. M., & Reyner, S. W., (1989). An analysis of a good algorithm for the
subtree problem, corrected. SIAM Journal on Computing, 18(5), 906–908.
131. Wilson, R. C., & Hancock, E. R., (1997). Structural matching by discrete relaxation.
IEEE Transactions on Pattern Analysis and Machine Intelligence, 19(6), 634–648.
132. Xu, Y., Saber, E., & Tekalp, A. M., (1999). Hierarchical content description and object
formation by learning. In: Content-Based Access of Image and Video Libraries, 1999
(CBAIVL’99) Proceedings IEEE Workshop (pp. 84–88). IEEE.
CHAPTER
7
CHAPTER 8
GOVERNANCE OF ALGORITHMS
IN DATA STRUCTURES
UNIT INTRODUCTION
The purpose of this chapter is to provide a contribution to the development of a greater
grasp of governance choice in the context of algorithm selection. Algorithms on the Internet
have a role in the creation of our realities and the conduct of our everyday lives. It is
their job to first choose the material, then mechanically assign significance to it, saving
individuals from drowning in a sea of knowledge. Nonetheless, the benefits of algorithms
come with a number of hazards as well as governance difficulties to consider.
According to assessments of actual case studies and a literature study, we shall
outline a risk-based approach to corporate governance. This technique analyzes and then
categorizes the applications of algorithmic choice, as well as the dangers associated with
them. Following that, it investigates the wide range of institutional governance alternatives
available and briefly analyzes the many governance measures that have been implemented
and recommended for algorithmic selection, as well as the constraints of governance
options. According to the findings of the study, there are no one-size-fits-all methods for
regulating algorithms.
A growing number of algorithms have been implemented into the Internet-based
apps that we use in our everyday lives. These software intermediaries operate in the
background and have an impact on a wide range of operations. The ingestion of video
and music entertainment through recommender systems, the selection of online news
through various news and aggregators search engines, the display of status messages
on various online social networks, the selection of products and services in online shops,
and algorithmic trading in stock exchange markets worldwide are some of the most visible
198 Data Structures and Algorithms
examples of this pervasive trend. While their purpose and operation modes greatly differ
in detail, Latzer et al. (2015) identified nine groups of different Internet services that are
dependent on algorithmic selection applications because while their purpose and operation
modes greatly differ in feature, all of these applications are categorized by a mutual basic
functionality; they all automatically select information elements (Senecal & Nantel, 2004;
Hinz & Eckert, 2010).
Figure 8.1. Theoretical model of variables measuring the significance of algorithmic governance
in everyday life (Source: Beau Cranes, Creative Commons License).
Types Examples
General search engines
Metasearch engines
CHAPTER
8
Governance of Algorithms in Data Structures 199
CHAPTER
8
200 Data Structures and Algorithms
Learning Objectives
At the chapter’s end, students will get the ability to comprehend the following:
• The basic concept of algorithm governance in data structures
• Understanding risk associated with algorithmic selection
• Learn about governance mechanisms to address the risks associated with
algorithmic selection
• Limitations of algorithmic governance options
Key Terms
1. Accountability
2. Algorithm
3. Bias
4. Best Practices
5. Data
6. Decision-making
7. Discrimination
8. Framework
9. Governance
10. Heteronomy
CHAPTER
8
Governance of Algorithms in Data Structures 201
CHAPTER
8
202 Data Structures and Algorithms
Figure 8.2.
Framework of
the data analysis
algorithms (Source:
Ke Yan, Creative
Commons License).
KEYWORD For all of the dangers connected with algorithmic selection, specific
governance mechanisms are not required. We may also lower risks
Virtual private by changing the market behavior of content providers, consumers,
network is a and algorithmic service providers voluntarily. Consumers, for
service that creates example, might avoid utilizing problematic services by transferring
a safe, encrypted to another supplier or relying on knowledge to protect themselves
online connection. against hazards. Consumers can benefit from technological self-help
solutions, for example. Bias, censorship, and privacy infringement
are reduced as a result of such solutions. Clients can use a variety
of anonymization techniques, like Virtual Private Networks (VPNs),
Tor, or Open DNS, to avoid censorship and protect their privacy.
Cookie management, encryption, and do-not-track technologies
are examples of privacy-enhancing technologies (PETs) that may
be used to secure data (browser). As a result, using chances for
de-personalization of services can help to eliminate prejudice.
In general, these examples show several choices for user self-
protection, although several of these “demand-side solutions” are
reliant on and reduced by the availability of sufficient supply
(Cavoukia, 2012).
Providers of such services, which are based on algorithmic selection,
might mitigate risks by employing commercial tactics. This can be
accomplished through introducing product innovations, such as
updates to existing services or the introduction of new services. There
are various instances of such services that have been developed to
avoid copyright and privacy infringement and prejudice. Few news
aggregators’ business models include content suppliers who are
compensated (for example. nachrichten.de). It is possible to minimize
privacy problems by using algorithmic services that do not gather user
data (Resnick et al., 2013; Krishnan et al., 2014). If these product
developments are successful, they may contribute to market variety
as well as a decrease in market concentration (Schaar, 2010).
CHAPTER
8
Governance of Algorithms in Data Structures 205
CHAPTER
8
208 Data Structures and Algorithms
may also reduce the value of the service for users. As a result,
“alternative goods” are frequently specialized services with a small
customer base. Reduced quality and a small number of consumers
reinforce each other, lowering the attraction of specialized services
even further.
CHAPTER
8
212 Data Structures and Algorithms
SUMMARY
With the increasing use of algorithms and data structures in decision-making processes,
there is a growing concern about how these technologies are governed. The governance
of algorithms in data structures refers to the ways in which these technologies are
managed, regulated and held accountable. This includes issues such as transparency,
accountability, and fairness in algorithmic decision-making processes. The algorithms should
be explainable so that users can understand how they work and the criteria they use to
make decisions. Algorithms should be designed to avoid bias and discrimination, and to
ensure that all individuals are treated equally. Algorithm governance also involves issues
such as data privacy, security and accountability. The governance of algorithms in data
structures is a complex and evolving field that requires ongoing attention and regulation.
REVIEW QUESTIONS
1. What is algorithm governance and why is it important in the context of data
structures?
2. What are some of the key challenges associated with algorithm governance?
3. How can transparency and accountability be ensured in algorithm decision-making
process?
4. How can algorithm governance be effectively implemented across different industries
and sectors?
5. What are some of the current trends and developments in algorithm governance
and what do they mean for the future of this field?
REFERENCES
1. Argenton, C., & Prüfer, J., (2012). Search engine competition with network externalities.
Journal of Competition Law & Economics, 8(1), 73–105.
2. Bar-Ilan, J., (2007). Google bombing from a time perspective. Journal of Computer-
Mediated Communication, 12(3), 910–938.
3. Bartle, I., & Vass, P., (2005). Self-Regulation and the Regulatory State: A Survey of
Policy and Practice (Vol. 1, pp. 1–22). University of Bath School of Management.
4. Black, J., (2010). Risk-based regulation: Choices, practices and lessons learnt. In:
OECD, (ed.), Risk and Regulatory Policy: Improving the Governance of Risk (pp.
185–224). OECD Publishing, Paris.
5. Bozdag, E., (2013). Bias in algorithmic filtering and personalization. Ethics and
Information Technology, 15(3), 209–227.
6. Bracha, O., & Pasquale, F., (2008). Federal search commission? Access, fairness
and accountability in the law of search. Cornell Law Review, 93(6), 1149–1210.
7. Bucher, T., (2012). Want to be on top? Algorithmic power and the threat of invisibility
on Facebook. New Media & Society, 14(7), 1164–1180.
8. Cavoukia, A., (2012). Privacy by Design: Origins, Meaning, and Prospects for Ensuring
Privacy and Trust in the Information Era (Vol. 1, pp. 1–20).
CHAPTER
8
214 Data Structures and Algorithms
9. Collin, P., & Colin, N., (2013). Mission D’expertise Sur la Fiscalité de L’économie
Numérique (Vol. 1, pp. 1–20). Available at: www.redressement-productif.gouv.fr/files/
rapport-fiscalite-du-numerique_2013.pdf (accessed on 25 April 2023).
10. Döpfner, M., (2014). Warum wir Google Fürchten: Offener Brief an Eric Schmidt (Vol. 1,
pp. 1–19). Frankfurter Allgemeine Zeitung. Available at: www.faz.net/aktuell/feuilleton/
medien/mathias-doepfnerwarum-wir-google-fuerchten-12897463.html (accessed on
25 April 2023).
11. Elgesem, D., (2008). Search engines and the public use of reason. Ethics and
Information Technology, 10(4), 233–242.
12. Epstein, R., & Robertson, R. E., (2013). Democracy at Risk: Manipulating Search
Rankings Can Shift Voters’ Preferences Substantially Without Their Awareness (Vol.
1, pp. 1–20). Available at: http://aibrt.org/downloads/EPSTEIN_and_Robertson_2013-
Democracy_at_Risk-APS-summary-5-13.pdf (accessed on 25 April 2023).
13. Gillespie, T., (2014). The relevance of algorithms. In: Gillespie, T., Boczkowski, P.,
& Foot, K., (eds.), Media Technologies: Essays on Communication, Materiality, and
Society (pp. 167–194). MIT Press, Cambridge, MA.
14. Granka, L. A., (2010). The politics of search: A decade retrospective. The Information
Society, 26(5), 364–374.
15. Grasser, U., & Schulz, W., (2015). Governance of Online Intermediaries Observations
from a Series of National Case Studies (Vol. 1, pp. 1–23).
16. Gunningham, N., & Rees, J., (1997). Industry self-regulation: An institutional
perspective. Law & Policy, 19(4), 363–414.
17. Hinz, O., & Eckert, J., (2010). The impact of search and recommendation systems
on sales in electronic commerce. Business & Information Systems Engineering,
2(2), 67–77.
18. Hustinx, P., (2010). Privacy by design: Delivering the promises. Identity in the
Information Society, 3(2), 253–255.
19. Jansen, B. J., (2007). Click fraud. Computer, 40(7), 85, 86.
20. Jürgens, P., Jungherr, A., & Schoen, H., (2011). Small Worlds with a Difference:
New Gatekeepers and the Filtering of Political Information on Twitter (Vol. 1, pp.
1–20). Association for Computing Machinery (ACM).
21. Katzenbach, C., (2011). Technologies as institutions: Rethinking the role of technology
in media governance constellations. In: Puppis, M., & Just, N., (eds.), Trends in
Communication Policy Research (pp. 117–138). Intellect, Bristol.
22. König, R., & Rasch, M., (2014). Society of the Query Reader: Reflections on Web
Search (Vol. 1, pp. 1–23). Institute of Network Cultures, Amsterdam.
23. Krishnan, S., Patel, J., Franklin, M. J., & Goldberg, K., (2014). Social Influence Bias
in Recommender Systems: A Methodology for Learning, Analyzing, and Mitigating
Bias in Ratings (Vol. 1, pp. 1–28). Available at: http://goldberg.berkeley.edu/pubs/
sanjay-recsys-v10.pdf (accessed on 25 April 2023).
CHAPTER
8
Governance of Algorithms in Data Structures 215
39. Munson, S. A., & Resnick, P., (2010). Presenting diverse political opinions: How
and how much. Proceedings of ACM CHI 2010 Conference on Human Factors in
Computing Systems 2010 (pp. 1457–1466). Atlanta, Georgia.
40. Musiani, F., (2013). Governance by algorithms. Internet Policy Review, 2(3), available
at: http://policyreview.info/articles/analysis/governance-algorithms (accessed on 25
April 2023).
41. Napoli, P. M., (2013). The Algorithm as Institution: Toward a Theoretical Framework
for Automated Media Production and Consumption (Vol. 1, pp. 1–10). Paper presented
at the Media in Transition Conference, MIT Cambridge.
42. Ohm, P., (2010). Broken promises of privacy: Responding to the surprising failure
of anonymization. UCLA Law Review, 57, 1701–1777.
43. Pariser, E., (2011). The Filter Bubble: What the Internet is Hiding from You (Vol. 1,
pp. 1–20). Penguin Books, London.
44. Pasquale, F., (2015). The Black Box Society. The Secret Algorithms That Control
Money and Information (Vol. 1, pp. 1–20). Harvard University Press.
45. Resnick, P., Kelly, G. R., Kriplean, T., Munson, S. A., & Stroud, N. J., (2013).
Bursting your (filter) bubble: Strategies for promoting diverse exposure. Proceedings
of the 2013 Conference on Computer-Supported Cooperative Work Companion (pp.
95–100). San Antonio, Texas.
46. Rieder, B., (2005). Networked control: Search engines and the symmetry of confidence.
International Review of Information Ethics, 3, 26–32.
47. Rietjens, B., (2006). Trust and reputation on eBay: Towards a legal framework for
feedback intermediaries. Information & Communications Technology Law, 15(1), 55–78.
48. Saurwein, F., (2011). Regulatory choice for alternative modes of regulation: How
context matters. Law & Policy, 33(3), 334–366.
49. Schaar, P., (2010). Privacy by design. Identity in the Information Society, 3(2),
267–274.
50. Schedl, M., Hauger, D., & Schnitzer, D., (2012). A model for serendipitous music
retrieval. Proceedings of the 2nd Workshop on Context-awareness in Retrieval and
Recommendation (pp. 10–13). Lisbon.
51. Schormann, T., (2012). Online-Portale: Großer Teil der Hotelbewertungen ist Manipuliert
(Vol. 1, pp. 1–10). Spiegel Online. Available at: www.spiegel.de/reise/aktuell/online-
portale-grosser-teil-der-hotelbewertungenist-manipuliert-a-820383.html (accessed on
25 April 2023).
52. Schulz, W., Held, T., & Laudien, A., (2005). Search engines as gatekeepers of
public communication: Analysis of the German framework applicable to internet
search engines including media law and anti-trust law. German Law Journal, 6(10),
1418–1433.
53. Senecal, S., & Nantel, J., (2004). The influence of online product recommendation
on consumers’ online choice. Journal of Retailing, 80(2), 159–169.
CHAPTER
8
Governance of Algorithms in Data Structures 217
54. Sinclair, D., (1997). Self-regulation versus command and control? Beyond false
dichotomies. Law & Policy, 19(4), 529–559.
55. Somaiya, R., (2014). How Facebook is Changing the Way its Users Consume
Journalism (Vol. 1, pp. 1–10). New York Times.
56. Steiner, C., (2012). Automate This: How Algorithms Came to Rule Our World (Vol.
1, pp. 1–10). Penguin Books, New York, NY.
57. Van, D. A., (2012). The algorithms behind the headlines. Journalism Practice, 6(5,
6), 648–658.
58. Wallace, J., & Dörr, K. (2015). Beyond traditional gatekeeping. How algorithms
and users restructure the online gatekeeping process. In Conference Paper, Digital
Disruption to Journalism and Mass Communication Theory, 1, 33-45.
59. Wittel, G. L., & Wu, S. F., (2004). On attacking statistical spam filters. Proceedings
of the First Conference on Email and Anti-Spam (CEAS). Available at: http://pdf.
aminer.org/000/085/123/on_attacking_statistical_spam_filters.pdf (accessed on 25
April 2023).
60. Zittrain, J., & Palfrey, J., (2008). Internet filtering: The politics and mechanisms of
control. In: Deibert, R., Palfrey, J., Rohozinski, R., & Zittrain, J., (eds.), Access
Denied: The Practice and Policy of Global Internet Filtering (pp. 29–56). MIT Press,
Cambridge.
CHAPTER
8
INDEX
A B
Accountability 200 Backtracking algorithm 37
ADT stack 96 Backtracking method 41
Algebraic expression 157, 158 Big O notation 160
Algorithm 1 Binary search algorithm model 174
Algorithm governance 212 Binary search method 84
Algorithmic applications 209 Binary search technique 83
Algorithmic paradigm 37 Binary search tree 138
Algorithmic selection 197, 198, 199, 200, 201, Binary tree 138
202, 203, 204, 205, 206, 207, 208, 209, 210, Binary-tree procedures 139
211, 215 Binary values 157
Algorithmic service 204 Biochemicals database systems 179
Algorithmic trading 206 Biological database system 175
Algorithm selection 197 Bottom-up methods 40
Alphabetical sorting 170 Bracket checker 104
Anonymization techniques 204 Branch-and-bound algorithm 37
Applet 120 Brute Force algorithm 41, 44
Approximation algorithms 33, 36 Brute Force technique 42
Approximation techniques 179
Archeological databases 184 C
Array 56 Calling process 18
Array-based sets 65 Censorship 209
Array Implementation 92 Chunk search algorithm 173
Array index 56 Classification schemes 31
Array operations 56 Cognitive capabilities 201
Ascending-priority queue 120 Combinatorial costs 178
Average-case evaluation 20 Commercial activities 4
Commercial databases 167
220 Data Structures and Algorithms
Gupta
Data structures are specialized formats for organizing, storing, and manipulating
data in computer programs. There are several types of data structures, including
arrays, linked lists, stacks, queues, and trees. Algorithms are a set of instructions or
procedures that are designed to solve a specific computation problem.
About the Author In today’s fast-paced digital age, efficient data management is more critical than
ever before. As the amount of data being generated and processed increases
exponentially, the need for powerful algorithms and data structures becomes
increasingly pressing. Data structures and algorithms are important concepts in
computer science, and their understanding is crucial for building efficient and
scalable software systems. A good understanding of data structures and algorithms
can help software developers to design efficient programs, optimize code perfor-
mance and solve complex computational problems.
The discussion of the book starts with the fundamentals of data structures and
algorithms, followed by the classification of algorithms used in data structures. Also,
Shubham Gupta is a highly skilled software the concept of arrays and sets used in data structures and the selection of
engineer with over seven years of experience in algorithms are described in detail in this book. The fundamental concept of two very
important data structures, stacks, and queues, is also explained in detail. Another
DATA STRUCTURES
on numerous projects involving the development process or flows for the selection of a suitable algorithm. The fifth chapter describes
of web-based applications and services. In the concept of stacks and queues in data structures, along with the implementation
addition to his technical skills, Shubham is also a of these concepts using arrays and linked lists. The sixth chapter deals with the
strong communicator and team player. He enjoys concepts of trees, basic binary operations, and the efficiency of binary trees. The
collaborating with other professionals to develop
seventh chapter is about search algorithms that are used in the data structure to
AND ALGORITHMS
solutions that are both technically sound and
user-friendly. Shubham is passionate about his
find an element inside the structure, and the eighth chapter is about the gover-
work and is always looking for ways to improve nance of algorithms and the limitations of governance options.
his skills and stay up-to-date with the latest This book has been written specifically for students and scholars to meet their needs
industry trends and technologies. His commit- in terms of knowledge and to provide them with a broad understanding of data
ment to excellence has earned him a reputation structures and algorithms. We aim for this book to be a resource for academics in
as a trusted and reliable software engineer. many subjects since we believe it will provide clarity and insight for its readers.
ISBN 978-1-77956-178-7
TAP
00000
TAP
TAP
9 781779 561787
Toronto Academic Press