The document discusses data structures and algorithms. It defines data structures and different types including primitive and non-primitive structures. It describes operations on data structures like traversing, searching, insertion and deletion. It also defines concepts like abstract data types, asymptotic analysis, and different algorithm analysis methods. Examples provided include linear search algorithm and binary search algorithm in pseudocode and C code.
Introduction to IEEE STANDARDS and its different types.pptx
UNIT 1.pptx
1. DATA STRUCTURES
Unit 1: Introduction to Algorithms & Data Structure
Introduction: Data types, Abstraction, Abstract Data Type (ADT), Concept of
data structure, Types of data structures, Operations on Data Structures,
Introduction to Algorithms, Writing Pseudocodes, Algorithm analysis,
Complexity of algorithms and Time space trade-off, Searching: Linear and Binary
Search Techniques and their complexity analysis.
2. Introduction to Data Structure
• A data structure is a particular way of organizing data in a computer so that it can be
used effectively. For example, we can store a list of items having the same data-type
using the array data structure.
• CLASSIFICATION OF DATA STRUCTURES: Primitive vs Non primitive
Primitive data structures are the fundamental data types which are supported
by a programming language. Some basic data types are integer, real, character,
and Boolean. The terms ‘data type’, ‘basic data type’, and ‘primitive data type’
are often used interchangeably.
Non-primitive data structures are those data structures which are created using
primitive data structures. Examples of such data structures include linked lists,
stacks, trees, and graphs. Non-primitive data structures can further be classified
into two categories: linear and non-linear data structures.
3. Linear vs Non-linear data
structure
• Data structure where data elements are arranged sequentially or linearly where the
elements are attached to its previous and next adjacent in what is called a linear data
structure. In linear data structure, single level is involved. Therefore, we can traverse
all the elements in single run only. Its examples are array, stack, queue, linked list, etc.
• Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures. In a non-linear data structure, single level is not
involved. Therefore, we can’t traverse all the elements in single run only. Non-linear
data structures are not easy to implement in comparison to linear data structure. It
utilizes computer memory efficiently in comparison to a linear data structure. Its
examples are trees and graphs.
4. Operations on data structure
• There are different types of operations that can be performed for the manipulation of data in
every data structure. Some operations are explained and illustrated below:
• Traversing: Traversing a Data Structure means to visit the element stored in it. This can be
done with any type of DS.
• Searching: Searching means to find a particular element in the given data-structure. It is
considered as successful when the required element is found. Searching is the operation which
we can performed on data-structures like array, linked-list, tree, graph, etc.
• Insertion: It is the operation which we apply on all the data-structures. Insertion means to add
an element in the given data structure. The operation of insertion is successful when the
required element is added to the required data-structure. It is unsuccessful in some cases
when the size of the data structure is full and when there is no space in the data-structure to
add any additional element. The insertion has the same name as an insertion in the data-
structure as an array, linked-list, graph, tree. In stack, this operation is called Push. In the
queue, this operation is called enqueue.
5. Operations on data structure
• Deletion: It is the operation which we apply on all the data-structures. Deletion means
to delete an element in the given data structure. The operation of deletion is
successful when the required element is deleted from the data structure. The deletion
has the same name as a deletion in the data-structure as an array, linked-list, graph,
tree, etc. In stack, this operation is called Pop. In Queue this operation is called
Dequeue.
• Merging : Lists of two sorted data items can be combined to form a single list of sorted
data items
6. Abstract Data Type(ADT)
• An abstract data type (ADT) is a mathematical model for data types. An abstract data
type is defined by its behavior (semantics) from the point of view of a user, of the data,
specifically in terms of possible values, possible operations on data of this type, and
the behavior of these operations. This mathematical model contrasts with data
structures, which are concrete representations of data, and are the point of view of an
implementer, not a user.
• The definition of ADT only mentions what operations are to be performed but not how
these operations will be implemented. It does not specify how data will be organized
in memory and what algorithms will be used for implementing the operations. It is
called “abstract” because it gives an implementation-independent view. The process
of providing only the essentials and hiding the details is known as abstraction.
7. List- ADT
A list contains elements of the same type arranged in sequential order and following
operations can be performed on the list.
• get() – Return an element from the list at any given position.
• insert() – Insert an element at any position of the list.
• remove() – Remove the first occurrence of any element from a non-empty list.
• removeAt() – Remove the element at a specified location from a non-empty list.
• replace() – Replace an element at any position by another element.
• size() – Return the number of elements in the list.
• isEmpty() – Return true if the list is empty, otherwise return false.
• isFull() – Return true if the list is full, otherwise return false.
8. Stack-ADT
A Stack contains elements of the same type arranged in sequential order. All operations
take place at a single end that is top of the stack and following operations can be
performed:
push() – Insert an element at one end of the stack called top.
pop() – Remove and return the element at the top of the stack, if it is not empty.
peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
size() – Return the number of elements in the stack.
isEmpty() – Return true if the stack is empty, otherwise return false.
isFull() – Return true if the stack is full, otherwise return false.
9. Algorithm
An algorithm is a finite set of Instructions for solving a particular
problem in a finite amount of time using a finite amount of data.
In order to qualify as an algorithm, a sequence of instructions must
have following characteristics:
• Each and every instruction should be precise and unambiguous
• Each instruction should be such that it can be performed in a
finite time
• One or more instructions should not be repeated infinitely.
• This ensures that the algorithm will ultimately terminate
• After performing the instructions, that is after the algorithm
terminates, the desired results must be obtained
Algorithm can be represented as programs, flowcharts and pseudo-
codes.
10. Sample
Algorithm(Example 1)
Write an algorithm to find sum of two numbers.
Algorithm:
Step 1: Read two numbers in variable num1 and num2
Step 2: Add num1 and num2 and
sum=num1+num2
Step 3: Display result sum
11. Pseudo-Code
A mixture of natural language and high-level
programming concepts that describes the main ideas
behind a generic implementation of a data structure
or algorithm.
Eg: Algorithm arrayMax(A, n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i 1 to n-1 do
if currentMax < A[i] then currentMax A[i]
return currentMax
12. Pseudo-Code
It is more structured than usual prose but
less formal than a programming language
Expressions:
use standard mathematical symbols to
describe numeric and boolean expressions
use for assignment (“=” in C)
use = for the equality relationship (“==” in
C)
Method Declarations:
Algorithm name(param1, param2)
13. Time and space complexity of
algorithm
• Analyzing an algorithm means determining the amount of resources (such as time and
memory) needed to execute it. Algorithms are generally designed to work with an
arbitrary number of inputs, so the efficiency or complexity of an algorithm is stated in
terms of time and space complexity.
• The time complexity of an algorithm is basically the running time of a program as a
function of the input size.
• The space complexity of an algorithm is the amount of computer memory that is
required during the program execution as a function of the input size.
14. Time space-Trade off
• The best algorithm to solve a particular problem at hand is no doubt the one that
requires less memory space and takes less time to complete its execution. But
practically, designing such an ideal algorithm is not a trivial task.
• There can be more than one algorithm to solve a particular problem. One may require
less memory space, while the other may require less CPU time to execute. Thus, it is
not uncommon to sacrifice one thing for the other.
• There exists a time–space trade-off among algorithms. So, if space is a big constraint,
then one might choose a program that takes less space at the cost of more CPU time.
On the contrary, if time is a major constraint, then one might choose a program that
takes minimum time to execute at the cost of more space.
15. Worst-case, Average-case,
Best-case
• Worst-case running time: This 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.
• Average-case running time: The average-case running time of an algorithm 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.
• Best-case running time: The term ‘best-case performance’ is used to analyse an algorithm
under optimal conditions. For example, the best case for a simple linear search on an array
occurs when the desired element is the first in the list. However, while developing and
choosing an algorithm to solve a problem, we hardly base our decision on the best-case
performance. It is always recommended to improve the average performance and the worst-
case performance of an algorithm.
16. Asymptotic analysis and
notations
• In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input
size (we don’t measure the actual running time). We calculate, how the time (or
space) taken by an algorithm increases with the input size.
• The main idea of asymptotic analysis is to have a measure of the efficiency of
algorithms that don’t depend on machine-specific constants and doesn’t require
algorithms to be implemented and time taken by programs to be compared.
Asymptotic notations are mathematical tools to represent the time complexity of
algorithms for asymptotic analysis. The following 3 asymptotic notations are mostly
used to represent the time complexity of algorithms.
17. BIG O Notation
Big O Notation: The Big O notation
defines an upper bound of an algorithm.
O(g(n)) = { f(n): there exist positive
constants c and n0 such that 0 <= f(n)
<= c*g(n) for all n >= n0}
18. Categories of Algorithms
According to the Big O notation, we have five different categories of algorithms:
• Constant time algorithm: running time complexity given as O(1)
• Linear time algorithm: running time complexity given as O(n)
• Logarithmic time algorithm: running time complexity given as O(log n)
• Polynomial time algorithm: running time complexity given as O(𝑛𝑘) where k > 1
• Exponential time algorithm: running time complexity given as O(2𝑛)
• O(1)<O(log n)<O(n)<O(𝑛2 )<O(2𝑛)<O(n!)
19. BIG Omega Notation
Ω Notation: Just as Big O notation
provides an asymptotic upper bound
on a function, Ω notation provides an
asymptotic lower bound.
Ω Notation can be useful when we
have lower bound on time complexity
of an algorithm.
Ω (g(n)) = {f(n): there exist positive
constants c and n0 such that 0 <=
c*g(n) <= f(n) for all n >= n0}.
20. BIG Theta Notation
Theta Notation: The theta notation
bounds a function from above and below,
so it defines exact asymptotic behavior.
A simple way to get Theta notation of an
expression is to drop low order terms and
ignore leading constants. For example,
consider the following expression.
3n3 + 6n2 + 6000 = Θ(n3).
Θ(g(n)) = {f(n): there exist positive
constants c1, c2 and n0 such that 0 <=
c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}
21. Linear Search Algorithm
1. Start
2. Declare an array and search data variable x
3. Traverse the entire array until search data is found
If( search data is present) then
return it’s location
Else
return 0
4. Print data
5. Stop
22. Linear Search C Code
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
{
if(a[i]==x)
{
break;
}
}
if(i<n)
{
printf("Element found at index %d",i);
}
else
{
printf("Element not found");
}
23. Binary Search
• Search a sorted array by repeatedly dividing the search
interval in half.
• Begin with an interval covering the whole array.
• If the value of the search key is less than the item in the
middle of the interval, narrow the interval to the lower
half.
• Otherwise narrow it to the upper half. Repeatedly check
until the value is found or the interval is empty.
24. Binary Search Algorithm
BinarySearch_IT (A[0..N-1], value)
{
low = 0
high = N - 1
while (low <= high)
{
mid = (low +high) / 2)
if (A[mid] > value)
high = mid - 1
else if (A[mid] < value)
low = mid + 1
else
return mid
}
return -1
}
Iterative Pseudocode
25. Binary Search Algorithm
Recursive Pseudocode
// initially called with low = 0, high = N – 1
BinarySearch_REC(A[0..N-1], value, low, high)
{
if (high>=low)
{
mid = (low +high) / 2
if (A[mid] > value)
return BinarySearch_REC(A, value, low, mid-1)
else if (A[mid] <value)
return BinarySearch_REC(A, value, mid+1, high)
else
return mid
}
return -1
}
26. C code for Binary Search
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
27. C code for Binary Search
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}