0% found this document useful (0 votes)
450 views51 pages

Chapter 01

The document provides an overview of data structures and algorithms, defining data structures as specialized formats for organizing and storing data, and algorithms as sequences of steps for processing that data. It categorizes data structures into primitive and non-primitive types, detailing various structures such as arrays, linked lists, stacks, queues, trees, and graphs, along with their applications and advantages. Additionally, it discusses operations performed on data structures, the concept of abstract data types, and characteristics of algorithms.

Uploaded by

lap use
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
450 views51 pages

Chapter 01

The document provides an overview of data structures and algorithms, defining data structures as specialized formats for organizing and storing data, and algorithms as sequences of steps for processing that data. It categorizes data structures into primitive and non-primitive types, detailing various structures such as arrays, linked lists, stacks, queues, trees, and graphs, along with their applications and advantages. Additionally, it discusses operations performed on data structures, the concept of abstract data types, and characteristics of algorithms.

Uploaded by

lap use
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Data Structure &

algorithm
Introduction
Data Structure & Algorithm
• A data structure is a specialized format for organizing,
processing, retrieving, and storing data. Data structures make it
easy for users to access and work with the data they need in
appropriate ways.
• A data structure is a method of organizing data in a virtual
system. Think of sequences of numbers, or tables of data:
these are both well-defined data structures.
• An algorithm is a sequence of steps executed by a computer
that takes an input and transforms it into a target output.
Data Types & Data Structure
•Data Type is the kind or form of a variable which is being used throughout
the program. It defines that the variable will assign the values of the given
data type only.
•Data Structure is the collection of different kinds of data.

• Data structures can be broadly classified into two categories - Primitive


and Non-Primitive.

• Non-primitive data structures can be further classified into two categories -


Linear and Non-linear.
Data structure
• A data structure is a fundamental concept in computer science
and software engineering. It refers to a way of organizing and
storing data in a computer's memory or other storage media so
that it can be efficiently accessed, manipulated, and managed.
Data structures provide a means to store and organize data in a
way that optimizes various operations, such as searching,
sorting, inserting, and deleting data.
• Data structures are essential for solving a wide range of
computational problems and are a fundamental building block
for designing algorithms and software.
Data structure
•Some common data structures include:
•Arrays: Ordered collections of elements stored in contiguous memory
locations.
•Linked Lists: A series of nodes, where each node contains data and a
reference to the next node.
•Stacks: A linear data structure that follows the Last-In-First-Out (LIFO)
principle.
•Queues: A linear data structure that follows the First-In-First-Out (FIFO)
principle.
•Trees: Hierarchical structures that consist of nodes with parent-child
relationships, such as binary trees, AVL trees, and B-trees.
Data structure
•Graphs: A set of nodes (vertices) and edges connecting them, used for
modeling complex relationships.
•Hash Tables: Key-value data structures that allow for fast retrieval of data
based on a unique key.
•Heaps: Specialized trees used for priority queue operations, like a binary
heap.
•Hash Maps: Data structures that map keys to values, providing efficient key-
based data retrieval.
• The choice of a data structure depends on the specific requirements of the
problem you are trying to solve.
Basic Terminology of Data structure
•Data: Data can be defined as an elementary value or the
collection of values, for example, student's name and its id are
the data about the student.
•Group Items: Data items which have subordinate data items are
called Group item, for example, name of a student can have first
name and the last name.
•Record: Record can be defined as the collection of various data
items, for example, if we talk about the student entity, then its
name, address, course, and marks can be grouped together to
form the record for the student.
Basic Terminology of Data structure

•File: A File is a collection of various records of one type of entity, for


example, if there are 60 employees in the class, then there will be 20
records in the related file where each record contains the data about each
employee.
•Attribute and Entity: An entity represents the class of certain objects. it
contains various attributes. Each attribute represents the particular property
of that entity.
•Field: Field is a single elementary unit of information representing the
attribute of an entity.
Goals of Data Structure
•Data structure basically implements two complementary goals.
•Correctness: Data structure is designed such that it operates correctly for
all kinds of input, which is based on the domain of interest. In other words,
correctness forms the primary goal of data structure, which always depends
on the specific problems that the data structure is intended to solve.
•Efficiency: Data structure also needs to be efficient. It should process the
data at high speed without utilizing much of the computer resources such as
memory space. In real time state, the efficiency of a data structure is an
important factor that determines the success and failure of the process.
•Need for Data Structure
 It gives different level of organization data.
 It tells how data can be stored and accessed in its elementary
level.
 Provide operation on group of data, such as adding an item,
looking up highest priority item.
 Provide a means to manage huge amount of data efficiently.
 Provide fast searching and sorting of data.
Features of Data Structure
•Some of the important features of data structures are:
•Robustness: Generally, all computer programmers wish to produce software that generates correct
output for every possible input provided to it, as well as execute efficiently on all hardware
platforms. This kind of robust software must be able to manage both valid and invalid inputs.
•Adaptability: Developing software projects such as word processors, Web browsers and Internet
search engine involves large software systems that work or execute correctly and efficiently for
many years. Moreover, software evolves due to ever changing market conditions or due to
emerging technologies.
•Reusability: Reusability and adaptability go together. It is a known fact that the programmer
requires many resources for developing any software, which makes it an expensive enterprise.
However, if the software is developed in a reusable and adaptable way, then it can be implemented
in most of the future applications. Thus, by implementing quality data structures, it is possible to
develop reusable software, which tends to be cost effective and time saving.
Classification of Data Structure
•A data structure provides a structured set of variables that are
associated with each other in different ways. It forms a basis of
programming tool that represents the relationship between data
elements and helps programmers to process the data easily. Data
structure can be classified into two categories:
•Primitive data structure
•Non-primitive data structure
Classification of Data Structure
•Array
Array, in general, refers to an orderly arrangement of data elements. Array is a type of data
structure that stores data elements in adjacent locations. Array is considered as linear data
structure that stores elements of same data types. Hence, it is also called as a linear homogenous
data structure. When we declare an array, we can assign initial values to each of its elements by
enclosing the values in braces { }.
•int Num [5] = { 26, 7, 67, 50, 66 };
•This declaration will create an array as shown below:

• 1 2 3 4 5
Num 26 7 67 50 66
Array
•Limitations:
 Arrays are of fixed size.
 Data elements are stored in contiguous memory locations which may not be always available.
 Insertion and deletion of elements can be problematic because of shifting of elements from
their positions.
However, these limitations can be solved by using linked lists.

•Applications:
 Storing list of data elements belonging to same data type
 Auxiliary storage for other data structures
 Storage of binary tree elements of fixed count
 Storage of matrices
Linked List
A linked list is a data structure in which each data element contains a
pointer or link to the next element in the list. Through linked list,
insertion and deletion of the data element is possible at all places of a
linear list.
Advantage:
•Easier to insert or delete data elements
•Disadvantage:
•Slow search operation and
•Requires more memory space
Linked List
•Applications:
 Implementing stacks, queues, binary trees, and graphs of predefined size.
 Implement dynamic memory management functions of operating system.
 Polynomial implementation for mathematical operations
 Circular linked list is used to implement OS or application functions that require round
robin execution of tasks.
 Circular linked list is used in a slide show where a user wants to go back to the first
slide after last slide is displayed.
 Doubly linked list is used in the implementation of forward and backward buttons in a
browser to move backwards and forward in the opened pages of a website.
 Circular queue is used to maintain the playing sequence of multiple players in a game.
Stacks
•A stack is a linear data structure in which insertion and deletion of elements are done at only one
end, which is known as the top of the stack. Stack is called a last-in, first-out (LIFO) structure
because the last element which is added to the stack is the first element which is deleted from the
stack.
Stacks
•Applications
 Temporary storage structure for recursive operations
 Auxiliary storage structure for nested operations, function calls, deferred/postponed functions
 Manage function calls
 Evaluation of arithmetic expressions in various programming languages
 Conversion of infix expressions into postfix expressions
 Checking syntax of expressions in a programming environment
 Matching of parenthesis
 String reversal
 In all the problems solutions based on backtracking.
 Used in depth first search in graph and tree traversal.
 Operating System functions
 UNDO and REDO functions in an editor.
Queues
A queue is a first-in, first-out (FIFO) data structure in which the element that is
inserted first is the first one to be taken out. The elements in a queue are added at
one end called the rear and removed from the other end called the front. Like
stacks, queues can be implemented by using either arrays or linked lists.
Queues

Applications
 It is used in breadth search operation in graphs.
 Job scheduler operations of OS like a print buffer queue, keyboard buffer queue to store
the keys pressed by users
 Job scheduling, CPU scheduling, Disk Scheduling
 Priority queues are used in file downloading operations in a browser
 Data transfer between peripheral devices and CPU.
 Interrupts generated by the user applications for CPU
 Interrupts generated by the user applications for CPU
 Calls handled by the customers in BPO
Trees
•A tree is a non-linear data structure in which data is organized in branches. The data elements in
tree are arranged in a sorted order. It imposes a hierarchical structure on the data elements. Figure
represents a tree which consists of 8 nodes. The root of the tree is the node 60 at the top. Node 29
and 44 are the successors of the node 60. The nodes 6, 4, 12 and 67 are the terminal nodes as they
do not have any successors.
•Advantage: Provides quick search, insert, and delete operations
•Disadvantage: Complicated deletion algorithm
Trees
Applications
 Implementing the hierarchical structures in computer systems like directory and file system.
 Implementing the navigation structure of a website.
 Code generation like Huffman’s code.
 Decision making in gaming applications.
 Implementation of priority queues for priority-based OS scheduling functions
 Parsing of expressions and statements in programming language compilers
 For storing data keys for DBMS for indexing
 Spanning trees for routing decisions in computer and communications networks
 Hash trees
 path-finding algorithm to implement in AI, robotics, and video games applications
Graphs
•A graph is also a non-linear data structure. In a tree data structure, all data elements are stored in
definite hierarchical structure. In other words, each node has only one parent node. While in
graphs, each data element is called a vertex and is connected to many other vertexes through
connections called edges. Thus, a graph is considered as a mathematical structure, which is
composed of a set of vertexes and a set of edges. Figure shows a graph with six nodes A, B, C, D, E, F
and seven edges [A, B], [A, C], [A, D], [B, C], [C, F], [D, F] and [D, E].
Graphs
•Advantage: Best models real-world situations
•Disadvantage: Some algorithms are slow and very complex

•Applications
• Representing networks and routes in communication, transportation and travel applications
• Routes in GPS
• Interconnections in social networks and other network-based applications
• Mapping applications
• Ecommerce applications to present user preferences
• Utility networks to identify the problems posed to municipal or local corporations
• Resource utilization and availability in an organization
• Document link map of a website to display connectivity between pages through hyperlinks
• Robotic motion and neural networks
Static & Dynamic Data Structure
• In Static data structure the size of the structure is fixed. The content of the data
structure can be modified but without changing the memory space allocated to
it.
• Example of Static Data Structures: Array
Static & Dynamic Data Structure
• In Dynamic data structure the size of the structure in not fixed and can be modified during the
operations performed on it. Dynamic data structures are designed to facilitate change of data
structures in the run time.
• Example of Dynamic Data Structures: Linked List
Static & Dynamic Data Structure

Data Structure Static Data structure has fixed memory size whereas in Dynamic
Data Structure, the size can be randomly updated during run time which may be
considered efficient with respect to memory complexity of the code. Static Data
Structure provides more easier access to elements with respect to dynamic data
structure. Unlike static data structures, dynamic data structures are flexible.
Operations in Data Structure
•Traversing
•It means to access each data item exactly once so that it can be processed. For
example, to print the names of all the students in a class.
•Searching
•It is used to find the location of one or more data items that satisfy the given
constraint. Such a data item may or may not be present in the given collection of
data items. For example, to find the names of all the students who secured 100
marks in mathematics.
•Inserting
•It is used to add new data items to the given list of data items. For example, to add
the details of a new student who has recently joined the course.
Operations in Data Structure
•Deleting
•It means to remove (delete) a particular data item from the given collection of data
items. For example, to delete the name of a student who has left the course.
•Sorting
•Data items can be arranged in some order like ascending order or descending order
depending on the type of application. For example, arranging the names of students
in a class in an alphabetical order, or calculating the top three winners by arranging
the participants’ scores in descending order and then extracting the top three.
•Merging
•Lists of two sorted data items can be combined to form a single list of sorted data
items.
Abstract Data Type
•The word “abstract” refers to the fact that the data and the basic
operations defined on it are being studied independently of how they
are implemented. It involves what can be done with the data, not how
has to be done.
•An implementation of ADT consists of storage structures to store the
data items and algorithms for basic operation. All the data structures
i.e., array, linked list, stack, queue etc. are examples of ADT.
Abstract Data Type
•In the real world, programs evolve as a result of new requirements or
constraints, so a modification to a program commonly requires a change in
one or more of its data structures. For example, if you want to add a new
field to a student’s record to keep track of more information about each
student, then it will be better to replace an array with a linked structure to
improve the program’s efficiency. In such a scenario, rewriting every
procedure that uses the changed structure is not desirable. Therefore, a
better alternative is to separate the use of a data structure from the details of
its implementation. This is the principle underlying the use of abstract data
types.
Algorithm
•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 created independent of underlying 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 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.
Algorithm
Characteristics of an Algorithm
 Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its steps should be
clear in all aspects and must lead to only one meaning.
 Well-Defined Inputs: If an algorithm says to take inputs, it should be well defined inputs.
 Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be
well-defined as well.
 Finite-ness: The algorithm must be finite, i.e., it should not end up in an infinite loops or similar.
 Feasible: The algorithm must be simple, generic and practical, such that it can be executed upon will
the available resources. It must not contain some future technology, or anything.
 Language Independent: The Algorithm designed must be language independent, i.e., it must be just
plain instructions that can be implemented in any language, and yet the output will be same, as
expected.
Algorithm
Advantages of Algorithms
 It is easy to understand.
 Algorithm is a step-wise representation of a solution to a given problem.
 In Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual program.
•Disadvantages of Algorithms:
 Writing an algorithm takes a long time so it is time-consuming.
 Branching and Looping statements are difficult to show in Algorithms.
Different Approach to Design an
Algorithm
• Top-Down Approach: A top-down approach starts with identifying
major components of system or program decomposing them into
their lower level components & iterating until desired level of module
complexity is achieved . In this we start with topmost module &
incrementally add modules that is calls.
• Bottom-Up Approach: A bottom-up approach starts with designing
most basic or primitive component & proceeds to higher level
components. Starting from very bottom , operations that provide
layer of abstraction are implemented
Complexity of Algorithm
•An algorithm's complexity is a measure of the amount of data that it must process to be efficient.
•Complexity in algorithms refers to the number of resources (such as time or memory) required to
solve a problem or perform a task. The most common measure of complexity is time complexity, which
refers to the amount of time an algorithm takes to produce a result as a function of the size of the
input.
•Suppose X is an algorithm and n are the size of input data, the time and space used by the algorithm
X are the two main factors, which decide the efficiency of X.
 Time Factor − Time is measured by counting the number of key operations such as comparisons in
the sorting algorithm.
 Space Factor − Space is measured by counting the maximum memory space required by the
algorithm.
•The complexity of an algorithm f(n) gives the running time and/or the storage space required by the
algorithm in terms of n as the size of input data.
Complexity of Algorithm
•Space Complexity
•Space complexity of an algorithm represents the amount of memory space required by the algorithm
in its life cycle. The space required by an algorithm is equal to the sum of Algorithm: SUM(A, B) Step 1
- START Step 2 - C ← A + B + 10 Step 3 - Stop the following two components –
 A fixed part that is a space required to store certain data and variables, that are independent of the
size of the problem. For example, simple variables and constants used, program size, etc.
 A variable part is a space required by variables, whose size depends on the size of the problem. For
example, dynamic memory allocation, recursion stack space, etc.
•Space complexity S(P) of any algorithm P is
•S(P) = C + SP(I),
•where C is the fixed part and S(I) is the variable part of the algorithm, which depends on instance
characteristic I.
•Following is a simple example that tries to explain the concept − Here we have three variables A, B,
and C and one constant. Hence S(P) = 1 + 3. Now, space depends on data types of given variables
and constant types and it will be multiplied accordingly.
Complexity of Algorithm
•Time Complexity
•Time complexity of an algorithm represents the amount of time required by
the algorithm to run to completion. Time requirements can be defined as a
numerical function T(n), where T(n) can be measured as the number of
steps, provided each step consumes constant time.
• For example, addition of two n-bit integers takes n steps. Consequently,
the total computational time is T(n) = c ∗ n, where c is the time taken for
the addition of two bits. Here, we observe that T(n) grows linearly a
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 or Performance or Asymptotic 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 or Performance Measurement − 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.
Types of algorithm analysis
• Best Case Analysis
• If an algorithm takes the least amount of time to execute a specific
set of input, then it is called the best case time complexity.
• Average Case Analysis
• If the time complexity of an algorithm for certain sets of inputs are
on an average, then such a time complexity is called average case
time complexity.
• Worst Case Analysis
• If an algorithm takes maximum amount of time to execute for a
specific set of input, then it is called the worst case time complexity.
Mathematical notation
• The mathematical notations use symbols or symbolic
expressions, which have a precise semantic meaning.
• Asymptotic Notations
• The efficiency of each algorithm can be checked by computing its
time complexity. The asymptotic notations help to represent the
time complexity in a shorthand way. It can generally be
represented as the fastest possible, slowest possible or average
possible.
• The notations such as O (Big-O), Ώ (Omega), and θ (Theta) are
called as asymptotic notations. These are the mathematical
notations that are used in three different cases of time complexity.
Big O Notation
• In computer science, big O notation is used to classify
algorithms according to how their run time or space
requirements grow as the input size grows.
• Big O notation is the method used to express the upper bound
of the running time of an algorithm. It is used to describe the
performance or time complexity of the algorithm. Big O notation
specifically describes the worst-case scenario and can be used
to describe the execution time required or the space used by
the algorithm.
Table 01 gives some names and examples of the common orders
used to describe functions. These orders are ranked from top to
bottom.
Common Orders
Time Complexity Examples
O(1) Constant Adding to the front of a linked list
O(log n) Logarithmic Finding an entry in a sorted array
O(n) Linear Finding an entry in an unsorted array
O(n log n) Linearithmic Sorting ‘n’ items by ‘divide-and-conquer’
O(n2) Quadratic Shortest path between two nodes in a graph
O(n3) Cubic Simultaneous linear equations
O(2n) Exponential The Towers of Hanoi problem
Big O Notation

• Big-O notation is generally used to express an ordering property


among the functions. This notation helps in calculating the maximum
amount of time taken by an algorithm to compute a problem. Big-O is
defined as:
• f(n) ≤ c ∗ g(n)
• where, n can be any number of inputs or outputs and f(n) as well as
g(n) are two non-negative functions. These functions are true only if
there is a constant c and a non-negative integer n0 such that, n ≥ n0.
Big O Notation

• Consider function f(n) = 2(n)+2 and g(n) = n2.


• We need to find the constant c such that f(n) ≤ c ∗ g(n).
• Let n = 1, then f(n) = 2(n)+2 = 2(1)+2 = 4
g(n) = n2 = 12 = 1
Here, f(n)>g(n)
• Let n = 2, then f(n) = 2(n)+2 = 2(2)+2 = 6
g(n) = n2 = 22 = 4
Here, f(n)>g(n)
• Let n = 3, then f(n) = 2(n)+2 = 2(3)+2 = 8
g(n) = n2 = 32 = 9
Here, f(n)<g(n)
Limitations of Big O Notation
• Many algorithms are simply too hard to analyze mathematically.
• There may not be sufficient information to calculate the
behavior of the algorithm in the average case.
• Big O analysis only tells us how the algorithm grows with the
size of the problem, not how efficient it is, as it does not
consider the programming effort.
• It ignores important constants. For example, if one algorithm
takes O(n2 ) time to execute and the other takes O(100000n2 )
time to execute, then as per Big O, both algorithm have equal
time complexity. In real-time systems, this may be a serious
consideration.
Omega Notation
• Omega describes the manner in which an algorithm performs in the
best case time complexity. This notation provides the minimum
amount of time taken by an algorithm to compute a problem. Thus, it
is considered that omega gives the "lower bound" of the algorithm's
run-time. Omega is defined as:
• f(n) ≥ c ∗ g(n)
• Where, n is any number of inputs or outputs and f(n) and g(n) are two
non-negative functions. These functions are true only if there is a
constant c and a non-negative integer n0 such that n>n0.
Omega Notation
• Consider function f(n) = 2n2+5 and g(n) = 7n.
• We need to find the constant c such that f(n) ≥ c ∗ g(n).
• Let n = 0, then f(n) = 2n2+5 = 2(0)2+5 = 5
g(n) = 7(n) = 7(0) = 0
Here, f(n)>g(n)
• Let n = 1, then f(n) = 2n2+5 = 2(1)2+5 = 7
g(n) = 7(n) = 7(1) = 7
Here, f(n)=g(n)
• Let n = 2,
• then f(n) = 2n2+5 = 2(2)2+5 = 13
g(n) = 7(n) = 7(2) = 14
Here, f(n)<g(n)
Theta Notation
• Theta notation is used when the upper bound and lower bound of an
algorithm are in the same order of magnitude. Theta can be defined
as:
• c1 ∗ g(n) ≤ f(n) ≤ c2 ∗ g(n) for all n>n0
• Where, n is any number of inputs or outputs and f(n) and g(n) are two
non negative functions. These functions are true only if there are two
constants namely, c1, c2, and a non-negative integer n0.
Theta Notation
• Consider function f(n) = 4n + 3 and g(n) = 4n for all n ≥ 3; and
f(n) = 4n + 3 and g(n) = 5n for all n ≥ 3.
• Then the result of the function will be:
• Let n = 3
• f(n) = 4n + 3 = 4(3)+3 = 15
• g(n) = 4n =4(3) = 12 and
• f(n) = 4n + 3 = 4(3)+3 = 15
• g(n) = 5n =5(3) = 15 and
• here, c1 is 4, c2 is 5 and n0 is 3

You might also like