Data Structures and Program Design Using Python: A Self-Teaching Introduction to Data Structures and Python
()
About this ebook
This book, part of the Pocket Primer series, introduces the basic concepts of data science using Python 3 and other applications. It offers a fast-paced introduction to data analytics, statistics, data visualization, linear algebra, and regular expressions. The book features numerous code samples using Python, NumPy, R, SQL, NoSQL, and Pandas. Companion files with source code and color figures are available.
Understanding data science is crucial in today's data-driven world. This book provides a comprehensive introduction, covering key areas such as Python 3, data visualization, and statistical concepts. The practical code samples and hands-on approach make it ideal for beginners and those looking to enhance their skills.
The journey begins with working with data, followed by an introduction to probability, statistics, and linear algebra. It then delves into Python, NumPy, Pandas, R, regular expressions, and SQL/NoSQL, concluding with data visualization techniques. This structured approach ensures a solid foundation in data science.
Read more from Mercury Learning And Information
Autodesk Revit 2025 Architecture: Mastering Revit Techniques for Efficient Architectural Design Rating: 0 out of 5 stars0 ratingsAccess 365 Project Book: Hands-On Database Creation Rating: 0 out of 5 stars0 ratingsAutoCAD 2024 Beginning and Intermediate: Mastering 2D Drafting Techniques for All Levels Rating: 0 out of 5 stars0 ratingsComputer Concepts and Management Information Systems: A Comprehensive Guide to Modern Computing and Information Management Rating: 0 out of 5 stars0 ratings3D Printing: The Complete Guide to Mastering 3D Printing Techniques Rating: 0 out of 5 stars0 ratingsAccess 2021 / Microsoft 365 Programming by Example: Mastering VBA for Data Management and Automation Rating: 0 out of 5 stars0 ratingsGame Development Using Python: Mastering Interactive Game Creation and Development through Python Rating: 0 out of 5 stars0 ratingsText Analytics for Business Decisions: Mastering Techniques for Insightful Data Interpretation through a Case Study Approach Rating: 0 out of 5 stars0 ratingsArtificial Intelligence and Expert Systems: Techniques and Applications for Problem Solving Rating: 0 out of 5 stars0 ratingsComputer Graphics Programming in OpenGL With C++ (Edition 3): Mastering 3D Graphics and Animation Techniques Rating: 0 out of 5 stars0 ratingsData Wrangling Using Pandas, SQL, and Java: A Comprehensive Guide to Data Cleaning and Transformation Rating: 0 out of 5 stars0 ratingsData Analytics: Master the Art of Data Analytics with Essential Tools and Techniques Rating: 0 out of 5 stars0 ratingsClassic Game Design: From Pong to Pac-Man with Unity: Crafting Timeless Retro Games with Expert Techniques Rating: 0 out of 5 stars0 ratingsPython Data Structures Pocket Primer: A concise guide to Python data structures to enhance your skills Rating: 0 out of 5 stars0 ratingsDatabase Security: Master the Art of Protecting Your Data with Cutting-Edge Techniques Rating: 0 out of 5 stars0 ratingsAngular and Deep Learning Pocket Primer: A Comprehensive Guide to AI and Expert Systems for Professionals Rating: 0 out of 5 stars0 ratingsDigital Signal Processing: An Introduction to Mastering Advanced Techniques for Transforming and Analyzing Signals Rating: 0 out of 5 stars0 ratingsBash Command Line and Shell Scripts Pocket Primer: Mastering Bash Commands and Scripting Techniques Rating: 0 out of 5 stars0 ratingsUnmanned Aerial Vehicles: A Comprehensive Guide to UAV Technology and Applications Rating: 0 out of 5 stars0 ratingsFlowchart and Algorithm Basics: Learn the Art of Programming through this Guide to Selection, Looping, Arrays, and File Processing Rating: 0 out of 5 stars0 ratingsData Visualization for Business Decisions: Transforming Data into Actionable Insights Rating: 0 out of 5 stars0 ratingsArtificial Intelligence in the 21st Century: The Future of Technology and Human Innovation Rating: 0 out of 5 stars0 ratingsWORKING WITH grep, sed, AND awk Pocket Primer: A Quick Guide to Mastering Powerful Command Line Tools Rating: 0 out of 5 stars0 ratingsData Science for IoT Engineers: Master Data Science Techniques and Machine Learning Applications for Innovative IoT Solutions Rating: 0 out of 5 stars0 ratingsTensor Analysis for Engineers: Mastering Coordinate Systems, Transformations and Applications using Mathematics Rating: 0 out of 5 stars0 ratingsPython 3 and Data Analytics Pocket Primer: A Quick Guide to NumPy, Pandas, and Data Visualization Rating: 0 out of 5 stars0 ratingsPython 3 for Machine Learning: Harness the Power of Python for Advanced Machine Learning Projects Rating: 0 out of 5 stars0 ratingsPrompt Engineering Using ChatGPT: Crafting Effective Interactions and Building GPT Apps Rating: 0 out of 5 stars0 ratingsComputer Security and Encryption: Advanced Techniques for Securing Digital Information Rating: 0 out of 5 stars0 ratings
Related to Data Structures and Program Design Using Python
Related ebooks
Data Structures and Program Design Using C++: A Self-Teaching Introduction to Data Structures and C++ Rating: 0 out of 5 stars0 ratingsData Structures and Program Design Using Java: A Self-Teaching Introduction to Data Structures and Java Rating: 0 out of 5 stars0 ratingsProgramming Fundamentals Using JAVA: A Game Application Approach: Unlock Your Potential with Comprehensive Java Training Rating: 0 out of 5 stars0 ratingsPython 3 Data Visualization Using ChatGPT / GPT-4: Master Python Visualization Techniques with AI Integration Rating: 0 out of 5 stars0 ratingsPython 3 Data Visualization Using Google Gemini: Unlock the Power of Python and Google Gemini for Stunning Data Visualizations Rating: 0 out of 5 stars0 ratingsPython 3 and Machine Learning Using ChatGPT / GPT-4: Harness the Power of Python, Machine Learning, and Generative AI Rating: 0 out of 5 stars0 ratingsDealing With Data Pocket Primer: A Comprehensive Guide to Data Handling Techniques Rating: 0 out of 5 stars0 ratingsPython: An Introduction to Python Programming Rating: 0 out of 5 stars0 ratingsData Wrangling Using Pandas, SQL, and Java: A Comprehensive Guide to Data Cleaning and Transformation Rating: 0 out of 5 stars0 ratingsUnmanned Aerial Vehicles: A Comprehensive Guide to UAV Technology and Applications Rating: 0 out of 5 stars0 ratingsComputational Physics: A Comprehensive Guide to Numerical Methods in Physics Rating: 0 out of 5 stars0 ratingsArtificial Intelligence and Expert Systems: Techniques and Applications for Problem Solving Rating: 0 out of 5 stars0 ratingsData Literacy With Python: A Comprehensive Guide to Understanding and Analyzing Data with Python Rating: 0 out of 5 stars0 ratingsAngular and Machine Learning Pocket Primer: A Comprehensive Guide to Angular and Integrating Machine Learning Rating: 0 out of 5 stars0 ratingsPython Tools for Data Scientists Pocket Primer: A Quick Guide to Essential Python Libraries for Data Science Rating: 0 out of 5 stars0 ratingsPython 3 and Data Visualization: Mastering Graphics and Data Manipulation with Python Rating: 0 out of 5 stars0 ratingsComputer Graphics Programming in OpenGL with Java: A Comprehensive Guide to Modern 3D Graphics Programming Rating: 0 out of 5 stars0 ratingsCybersecurity: A Self-Teaching Introduction Rating: 0 out of 5 stars0 ratingsData Science Fundamentals Pocket Primer: An Essential Guide to Data Science Concepts and Techniques Rating: 0 out of 5 stars0 ratingsMulti-Cloud Administration Guide: Manage and Optimize Cloud Resources across Azure, AWS, GCP, and Alibaba Cloud Rating: 0 out of 5 stars0 ratingsPython 3 and Data Analytics Pocket Primer: A Quick Guide to NumPy, Pandas, and Data Visualization Rating: 0 out of 5 stars0 ratingsMicrosoft Access 2021 Programming Pocket Primer: A Comprehensive Guide to Mastering Access VBA Rating: 0 out of 5 stars0 ratingsPandas Basics: Mastering Data Analysis with Pandas Rating: 0 out of 5 stars0 ratingsDatabase Security: Master the Art of Protecting Your Data with Cutting-Edge Techniques Rating: 0 out of 5 stars0 ratingsComputer Security and Encryption: Advanced Techniques for Securing Digital Information Rating: 0 out of 5 stars0 ratingsAccess 2021 / Microsoft 365 Programming by Example: Mastering VBA for Data Management and Automation Rating: 0 out of 5 stars0 ratingsDigital Signal Processing: An Introduction to Mastering Advanced Techniques for Transforming and Analyzing Signals Rating: 0 out of 5 stars0 ratingsPython 3 for Machine Learning: Harness the Power of Python for Advanced Machine Learning Projects Rating: 0 out of 5 stars0 ratingsTech Trends of the 4th Industrial Revolution: Navigating the Future of Technology in Business Rating: 0 out of 5 stars0 ratingsData Science Tools: Comprehensive Guide to Mastering Fundamental Data Science and Statistics Techniques Rating: 0 out of 5 stars0 ratings
Programming For You
Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsPython Machine Learning By Example Rating: 4 out of 5 stars4/5Unreal Engine from Zero to Proficiency (Foundations): Unreal Engine from Zero to Proficiency, #1 Rating: 3 out of 5 stars3/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5
Reviews for Data Structures and Program Design Using Python
0 ratings0 reviews
Book preview
Data Structures and Program Design Using Python - Mercury Learning and Information
CHAPTER 1
INTRODUCTION TO DATA STRUCTURES
1.1 INTRODUCTION
A data structure is an efficient way of storing and organizing the data elements in a computer’s memory. Data means a value or a collection of values. Structure refers to a method of organizing the data. The mathematical or logical representation of data in the memory is referred to as a data structure. The objective of a data structure is to store, retrieve, and update the data efficiently. A data structure can be considered as all the elements grouped under one name. The data elements are called members, and they can be of different types. Data structures are used in almost every program and software system. There are various kinds of data structures that are suited for different types of applications. Data structures are the building blocks of a program. For a program to run efficiently, a programmer must choose the appropriate data structures. A data structure is a crucial part of data management. As the name suggests, data management is a task that includes different activities, like the collection of data and the organization of data into structures. Data structures are used in stacks, queues, arrays, binary trees, linked lists, and hash tables.
A data structure helps us to understand the relationship of one element to another element and organize it within the memory. It is a mathematical or logical representation or organization of data in the memory. Data structures are extensively applied in the following areas:
•Compiler Design
•Database Management Systems (DBMS)
•Artificial Intelligence
•Network and Numerical Analysis
•Statistical Analysis Packages
•Graphics
•Operating Systems (OS)
•Simulations
There are many applications in which different data structures are used for their operations. Some data structures sacrifice speed for the efficient utilization of memory, while others sacrifice memory utilization and result in a faster speed. In today’s world, programmers aim not just to build a program, but to build an effective program. As previously discussed, for a program to be efficient, a programmer must choose the appropriate data structures. Hence, data structures are classified into various types. Now, let us discuss and learn about different types of data structures.
Frequently Asked Questions
1. Define the term data structure.
Answer:
A data structure is an organization of data in a computer’s memory or disk storage. In other words, a logical or mathematical model of a particular organization of data is called a data structure. A data structure in computer science is also a way of storing data in a computer so that it can be used efficiently. An appropriate data structure allows a variety of important operations to be performed using both resources, that is, the memory space and execution time, efficiently.
1.2 TYPES OF DATA STRUCTURES
Data structures are classified into various types.
1.2.1 Linear and Non-Linear Data Structures
A linear data structure is one in which the data elements are stored in a linear, or sequential, order; that is, data is stored in consecutive memory locations. A linear data structure can be represented in two ways; either it is represented by a linear relationship between various elements utilizing consecutive memory locations as in the case of arrays, or it may be represented by a linear relationship between the elements utilizing links from one element to another as in the case of linked lists. Examples of linear data structures include arrays, linked lists, stacks, and queues.
A non-linear data structure is one in which the data is not stored in any sequential order or consecutive memory locations. The data elements in this structure are represented by a hierarchical order. Examples of non-linear data structures include graphs and trees.
1.2.2 Static and Dynamic Data Structures
A static data structure is a collection of data in memory that is fixed in size and cannot be changed during runtime. The memory size must be known in advance, as the memory cannot be reallocated later in a program. One example is an array.
A dynamic data structure is a collection of data in which memory can be reallocated during the execution of a program. The programmer can add or remove elements according to his/her need. Examples include linked lists, graphs, and trees.
1.2.3 Homogeneous and Non-Homogeneous Data Structures
A homogeneous data structure is one that contains data elements of the same type (for example, arrays).
A non-homogeneous data structure contains data elements of different types (for example, structures).
1.2.4 Primitive and Non-Primitive Data Structures
Primitive data structures are the fundamental data structures or predefined data structures that are supported by a programming language. Examples of primitive data structure types are integer, float, and char.
Non-primitive data structures are comparatively more complicated data structures that are created using primitive data structures. Examples of non-primitive data structures are arrays, files, linked lists, stacks, and queues.
The classification of different data structures is shown in Figure 1.1.
FIGURE 1.1 Classification of different data structures
Python supports various data structures. We now introduce all these data structures, and they are discussed in detail in the upcoming chapters.
Frequently Asked Questions
2. What is the difference between primitive data structures and non-primitive data structures?
Answer:
The data structures that are typically directly operated upon by machine-level instructions, that is, the fundamental data types such as int, float, and char, are known as primitive data structures. The data structures that are not fundamental are called non-primitive data structures.
Frequently Asked Questions
3. What is the difference between linear and non-linear data structures?
Answer:
The main difference between linear and non-linear data structures lies in the way in which data elements are organized. In the linear data structure, elements are organized sequentially, and therefore they are easy to implement in a computer’s memory. In non-linear data structures, a data element can be attached to several other data elements to represent specific relationships existing among them.
1.2.5 Arrays/Lists
The array structure looks very similar to Python’s list structure. That’s because the two structures are both sequences that are composed of multiple sequential elements that can be accessed by position. But there are two major differences between the array and the list. First, an array has a limited number of operations, which commonly include those for array creation, reading a value from a specific element, and writing a value to a specific element. The list provides a large number of operations for working with the content of the list. Second, the list can grow and shrink during execution as elements are added or removed while the size of an array cannot be changed after it has been created.
Python’s list structure is a mutable sequence container that can change size as items are added or removed. It is an abstract data type that is implemented using an array structure to store the items contained in the list.
In Python, a list is declared using the following syntax:
Syntax – pyList = [4, 12, 2, 34, 17]
1.2.6 Stacks
A stack is a collection of objects that are inserted and removed according to the Last-In, First-Out (LIFO) principle. A user may insert objects into a stack at any time, but may only access or remove the most recently inserted object that remains (at the so-called top
of the stack). The name stack
is derived from the metaphor of a stack of plates in a spring-loaded, cafeteria plate dispenser. In this case, the fundamental operations involve the pushing
and popping
of plates on the stack. When we need a new plate from the dispenser, we pop
the top plate off the stack, and when we add a plate, we push
it down on the stack to become the new top plate.
Practical Application:
A real-life example of a stack is a pile of plates arranged on a table.
A person will pick up the first plate from the top of the stack.
The Stack ADT can be implemented in several ways. The two most common approaches to implement Stack ADT in Python include the use of a Python list and a linked list. The choice depends on the type of application involved.
1.2.7 Queues
Another fundamental data structure is the queue. It is a close cousin of the stack, as a queue is a collection of objects that are inserted and removed according to the First-In, First-Out (FIFO) principle. That is, elements can be inserted at any time, but only the element that has been in the queue the longest can be next removed. We usually say that elements enter a queue at the back and are removed from the front. A metaphor for this terminology is a line of people waiting to get on an amusement park ride. People waiting for such a ride enter at the back of the line and get on the ride from the front of the line.
Practical Application:
For a simple illustration of a queue, imagine there is a line of people standing at the bus stop and waiting for the bus. The first person standing in the line will get into the bus first.
The Queue ADT can be implemented in several ways. The two most common approaches in Python include the use of a Python list and a linked list. The choice depends on the type of application involved.
1.2.8 Linked Lists
The major drawback of the array is that the size or the number of elements must be known in advance. Thus, this drawback gave rise to the new concept of a linked list. A linked list is a linear collection of data elements. These data elements are called nodes, which store the address of the next node. A linked list is a sequence of nodes in which each node contains one or more than one data field and an address field that stores the address of the next node. Linked lists are dynamic; that is, memory is allocated when required.
FIGURE 1.2 Memory representation of a linked list
Figure 1.2 shows a linked list in which each node is divided into two slots:
1. The first slot contains information/data.
2. The second slot contains the address of the next node.
Practical Application:
A simple real-life example is a train; here, each train car is connected to the previous one and next one (except the first car (the engine) and the last car (the coach)).
The address part of the last node stores a special value called NULL, which denotes the end of the linked list. The advantage of a linked list over arrays is that now it is easier to insert and delete data elements, as we don’t have to do shifting each time. Yet searching for an element is difficult. More time is required to search for an element, and it requires a large amount of memory space. Hence, linked lists are used where a collection of data elements is required but the number of data elements in the collection is not known to us in advance.
Frequently Asked Questions
4. Define the term linked list.
Answer:
A linked list or one-way list is a linear collection of data elements called nodes, which give a linear order. It is a popular dynamic data structure. The nodes in the linked list are not stored in consecutive memory locations. For every data item in a node of the linked list, there is an associated address field that gives the address location of the next node in the linked list.
1.2.9 Trees
A tree is a popular non-linear data structure in which the data elements or the nodes are represented in a hierarchical order. Here, one of the nodes is shown as the root node of the tree, and the remaining nodes are partitioned into two disjointed sets such that each set is a part of a sub-tree. A tree makes the search process very easy, and its recursive programming makes a program optimized and easy to understand.
A binary tree is the simplest form of a tree. A binary tree consists of a root node and two sub-trees known as the left sub-tree and the right sub-tree, where both sub-trees are also binary trees. Each node in a tree consists of three parts, that is, the extreme left part stores the address of the left sub-tree, the middle part consists of the data element, and the extreme right part stores the address of the right sub-tree. The root is the topmost element of the tree. When there are no nodes in a tree, that is, when ROOT = NULL, then it is called an empty tree.
For example, consider a binary tree where R is the root node of the tree. LEFT and RIGHT are the left and right sub-trees of R, respectively. Node A is designated as the root node of the tree. Nodes B and C are the left and right child of A, respectively. Nodes B, D, E, and G constitute the left sub-tree of the root. Similarly, nodes C, F, H, and I constitute the right sub-tree of the root.
FIGURE 1.3 A binary tree
Advantages of a tree
1. The searching process is very fast in trees.
2. The insertion and deletion of the elements is easier compared to other data structures.
Frequently Asked Questions
5. Define the term binary tree.
Answer:
A binary tree is a hierarchal data structure in which each node has at most two children, that is, the left and right child. In a binary tree, the degree of each node can be at most two. Binary trees are used to implement binary search trees, which are used for efficient searching and sorting. A variation of BST is an AVL tree, where the height of the left and right subtree differs by one. A binary tree is a popular subtype of a k-ary tree, where k is 2.
1.2.10 Graphs
A graph is a general tree with no parent-child relationship. It is a non-linear data structure that consists of vertices, also called nodes, and the edges that connect those vertices. In a graph, any complex relationship can exist. A graph G may be defined as a finite set of V vertices and E edges. Therefore, G = (V, E) where V is the set of vertices and E is the set of edges. Graphs are used in various applications of mathematics and computer science. Unlike a root node in trees, graphs don’t have root nodes; rather, the nodes can be connected to any node in the graph. Two nodes are called neighbors when they are connected via an edge.
Practical Application:
A real-life example of a graph can be seen in workstations where several computers are joined to one another via network connections.
For example, consider a graph G with six vertices and eight edges. Here, Q and Z are neighbors of P. Similarly, R and T are neighbors of S.
FIGURE 1.4 A graph
1.3 OPERATIONS ON DATA STRUCTURES
Here, we discuss various operations that are performed on data structures.
•Creation – This is the process of creating a data structure. The declaration and initialization of the data structure are done here. It is the first operation.
•Insertion – This is the process of adding new data elements in the data structure, for example, to add the details of an employee who has recently joined an organization.
•Deletion – This is the process of removing a particular data element from the given collection of data elements, for example, to remove the name of an employee who has left the company.
•Updating – This is the process of modifying the data elements of a data structure. For example, if the address of a student is changed, it should be updated.
•Searching – This is used to find the location of a particular data element or all the data elements with the help of a given key, for example, to find the names of people who live in New York.
•Sorting – This is the process of arranging the data elements in some order, that is, either ascending or descending order. An example is arranging the names of students of a class in alphabetical order.
•Merging – This is the process of combining the data elements of two different lists to form a single list of data elements.
•Traversal – This is the process of accessing each data element exactly once so that it can be processed. An example is to print the names of all the students of a class.
•Destruction – This is the process of deleting the entire data structure. It is the last operation in the data structure.
1.4 ALGORITHMS
An algorithm is a systematic set of instructions combined to solve a complex problem. It is a step-by-finite-step sequence of instructions, each of which has a clear meaning and can be executed in a minimum amount of effort in finite time. In general, an algorithm is a blueprint for writing a program to solve the problem. Once we have a blueprint of the solution, we can easily implement it in any high-level language like C, C++, or Python. It divides the problem into a finite number of steps. An algorithm written in a programming language is known as a program. A computer is a machine with no brain or intelligence. Therefore, the computer must be instructed to perform a given task in unambiguous steps. Hence, a programmer must define his problem in the form of an algorithm written in English. Thus, such an algorithm should have the following features:
1. An algorithm should be simple and concise.
2. It should be efficient and effective.
3. It should be free of ambiguity; that is, the logic must be clear.
Similarly, an algorithm must have the following characteristics:
•Input – It reads the data of the given problem.
•Output – The desired result must be produced.
•Process/Definiteness – Each step or instruction must be unambiguous.
•Effectiveness – Each step should be accurate and concise. The desired result should be produced within a finite time.
•Finiteness – The number of steps should be finite.
1.4.1 Developing an Algorithm
To develop an algorithm, some steps are necessary:
1. Defining or understanding the problem.
2. Identifying the result or output of the problem.
3. Identifying the inputs required by the problem and choosing the best input.
4. Designing the logic from the given inputs to get the desired output.
5. Testing the algorithm for different inputs.
6. Repeating the previous steps until it produces the desired result for all the inputs.
1.5 APPROACHES FOR DESIGNING AN ALGORITHM
A complicated algorithm is divided into smaller units called modules. These modules are further divided into sub-modules. Thus, in this way, a complex algorithm can easily be solved. The process of dividing an algorithm into modules is called modularization. There are two popular approaches for designing an algorithm:
•Top-Down Approach
•Bottom-Up Approach
Now let us understand both approaches.
1. Top-Down Approach –A top-down approach states that the complex/complicated problem/algorithm should be divided into a smaller number of one or more modules. These smaller modules are further divided into one or more sub-modules . This process of decomposition is repeated until we achieve the desired output of module complexity. A top-down approach starts from the topmost module, and the modules are incremented accordingly until a level is reached where we don’t require any more sub-modules, that is, the desired level of complexity is achieved.
FIGURE 1.5 Top-down approach
Bottom-Up Approach–A bottom-up algorithm design approach is the opposite of a top-down approach. In this kind of approach, we first start with designing the basic modules and proceed further toward designing the high-level modules. The sub-modules are grouped to form a module of a higher level. Similarly, all high-level modules are grouped to form more high-level modules. Thus, this process of combining the sub-modules is repeated until we obtain the desired output of the algorithm.
FIGURE 1.6 Bottom-up approach
1.6 ANALYZING AN ALGORITHM
An algorithm can be analyzed by two factors: space and time. We should develop an algorithm that makes the best use of both these resources. Analyzing an algorithm measures the efficiency of the algorithm. The efficiency of the algorithm is measured in terms of the speed and time complexity. The complexity of an algorithm is a function that measures the space and time used by an algorithm in terms of input size.
Time Complexity–The time complexity of an algorithm is the amount of time taken by an algorithm to run the program completely. It is the runtime of the program. The time complexity of an algorithm depends upon the input size. The time complexity is commonly represented by using big O notation. For example, the time complexity of a linear search is O (n).
Space Complexity–The space complexity of an algorithm is the amount of memory space required to run the program completely. The space complexity of an algorithm depends upon the input size.
Time Complexity is categorized into three types:
1. Best Case Running Time –The performance of the algorithm is best under optimal conditions. For example, the best case for a binary search occurs when the desired element is the middle element of the list. Another example is that of sorting; that is, if the elements are already sorted in a list, then the algorithm will execute in the best time.
2. Average Case Running Time –This denotes the behavior of an algorithm when the input is randomly drawn from a given collection or distribution. It is an estimate of the running time for average
input. It is usually assumed that all inputs of a given size are likely to occur with equal probability.
3. Worst Case Running Time –The behavior of the algorithm during the worst possible case of the input instance. The worst case running time of an algorithm is an upper bound on the running time for any input. For example, the worst case for a linear search occurs when the desired element is the last element in the list or the element does not exist in the list.
Frequently Asked Questions
6. Define time complexity.
Answer:
Time complexity is a measure that evaluates the count of the operations performed by a given algorithm as a function of the size of the input. It is the approximation of the number of steps necessary to execute an algorithm. It is commonly represented with asymptotic notation, that is, the O(g) notation, also known as big O notation, where g is the function of the size of the input data.
1.6.1 Time-Space Trade-Off
In computer science, the time-space trade-off is a way of solving a particular problem either in less time and more memory space or in more time and less memory space. But if we talk in practical terms, designing such an algorithm in which we can save both space and time is a challenging task. So, we can use more than one algorithm to solve a problem. One may require less time, and the other may require less memory space to execute. Therefore, we sacrifice one thing for the other. Hence, there exists a time-space or time-memory trade-off between algorithms. The time-space trade-off gives the programmer a rational choice from an informed point of view. If time is a big concern for a programmer, then she might choose a program that takes less or the minimum time to execute. If space is a prime concern for a programmer, then, she might choose a program that takes less memory space to execute at the cost of more time.
1.7 ABSTRACT DATA TYPES
An Abstract Data Type (ADT) is a popular mathematical model of data objects that defines a data type along with various functions that operate on these objects. To understand the meaning of an abstract data type, we can break the term into two parts, that is, data type
and abstract.
The data type of a variable is a collection of values that a variable can take. There are various data types in Python (such as integer, float, character, long, and double). When we