Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

1 Algorithms

An algorithm is a finite set of instructions to perform a task, characterized by input, output, definiteness, finiteness, and effectiveness. The performance of an algorithm is analyzed in terms of time and space complexity, which measure the resources needed for execution. Data structures, which can be linear or non-linear, are fundamental for organizing and managing data, with basic operations including traversing, searching, inserting, deleting, sorting, and merging.

Uploaded by

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

1 Algorithms

An algorithm is a finite set of instructions to perform a task, characterized by input, output, definiteness, finiteness, and effectiveness. The performance of an algorithm is analyzed in terms of time and space complexity, which measure the resources needed for execution. Data structures, which can be linear or non-linear, are fundamental for organizing and managing data, with basic operations including traversing, searching, inserting, deleting, sorting, and merging.

Uploaded by

hemanthnk04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Algorithm:

 An algorithm is defined as a finite set of instructions or rules followed to perform a


particular task.
 An algorithm is a step by step procedure to solve the Given Problem.

Specifications or Characteristics of an Algorithm:


All algorithms must satisfy the following specifications

Input: An algorithm has zero or more inputs, taken or collected from a specified set of objects.
Output: An algorithm has one or more outputs having a specific relation to the inputs.
Definiteness: Each step must be clearly defined, Each instruction must be clear and
unambiguous.
Finiteness: The algorithm must always finish or terminate after a finite number of steps.
Effectiveness: All operations to be accomplished must be sufficiently basic that they can be
done exactly and in finite length.
Example:
1) Area of a rectangle
Input: Breadth, Height
Output: Area

Step 1: Start
Step 2: Read Breadth and Height
Step 3: Area = Breadth * Height
Step 4: Display area
Step 5: Stop

2) Given Number is even or odd


Input: Number
Output: Even or Odd

Step 1: Start
Step 2: Read Number
Step 3: if Number % 2 = 0
Print “Even”
Else
Print “Odd”
Step 5: Stop

Performance Analysis:
Analysing 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.

• Hence the Complexity of an algorithm refers to the measure of the Time that it will
need to execute and get the expected output, and the Space it will need to store all the
data (input, temporary data and output).

• Hence these two factors define the efficiency of an algorithm.

The two factors of Algorithm Complexity are:

• 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.

Time Complexity:

• The time complexity of an algorithm refers to the amount of time that is required by the
algorithm to execute and get the result. This can be for normal operations, conditional
if-else statements, loop statements, etc.
How to calculate Time Complexity?
The time complexity of an algorithm is also calculated by determining the following 2
components:

• Constant time part: Any instruction that is executed just once comes in this part. For
example, input, output, if-else, switch, etc.

• Variable Time Part: Any instruction that is executed more than once, say n times,
comes in this part. For example, loops, recursion, etc.

Therefore Time complexity of any algorithm P is T(P) = C + TP(I), where C is the constant
time part and TP(I) is the variable part of the algorithm, which depends on the instance
characteristic I.

Space Complexity:
The space complexity of an algorithm refers to the amount of memory used by the algorithm
to store the variables and get the result. This can be for inputs, temporary operations, or
outputs.

How to calculate Space Complexity?


The space complexity of an algorithm is calculated by determining the following 2
components:

• Fixed Part: This refers to the space that is definitely required by the algorithm. For
example, input variables, output variables, program size, etc.

• Variable Part: This refers to the space that can be different based on the
implementation of the algorithm. For example, temporary variables, dynamic memory
allocation, recursion stack space, etc.

Therefore Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed
part and SP(I) is the variable part of the algorithm, which depends on instance characteristic I.

Asymptotic Notations:
• Asymptotic analysis is to have a measure of the efficiency of algorithms that don’t
depend on machine-specific constants and don’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.

There are mainly three asymptotic notations:


 Big-O Notation (O-notation)
 Big-Omega Notation (Ω-notation)
 Big-Theta Notation (Θ-notation)
Big-O Notation (O-notation):
• Big-O notation represents the upper bound of the running time of an algorithm.
Therefore, it gives the worst-case complexity of an algorithm.
• If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a
positive constant C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
• Examples :
• { 100 , log (2000) , 10^4 } belongs to O(1)
{ (n/4) , (2n+3) , (n/100 + log(n)) } belongs to O(n)
{ (n^2+n) , (2n^2) , (n^2+log(n)+n^2} belongs to O( n^2)

Omega Notation (Ω-notation):


• Omega notation represents the lower bound of the running time of an algorithm. Thus,
it provides the best case complexity of an algorithm.
• If f(n) describes the running time of an algorithm, f(n) is Ω (g(n)) if there exist a
positive constant C and n0 such that, 0 ≤ f(n) ≥ cg(n) for all n ≥ n0

Examples :
• { (n^2+n) , (2n^2) , (n^2+log(n))} belongs to Ω( n^2)
{ (n/4) , (2n+3) , (n/100 + log(n)) } belongs to Ω(n)
{ 100 , log (2000) , 10^4 } belongs to Ω(1)
Theta Notation (Θ-notation):

• Theta notation encloses the function from above and below. Since it represents the
upper and the lower bound of the running time of an algorithm, it is used for
analyzing the average-case complexity of an algorithm. Θ provides exact bounds.
• Let g and f be the function from the set of natural numbers to itself. The function f is
said to be Θ(g), if there are constants c1, c2 > 0 and a natural number n0 such that c1*
g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0
• Examples :
• { 100 , log (2000) , 10^4 } belongs to Θ(1)
{ (n/4) , (2n+3) , (n/100 + log(n)) } belongs to Θ(n)
{ (n^2+n) , (2n^2) , (n^2+log(n))} belongs to Θ( n2)

Overview or Classification of data structures:


Definition of data structure: The mathematical or logical representation of data elements is
known as data structure. Data structures are also known as fundamentals of data structures or
classic data structures.

Fig: Classification of data structures

The classic data structures are classified into two types:

1). Linear data structure: In linear data structure data is stored in consecutive memory
location or in a sequential form.

Ex: Arrays ,linked lists, stacks, queues.


Arrays:

An array is a fixed size sequenced collection of elements of the same data type.

Example: int a[5]={1,2,3,4,5}

Linked List:

A linked list is an ordered collections of finite, homogeneous data elements called


nodes.

Stacks:

Stack is a collection of ordered elements. Stack is a data structure in which insertion and
deletion operations are performed at one end only.

 The insertion operation is referred to as “PUSH” and deletion operation is referred to


as “POP”.

 Stack is also called LIFO (Last In First Out) or FILO(First In Last Out) data
structures.

Queues:

Queue is a data structure which permits insertion at one end and deletion at another end.

 End at which deletion is occurs is known as FRONT end and another end at which
insertion occurs is known as REAR end.

 Queue is also called FIFO( First In First Out) data structure.

2). Non-linear data structure: In non linear data structure data is stored in non consecutive
memory locations or not in a sequential form.

Ex: Trees, graphs, tables, sets.

Trees:

A tree can be defined as finite set of data items (nodes) in which data items are arranged in
branches and sub branches according to requirements. Tree represents the hierarchical
relationship between various elements.

Graphs:

Graph is collection of nodes (information) and connecting edges (logical relation) between
nodes. A tree can be viewed as restricted graph.
 OPERATIONS ON DATA STRUCTURES:
The basic operations that are performed on data structures are as follows:
1) 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.
2) 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?
3) 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?
4) 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.
5) 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.
6) Merging: Lists of two sorted data items can be combined to form a single list of
sorted data items.

You might also like