CH1 1
CH1 1
INTRODUCTION TO DATA
STRUCTURE AND
ALGORITHM
BIBHA STHAPIT
ASST. PROFESSOR
IoE, PULCHOWK CAMPUS
SYLLABUS OUTLINE
• 1. Concept of Data Structure
• 2. The stack and queue
• 3. List
• 4. Linked List
• 5. Recursion
Chapter 1
• 6. Trees
• 7. Sorting
• 8. Searching
• 9. Growth functions
• 10. Graphs
Chapter 1
• To understand the relevant concepts and decisions to apply
the best fit data structure for the particular requirements.
Some mysterious
Input Output
processing
Chapter 1
Example :
• Data structure for storing data of students:-
• Arrays , Linked Lists
• Issues
• Space needed
• Operations efficiency (Time required to complete operations) 4
• Retrieval, Insertion, Deletion
Data Structure
• Data structures let the input and output be represented in a way
that can be handled efficiently and effectively.
array
Chapter 1
Linked list
queue
5
tree stack
Data Structure
• DEFINITION:
• Particular way of storing and organizing data in a computer so
that it can be used efficiently
• Data :
Chapter 1
• A piece of information
• Taken as input, processed within a computer, and provided as
output as information
• Can be “atomic” or “composite”
• Atomic data takes single value like integer 123
• Composite data which can be further broken down to subfields
like student record consists of roll_no, name, faculty, year etc.
6
Data Structure
• Data type:
• Kind of data that a variable can store.
• Built-in data types are system defined data types like int, float,
char.
• User-defined data type are created according to the need of the
Chapter 1
program like structure, union, class.
Chapter 1
• Linear and non-linear data structure
• In linear, data is stored in consecutive memory i.e. every element
has unique predecessor and unique successor. E.g. Array, linked
list, stack, queue
• In non-linear, data is stored in non-consecutive memory. Eg. Tree,
graph
8
Types of Data Structure
• Primitive and non-primitive data structure
• Primitive data structures are generally primary or built-in types and
are directly operated upon by machine instructions. E.g. integers,
floating points, characters
• Non-primitive emphasizes on structuring of homogeneous or
Chapter 1
heterogeneous data. E.g. Array, lists, linked list, tree
9
Types of Data Structure
• Sequential and direct/random access data structure
• In sequential, we must access preceding (n-1) data to access nth data.
E.g. Linked list
• In direct access, we can directly access nth element. E.g. array
Chapter 1
10
Types of Data Structure
Data Structure
Chapter 1
Non
Linear
Linear
Tree
Stack
Graph
Queue
11
Linked
lists
Overview of different Data Structures
• Arrays:
• Collection of similar data elements
• Elements are stored in consecutive memory.
• Have random access to data
• Application:
Chapter 1
• Used in program that require storing
large collection of similar data
12
Overview of different Data Structures
• Linked list
• It provides a flexible storage system by dynamically assigning the
required memory.
• Linked lists are special list of some data elements linked to one
another.
Chapter 1
• Application:
• Used wherever dynamic memory allocation is needed
13
Overview of different Data Structures
• Stack:
• List of elements with insertions and deletions permitted at one
end only – called stack top
• A stack data structure exhibits the LIFO (last in first out) property
• Application:
Chapter 1
• System processes such as compilation and program control
14
Overview of different Data Structures
• Queue
• A list of elements with insertions permitted at one end—called
the rear, and deletions permitted from the other end—called the
front
• a queue data structure exhibits the FIFO (first in first out)
Chapter 1
property.
• Application:
• Used in CPU scheduling, resource sharing
15
Overview of different Data Structures
• Trees:
• Non- linear data structure
• Arranges its nodes in the form of hierarchal tree structure
• Application:
• Used for storing hierarchical data, implementing search trees,
Chapter 1
maintaining sorted data.
16
Overview of different Data Structures
• Graphs:
• A graph is a structure made of two components: a set of vertices
V, and a set of edges E
• Application:
• Modelling of networking systems, costing of network paths
Chapter 1
17
Operations on Data Structures
• Traversing
• Accessing each record of data structure exactly once
• Searching
• Identifying location of record that consists specific key value
• Inserting
Chapter 1
• Adding new record
• Deleting
• Removing existing record
• Sorting
• Arranging data in specific order
• Merging
• Combining two different sorted records to produce single sorted data set. 18
Algorithm
• Once the data structure of particular application is chosen, an
algorithm must be developed to manipulate related data
items stored in it.
Chapter 1
• Definition:
• It is step-by-step finite set of instructions to solve a well-defined
computational problem.
• Sum of Two Numbers:
• step 1 − START ADD
• step 2 − get values of a & b
• step 3 − c ← a + b
• step 4 − display c 19
• step 5 − STOP
Algorithm
• Every algorithm must satisfy following criteria –
• input: there are zero or more quantities which are externally supplied;
Chapter 1
• definiteness: each instruction must be clear and unambiguous;
Chapter 1
• Bottom up approach
• designing the most basic or concrete modules and then proceed
towards designing higher level modules.
• The higher level modules are implemented by using the operations
performed by lower level modules.
• Thus, in this approach sub-modules are grouped together to form a
higher level module.This process is repeated until the design of the
complete algorithm is obtained 21
Algorithm design techniques
• Brute Force
• goes through all the possible solutions one after another until you find
the optimum solution.
• Linear Search algorithm
Chapter 1
• Divide and conquer
• This algorithm solves a problem by dividing the original problem into
smaller chunks which are similar sub-problems.
• The solutions of these smaller sub problems are later combined to get
the solution of the given original problem.
• Quick sort and merge sort
22
Algorithm design techniques
• Greedy
• works by taking a decision that appears the best at the moment,
without thinking about the future.
• A greedy algorithm solution isn't necessarily an overall optimal solution
since it only goes from one best solution to the next. Additionally, there
is no backtracking involved if it chooses the wrong option or step.
Chapter 1
• Djikstra’s algorithm, Prim’s algorithm
Chapter 1
algorithm
• Simple Recursive
• Process in which a problem is defined in terms of itself.
• The problem is solved by repeatedly breaking it into smaller problems,
which are similar in nature to the original problems.
• Fibonacci series, Tower of Hanoi
24
Algorithm design techniques
• Backtracking
• finds all the possible combinations of a solution and evaluates if it isn't
optimal. If it isn't, the algorithm backtracks and starts evaluating other
solutions.
• Backtracking algorithms share a common approach with the brute force
algorithm design technique but are much faster than brute-force
Chapter 1
algorithms.
• Depth-first recursive search in a tree
25
Algorithm design techniques
• Dynamic programming
• solves problems that have overlapping sub-problems. Therefore, they are
well-suited for problems where certain sub-problems get solved repeatedly.
• optimizes the solution by storing the answers to sub-problems in an
optimal structure and retrieving them when needed.
• for example, generating a Fibonacci sequence. If we found the 4th number,
Chapter 1
we must have found all the ones before. Therefore, they are handy for
finding the 5th number.
26
Analysis of algorithm
• The two factors of Algorithm Complexity are:
• Time Factor:
• Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
• Space Factor:
Chapter 1
• Space is measured by counting the maximum memory space
required by the algorithm.
27
Analysis of algorithm
• Therefore the complexity of an algorithm can be divided into two types:
• Space Complexity:
• Space complexity of an algorithm refers to the amount of memory
that this algorithm requires to execute and get the result. This can be
Chapter 1
for inputs, temporary operations, or outputs.
• Generally, the space needed by a program depends on the following
two parts:
• Fixed part: It varies from problem to problem. It includes the space
needed for storing instructions, constants, variables, and structured
variables (like arrays and structures).
• Variable part: It varies from program to program. It includes the space
needed for recursion stack, and for structured variables that are allocated
space dynamically during the runtime of a program. 28
Analysis of algorithm
• Time Complexity:
• Time complexity of an algorithm refers to the amount of time that this
algorithm requires to execute and get the result. This can be for normal
operations, conditional if-else statements, loop statements, etc.
• It is basically the running time of a program as a function of the input
size. In other words, the number of machine instructions which a
Chapter 1
program executes is called its time complexity. This number is primarily
dependent on the size of the program’s input and the algorithm used.
29
Analysis of algorithm
• Worst case running time
• It denotes the behaviour of an algorithm with respect to 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. Therefore, having the knowledge of worst-
case running time gives us an assurance that the algorithm will never go
beyond this time limit.
Chapter 1
• Average case running time
• It is an estimate of the running time for an ‘average’ input. It specifies the
expected behaviour of the algorithm when the input is randomly drawn
from a given distribution. Average-case running time assumes that all
inputs of a given size are equally likely
Chapter 1
• An abstract data type (ADT) is composed of
• A collection of data
• A set of operations on that data
Chapter 1
32
Abstract Data Type (ADT)
• To implement an ADT, you need to choose:
• A data representation (VALUE DEFINITION)
• must be able to represent all necessary values of the ADT
• It consists:
• definition clause (required)
Chapter 1
• condition clause (optional)
Chapter 1
element_type A [max_size]
index_type i
//condition clause
index_type = = integer
34
Abstract Data Type (ADT)
//operator definitions
abstract <element_type> Extract (A, i) //written as A[i]
integer i
Array A
//precondition
0 <= i <max_size
Chapter 1
//postcondition
Extract = = A[i]
Chapter 1
36
THANK YOU!
Chapter 1
37