0% found this document useful (0 votes)
4 views

Data Structure Unit I

The document provides an overview of data structures and algorithms, explaining key concepts such as data organization, characteristics of data structures, and the importance of algorithms in processing data. It discusses various data structures like arrays, stacks, and queues, detailing their operations, characteristics, and applications. Additionally, it covers algorithm analysis, including time and space complexity, and introduces different notations for arithmetic expressions.

Uploaded by

ATOMIC GAMER
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structure Unit I

The document provides an overview of data structures and algorithms, explaining key concepts such as data organization, characteristics of data structures, and the importance of algorithms in processing data. It discusses various data structures like arrays, stacks, and queues, detailing their operations, characteristics, and applications. Additionally, it covers algorithm analysis, including time and space complexity, and introduces different notations for arithmetic expressions.

Uploaded by

ATOMIC GAMER
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Unit I

Algorithms and Data


Structures
Priyanka Dadhich
Data Structures
• A data structure is a particular way of organizing data in a computer so that
it can be used effectively.
• Data Structure is a systematic way to organize data in order to use it
efficiently. Foundation terms of a data structure are as follows:
• Interface − Each data structure has an interface. Interface represents the
set of operations that a data structure supports. An interface only provides
the list of supported operations, type of parameters they can accept and
return type of these operations.
• Implementation − Implementation provides the internal representation of
a data structure. Implementation also provides the definition of the
algorithms used in the operations of the data structure.
Data Structures cont…
• 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.

• 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 − Consider an inventory of 1 million(106) items of a store. If the application is to search an
item, it has to search an item in 1 million(106) 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.
• To solve the above-mentioned problems, data structures come to rescue. Data can be organized in a
data structure in such a way that all items may not be required to be searched, and the required data
can be searched almost instantly.
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).
Basic Terminology
• Data − Data are values or set of values.
• Data Item − Data item refers to single unit of values.
• Group Items − Data items that are divided into sub items are called as Group Items.
• Elementary Items − Data items that cannot be divided are called as Elementary Items.
• Attribute and Entity − An entity is that which contains certain attributes or properties,
which may be assigned values.
• 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.
Data Structure
Data Structure
Algorithms
• 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.
Characteristics of an Algorithm
• Not all procedures can be called 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.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
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.
• We shall learn about a priori algorithm analysis. Algorithm analysis deals with the
execution or running time of various operations involved. The running time of an
operation can be defined as the number of computer instructions executed per
operation.
Algorithm Complexity
• Suppose X is an algorithm and n is 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.
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 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.
Calculating Space Complexity
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 −
Algorithm: SUM(A, B)
Step 1 – START
Step 2 - C ← A + B + 10
Step 3 - Stop
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.
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 as the input size increases.
Arrays
• Array is a container which can hold a fix number of items and these
items should be of the same type. Most of the data structures make
use of arrays to implement their algorithms. Following are the
important terms to understand the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index,
which is used to identify the element.
Array Representation
• Arrays can be declared in various ways in different languages. For
illustration, let's take C array declaration.

• Arrays can be declared in various ways in different languages. For


illustration, let's take C array declaration.
Basic Operations
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
Stack
• Stack is an abstract data type with a bounded(predefined) capacity.
• It is a simple data structure that allows adding and removing elements
in a particular order.
• Every time an element is added, it goes on the top of the stack and
the only element that can be removed is the element that is at the
top of the stack, just like a pile of objects.
Stack Cont…
• A real-world stack allows operations at one end only. For example, we
can place or remove a card or plate from the top of the stack only.
Likewise, Stack ADT allows all data operations at one end only. At any
given time, we can only access the top element of a stack.
• This feature makes it LIFO data structure. LIFO stands for Last-in-first-
out. Here, the element which is placed (inserted or added) last, is
accessed first. In stack terminology, insertion operation is called PUSH
operation and removal operation is called POP operation.
Stack Representation
Basic features of Stack

1.Stack is an ordered list of similar data type.

2.Stack is a LIFO(Last in First out) structure or we can say FILO(First in


Last out).

3.push() function is used to insert new elements into the Stack and pop()
function is used to remove an element from the stack.
Both insertion and removal are allowed at only one end of Stack called
Top.
4.Stack is said to be in Overflow state when it is completely full
and is said to be in Underflow state if it is completely empty.
Algorithm for PUSH operation
• Check if the stack is full or not.If the stack is full, then print error of
overflow and exit the program. If the stack is not full, then increment
the top and add the element.
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty
space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.
Algorithm for PUSH operation
Algorithm for POP operation
• Check if the stack is empty or not. If the stack is empty, then print
error of underflow and exit the program. If the stack is not empty,
then print the element at the top and decrement the top.
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at which
top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
Algorithm for POP operation
Position of the TOP

Position of Top Status of Stack


-1 Stack is Empty
0 Only one element in Stack
N-1 Stack is Full
N Overflow state of Stack
Analysis of Stack Operations
Below mentioned are the time complexities for various operations
that can be performed on the Stack.
•Push Operation : O(1)
•Pop Operation : O(1)
•Top Operation : O(1)
•Search Operation : O(n)
The time complexities for push() and pop() functions are O(1)
because we always have to insert or remove the data from the
top of the stack, which is a one step process.
Other Operations Performed on
Stack
• To use a stack efficiently, we need to check the status of stack as well.
For the same purpose, the following functionality is added to stacks −
• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
Applications of Stacks
• Expression evaluation (Conversion between infix, prefix and postfix
notations)
• Syntax parsing by compilers.
• Backtracking Algorithms(game playing, finding path, exhaustive
searching)
• Recursive function calls.
• An Undo operation in text editor.
• Browser back button and many more….
Polish Notation
• The way to write arithmetic expression is known as a notation. An
arithmetic expression can be written in three different but equivalent
notations, i.e., without changing the essence or output of an
expression. These notations are −
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
These notations are named as how they use operator in expression.
Infix Notation
• We write expression in infix notation, e.g. a - b + c, where operators
are used in-between operands. It is easy for us humans to read, write,
and speak in infix notation but the same does not go well with
computing devices. An algorithm to process infix notation could be
difficult and costly in terms of time and space consumption.
Prefix Notation
• In this notation, operator is prefixed to operands, i.e. operator is
written ahead of operands. For example, +ab. This is equivalent to its
infix notation a + b. Prefix notation is also known as Polish Notation.

Postfix Notation
• This notation style is known as Reversed Polish Notation. In this
notation style, the operator is postfixed to the operands i.e., the
operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
Some examples of infix, prefix and
postfix

S.No. Infix Notation Prefix Notation Postfix Notation


1 a+b +ab ab+
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
4 a/b+c/d +/ab/cd ab/cd/+
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-
Queue
• Queue is also an abstract data type or a linear data structure, just like
stack data structure, in which the first element is inserted from one
end called the REAR(also called tail), and the removal of existing
element takes place from the other end called as FRONT(also called
head).

• This makes queue as FIFO(First in First Out) data structure, which


means that element inserted first will be removed first.
Queue
• Queue system works in real world. If you go to a ticket counter to buy
movie tickets, and are first in the queue, then you will be the first one
to get the tickets. Right? Same is the case with Queue data structure.
Data inserted first, will leave the queue first.
• The process to add an element into queue is called Enqueue and the
process of removal of an element from queue is called Dequeue.
Queue
Basic features of Queue

• Like stack, queue is also an ordered list of elements of similar data types.
• Queue is a FIFO( First in First Out ) structure. Once a new element is inserted
into the Queue, all the elements inserted before the new element in the
queue must be removed, to remove the new element.
• peek( ) function is oftenly used to return the value of first element without
dequeuing it.
Applications of Queue
• Queue, as the name suggests is used whenever we need to manage
any group of objects in an order in which the first one coming in, also
gets out first while the others wait for their turn, like in the following
scenarios:
1. Serving requests on a single shared resource, like a printer, CPU task
scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold
people calling them in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive i.e First come first served.
Implementation of Queue Data
Structure
• Queue can be implemented using an Array, Stack or Linked List. The easiest
way of implementing a queue is by using an Array.

• Initially the head(FRONT) and the tail(REAR) of the queue points at the first
index of the array (starting the index of array from index 0.

• As we add elements to the queue, the REAR keeps on moving ahead, always
pointing to the position where the next element will be inserted, while the
FRONT remains at the first index.
Implementation of Queue Data
Structure
Implementation of Queue Data
Structure
• When we remove an element from Queue, we can follow two
possible approaches (mentioned [A] and [B] in above diagram). In [A]
approach, we remove the element at head position, and then one by
one shift all the other elements in forward position.

• In approach [B] we remove the element from head position and then
move head to the next position.
Algorithm for ENQUEUE
operation
Check if the queue is full or not. If the queue is full, then print overflow error
and exit the program. If the queue is not full, then increment the tail and add
the element.
The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Algorithm for DEQUEUE
operation
Check if the queue is empty or not. If the queue is empty, then print
underflow error and exit the program. If the queue is not empty, then print
the element at the head and increment the head.
The following steps are taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data
element.
• Step 5 − Return success.
Additional Functions can be
done on Queues
• peek() − Gets the element at the front of the queue without removing
it.

• isfull() − Checks if the queue is full.

• isempty() − Checks if the queue is empty.


Complexity Analysis of Queue
Operations
• Just like Stack, in case of a Queue too, we know exactly, on which
position new element will be added and from where an element will
be removed, hence both these operations requires a single step.
• Enqueue: O(1)
• Dequeue: O(1)
• Size: O(1)
Types of Queue in Data
Structure
• There are four types of Queue:

1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)
Simple Queue
• Simple queue defines the simple operation of queue in which
insertion occurs at the rear of the list and deletion occurs at the front
of the list.
Dequeue (Double Ended Queue)
• In Double Ended Queue, insert and delete operation can be occur at
both ends that is front and rear of the queue.
• Two types of Dequeue:
• Input restricted Dequeue
• Output restricted Dequeue
Priority Queue
• Priority queue contains data items which have some preset priority.
While removing an element from a priority queue, the data item with
the highest priority is removed first.

• In a priority queue, insertion is performed in the order of arrival and


deletion is performed based on the priority.
Circular Queue
• In a Linear queue, once the queue is completely full, it's not possible
to insert more elements. Even if we dequeue the queue to remove
some of the elements, until the queue is reset, no new elements can
be inserted.
Circular Queue
• When we dequeue(Delete element) any element to remove it from
the queue, we are actually moving the front of the queue forward,
thereby reducing the overall size of the queue.
• And we cannot insert new elements, because the rear pointer is still
at the end of the queue.
Circular Queue
• To overcome this situation we use circular queue…
• Circular Queue is also a linear data structure, which follows the
principle of FIFO(First In First Out), but instead of ending the queue at
the last position, it again starts from the first position after the last,
hence making the queue behave like a circular data structure.
Circular Queue
• Basic features of Circular Queue
• In case of a circular queue, head pointer will always point to the front of the
queue, and tail pointer will always point to the end of the queue.
• Initially, the head and the tail pointers will be pointing to the same location,
this would mean that the queue is empty.
• New data is always added to the location pointed by the tail pointer, and once
the data is added, tail pointer is incremented to point to the next available
location.
• The pointer tail and head will get reinitialised to 0 every time they reach the
end of the queue.
Circular Queue
Circular Queue
Application of Circular Queue
• Some common real-world examples where circular queues are used:

• Computer controlled Traffic Signal System uses circular queue.


• CPU scheduling and Memory management.
Thank you

You might also like