Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

21
Algorithms & Flowcharts Lecture 10

Transcript of Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Page 1: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Algorithms & Flowcharts Lecture 10

Page 2: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Algorithm’s CONCEPTAlgorithm’s CONCEPTAlgorithm’s CONCEPTAlgorithm’s CONCEPT

Page 3: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Solving Problems

When faced with a problem:

1. We first clearly define the problem

2. Think of possible solutions

3. Select the one that we think is the best under the prevailing circumstances

4. And then apply that solution

5. If the solution works as desired, fine; else we go back to step 2

Page 4: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

al.go.rithmSequence of steps

that can be taken to solve a given problem

Page 5: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Algorithm (Better Definition)

1st Definition:Sequence of steps that can be taken to

solve a problem

Better Definition:A precise sequence of a limited number

of unambiguous, executable steps that terminates in the form of a solution

Page 6: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Examples

• Addition

• Conversion from decimal to binary

• The process of boiling an egg

• The process of mailing a letter

• Sorting

• Searching

Page 7: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Informal definition of an algorithm used in a computer

Page 8: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Finding the largest integer among five integers

Page 9: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Defining actions in FindLargest algorithm

Page 10: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

FindLargest refined

Page 11: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Generalization of FindLargest

Page 12: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

THREE BUILDING BLOCKSTHREE BUILDING BLOCKSTHREE BUILDING BLOCKSTHREE BUILDING BLOCKS

Page 13: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Sequences

A sequence of instructions that are executed in the precise order they are written in:

statement block 1

statement block 2

statement block 3

statement block 1

statement block 2

statement block 3

Page 14: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

DecisionsSelect between alternate courses of action depending upon the evaluation of a condition

If ( condition = true )

statement block 1

Else

statement block 2

End ifstatement

block 1

conditionTrue False

statementblock 2

Page 15: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Repetitions/Loops

Loop through a set of statements as long as a condition is true

Loop while ( condition = true )

statement block

End Loop

conditionTrue

False

statementblock

Page 16: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Three building blocks

Page 17: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Algorithm TYPESAlgorithm TYPESAlgorithm TYPESAlgorithm TYPES

Page 18: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Greedy Algorithm

• An algorithm that always takes the best immediate, or local solution while finding an answer

• Greedy algorithms may find the overall or globally optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems

• KEY ADVANTAGE: Greedy algorithms are usually faster, since they don't consider the details of possible alternatives

Page 19: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Deterministic Algorithm

• An algorithm whose behavior can be completely predicted from the inputs

• That is, each time a certain set of input is presented, the algorithm gives the same results as any other time the set of input is presented

Page 20: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Randomized Algorithm

• Any algorithm whose behavior is not only determined by the input, but also values produced by a random number generator

• These algorithms are often simpler and more efficient than deterministic algorithms for the same problem

• Simpler algorithms have the advantages of being easier to analyze and implement

Page 21: Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.

Syntax & SemanticsAn algo. is “correct” if its:

– Semantics are correct– Syntax is correct

Semantics:The concept embedded in an algorithm (the soul!)

Syntax:The actual representation of an algorithm (the body!)

WARNINGS:

1. An algo. can be syntactically correct, yet semantically incorrect – very dangerous situation!

2. Syntactic correctness is easier to check as compared with semantic