Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit Test 1 Need of DS: Data Structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

DSA

NOTE: OPEN PPT SIDE BY SIDE


UNIT TEST 1

NEED OF DS
● Data Organised (More efficient programs)
● Enhancing Logical thinking
● Optimising code
● Handle large amount of data

Q What is data Structure


● A data structure is basically a group of data
elements that are put together under one name,
and which defines a particular way of storing and
organising data in a computer so that it can be
use efficiently. Physical implementation of ADT
● Some common examples of data:
● structures are arrays, linked lists, queues, stacks,
binary trees, and hash tables
● Applications: DBMS OS AI Graphics

ORGANIZATION
Collection of record can be searched, processed in any
order or modified
Choice of dsa can make a program run in seconds or
days
– Solution said to be efficient when it solves
problem within its resource constraints
Space
Time

● Select A ds that resource constraints a solution



must meet & basic operations must be supported
● Ds req
Space for each data it stores
Time to perform each basic operation
Programming effort

ADT (Physical Implementation Of Ds)


– Theoretical model that defines Ds purely by
behaviour focusing on operations it supports and
rules for it
– w/o specifying how it is implemented

CLASSIFICATION OF DS

1)Primitive DS

– Basic DS That directly operate on machine


instructions
– Ex: int float char string pointers

2)Non Primitive Ds

– DS More complicated and are derived of Primitive


Ds
– Grouping same / different data
– Ex: Arrays lists Files

Ephemeral: Docent revert Previous Versions


Uses Single transitory structure where a change
destroys its old version
Persistent: Supports Access to multiple versions
○ Partially Persistent: all versions can be
accessed but Only new Version. To be
modified
○ Fully persistent: It can be both accessed and
modified

No matter how complex Ds they can be broken


down into 2 fundamental types
. Contiguous
. Non Contiguous

ARRAY REPRESENTATION
(Draw diagram in exam)
– Arrays store fixed size sequential condition of
elements of same type
– They store collection of data of same type

STACK
(Draw Diagram In Exam)
– Stack store data in particular oder
– They perfrom only 2 operations: Push And Pop
– Push : Inserts Pop: Removes Last Element
– LIFO - Last In first out rule followed

QUEUE
(Draw diagram in exam)
– Linear structure follows particular order
– FIFO - first in first out

GRAPHS
(Draw diagram in exam)
– It contains finite set of vertices called NODES
– Finite set of ordered pair of form (u,v) called edge

TREES
– Hierarchical Ds
– Topmost Node = Root
– Elements are directly under an element are called
its children
– Elements are directly above are called its parent

OPERATIONS OF DS

. Traversing: Access Each data item exactly once


so it can be processed
. Searching: Used to find location of one or more
item to satisfy given constrain , in which the
element may or may not be presented
. Inserting: Adds new data Item into the given list of
items
. Deleting: Delete a particular item in given list of
items
. Sorting: Data Items to be arranged in Ascending/
descending order depending on type of the
application
. Merging: List of 2 sorted data items can be
combined to from single list of sorted data items

APPROACHES TO DESGIN ALGORITHMS


(Draw both diagram in exam)

. Top Down aproach: Stepwise Process Of Breaking


down a large program module into smaller/simpler
modules to organise data and code program in
efficient way.
– Flow of control always in downward direction .
– Application: C Language by using functions.
– It begins with abstract design then sequentially
refined to create more concrete levels
– Repeated until there’s is no req of addtional
refinement

. Bottom Up Approach: It Includes the designing of


the most fundamental parts which are then
combined to make higher level module. Repeated
until the complete algorithm is obtained
– Functions with layer of abstractions
– Application: Testing each fundamental module
before merging

Comparison chat given in ppt itself go through imp stuff

Analysis Of Algorithms

Means determining amount of resources


(time&memory)
algo are designed to work with an arbitrary no. of
inputs
Efficiency or complexity depends on time and
space complexity

Also depends on:

the programming language used,


the quality of the compiler or interpreter,
the CPU speed,
the size of the main memory and the access time to
it,
the knowledge of the programmer, and
the algorithm itself, which may require simple but
also time-consuming machine instructions

Time Complexity: Running time of program or


Algorithm as function of input size . It depends
on programmers input and algorithm used
Space complexity: Amount of computer memory
required during program excution as function in
input size. It depends on 2 factors:

. Fixed Part: varies from problem to problem .


Includes space needed for storing instructions,
var and structured var (arrays and structured)
. Variable Part: Varies from program to program .
Includes space needed for recursion, stack and
structured var that are allocated space
dynamically during runtime of program
Worst Case: Denoted behaviour of an algorithm
w respect to its worst possible case .
– It is an upper bound of the running time of any
input
– Having knowledge of worst case gives assurance
that algo runtime won’t exceed this limit

Average Case: denoted behaviour of an


algorithm w respect to estimate running time for
an average input.
– Specifies expected behaviour when input is
randomly drawn
– It assumes all input of given size are equally likely

Best case : Used to analyse algorithms under


optimal conditions Ex: linear search
– We hardly base our decision on best case
scenario while developing an algorithm
– Always preferred to improve average
performance and worst case performance of an
algorithm

Amortised Running time : Refers to time req to


perform sequence or related operations
averaged over all operations performed . It
guarantees the average performance of each
operation in worst case

EXPRESSING TIME AND SPACE COMPLEXITY


. Linear Loops (single Loop)
Number of times the statement in the loop will
be excuted
Number of iterations directly proportional to
loop factor
For (I=0;i<100;i++) f(n) = n
For ()I=0;i<100;i+=2) f(n) = n/2

. Logarithmic Loops
It contains either multiplication or dividing
during each iteration of the loop
for(I=0;i<100;i×=2) n = 100 , no. of iterations
= log 100 = approx. 10
for(I=100;i>=1;i /=2) f(n) = log n for both cases

. Nested Loop
we need to determine the number of iterations
each loop completes.
The total is then obtained as the product of the
number of iterations in the inner loop and the
number of iterations in the outer loop
Linear Logarithmic Loop: for(I=0;i<10;i++)
for(j=1;j<10;j++) f(n) = n log n
Quadratic Loop : for(I = 0; I<10; I++) for(j = 0;
j<10;j++) f(n) = n^2

ASYMPTOTIC NOTATIONS
. BIG O NOTATION
● It is expressed as O(n) O = Oder Of , meaning
what happens for very large values for n
● It describes upper bound for worst case input
combinations (possibly greater than worst case) .
● Best case = when array is already sorted

O(1) = Constant Time


O(n) = LInear Time
O(Log n) = Logarithmic Time

LIMITATIONS
– Many also are hard to analyse
mathematically
– Must Have sufficient information to
calculate behaviour of algorithms in
average case
– Big O analysis only tells how the algorithm
grows w size of program , not efficiency
– Ignores important constants

. Omega Notation
● It describes Lower bound for the best case
complexity of any algorithm
● Ex: f(n) = 3n+2 , > 3n for all n hence f(n) =
omega(n)

LIMITATIONs
– Function Can Never Do Better then
specified val but it may do worse as tight

lower bound
– It can never get better then specified val as
it describes lower bound for worst case
input combo
– It could be possibly greater thn best case
scenario.
– If we simply write omega it means same as
best case

. Theta Notation
● It provides asymptotically tight bound for f(n)
● f(n) grows between at growth rate that is both
upper & lower bound by same function
● It means its squeezed between 2 constants
multiplied as n becomes very large
● worst case theta notation not used , worst case
theta notation describes asymptotic bounds for
combination of input val
● If we simply write theta notation it means same as
worst case

Complexity Analysis Of Bubble sort


N-1 comparisons will be done in first pass , n-2 in
second and it goes on so total comparison will
be,
Sum = n(n+1)/2 ie O(n^2)
Main adv = simplicity of algorithms
Space complexity for bubble sort is O(1) = single
additional memory space is required ie temp var
Best case O(n) is when the list is sorted

Selection Sort Analysis


● To analyse, count no. of key comparisons and
number of moves
● Outer loop executes n-1, total moves 3*(n-1) each
swap has three moves
● Inner loop excites (1 to n-1) and make one key
comparison
● Selection sort O(n^2) best case worst case
average case same
● Selection sort could be good if data moves are
costly but key comparisons are not costly (long
records)

The End Of Unit 1

SEARCHING AND SORTING


UNIT 2
Searching
Finding wether a particular val present or not ,
if present (successfull)
If not present it displays appropriate message
(Unsuccessful)
2 Types of searching linear and binary
Sorted list = binary search recommended
1.Linear search (sequential search)
● It works by sequentially by comparing val to be
searched with every element of array one by one
● Mostly used for unsorted arrays
● Traversing array using for loop
● If element found return index val or else return -1

Complexity of linear search algorithm


● It executes O(n) time
● Best case val in index 0 (only one comparison
needed)
● Worst case val isn’t present or equal to last
element in array
● In both cases n comparisons will be made,
performance to be improved by using sorted
array

2. Binary Search
● Works efficient ly with sorted array list
● Give example of dictionary
● The size of segment where search has to be
reduced to half so total number of comp will be
halved
● If flag variable is in middle best case , if greater
then middle index pick element to right via versa
● When match found return index val or else return
-1

Sorting
Arranging data in ascending and descending
order
Everything would be a mess and unsorted
stuff , fortunately sorting concept exists
Arranges data in sequence which makes
searching easier

Sorting efficiency
2 main criteria to judge an algorithm:
1) Time 2) memory rest there are many
different sorting algorithms
Bubble, selection , insertion, radix , heap ,
merge

Bubble Sort
– Repeatedly moving largest element to highest
index position of array
– Consecutive adjacent pair is compared
together
– Lower index greater then element at higher
index , they’ll be interchanged such tht the
lower element will be placed before the bigger
one
– With every complete iteration the largest
element bubbles up towards last place just
like water tht rises up to the surface
– Complexity of bubble sort is based on their
n-1 passes in total that goes up to O(n^2) , n =
total element in array

Insertion Sort
– Its less efficient as compared to other sorting
algorithms like quick heap merge
– Efficient to only small data and efficient

implementation on those data sets tht are alr
sorted
– Performs better algorithm then selection ,
bubble sort
– Twice as fast then bubble and almost 40%
fast then selection
– O(1) space required (less memory space)
– Worst time complexity[Big o] = O(n^2)
– Best time complexity[Omega] = O(n)
– Average time complexity [theta ]= O(n^2)
– Space toh alr know O(1)
– Stable sorting technique (docent change
order of elements)

Selection Sort Algorithm


– Simplest sort of algorithm
– First Finds the smallest element and swap with
first position then second smallest element
will swap with element in second position
– Basically , it rapidly selects next smallest elect
ans swaps into the right place
– Scans all n elements, thus n-1 comparison =
O(n^2)
– Small datasets , 60% more efficient then
bubble sort
– Large data sets mai insertion sort better

Merge Sort
– Divide and rule policy
– Dividing = sorted into 2 sub arrays of n/2
element
– Conquer = Merging 2 sorted sub arrays of n/2

to produce sorted array elements
– Less Time to sort large list
– Array length = 0 or 1 (alr sorted)
– Divide unsorted arrays into 2 halves
– Using merge sort algorithm recursively to sort
each sub array
– In the end merge sub arrays to get original
sorted array
– Time Complexity = O(n*log n )
– Worst case time [Big O} = O(n× log n) =. Best
case = average case
– Space Complexity O(n)
– It requires linear time to merge 2 halves
– It requires equal amount of additional space
as unsorted array
– Used for sorting Linked List

Radix Sort
○ Its sorting is done on each of digits in the
number
○ Sorting least significant -> most significant
○ It contains n numbers & k is number of digits
in the largest number , radix sort algorithm is
called total of. K times and inner loop excuses
n times so in total O(kn) time to excuse
○ When radix sort is applied on a data set of
finite size (very small set of numbers), then
the algorithm runs in O(n) asymptotic time.

Heap Sort
○ Complete binary tree
○ Nodes = greater then / lesser then each of its

children
○ If Parent nodes are greater then child nodes =
Max heap(descending order)
○ If parent nodes are smaller then child nodes =
Min heap(ascending order)
○ Sorted array is obtained by removing largest/
smallest element from heap and inserting into
the array & is reconstructed after each
removal
○ Worst case = best case = average case =
O(n*log n)

Stack

– Its an important element that will store


elements in an ordered manner
– Linear data Structure (LIFO Principle)
– The end is known as top of the stack
– It performs only 2 operations Push and Pop

◆ Push Operation
■ Used to insert an element on stack
■ Before inserting check top = max -1 if
yes then Stack Overflow so we cant
insert any Value
■ If false then the value of TOP and store
the new element at the position given
by stack [TOP].

◆ Pop Operation
■ Used To delete an element on stack
■ Before deleting check top=null if yes
then Stack Underflow then we cant
delete the element
■ If false then we decrement he value
printed by top

Applications Of Stack

Recursion
Conversion of an infix expression into a postfix
expression
Conversion of an infix expression into a prefix
expression
Evaluation of a Postfix and Prefix expression

Do All Algorithms for All Infix ,Prefix, Postfix


and All Dry Runs

Recursion

● Implicit Application Of Stack ADT


● A recursive function is a function that calls itself
to solve smaller parts of a problem until a base
condition is met.
● Since a recursive function repeatedly calls itself,
it makes use of the system stack to temporarily
store the return address and local variables of the
calling function.
● Every Recursive solution Has 2 major cases : Base
Class and recursive Class
○ Base Class: in which the problem is simple
enough to be solved directly without making
any further calls to the same function.
○ Recursive Function: Basically 3 Step Process
◇ First Divided Into Smaller Parts
◇ the function calls itself but with sub-
parts of the problem obtained in the first
step
◇ the result is obtained by combining the
solutions of simpler sub-parts
◇ Basically, recursion is defining large and
complex problems in terms of smaller
and more easily solvable problems.

● Formula: n! =n*(n-1)

– Advantages Of Recursion
◆ Recursive solutions often tend to be shorter
and simpler than non-recursive ones.
◆ Code Is Easier to Use
◆ Divide and Conquer Rule
◆ It Works To the Similar to the original
formula to Solve a Problem

– Disadvantages Of Recursion
◆ For Programmers and readers, Its Taken As
Difficult Concept
◆ As Space Limited due to system Stack,
Going to deeper level may Sound difficult
◆ Difficult To Find Bugs
Recursion Iteration
1. Top - Down Approach 1. Bottom- Up Approach
2. Infinite Recursion can 2. Have Safer CPU
crash The System Cycles
3. Shorter Code 3. Longer Code
4. terminates when a 4. terminates when the
base case is recognized loop condition fails.
5. Uses More Memory 5. Uses Less Memory
Then Iteration Then Recusrion

Queues

● It Works On FIFO Principle


● The elements in a queue are added at one end
called the REAR
● Removed from the other end called the FRONT
● It Can Be Applied on either Arrays or Linked Lists
(Easily Represented by Arrays)
● Front = Dequeue Rear = Enqueue
● While Adding REAR Is Incremented And Front is
Decremented While Deleting
● Primitive Operations : enqueue(Insert) , dequeue
(Delete) , Traverse (Display)
● While Inserting Check The Condition Rear =
MAX-1 (Stack Overflow)
● IF Rear =-1 and Front = -1 Means No Element in
Queue
● If Front and Front>Rear (Stack Underflow)
● Types Of Queue: Linear , Circular And Priority
Queue

Applications Of Queue
– Online Banking Fund Transfer Requests
– FCFS Job Scheduling
– Call Centre Phone System
– Serving requests on a single shared resource,
like a printer, CPU task

Limitations Of Linear Queue

● One cannot use free memory space available and


memory remains un utilized.
● Even though space is available at the front of the
queue one cannot insert in the queue if Rear =
max – 1 is true

Circular Queues
Do Its Algorithm

● In the circular queue, the first index comes right


after the last index.
● when front = 0 and rear = Max – 1 ( Circular
Queue Overflow)
● It Works similar to linear queue only difference is
that it comes with insertion and deletion
Operations

Applications For Circular Queue

● CPU scheduling and Memory management


● Computer controlled Traffic Signal System uses
circular queue.

Duque
○ Also Known as Double Ended Queue
○ 2 ends front and rear ,elements aligned
accordingly
○ New items can be added at either the front or
the rear. Likewise, existing items can be
removed from either end.
○ this hybrid linear structure provides all the
capabilities of stacks and queues in a single
data structure.
○ 2 pointers left and right are maintained , and
goes from right to left as circular queue

Input restricted deqeue In this dequeue, insertions


can be done only at one of the ends, while deletions
can be done from both ends.

Dequeue Operations

addFront(item) adds a new item to the front of the


deque. It needs the item and returns
nothing
removeFront() removes the front item from the
deque. It needs no parameters and
returns the item. The deque is modified
removeRear() removes the rear item from the
deque. It needs no parameters and returns
the item. The deque is modified.
isEmpty() tests to see whether the deque is empty.
It needs no parameters and returns a
boolean value.
size() returns the number of items in the deque. It
needs no parameters and returns an
integer

Output restricted deque In this dequeue, deletions


can be done only at one of the ends, while insertions
can be done on both ends.

Application Of Deqeue
● Storing a software application's list of undo
operations.
● Palindrome checker

Priority Queues
– Each element Is assigned priority Used to
determine the order of elements to be
processed
– Any element to be removed works like the
higher priority one is removed first
– Same Operations Insert delete and traverse
For same asc and desc also
– Basically works on 2 rules
1. An element with higher priority is
processed before an element with a lower priority.
2. Two elements with the same priority are
processed on a first-come-first-served (FCFS) basis.

Priority Based On Priority Queue Process


○ OS
○ The Timestamp when the process created in
the system
○ The CPU time it requires to get executed
completely.

Based On 2 Types : Ascending and Descending Priority


Queue
1. Ascending Priority Queue
– Sorted In Ascending Order
– Smallest one is removed first

2.Descending Priority Queue


– Sorted In Descending Order
– Bigger value is removed first

Applications Of Priority Queue


◆ Compress Codes
◆ Os Algo
◆ Traffic Light controls by Light sensors
◆ Shortest Path algo

You might also like