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

Chapter-1 Introduction and Searching

Data structures and algorithms are closely related. An algorithm is a set of steps to solve a problem, while a data structure is how data is organized and related. The choice of data structure affects how efficiently an algorithm can run. Common data structure operations include traversing, searching, inserting, deleting, and sorting data. Data structures can be linear (like stacks and queues) or non-linear (like trees and graphs). The time and space complexity of an algorithm must be analyzed to determine how efficiently it uses resources. Complexity is described using asymptotic notations like Big-O, which provides tight or loose bounds on how a function grows relative to input size.

Uploaded by

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

Chapter-1 Introduction and Searching

Data structures and algorithms are closely related. An algorithm is a set of steps to solve a problem, while a data structure is how data is organized and related. The choice of data structure affects how efficiently an algorithm can run. Common data structure operations include traversing, searching, inserting, deleting, and sorting data. Data structures can be linear (like stacks and queues) or non-linear (like trees and graphs). The time and space complexity of an algorithm must be analyzed to determine how efficiently it uses resources. Complexity is described using asymptotic notations like Big-O, which provides tight or loose bounds on how a function grows relative to input size.

Uploaded by

Project Tims
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Data Structures and

Algorithms
Definition: Data Structures
• Data structure is representation of the logical
relationship existing between individual elements of
data.
• In other words, a data structure is a way of
organizing all data items that considers not only the
elements stored but also their relationship to each
other
Definition: Algorithms
• In mathematics and computer science, an algorithm
is a set of instructions, typically to solve a class of
problems or perform a computation. 

• Algorithms are unambiguous specifications for


performing calculation, data processing, automated
reasoning, and other tasks
Introduction
• Data structure affects the design of both structural
& functional aspects of a program.
Program=algorithm + Data Structure
• You know that a algorithm is a step by step
procedure to solve a particular function.
Introduction
• That means, algorithm is a set of instruction written
to carry out certain tasks & the data structure is the
way of organizing the data with their logical
relationship retained.
• To develop a program of an algorithm, we should
select an appropriate data structure for that algorithm.
• Therefore algorithm and its associated data structures
from a program.
Data, Entity and Information
 Data represents a single value or a set of values assigned to entities.
Data item refers a single or group of values with in the data
 An entity is a thing that has some properties which can take values.

 Processed or meaning full data is called information. This is


used for taking some action.
Primitive data types
 These are the data structures which are directly supported by the
machine.i.e.Any operation can be performed in these data items.
 The different primitive data types are
 Integer

 Float

 Double

 Character

 boolean
Non Primitive data types
 These Datastructures do not allow any specific instructions to be
performed on the Data items directly.
 The different non primitive data types are
 Arrays

 Structures

 Unions

 Class etc.
Data structure
 When working with certain data structures you need to know how
to insert new data, search for a specified item, and deleting a
specific item.
 Commonly used algorithms include are useful for:
 Searching for a particular data item (or record).

 Sorting the data. There are many ways to sort data. Simple sorting,
Advanced sorting
 Iterating through all the items in a data structure. (Visiting each item in turn
so as to display it or perform some other action on these items)
Classification

 There are two types of data structure. They are


 Linear Data structures

 Non-Linear Data structures


Linear Data structures
 This Data Structures involve arranging the elements in
Linear fashion.
 Eg.

 Stacks
 Queue
 Lists
Non-Linear Data structures
 This Data structures involve representingthe
elements in Hierarchical order.
 Eg:
 Trees

 Graphs
Data structure operations

 Operation means processing the data in the data structure. The


following are some important operations.
 Traversing

 Searching

 Inserting

 Deleting

 Sorting
operations
 Traversing

 To visit or process each data exactly once in the data structure

 Searching

 To search for a particular value in the data structure for the given
key value.
 Inserting

 To add a new value to the data structure


operations
 Deleting

 To remove a value from the data structure

 Sorting

 To arrange the values in the data structure in a particular order.

 Merging

 To join two same type of data structure values


Analysis of algorithm
The efficient algorithm
• How to decide that the algorithm is efficient or not??
• The efficiency of algorithm is decided by the analysis
of algorithm
• Three types of approaches
– Empirical approach
– Theoretical approach
– Hybrid approach
Cont.
• Empirical approach
– It is to choosing an algorithm consists of programming the competing
techniques and trying them on different instances with the help of a computer
• Theoretical approach
– It consists of determining mathematically the quantity of resources needed by
each algorithm as a function of the size of instances considered
• Hybrid approach
– Here the form of the function describing the algorithm’s efficiency is
determined theoretically and any required numerical parameters are
determined empirically for a particular program and machine
Time complexity and Space complexity
• Time complexity:
– The amount of time required by an algorithm to be
executed is called its time complexity
• Space Complexity:
– The amount of space required by an algorithm to be
executed is called its space complexity
Average, Best and Worst case analysis
• The best, worst and average cases of a given algorithm express what the
resource usage is at least, at most and on average, respectively
• The resource being considered is running time, but it could also be
memory or the other resource
• The worst case complexity of algorithm is the function defined by the
maximum number of steps taken on instance of size n
• The best case complexity of the algorithm is the function defined by the
minimum number of steps taken on any instance of size n.
• The average case is a function defined by the average number of steps
taken on any instance of size n
Worst case running time
• It is an upper bound on the running time for any
input
• Knowing it gives guarantee that the algorithm will
never take any longer time
Average case running time
• The expected behavior when the input is randomly
drawn from the given distribution
• The average case running time of an algorithm is an
estimate of running time for an average input
Best case running time
• It is an lower bound on the running time for any
input
• Knowing it gives guarantee that the algorithm will
take minimum this amount of time
Asymptotic Notations
Asymptotic Complexity
• Running time of an algorithm as a function of input size
n for large n.
• Expressed using only the highest-order term in the
expression for the exact running time.
– Instead of exact running time, say Q(n2).
• Describes behavior of function in the limit.
• Written using Asymptotic Notation.
Asymptotic Notation
• Q, O, W
• Defined for functions over the natural numbers.
– Ex: f(n) = Q(n2).
– Describes how f(n) grows in comparison to n2.
• Define a set of functions; in practice used to compare two
function sizes.
• The notations describe different rate-of-growth relations
between the defining function and the defined set of
functions.
-notation
For function g(n), we define (g(n)), big-
Theta of n, as the set:
(g(n)) = {f(n) :
 positive constants c1, c2, and n0, such
that n  n ,
0

we have 0  c1g(n)  f(n)  c2g(n)

}
Intuitively: Set of all functions that
have the same rate of growth as g(n).
g(n) is an asymptotically tight bound for f(n).
-notation
For function g(n), we define (g(n)), big-
Theta of n, as the set:
(g(n)) = {f(n) :
 positive constants c1, c2, and n0, such
that n  n ,
0

we have 0  c1g(n)  f(n)  c2g(n)

}
Technically, f(n)  (g(n)).
Older usage, f(n) = (g(n)).
I’ll accept either…

f(n) and g(n) are nonnegative, for large n.


Example
(g(n)) = {f(n) :  positive constants c1, c2, and n0, such that
n  n0, 0  c1g(n)  f(n)  c2g(n)}
• 10n2 - 3n = Q(n2)
• What constants for n0, c1, and c2 will work?
• Make c1 a little smaller than the leading coefficient, and
c2 a little bigger.
• To compare orders of growth, look at the leading term.
• Exercise: Prove that n2/2-3n= Q(n2)
Example
(g(n)) = {f(n) :  positive constants c1, c2, and n0, such that
n  n0, 0  c1g(n)  f(n)  c2g(n)}
• Is 3n3  Q(n4) ??
• How about 2  Q(2 )??
2n n
O-notation
For function g(n), we define O(g(n)), big-O
of n, as the set:
O(g(n)) = {f(n) :
 positive constants c and n0, such that
n  n0,

we have 0  f(n)  cg(n) }


Intuitively: Set of all functions whose rate of
growth is the same as or lower than that of
g(n).

g(n) is an asymptotic upper bound for f(n).


f(n) = (g(n))  f(n) = O(g(n)).
(g(n))  O(g(n)).
Examples
O(g(n)) = {f(n) :  positive constants c and n0, such that
n  n0, we have 0  f(n)  cg(n) }
• Any linear function an + b is in O(n ). How?
2

• Show that 3n =O(n ) for appropriate c and n0.


3 4
 -notation
For function g(n), we define (g(n)), big-
Omega of n, as the set:
(g(n)) = {f(n) :
 positive constants c and n0, such that
n  n0,

we have 0  cg(n)  f(n)}


Intuitively: Set of all functions whose rate of growth is
the same as or higher than that of g(n).

g(n) is an asymptotic lower bound for f(n).


f(n) = (g(n))  f(n) = (g(n)).
(g(n))  (g(n)).
Example
(g(n)) = {f(n) :  positive constants c and n0, such that n 
n0, we have 0  cg(n)  f(n)}

• n = (lg n). Choose c and n0.


Relations Between Q, O, W
Growth rates
O(N2)

O(Nlog N)

For a short time N2 is


better than NlogN

Number of Inputs
Linear Search
Linear Search: A Simple Search
• A search traverses the collection until
– The desired element is found
– Or the collection is exhausted

• If the collection is ordered, I might not have to look at


all elements
– I can stop looking when I know the element cannot
be in the collection.
Un-Ordered Iterative Array Search
procedure Search(my_array Array,
target Num)
i Num
Scan the array
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
Complexity Analysis
• Worst case time complexity: O(N)
• Average case time complexity: O(N)
• Best case time complexity: O(1)
• Space complexity: O(1)
Binary Search
Introduction
• For a binary search to work, it is mandatory for the target array to be sorted. We
shall learn the process of binary search with a pictorial example. The following is
our sorted array and let us assume that we need to search the location of value 31
using binary search.
Algorithm
• Procedure binary_search
• A ← sorted array
• n ← size of array
• x ← value to be searched
• Set lowerBound = 1
• Set upperBound = n
• while x not found
• if upperBound < lowerBound
• EXIT: x does not exists.
• set midPoint = lowerBound + upperBound / 2
• if A[midPoint] < x
• set lowerBound = midPoint + 1
• if A[midPoint] > x
• set upperBound = midPoint - 1
• if A[midPoint] = x
• EXIT: x found at location midPoint
• end while
• end procedure
Cont.
• First, we shall determine half of the array by using
this formula −
• mid = low + (high - low) / 2 Here it is, 0 + (9 - 0 ) / 2 =
4 (integer value of 4.5). So, 4 is the mid of the array.
Cont.
• Now we compare the value stored at location 4,
with the value being searched, i.e. 31. which is not a
match. As the value is greater than 27 and we have
a sorted array, so we also know that the target value
must be in the upper portion of the array.
Cont.
• We change our low to mid + 1 and find the new mid
value again.
• low = mid + 1 mid = low + (high - low) / 2 Our new
mid is 7 now. We compare the value stored at
location 7 with our target value 31.
Cont.
• The value stored at location 7 is not a match, rather
it is more than what we are looking for. So, the value
must be in the lower part from this location.
Cont.
• Hence, we calculate the mid again. This time it is 5.

• We conclude that the target value 31 is stored at location 5.


• Binary search halves the searchable items and thus reduces
the count of comparisons to be made to very less numbers.
Complexity Analysis
• Let say the iteration in Binary Search terminates
after k iterations. In the above example, it
terminates after 3 iterations, so here k = 3
• At each iteration, the array is divided by half. So let’s
say the length of array at any iteration is n
• At Iteration 1,Length of array = n
• At Iteration 2,Length of array = n⁄2
Complexity Analysis
• At Iteration 3,Length of array = ⁄ = ⁄2
(n⁄ )
2 2
n 2

• Therefore, after Iteration k,Length of array = ⁄ n


2
k

• Also, we know that after k divisions, the length of


array becomes 1
• Therefore Length of array = ⁄2 = 1 => n = 2
n k k
Complexity Analysis
• Applying log function on both sides:=> log2 (n) = log2
(2 ) => log2 (n) = k log2 (2)
k

• As (loga (a) = 1)
Therefore,=> k = log2 (n)
• Hence the time complexzity of Binary Search is
• log2 (n)
Question - MCQ
• Given an input arr = {2,5,7,99,899}; key = 899; What
is the level of recursion?
a) 5
b) 2
c) 3
d) 4
Answer
• Answer: c
Explanation:
level 1: mid = 7
level 2: mid = 99
level 3: mid = 899(this is the key)
Question - MCQ
• Given an array arr = {45,77,89,90,94,99,100} and key
= 99; what are the mid values(corresponding array
elements) in the first and second levels of recursion?
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94
Answer
• Answer: a
Explanation: Trace the input with the binary search
recursive code.
k You …
Th a n

You might also like