DSA Lecture 01
DSA Lecture 01
Algorithms
Lecture 1
Introduction to Data Structures
and Algorithms
Dr. Fazal-e-Malik
Assistant Professor , Computer Science Department,
Iqra National University, Peshawar.
Phone: 0346-9138373
Email: [email protected]
1
Outline
Overview Data Structures and Algorithms
Data Structures – Overview
Characteristics of a Data Structure
Need for Data Structure
Execution Time Cases
Basic Terminology
Algorithms Overview
categories of algorithms on Data Structure point of view
Characteristics of an Algorithm
How to Write an Algorithm?
Example of an Algorithm
Development Of An Algorithm
Steps of an Algorithm
Algorithm Analysis
2
Data Structures and Algorithms
Data Structure
A data structure is an arrangement of data in a computer’s
memory (or sometimes on a disk).
Data structures include linked lists, stacks, binary trees, and
hash tables, among others.
Algorithms
Algorithms is a step by step procedure to manipulate the data
in these structures in various ways, such as inserting a new
data item, searching for a particular item, or sorting the
items.
An algorithm as a recipe: a list of detailed instructions for
carrying out an activity.
3
Overview of Data Structures and
Algorithms
Data structures and algorithms are fundamental concepts in computer science
and play a crucial role in the efficient design and manipulation of information in
software development. Here's an overview of these concepts:
Data Structures:
Definition: Data structures are specialized formats for organizing, storing, and
managing data to perform efficient operations such as insertion, deletion, and
retrieval.
Types:
Array: A collection of elements identified by index or key.
Linked List: A linear collection of nodes where each node points to the next.
Stack: A Last-In-First-Out (LIFO) structure for data.
Queue: A First-In-First-Out (FIFO) structure.
Tree: A hierarchical structure with a root and child nodes.
Graph: A collection of nodes with edges connecting them.
Operations:
Insertion: Adding new data.
Deletion: Removing data.
Traversal: Visiting each element.
Searching: Finding specific data.
4
Overview of Data Structures and
Algorithms
Algorithms:
Definition: Algorithms are step-by-step procedures or formulas to solve problems or
perform specific tasks.
Characteristics:
Input: Algorithms take input, which is transformed into an output.
Finiteness: Must terminate after a finite number of steps.
Definiteness: Each step must be clear and unambiguous.
Effectiveness: All operations must be basic enough to be performed by a person with limited
instructions.
Types:
Searching Algorithms: Locate an element in a data structure.
Sorting Algorithms: Arrange elements in a specified order.
Graph Algorithms: Manipulate data organized in a graph structure.
Complexity Analysis:
Time Complexity: Analyzing the amount of time an algorithm takes.
Space Complexity: Analyzing the amount of memory an algorithm uses.
Understanding data structures and algorithms is crucial for writing efficient and
optimized code in software development.
The choice of the appropriate data structure and algorithm significantly impacts the
performance and efficiency of software applications
5
Data Structures - Overview
Characteristics of a Data Structure
Correctness −
Data structure implementation should implement
its interface correctly.
Time Complexity −
Running time or the execution time of operations
of data structure must be as small as possible.
Space Complexity −
Memory usage of a data structure operation
should be as little as possible.
6
Data Structures - Overview
Need for Data Structure
As applications are getting complex and data rich, there are three common
problems that applications face now-a-days.
Data Search −
6
Consider an inventory of 1million(10 ) items of a store.
6
If the application is to search an item, it has to search an item in 1 million(10 ) items
every time slowing down the search.
As data grows, search will become slower.
Processor speed −
Processor speed although being very high, falls limited if the data grows to billion records.
Multiple requests −
As thousands of users can search data simultaneously on a web server, even the fast
server fails while searching the data.
7
Data Structures - Overview
Execution Time Cases
There are three cases which are usually used to compare various data
structure's execution time in a relative manner.
Worst Case −
This is the scenario where a particular data structure operation takes maximum
time it can take.
If an operation's worst case time is ƒ(n) then this operation will not take more
than ƒ(n) time where ƒ(n) represents function of n.
Average Case −
This is the scenario depicting the average execution time of an operation of a
data structure.
If an operation takes ƒ(n) time in execution, then m operations will take mƒ(n)
time.
Best Case −
This is the scenario depicting the least possible execution time of an operation of
a data structure.
If an operation takes ƒ(n) time in execution, then the actual operation may take
time as the random number which would be maximum as ƒ(n).
8
Data Structures - Overview
Basic Terminology
Data −
Data are values or set of values.
Data Item −
Data item refers to single unit of values.
ID, First Name etc.
Group Items −
Data items that are divided into sub items are called as Group Items.
Name
Elementary Items −
Data items that cannot be divided are called as Elementary Items.
DOB
Attribute and Entity −
An entity is that which contains certain attributes or properties, which may be assigned values.
ID, First Name etc.
Entity Set −
Entities of similar attributes form an entity set.
Field −
Field is a single elementary unit of information representing an attribute of an entity.
Record −
Record is a collection of field values of a given entity.
File −
File is a collection of records of the entities in a given entity set.
9
Categories of Data Structure
The data structure can be classified in to major types:
1. Linear Data Structure
2. Non-linear Data Structure
10
Algorithms Overview
Algorithm is a step-by-step procedure, which defines a set of
instructions to be executed in a certain order to get the desired
output.
Algorithms are generally independent of the computer languages,
i.e. an algorithm can be implemented in more than one
programming language.
From the data structure point of view, following are some
important categories (Operation) of algorithms −
Search −
Algorithm to search an item in a data structure.
Sort −
Algorithm to sort items in a certain order.
Insert −
Algorithm to insert item in a data structure.
Update −
Algorithm to update an existing item in a data structure.
Delete −
Algorithm to delete an existing item from a data structure.
11
Algorithms Overview
Characteristics of an Algorithm
An algorithm should have the following characteristics
−
Unambiguous −
Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear
and must lead to only one meaning.
Input −
An algorithm should have 0 or more well-defined inputs.
Output −
An algorithm should have 1 or more well-defined outputs,
and should match the desired output.
Finiteness −
Algorithms must terminate after a finite number of steps.
12
Algorithms Overview
Effectiveness -
Each step must be effective. i.e.; it should be easily convertible
into program statement and can be performed exactly in a
finite amount of time.
Feasibility (Achievability)−
Should be feasible with the available resources
Independent −
An algorithm should have step-by-step directions, which
should be independent of any programming code.
Generality -
Algorithm should be complete in itself, so that it can be used to
solve all problems of given type for any input data.
14
Example
To find the average of 3 numbers, the algorithm is as shown below.
In design and analysis of algorithms, usually the second method is used to describe an
algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanted
definitions. He can observe what operations are being used and how the process is flowing.
Writing step numbers, is optional.
An algorithm is designed to get a solution of a given problem. A problem can be solved in
more than one ways.
Hence, many solution algorithms can be derived for a given problem based on analysis of
those proposed solution algorithms and then implement the best suitable solution.
15
Development Of An Algorithm
The steps involved in the development of
an algorithm are as follows:
1. Specifying the problem statement.
2. Designing an algorithm.
3. Coding.
4. Debugging
5. Testing and Validating
6. Documentation and Maintenance
16
Development Of An Algorithm
1. Specifying the problem statement:
The problem which has to be implemented in to a program
must be thoroughly understood before the program is
written.
Problem must be analyzed to determine the input and
output requirements of the program.
2. Designing an Algorithm:
Once the problem is cleared then a solution method for
solving the problem has to be analyzed.
There may be several methods available for obtaining the
required solution.
The best suitable method is designing an Algorithm.
To improve the clarity and understandability of
the program flowcharts are drawn using algorithms.
17
Development Of An Algorithm
3. Coding:
The actual program is written in the required programming language with the help of
information depicted in flowcharts and algorithms.
4. Debugging:
There is a possibility of occurrence of errors in program. These errors must be removed
for proper working of programs. The process of checking the errors in the
program is known as ‗Debugging‘.
There are three types of errors in the program.
Syntactic Errors:
They occur due to wrong usage of syntax for the statements.
Ex: x=a*%b
Here two operators are used in between two operands.
Runtime Errors :
They are determined at the execution time of the program
Ex: Divide by zero
Range out of bounds.
Logical Errors :
They occur due to incorrect usage of instructions in the program.
They are neither displayed during compilation or execution nor cause any
obstruction to the program execution. They only cause incorrect outputs.
18
Development Of An Algorithm
5. Testing and Validating:
Once the program is written , it must be tested and then validated. i.e., to
check whether the program is producing correct results or not for
different values of input.
19
Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages,
before implementation and after implementation. They are the
following −
A Priori Analysis − This is a theoretical analysis of an algorithm.
Efficiency of an algorithm is measured by assuming that all other
factors, for example, processor speed, are constant and have no
effect on the implementation.
A Posterior Analysis − This is an empirical analysis of an
algorithm. The selected algorithm is implemented using
programming language. This is then executed on target computer
machine. In this analysis, actual statistics like running time and
space required, are collected.
20