# Algorithms

Definition: An algorithm is a set of finite, well defined steps which outlines the solution to a specific problem.

Alternative Definition : WHAT IS AN ALGORITHM? – DEFINITION

We use algorithms every day to solve problems, weather we’re conscious of it or not.

When we cook a new dish we follow a recipe, when we solve a math problem we follow a procedure.

In our case, we’d like to document the solution to a specific problem using an algorithm so that we can communicate it to a programmer , or create the tool  as a program itself.

We use algorithms, but what are the properties of a good algorithm?

• Vague recipe instructions, what exactly is 2 cups of flour?
• A videogame walkthrough that isn’t descriptive enough
• A video tutorial that talks  a lot about becoming a youtuber , but the steps aren’t outlined in order.

What are the properties of good instructions, good algorithms?

## Properties of good algorithms

• Has a  finite number of steps ( a definite beginning and an end)
• Is precise,
• Is unambiguous (not subject to interpretation)
• Shows the flow of control from one sub-process to another
• Must output results
• It must terminate.

# Algorithms In Narrative Form

An algorithm in narrative form is not written in discrete steps.   It is expressed as directions in paragraph form and instructions on how rules should be followed.

An algorithm in narrative form can be considered a precursor to pseudocode or a flowchart

## Example

An example is given below for the given problem statement

Problem Definition: A solution is needed which finds the circumference of a circle.

Algorithm in narrative form: Prompt the user to enter a non-negative value for the radius and store it. If the radius entered is negative or is not a number, keep on prompting the user and accept values until a suitable value is entered.  Use the formula C=2*3.14* r to calculate and store the circumference. Output the circumference to the user and stop processing.

## In class exercise

Write an algorithm in narrative form which gives instructions/directions on how to follow the rules in order to play “Snakes and ladders”.

# WHAT IS AN ALGORITHM? – DEFINITION

An algorithm is a well-defined, precise, finite set  of steps for solving a problem.

# The Importance of algorithms

In our discussion, we consider well defined precise algorithms (in flowcharts or pseudocode) as compared with narrative algorithms.

Some of the reasons why algorithms are beneficial or important to the problem solving process are:

1. Algorithms serve as a blueprint for other programmers to follow

2. When problems are complex, an algorithm’s precise structure allows for an accurate development of a solution in parts.  This documentation (the algorithm itself) is useful since it is difficult for  the problem solver to remember many sub-processes , procedures and inputs/outputs to the problem.

3. Because algorithms are intended for a computer system and programming language, it allows us to design for that particular programming language’s structure and for efficiency ,given    the limitations of system resources.

For example, an algorithm which processes DNA sequence information for matching DNA strands together (see Longest common sub sequence) can be developed strictly for solving the problem without considering system resources.   In this case the algorithm still resembles programming language.

Usually in this type of general solution, array sizes are allowed to be very large or of infinite size. If memory becomes a constraint, we can now modify the algorithm to use say, array sizes at a maximum of  1000 to allow for the intended program to run within the limitation of our system resources.

4. Algorithms are easily testable when using dry runs and trace tables.