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

Algorithm 1

Uploaded by

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

Algorithm 1

Uploaded by

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

Lecture one: Introduction to Algorithms (Basics of Algorithm)

By Dr. Milad Elgargni

Introduction
This course introduces the fundamental concepts of Designing Strategies, analysis of Algorithms. Also, it
includes the basic concepts on Complexity theory. Moreover; it outlines problems on searching, sorting,
divide and conquer and Graph theory methods.
Prerequisites
The students should have basic knowledge of programming and mathematics. The students should know
data structure very well. Moreover, it is preferred if the students have basic understanding of Formal
Language and Automata Theory.
References
1. Algorithms 4th Edition by Robert and Kevin,
2. Design and Analysis of Algorithm,
3. Data structures and algorithm analysis in C++ 4th Edition by Mark Allen and
4. Websites and YouTube.

1. Basics of Algorithm
An algorithm is known since early years in 9 decade. It has been invented by a Muslim scientist
Muhammad ben Musa Al-Khwarizmi. He lived in Bagdad in 780 - 847.

1.1. Definitions:
An Algorithm can be defined as writing in human language a sequence of finite logical steps for solving a
specific problem. An algorithm is an efficient method that can be expressed within finite amount of time and
space. Also, it is the best way to represent the solution of a particular problem in a very simple and efficient
way. Therefore; Design and Analysis of Algorithm (DAA) is very important for solving different types of
problems in the branch of computer science and information technology. 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.
Algorithm also; can be defined as step by step procedure (process) for solving a specific problem.
A program is a written sequence of finite steps in one of programming language for solving a particular
problem. Each of these steps is a command to the computer to do a specific operation. Therefore a program
is a set of instructors to the computer to perform a specific task.

1.2. Algorithm vs Program


The main differences between algorithm and program are listed in table 1:

Table 1: Algorithm vs Program


The algorithm The program
1- It is an abstract concept 2- It is a concrete (real) implementation
3- Can be written in any language 4- Can be writing using programming language
5- Developed during design stage 6- Developed during development stage
7- Independent of the hardware and operating system 8- Depends on the hardware and operating system
9- It is analysed 10- It is tested

1
Lecture one: Introduction to Algorithms (Basics of Algorithm)
By Dr. Milad Elgargni

2. Algorithm Design
The important aspect for designing an algorithm is to create it in an efficient way in order to solve a problem
in a minimum time and space. To solve a problem, different approaches can be followed. Some of them can
be efficient with respect to time consumption, whereas other approaches may be memory efficient.
However, one has to keep in mind that both time consumption and memory usage cannot be optimized all
together. If we require an algorithm to run in lesser time, we have to invest in more memory and if we
require an algorithm to run with lesser memory, we need to have more time.

3. Steps for solving a problem


For solving computational problems the following steps should be considered.
1. Problem definition.
2. Development of a model (discretion of the inputs and outputs).
3. Requirement of an Algorithm.
4. Designing an Algorithm.
5. Checking the correctness of an Algorithm.
6. Analysis of an Algorithm.
7. Implementation of an Algorithm.
8. Program testing.
9. Documentation.
4. Characteristics of Algorithms
The main characteristics of algorithms are as follows:
1. Algorithms must have a unique name
2. Algorithms should have clearly defined set of inputs and outputs
 Input: an algorithm may take 0 or more inputs.
Example 1: Algorithm with no input.
1. Start.
2. Generate a random number.
3. Output the random number.
4. Stop.
Example 2: Algorithm with two inputs.
1. Start.
2. Input the first number (num1).
3. Input the second number (num2).
4. Add num1 and num2
5. Output the result of the addition.
6. Stop.
 Output: an algorithm must have at least one output.
Example 3: Algorithm with one output.
1. Start.
2. Generate a random number.
3. Output the random number.
4. Stop.

2
Lecture one: Introduction to Algorithms (Basics of Algorithm)
By Dr. Milad Elgargni

Example 4: Algorithm with two outputs.


1. Start.
2. Input the first number (num).
3. Calculate the square of num.
4. Calculate the cube of num.
5. Output the result of the squareResult.
6. Output the result of the cubeResult.
7. Stop.
3. Finiteness: an algorithm must terminate in finite time.
Example 5: Algorithm that runs forever.
1. Start.
2. Set court to 0.
3. Do the loop.
4. Within the loop increase the court by 1.
5. Repeat steps 3 and 4.
6. Stop.
Example 6: Algorithm that terminates in finite time.
1. Start.
2. Set court to 0.
3. Set a limit for the number of counts (lets = 10)’
4. Do the loop up to specific limit.
5. Within the loop increase the court by 1.
6. Repeat steps 4 and 5 until the specified limit reached.
7. Stop.

4. Definiteness: Algorithm must well-ordered and clear with unambiguous operations.


Example 7: Algorithm with ambiguous.
1. Start.
2. Input the first number (num1).
3. Input the second number (num2).
4. Perform some operation on num1 and num2.
5. Output the result of the operation.
6. Stop.
Example 8: Algorithm without ambiguous.
1. Start.
2. Input the first number (num1).
3. Input the second number (num2).
4. Calculate the addition of num1 and num2.
5. Output the result of the addition.
6. Stop.
5. Effectiveness: Algorithms stop in a finite amount of time. Algorithms should not run for infinity, i.e.,
an algorithm must end at some point.

3
Lecture one: Introduction to Algorithms (Basics of Algorithm)
By Dr. Milad Elgargni

Example 9: Algorithm that takes long time to produce the output.


1. Start.
2. Input a positive integer number n.
3. Set a variable sum to 0.
4. Do the loop iterates from 1 to n.
5. Add the current iteration value to sum.
6. Repeat stages 4 and 5.
7. Output the value of sum.
8. Stop.
Example 10: Algorithm that takes short time to produce the output.
1. Start.
2. Input a positive integer number n.
3. Calculate the sum of the formula sum = (n (n+1))/2.
5. Output the value of sum.
6. Stop.
5. How we can write an algorithm
An algorithm can be writing in one the following two types:
5.1. Pseudo code
Pseudo code 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 Pseudo code to represent the
algorithm as a set of fundamental operations which can then be counted.

5.1.1. Algorithm vs Pseudo code


An algorithm is a formal definition with some specific characteristics that describes a process, which could
be executed by a Turing-complete computer machine to perform a specific task. Generally, the word
"algorithm" can be used to describe any high level task in computer science.
On the other hand, pseudo code is an informal and (often simple) human readable description of an
algorithm leaving many granular details of it. Writing a pseudo code has no restriction of styles and its only
objective is to describe the high level steps of algorithm in a much realistic manner in natural language.
For example, following is an algorithm for Insertion Sort.

Algorithm: sumOfArray
Start
Step 1: Input an array A of integers of length n.
Step 2: Keep S equal to 0.
Step 3: add array elements to the S one by one.
Step 5: Print the value of S (Output of sum of array elements)
Step 6: Stop
Here is a pseudo code, which describes how the high level abstract process mentioned above in the
algorithm Insertion-Sort could be described in a more realistic way.
Algorithm: sum (A, n);
S 0;
n 5;
A[5] {1,2,3,4,5};
for (int i=0; i< n; i++)
4
Lecture one: Introduction to Algorithms (Basics of Algorithm)
By Dr. Milad Elgargni

S S +A[i];
printf("%d",S);
In this tutorial, algorithms will be presented in the form of pseudo code, which is similar in many respects to
C, C++, Java, Python, and other programming languages.
5.2. Flowchart
Flowchart is a way for representing an algorithm using drawing blocks as shown in example below.
Example 11:
Write an algorithm using both pseudo code and flowchart for calculating the average of three numbers.

Table2: Different types of a pseudo code


Algorithm: sumVariables Algorithm: sum(a ,b) Algorithm: sumVariables Algorithm: sumVariables
Start { Begin Begin
int a = 9; double z = a + b; int a := 9; int a 9;
int b = 12; printf("%d", z); double :b =4.5; double b 4.5;
int z = a+b; } double z := a+b; double z a+b;
printf("%d", z); printf("%d", z); cout<<"z "<<z<<"\n ;
stop End End

Start

x1=9, x2=12, x3=66

Average = (x1+ x2+ x3)/3

Print the average

Stop

Figure1 : Flowchart for calculating the average

Home work:
1. Write an algorithm using both pseudo code and flowchart for calculating the average of ten numbers.
2. Write an algorithm using both pseudo code and flowchart to find the sum of array elements.

You might also like