enm 104 - 1 algorithms

28
Problems Trivial making breakfast, travelling to the workplace Sending an e-mail withdraw money from ATM Non-trivial Finding optimal route for a traveling salesman Discovering task assignment rules of workers Analysis of gene expression with DNA microarrays

Upload: others

Post on 12-Dec-2021

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: enm 104 - 1 Algorithms

ProblemsTrivial

– making breakfast,

– travelling to the workplace

– Sending an e-mail

– withdraw money from ATM

Non-trivial– Finding optimal route for a traveling salesman

– Discovering task assignment rules of workers

– Analysis of gene expression with DNA microarrays

Page 2: enm 104 - 1 Algorithms

Abu Ja'far Muhammad ibn Musa Al-Khwarizmi

14/02/2019 http://www-history.mcs.st-andrews.ac.uk

MacTutor History of Mathematics archive, School of Mathematics andStatistics University of St Andrews, Scotland

Page 3: enm 104 - 1 Algorithms

AlgorithmsENM 104 – Introduction to computations and programing for IE

Page 4: enm 104 - 1 Algorithms

Algorithms• The term algorithm originally referred to any computation

performed via a set of rules applied to numbers written in decimal form.

• Algorithm can be defined as: “A sequence of activities to be processed for getting desired output from a given input.”

• A formula or set of steps for solving a particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point. https://www.webopedia.com/TERM/A/algorithm.html

Page 5: enm 104 - 1 Algorithms

Algorithms• Getting specified output is essential after algorithm is executed.

• One will get output only if algorithm stops after finite time.

• Activities in an algorithm to be clearly defined in other words for it to be unambiguous.

Page 6: enm 104 - 1 Algorithms

Examples of simple Algorithms

Page 7: enm 104 - 1 Algorithms

Operators for writing an algorithmsymbol task

+ addition- subtraction* multiplication/ divisionß assignment.

14/02/2019

Page 8: enm 104 - 1 Algorithms

Problem 1:Find the area of a Circle of radius r.

Inputs to the algorithm : Radius r of the Circle. Expected output : Area of the Circle

Algorithm: Step 1: Read\input the Radius r of the Circle Step 2: Area ß Pi*r*r // calculation of area Step 3: Print Area

14/02/2019

Page 9: enm 104 - 1 Algorithms

Problem 2Write an algorithm to read two numbers and find their sum.

Inputs to the algorithm : First num1. Second num2. . Expected output : Sum of the two numbers

Algorithm: Step 1: StartStep 2: Read\input the first num1.Step 3: Read\input the second num2.Step 4: Sum ß num1 + num2 // calculation of sum Step 5: Print SumStep 6: End

14/02/2019

Page 10: enm 104 - 1 Algorithms

Problem 3Convert temperature Fahrenheit to Celsius Inputs to the algorithm

Inputs to the algorithm : Temperature in Fahrenheit, FExpected output : Temperature in Celsius, C

Algorithm: Step 1: StartStep 2: Read Temperature in Fahrenheit F Step 3: C ß 5/9*(F 32)Step 4: Print Temperature in Celsius: CStep 5: End

14/02/2019

Page 11: enm 104 - 1 Algorithms

Type of Algorithms

Page 12: enm 104 - 1 Algorithms

Type of Algorithms• The algorithm and flowchart, classification to the three types of

control structures. They are:

1. Sequence

2. Branching (Selection) The branch refers to a binary decision based on some condition. If the condition is true, one of the two branches is explored; if the condition is false, the other alternative is taken.

3. Loop (Repetition) The loop allows a statement or a sequence of statements to be repeatedly executed based on some loop condition.

14/02/2019

Page 13: enm 104 - 1 Algorithms

Problem 1 - branchingwrite algorithm to find the greater number between two numbers

Inputs to the algorithm : two umbers, A, BExpected output : greater number, C

Algorithm: Step 1: StartStep 2: Read/input A and BStep 3: If A greater than B then C=AStep 4: if B greater than A then C=BStep 5: Print CStep 6: End

14/02/2019

Page 14: enm 104 - 1 Algorithms

Problem 2 - branching• write algorithm to find the result of equation ! " = $−", " < 0

", " ≥ 0Inputs to the algorithm : XExpected output : FAlgorithm:

Step1: StartStep2: Read/input XStep3: If X Less than zero then F=-XStep4: if X greater than or equal zero then F=XStep5: Print FStep6: End

14/02/2019

Page 15: enm 104 - 1 Algorithms

Problem 1 - loop

• An algorithm to calculate even numbers between 0 and 99

Inputs to the algorithm : numbers from 0 to 99,Expected output : even numbers in given interval,

Algorithm:

Step 1: Start

Step 2: I ← 0

Step 3: Write I in standard output

Step 4: I ← I+2

Step 5: If (I <=98) then go to line 3

Step 6. End

14/02/2019

Page 16: enm 104 - 1 Algorithms

Problem 2 - loopDesign an algorithm which gets a natural value, n,as its input and calculates odd numbers equal or less than n. Then write them in the standard output: Inputs to the algorithm : nExpected output : even numbers from 0 to n,

Algorithm: Step 1. StartStep 2. Read nStep 3. I ← 1Step 4. Write IStep 5. I ← I + 2Step 6. If ( I <= n) then go to line 4 Step 7. End

14/02/2019

Page 17: enm 104 - 1 Algorithms

Problem 3 - loopDesign an algorithm which generates even numbers between 1000 and 2000 and then prints them in the standard output. It should also print total sum: Inputs to the algorithm : numbers in [1000,2000]Expected output : even numbers in1000 to 2000, total sum S

Algorithm: 1. Start2. I ← 1000 and S ← 03. Write I4. S ← S + I5. I ← I + 26. If (I <= 2000) then go to line 3 else go to line 77. Write S8. End

14/02/2019

Page 18: enm 104 - 1 Algorithms

Problem 3 - loopDesign an algorithm with a natural number, n, as its input which calculates the following formula and writes the result in the standard output: ! = #

$ +#& +⋯+ #

(Inputs to the algorithm : nExpected output : result of given formula, S

Algorithm: 1. Start 2. Read n 3. I ← 2 and S ← 0 4. S= S + 1/I 5. I ← I + 2 6. If (I <= n) then go to line 4 else write S in standard output 7. End

14/02/2019

Page 19: enm 104 - 1 Algorithms

Properties of algorithm

Page 20: enm 104 - 1 Algorithms

Properties of algorithms• Finiteness:

– An algorithm must always terminate after a finite number of steps.It means after every step one reach closer to solution of the problem and after a finite number of steps algorithm reaches to an end point.

• Definiteness: – Each step of an algorithm must be precisely defined.

It is done by well thought actions to be performed at each step of the algorithm. Also the actions are defined unambiguously for each activity in the algorithm.

• Input: – Any operation you perform need some beginning value/quantities associated

with different activities in the operation. So the value/quantities are given to the algorithm before it begins.

14/02/2019

Page 21: enm 104 - 1 Algorithms

Properties of algorithms• Output:

– One always expects output/result (expected value/quantities) in terms of output from an algorithm. The result may be obtained at different stages of the algorithm. If some result is from the intermediate stage of the operation then it is known as intermediate result and result obtained at the end of algorithm is known as end result. The output is expected value/quantities always have a specified relation to the inputs

• Effectiveness: – Algorithms to be developed/written using basic operations. Actually

operations should be basic, so that even they can in principle be done exactly and in a finite amount of time by a person, by using paper and pencil only.

14/02/2019

Page 22: enm 104 - 1 Algorithms

FLOWCHART

Page 23: enm 104 - 1 Algorithms

Flowchart SymbolsSymbol Name Function

Process Indicates any type of internal operation inside the Processor or Memory

Input / output Used for any Input / Output (I/O) operation. Indicates that the computer is to obtain data or output results

Decision Used to ask a question that can be answered in a binary format (Yes/No, True/False)

Connector Allows the flowchart to be drawn without intersecting lines or without a reverse flow.

Predefined process Used to invoke a subroutine or an Interrupt program.

Terminal Indicates the starting or ending of the program, process, or interrupt program

Flow Lines Shows direction of flow.

14/02/2019

Page 24: enm 104 - 1 Algorithms

Problem1Find the area of a circle of radius r

14/02/2019

start

Read r

Area = 3.14 * r * r

Print Area

end

Page 25: enm 104 - 1 Algorithms

Problem 2Convert temperature Fahrenheit (F) to Celsius (C)

14/02/2019

start

Read F

C= 5/9 * (F - 32)

Print C

end

Page 26: enm 104 - 1 Algorithms

Problem 3Flowchart for an algorithm which gets two numbers and prints sum of their value

14/02/2019

start

Read A, B

C= A + B

Print C

end

Page 27: enm 104 - 1 Algorithms

Problem 5Algorithm for find the greater number between two numbers.

14/02/2019

start

Read A, B

Print A

end

If A > B

Print B

true false

Page 28: enm 104 - 1 Algorithms

Problem 6Flowchart for an algorithm which gets two numbers and prints sum of their value

14/02/2019

start

Write I

I ß 8

end

I ß I +2

If I<=98