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

Algo Lect 1

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

Algo Lect 1

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

Algorithms:

Analysis and Design


Dr. Mohamed Abd Elfattah
Department of Computer Science

1
An algorithm is a set of
steps of operations to solve An algorithm is an efficient
a problem performing method that can be
calculation, data processing, expressed within finite
and automated reasoning amount of time and space.
tasks.

2
If we have an algorithm for a specific
problem, then we can implement it
in any programming language,
meaning that the algorithm is
independent from any programming
languages.

3
Algorithm Design
The important aspects
of algorithm design
include creating an
efficient algorithm to
solve a problem in an
efficient way using
minimum time and
space.

4
To solve a problem, different approaches
can be followed. Some of them can be
efficient with respect to time If we require an algorithm to run in
consumption, whereas other lesser time, we have to invest in more
approaches may be memory efficient. memory and if we require an algorithm
However, one has to keep in mind that to run with lesser memory, we need to
both time consumption and memory have more time.
usage cannot be optimized
simultaneously.

5
Problem Development Steps
The following steps are involved in solving computational
problems.
•Problem definition
•Development of a model
•Specification of an Algorithm
•Designing an Algorithm
•Checking the correctness of an Algorithm
•Analysis of an Algorithm
•Implementation of an Algorithm
•Program testing
•Documentation

6
Characteristics of Algorithms
The main characteristics of algorithms are as follows −
•Algorithms must have a unique name
•Algorithms should have explicitly defined set of
inputs and outputs
•Algorithms are well-ordered with unambiguous
operations
•Algorithms halt in a finite amount of time.
Algorithms should not run for infinity, i.e., an
algorithm must end at some point

7
Pseudocode

Pseudocode gives a high-level description of an algorithm without the ambiguity


associated with plain text but also without the need to know the syntax of a
particular programming language.

The running time can be estimated in a more general manner by using


Pseudocode
to represent the algorithm as a set of fundamental operations which can then be
counted.

8
Algorithm: Insertion-Sort
Input: A list L of integers of length n
Output: A sorted list L1 containing those integers present in L
Step 1: Keep a sorted list L1 which starts off empty
Step 2: Perform Step 3 for each element in the original list L
Step 3: Insert it into the correct position in the sorted list L1.
Step 4: Return the sorted list
Step 5: Stop

9
Analysis of algorithm is the process of analyzing the problem-solving capability
of the algorithm in terms of the time and size required (the size of memory for
storage while implementation). However, the main concern of analysis of
algorithms is the required time or performance. Generally, we perform the
following types of analysis −
Worst-case − The maximum number of steps taken on any instance of size a.
Best-case − The minimum number of steps taken on any instance of size a.
Average case − An average number of steps taken on any instance of size a.

10
What is Algorithm
Algorithm
◦ Any well-defined computational procedure that takes some value, or set of values, as
input and produces some value, or set of values, as output.
We can also view an algorithm as a tool for solving well specific
computational problem.
Example: Sorting problem
Input: A sequence of n numbers  a1 , a2 ,..., an 
Output: A permutation  a1' , a2 ' ,..., an ' 
of the input sequence such that a1'  a2 ' . ...  an '

11
Correctness of Algorithm
An instance of a problem consists of all inputs needed to compute a solution to the
problem.
An algorithm is said to be correct if for every input instance, it halts with the correct
output.
An algorithm is said to be correct if, for every input instance, it halts with the correct
output. We say that a correct algorithms solves the given computational problem.

An incorrect algorithm might not halt at all on some input instance, or it might halt with
other than the desired answer.

12
Why will we be studying?
Two main aims:
◦A set of practical tools
◦ A collection of fundamental algorithms for use in other courses, or in your
future jobs.
◦Theoretical applications
◦ An appreciation of the issues involved in designing, analyzing or selecting an
efficient algorithm for new problem.

13
Evaluation of the
algorithm
Correctness
◦ Theoretical correctness
◦ Numerical stability
Efficiency
◦ Complexity
◦ Speed
◦ Other resource usage

14
Idea

divide Dynamic
Linear Graph
and conquer programming

15
Example

16
The Human Genome Project

What Kind of The Internet Applications


Problem can
be Solved by
Algorithm? Electronic Commerce with Public-key
cryptography and digital signatures

Manufacturing and other commercial


settings

17
Insertion sort

18
An algorithm is a set of steps of operations to solve a problem
performing calculation, data processing, and automated reasoning
tasks.

An algorithm is an efficient method that can be expressed within


finite amount of time and space.

10/8/2024
Insertion sort is the sorting mechanism where the sorted array is built
having one item at a time.
The array elements are compared with each other sequentially and then
arranged simultaneously in some particular order.
The analogy can be understood from the style we arrange a deck of cards.
This sort works on the principle of inserting an element at a particular
position, hence the name Insertion Sort.

10/8/2024
Insertion Sort works as follows:
The first step involves the comparison of the element in question with its
adjacent element.
And if at every comparison reveals that the element in question can be
inserted at a particular position, then space is created for it by shifting the
other elements one position to the right and inserting the element at the
suitable position.
The above procedure is repeated until all the element in the array is at their
apt position.

10/8/2024
Let us now understand working with the following example:
Consider the following array: 25, 17, 31, 13, 27
Not sorted.
First Iteration: Compare 25 with 17. The comparison shows 17< 25.
Hence swap 17 and 25.
The array now looks like: 17, 25, 31, 13, 2

10/8/2024
10/8/2024
10/8/2024
10/8/2024
10/8/2024
Analysis of Insertion sort
Time Complexity

10/8/2024
The running time of the algorithm is the sum of running times for each
statement executed
10/8/2024
10/8/2024
10/8/2024
Time Complexity of insertion sort

Worst Case Time Complexity [ Big-O ]: O(n2)

Best Case Time Complexity [Big-omega]: O(n)

Average Time Complexity [Big-theta]: O(n2)

10/8/2024

You might also like