Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithms and Algorithmic Problem Solving, Study notes of Computer Applications

This document introduces the concept of algorithms and algorithmic problem solving. It covers the characteristics of algorithms, building blocks of algorithms, algorithm notations, and examples of algorithms. the importance of algorithms in computer programming and the qualities of a good algorithm. It also discusses the analysis of algorithms and the different notations used to express them. illustrative problems to help readers understand the concept of algorithmic problem solving.

Typology: Study notes

2022/2023

Available from 02/10/2024

karthik-a-nair
karthik-a-nair 🇮🇳

5 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
UNIT 1
ALGORITHMIC PROBLEM SOLVING
Algorithms, building blocks of algorithms (statements, state, control flow, functions), notation
(pseudo code, flow chart, programming language), algorithmic problem solving, simple strategies
for developing algorithms (iteration, recursion). Illustrative problems: find minimum in a list, insert a
card in a list of sorted cards, guess an integer number in a range, Towers of Hanoi.
1.1 ALGORITHM
What is algorithm?
An algorithm is a collection of well-defined, unambiguous and effectively
computable instructions ,if execute it will return the proper output.
well-defined- The instructions given in an algorithm should be simple and defined well.
Unambiguous- The instructions should be clear,there should not be ambiguity.
effectively computable- The i nstructions should be written step by step ,which helps
computer to understand the control flow .
PROGRAM = ALGORITHM + DATA
Algorithm is defined as “a sequence of instructions designed in such a way that if the instructions
are executed in the specified sequence, the desired result will be obtained”.
It is also defined as “any problem whose solution can be expressed in a list of executable
instruction”.
Set of step-by-step instructions that perform a specific task or operation
Natural language NOT programming language.
Algorithm is the sequence of steps to be performed in order to solve a problem by the
computer.
Three reasons for using algorithms are efficiency, abstraction and reusability.
Algorithms can be expressed in many different notations, including natural languages,
pseudocode, flowcharts and programming languages.
Analysis of algorithms is the theoretical study of computer program performance and
resource usage, and is often practiced abstractly without the use of specific programming
language or implementation.
The practical goal of algorithm analysis is to predict the performance of different
algorithms in order to guide program design decisions.
Most algorithms do not perform the same in all cases; normally an algorithm‟s
performance varies with the data passed to it.
Typically, three cases are recognized: the best case, average case and worst case
Worst case analysis of algorithms is considered to be crucial to applications such as
games, finance and robotics.
O-notation, also known as Big O-notation, is the most common notation used to express an
algorithm‟s performance in a formal manner.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Algorithms and Algorithmic Problem Solving and more Study notes Computer Applications in PDF only on Docsity!

UNIT – 1

ALGORITHMIC PROBLEM SOLVING

Algorithms, building blocks of algorithms (statements, state, control flow, functions), notation (pseudo code, flow chart, programming language), algorithmic problem solving, simple strategies for developing algorithms (iteration, recursion). Illustrative problems: find minimum in a list, insert a card in a list of sorted cards, guess an integer number in a range, Towers of Hanoi.

1.1 ALGORITHM

What is algorithm? An algorithm is a collection of well-defined, unambiguous and effectively computable instructions ,if execute it will return the proper output.

well-defined - The instructions given in an algorithm should be simple and defined well.

Unambiguous - The instructions should be clear,there should not be ambiguity.

effectively computable - The instructions should be written step by step ,which helps computer to understand the control flow.

PROGRAM = ALGORITHM + DATA

Algorithm is defined as “a sequence of instructions designed in such a way that if the instructions are executed in the specified sequence, the desired result will be obtained”.

It is also defined as “any problem whose solution can be expressed in a list of executable instruction”.

 Set of step-by-step instructions that perform a specific task or operation

 Natural language NOT programming language.

 Algorithm is the sequence of steps to be performed in order to solve a problem by the

computer.

 Three reasons for using algorithms are efficiency, abstraction and reusability.

 Algorithms can be expressed in many different notations, including natural languages,

pseudocode, flowcharts and programming languages.

 Analysis of algorithms is the theoretical study of computer program performance and

resource usage, and is often practiced abstractly without the use of specific programming language or implementation.

 The practical goal of algorithm analysis is to predict the performance of different

algorithms in order to guide program design decisions.

 Most algorithms do not perform the same in all cases; normally an algorithm‟s

performance varies with the data passed to it.

 Typically, three cases are recognized: the best case, average case and worst case

 Worst case analysis of algorithms is considered to be crucial to applications such as

games, finance and robotics.

 O-notation, also known as Big O-notation, is the most common notation used to express an

algorithm‟s performance in a formal manner.

Characteristics of algorithm

1. In the algorithm each and every instruction should be precise and unambiguous.

2. The instruction in an algorithm should not be repeated infinitely.

3. Ensure that the algorithm will ultimately terminate.

4. The algorithm should be written in sequence.

5. It looks like normal English.

6. The desired result should be obtained only after the algorithm terminates.

Qualities of a good algorithm

 Time - Lesser time required.

 Memory - Less memory required.

 Accuracy - Suitable or correct solution obtained.

 Sequence - Must be sequence and some instruction may be repeated in number of times or

until particular condition is met.

 Generability - Used to solve single problem and more often algorithms are designed to handle

a range of input data.

1.2 Building Blocks of Algorithms

Algorithms can be constructed from basic building blocks namely, sequence, selection and iteration.

1.2.1 Statements Statement is a single action in a computer.

In a computer statements might include some of the following actions

 input data-information given to the program

 process data-perform operation on a given input

 output data-processed result

1.2.2 State: Transition from one process to another process under specified condition with in a time is called state.

1.2.3 Control flow:

The process of executing the individual statements in a given order is called control flow. The control can be executed in three ways

1. sequence

2. selection

3. iteration

1.2.3.1 Sequence All the instructions are executed one after another is called sequence execution. Example:

Add two numbers:

Step 1 : Start Step 2 : get a,b Step 3 : calculate c=a+b Step 4 : Display c

1.2.4 Functions

 Function is a sub program which consists of block of code(set of instructions) that performs

a particular task.

 For complex problems, the problem is been divided into smaller and simpler tasks during

algorithm design. Benefits of Using Functions

 Reduction in line of code

 code reuse

 Better readability

 Information hiding

 Easy to debug and test

 Improved maintainability

Example:

Algorithm for addition of two numbers using function Main function() Step 1: Start Step 2: Call the function add() Step 3: Stop

sub function add() Step 1: Function start Step 2: Get a, b Values Step 3: add c=a+b Step 4: Print c Step 5: Return

Examples of algorithm

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 : Start Step 2 : Read input the Radius r of the Circle Step 3 : Area PIrr // calculation of area Step 4 : Print Area Step 5 : Stop

Problem2 : Write 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 : Start Step 2 : Read\input the first num1. Step 3 : Read\input the second num2. Step 4: Sum num1+num2 // calculation of sum Step 5 : Print Sum Step 6 : Stop

Problem 3 : Convert temperature Fahrenheit to Celsius Inputs to the algorithm: Temperature in Fahrenheit Expected output: Temperature in Celsius Algorithm: Step 1 : Start Step 2 : Read Temperature in Fahrenheit F Step 3 : C 5/9*(F-32) Step 4 : Print Temperature in Celsius: C Step 5 : End

Problem 4: Find the largest number between A and B Inputs to the algorithm : A, B Expected output : Largest A or B

1.3 Algorithm Notations(Expressing Algorithms)

As we know that ,an algorithm is a sequence of finite instructions,often used for calculation and data processing.

Algorithms can be expressed in many kinds of notation,including

1.3.1 PSEUDOCODE

Definition:-

 “Pseudo” means initiation or false.

 “Code” means the set of statements or instructions written in a programming language.

 Pseudocode is also called as “Program Design Language [PDL]”.

 Pseudocode is a Programming Analysis Tool, which is commonly used for

planning the program logic.

 Pseudocode is written in normal English and cannot be understood by the computer.

 Set of instructions that mimic programming language instructions

 An informal high-level description of the operating principle of a computer

program. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.

Pseudocode

Flowchart

Programming Language

Rules for writing Pseudocode

1. Write one statement per line

2. Capitalize initial keywords (READ, WRITE, IF, WHILE, UNTIL).

3. Indent to show hierarchy.

4. End multiline structure.

5. Keep statements language.

Advantages

 It can be done easily in any word processor.

 It can be easily modified as compared to flowchart.

 Its implementation is very useful in structured design elements.

 It can be written easily.

 It can be read and understood easily.

 Converting a pseudocode to programming language is very easy

compared with converting a flowchart to programming language.

Disadvantage

 It is not visual.

 We do not get a picture of the design.

 There is no standardized style or format.

 For a beginner, it is more difficult to follow the logic or write pseudocode as

compared to flowchart.

Pseudocode is made up of the following logic structure ,

 Sequential logic

 Selection logic

 Iteration logic

Sequence Logic

 It is used to perform instructions in a sequence,that is one after another

 Thus,for sequence logic ,pseudocode instructions are written in an order in which they are to be

performed. The logic flow of pseudocode is from top to bottom.

Pseudocode to add two numbers:

START READ a,b COMPUTE c by adding a &b PRINT c STOP

Selection Logic

 It is used for making decisions and for selecting the proper path out of two or more alternative

paths in program logic.

 It is also known as decision logic.

 Selection logic is depicted as either an IF..THEN or an IF…THEN..ELSE

Structure.

FOR i <= n, then DISPLAY i INCREMENT i END FOR END

Problem 4: Find the greater number between two numbers. BEGIN Read A, B IF A is less than B BIG = B SMALL = A ELSE BIG = A SMALL = B WRITE / DISPLAY BIG, SMALL END

Problem 5: To determine a student whether successful or fail. BEGIN READ student „s grade IF student's grade is greater than or equal to 50 Print "passed" ELSE

END

Print "failed“

1.3.2 FLOWCHARTS

Definitions: A flowchart is a schematic representation of an algorithm or a stepwise process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. Flowcharts are used in designing or documenting a process or program.

A flow chart, or flow diagram, is a graphical representation of a process or system that details the sequencing of steps required to create output.

A flowchart is a picture of the separate steps of a process in sequential order. The benefits of flowcharts are as follows:

1. Communication: Flowcharts are better way of communicating the

logic of a system to all concerned.

2. Effective analysis: With the help of flowchart, problem can be analysed in more

effective way.

3. Proper documentation: Program flowcharts serve as a good program

documentation, which is needed for various purposes.

4. Efficient Coding: The flowcharts act as a guide or blueprint during the

systems analysis and program development phase.

5. Proper Debugging: The flowchart helps in debugging process.

6. Efficient Program Maintenance: The maintenance of operating program

becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

The Limitations of using flowcharts :

1. Complex logic: Sometimes, the program logic is quite complicated. In

that case, flowchart becomes complex and clumsy.

2. Alterations and Modifications: If alterations are required the flowchart

may require re-drawing completely.

3. Reproduction: As the flowchart symbols cannot be typed, reproduction

of flowchart becomes a problem.

4. The essentials of what is done can easily be lost in the technical details of how it is

done.

GUIDELINES FOR DRAWING A FLOWCHART

Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required. Some standard symbols, which are frequently required for flowcharting many computer programs.

 Terminator:

An oval flow chart shape indicates the start or end of the process, usually containing the word “Start” or “End”.

Terminator

These are the basic symbols used generally. Now, the basic guidelines for drawing a flowchart with the above symbols are that:

 In drawing a proper flowchart, all necessary requirements should

be listed out in logical order.

 The flowchart should be neat, clear and easy to follow. There should

not be any room for ambiguity in understanding the flowchart.

 The flowchart is to be read left to right or top to bottom.

 A process symbol can have only one flow line coming out of it.

 For a decision symbol, only one flow line can enter it, but multiple

lines can leave it to denote possible answers.

 The terminal symbols can only have one flow line in conjunction with them.

Flowchart is made up of the following logic structure ,

 Sequential logic

 Selection logic

 Iteration logic

Sequence Logic

In a computer program or an algorithm, sequence involves simple steps which are to be executed one after the other. The steps are executed in the same order in which they are written.

Below is an example set of instructions to add two numbers and display the answer.

Flowchart to find factorial of given no

pre-test loop post-test

Problem 3 : Convert temperature Fahrenheit to Celsius.

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

Problem5 : Flowchart for the problem of printing even numbers between 0 and 99.