0% found this document useful (0 votes)
3 views8 pages

Chapter 1 Data Structure-1

Uploaded by

tilayefkadie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views8 pages

Chapter 1 Data Structure-1

Uploaded by

tilayefkadie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Chapter 1: Introduction to Data Structures and Algorithms

1.1. Introduction to Data Structures


In the modern world, data and its information have significance, and different
implementations take place to store it in different ways. Data is simply a
collection of facts and figures, or you can say that data is a set of values or
values in a particular format that refers to a single set of item values. The
data items are then classified into sub-items, the group of items that are not
called the simple primary form of the item.
Let's take an example where a student's name can be broken down into
three sub-items: first, middle, and last. But, an ID assigned to a student will
usually be considered a single item.

The example mentioned above, such as ID, Age, Gender, First, Middle, Last,
Street, Area, etc., are elementary data items, whereas the Name and the
Address are group data items.

What is Data Structure?


In the context of computers, the data structure is a specific way of storing
and organizing data in the computer's memory so that these data can be
easily retrieved and efficiently used when needed later. The data can be
managed in many ways, such as a logical or mathematical model for a
particular data organization called a data structure.
The variety of a specific data model depends on two factors:

 First, it must be loaded enough into the structure to reflect the actual
relationship of the data with a real-world object.
 Second, the formation should be so simple that one can efficiently
process the data whenever necessary.

Categories of Data Structure


Data structures can be subdivided into two major types:

 Linear Data Structure


 Non-linear Data Structure

Linear Data Structure

PRepared by: Eyoas A. 1


A data structure is said to be linear if its elements combine to form any
specific order. There are two techniques for representing such linear
structure within memory.

 The first way is to provide a linear relationship between all the


elements represented using a linear memory location. These linear
structures are called arrays.
 The second technique provides a linear relationship between all the
elements represented using the concept of pointers or links. These
linear structures are called linked lists.
The typical examples of the linear data structure are:

 Arrays
 Queues
 Stacks
 Linked lists

Non-linear Data Structure


This structure mainly represents data with a hierarchical relationship
between different elements.
Examples of Non-Linear Data Structures are listed below:

 Graphs
 Family of trees and
 Table of contents
Tree: In this case, the data often has a hierarchical relationship between the
different elements. The data structure representing this relationship is called
a rooted tree graph or tree.
Graph: In this case, the data sometimes has relationships between pair
elements, which do not necessarily follow a hierarchical structure. Such a
data structure is called a graph.
You will learn more about Linear and Non-linear Data Structures in
subsequent lessons.
1.1.1 Abstract data Types
Abstract Data type (ADT) is a type (or class) for objects whose behavior is
defined by a set of values and a set of operations. The definition of ADT
only mentions what operations are to be performed but not how these
operations will be implemented. It does not specify how data will be
organized in memory and what algorithms will be used for implementing
the operations. It is called “abstract” because it gives an implementation-
independent view.

Features of ADT:

PRepared by: Eyoas A. 2


Abstract data types (ADTs) are a way of encapsulating data and operations
on that data into a single unit. Some of the key features of ADTs include:
 Abstraction: The user does not need to know the implementation of the
data structure only essentials are provided.
 Better Conceptualization: ADT gives us a better conceptualization of
the real world.
 Robust: The program is robust and has the ability to catch errors.
 Encapsulation: ADTs hide the internal details of the data and provide a
public interface for users to interact with the data. This allows for easier
maintenance and modification of the data structure.
 Data Abstraction: ADTs provide a level of abstraction from the
implementation details of the data. Users only need to know the
operations that can be performed on the data, not how those operations
are implemented.
 Data Structure Independence: ADTs can be implemented using
different data structures, such as arrays or linked lists, without affecting
the functionality of the ADT.
 Information Hiding: ADTs can protect the integrity of the data by
allowing access only to authorized users and operations. This helps
prevent errors and misuse of the data.
 Modularity: ADTs can be combined with other ADTs to form larger,
more complex data structures. This allows for greater flexibility and
modularity in programming.
Overall, ADTs provide a powerful tool for organizing and manipulating data
in a structured and efficient manner.
Abstract data types (ADTs) have several advantages and disadvantages
that should be considered when deciding to use them in software
development. Here are some of the main advantages and disadvantages of
using ADTs:
Advantages:
 Encapsulation: ADTs provide a way to encapsulate data and operations
into a single unit, making it easier to manage and modify the data
structure.
 Abstraction: ADTs allow users to work with data structures without
having to know the implementation details, which can simplify
programming and reduce errors.
 Data Structure Independence: ADTs can be implemented using
different data structures, which can make it easier to adapt to changing
needs and requirements.
 Information Hiding: ADTs can protect the integrity of data by
controlling access and preventing unauthorized modifications.
 Modularity: ADTs can be combined with other ADTs to form more
complex data structures, which can increase flexibility and modularity in
programming.

PRepared by: Eyoas A. 3


Disadvantages:
 Overhead: Implementing ADTs can add overhead in terms of memory
and processing, which can affect performance.
 Complexity: ADTs can be complex to implement, especially for large
and complex data structures.
 Learning Curve: Using ADTs requires knowledge of their
implementation and usage, which can take time and effort to learn.
 Limited Flexibility: Some ADTs may be limited in their functionality or
may not be suitable for all types of data structures.
 Cost: Implementing ADTs may require additional resources and
investment, which can increase the cost of development.
Overall, the advantages of ADTs often outweigh the disadvantages, and
they are widely used in software development to manage and manipulate
data in a structured and efficient way. However, it is important to consider
the specific needs and requirements of a project when deciding whether to
use ADTs.

1.1.2 Abstraction
Data abstraction is the reduction of a particular body of data to a simplified
representation of the whole. Abstraction, in general, is the process of
removing characteristics from something to reduce it to a set of essential
elements. To this end, data abstraction creates a simplified representation of
the underlying data, while hiding its complexities and associated operations.
In computing, data abstraction is commonly used in object-oriented
programming (OOP) and when working with a database management system
1.2. Algorithms

What is an Algorithm?

An algorithm is a process or a set of rules required to perform calculations or


some other problem-solving operations especially by a computer. The formal
definition of an algorithm is that it contains the finite set of instructions
which are being carried in a specific order to perform the specific task. It is
not the complete program or code; it is just a solution (logic) of a problem,
which can be represented either as an informal description using a Flowchart
or Pseudocode.

Characteristics of an Algorithm

The following are the characteristics of an algorithm:

o Input: An algorithm has some input values. We can pass 0 or some input
value to an algorithm.
o Output: We will get 1 or more output at the end of an algorithm.

PRepared by: Eyoas A. 4


o Unambiguity: An algorithm should be unambiguous which means that the
instructions in an algorithm should be clear and simple.
o Finiteness: An algorithm should have finiteness. Here, finiteness means that
the algorithm should contain a limited number of instructions, i.e., the
instructions should be countable.
o Effectiveness: An algorithm should be effective as each instruction in an
algorithm affects the overall process.
o Language independent: An algorithm must be language-independent so
that the instructions in an algorithm can be implemented in any of the
languages with the same output.

Dataflow of an Algorithm

o Problem: A problem can be a real-world problem or any instance from the


real-world problem for which we need to create a program or the set of
instructions. The set of instructions is known as an algorithm.
o Algorithm: An algorithm will be designed for a problem which is a step by
step procedure.
o Input: After designing an algorithm, the required and the desired inputs are
provided to the algorithm.
o Processing unit: The input will be given to the processing unit, and the
processing unit will produce the desired output.
o Output: The output is the outcome or the result of the program.

Why do we need Algorithms?

We need algorithms because of the following reasons:

o Scalability: It helps us to understand the scalability. When we have a big


real-world problem, we need to scale it down into small-small steps to easily
analyze the problem.
o Performance: The real-world is not easily broken down into smaller steps. If
the problem can be easily broken into smaller steps means that the problem
is feasible.

o The following are the steps required to add two numbers


entered by the user:

Step 1: Start
Step 2: Declare three variables a, b, and sum.
Step 3: Enter the values of a and b.
Step 4: Add the values of a and b and store the result in the sum variable, i.e.,
sum=a+b.

PRepared by: Eyoas A. 5


Step 5: Print sum
Step 6: Stop

Factors of an Algorithm

The following are the factors that we need to consider for designing
an algorithm:

o Modularity: If any problem is given and we can break that problem into
small-small modules or small-small steps, which is a basic definition of an
algorithm, it means that this feature has been perfectly designed for the
algorithm.
o Correctness: The correctness of an algorithm is defined as when the given
inputs produce the desired output, which means that the algorithm has been
designed algorithm. The analysis of an algorithm has been done correctly.
o Maintainability: Here, maintainability means that the algorithm should be
designed in a very simple structured way so that when we redefine the
algorithm, no major change will be done in the algorithm.
o Functionality: It considers various logical steps to solve the real-world
problem.
o Robustness: Robustness means that how an algorithm can clearly define
our problem.
o User-friendly: If the algorithm is not user-friendly, then the designer will not
be able to explain it to the programmer.
o Simplicity: If the algorithm is simple then it is easy to understand.
o Extensibility: If any other algorithm designer or programmer wants to use
your algorithm then it should be extensible.

The major categories of algorithms are given below:

o Sort: Algorithm developed for sorting the items in a certain order.


o Search: Algorithm developed for searching the items inside a data
structure.
o Delete: Algorithm developed for deleting the existing element from
the data structure.
o Insert: Algorithm developed for inserting an item inside a data
structure.
o Update: Algorithm developed for updating the existing element inside
a data structure.

Algorithm Analysis

PRepared by: Eyoas A. 6


The algorithm can be analyzed in two levels, i.e., first is before creating the
algorithm, and second is after creating the algorithm. The following are the
two analysis of an algorithm:

o Priori Analysis: Here, priori analysis is the theoretical analysis of an algorithm


which is done before implementing the algorithm. Various factors can be
considered before implementing the algorithm like processor speed, which
has no effect on the implementation part.
o Posterior Analysis: Here, posterior analysis is a practical analysis of an
algorithm. The practical analysis is achieved by implementing the algorithm
using any programming language. This analysis basically evaluate that how
much running time and space taken by the algorithm.

Algorithm Complexity

The performance of the algorithm can be measured in two factors:

o Time complexity: The time complexity of an algorithm is the amount of


time required to complete the execution. The time complexity of an algorithm
is denoted by the big O notation. Here, big O notation is the asymptotic
notation to represent the time complexity. The time complexity is mainly
calculated by counting the number of steps to finish the execution. Let's
understand the time complexity through an example.

1. sum=0;
2. // Suppose we have to calculate the sum of n numbers.
3. for i=1 to n
4. sum=sum+i;
5. // when the loop ends then sum holds the sum of the n numbers
6. return sum;

In the above code, the time complexity of the loop statement will be at least
n, and if the value of n increases, then the time complexity also increases.
While the complexity of the code, i.e., return sum will be constant as its
value is not dependent on the value of n and will provide the result in one
step only. We generally consider the worst-time complexity as it is the
maximum time taken for any given input size.

o Space complexity: An algorithm's space complexity is the amount of space


required to solve a problem and produce an output. Similar to the time
complexity, space complexity is also expressed in big O notation.

For an algorithm, the space is required for the following purposes:

1. To store program instructions


2. To store constant values

PRepared by: Eyoas A. 7


3. To store variable values
4. To track the function calls, jumping statements, etc.

Auxiliary space: The extra space required by the algorithm, excluding the
input size, is known as an auxiliary space. The space complexity considers
both the spaces, i.e., auxiliary space, and space used by the input.

So,

Space complexity = Auxiliary space + Input size.

Complexity analysis
Complexity analysis is defined as a technique to characterize the time
taken by an algorithm with respect to input size (independent from the
machine, language and compiler). It is used for evaluating the variations of
execution time on different algorithms.
What is the need for Complexity Analysis?
 Complexity Analysis determines the amount of time and space resources
required to execute it.
 It is used for comparing different algorithms on different input sizes.
 Complexity helps to determine the difficulty of a problem.
 often measured by how much time and space (memory) it takes to solve
a particular problem
How does Complexity affect any algorithm?
Time complexity of an algorithm quantifies the amount of time taken by an
algorithm to run as a function of length of the input. While, the space
omplexity of an algorithm quantifies the amount of space or memory taken
by an algorithm to run as a function of the length of the input.

PRepared by: Eyoas A. 8

You might also like