Вы находитесь на странице: 1из 37

Chapter 5

Algorithms

By:
J. Brookshear
Modified by:
Yacoub Sabatin, BEng, MSc

Chapter 5: Algorithms

5.1 The Concept of an Algorithm


5.2 Algorithm Representation
5.3 Algorithm Discovery
5.4 Iterative Structures
5.5 Recursive Structures
5.6 Efficiency and Correctness

2
2005 Pearson Addison-Wesley. & 2014 Yacoub Sabatin. All rights reserved

1
Algorithm: definition
Before a computer can perform a task, it must be
given an algorithm telling it precisely what to do.
An algorithm is an ordered set of unambiguous,
executable steps that defines a terminating
process.
Requirements of an algorithm must comply with
these definition.

3
2005 Pearson Addison-Wesley. & 2014 Yacoub Sabatin. All rights reserved

Algorithms: levels of abstraction


Problem = motivation for algorithm
Algorithm = procedure to solve the problem
Often one of many possibilities
Representation = description of algorithm
sufficient to communicate it to the desired
audience
Always one of many possibilities (algebraic, oral, flowchart,
electronic ckt, comp prog)

Distinguish between:
Algorithm: abstract, conceptual
Its representation 4

2
Figure 5.2 Folding a bird from a
square piece of paper

5
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.3 Origami primitives

6
2005 Pearson Addison-Wesley. & 2014 Yacoub Sabatin. All rights reserved

3
Pseudocode primitives

Assignment name  expression


Conditional selection if condition then action
Repeated execution while condition do activity
Procedure procedure name (generic names)

7
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.4 The procedure


Greetings in pseudocode

8
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

4
Figure 5.6 The sequential search
algorithm in pseudocode

9
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Problem solving steps

1. Understand the problem.


2. Get an idea how an algorithmic procedure
might solve the problem.
3. Formulate the algorithm and represent it as a
program.
4. Evaluate the program for accuracy and its
potential as a tool for solving other problems.

10
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

5
Techniques for
getting a foot in the door

Work the problem backwards


Solve an easier related problem
Relax some of the problem constraints
Solve pieces of the problem first = bottom up
methodology
Stepwise refinement = top-down methodology
Popular technique because it produces modular
programs

11
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Sample problem

Person A is charged with the task of determining the


ages of Bs three children.
B tells A that the product of the childrens ages is 36.
A replies that another clue is required.
B tells A the sum of the childrens ages.
A replies that another clue is needed.
B tells A that the oldest child plays the piano.
A tells B the ages of the three children.
How old are the three children?

12
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

6
Figure 5.5

13
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.7 Components of


repetitive control

14
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

7
Figure 5.8 The while loop
structure

15
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.9 The repeat loop


structure

16
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

8
Figure 5.10 Sorting the list Fred, Alex,
Diana, Byron, and Carol alphabetically

Insertion Sort:
Sort the list within itself.
Take a sublist, then sort
it alphabetically.
Details, textbook, p196

17
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.11 The insertion sort


algorithm expressed in pseudocode

Insertion Sort:
Sort the list within itself.
Take a sublist, then sort
it alphabetically.
Details, textbook, p196
18
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

9
Figure 5.12 Applying our strategy to
search a list for the entry John

19
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.13 A first draft of the


binary search technique

20
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

10
Figure 5.14 The binary search
algorithm in pseudocode

21
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.15

22
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

11
Figure 5.16

23
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.17

24
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

12
Software efficiency

Measured as number of instructions executed


notation for efficiency classes
Example: insertion sort is (n2)
Best, worst, and average case

25
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.18 Applying the insertion sort


in a worst-case situation

26
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

13
Figure 5.19 Graph of the worst-case
analysis of the insertion sort algorithm

27
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.20 Graph of the worst-case


analysis of the binary search algorithm

28
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

14
Software verification

Proof of correctness
Assertions
Preconditions
Loop invariants
Testing

29
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Example problem: Chain


separating

A traveler has a gold chain of seven links.


He must stay at an isolated hotel for seven nights.
The rent each night consists of one link from the chain.
What is the fewest number of links that must be cut so
that the traveler can pay the hotel one link of the chain
each morning without paying for lodging in advance?

30
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

15
Figure 5.21 Separating the chain
using only three cuts

31
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 5.22 Solving the problem


with only one cut

32
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

16
Figure 5.23 The assertions
associated with a typical while
structure

33
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Chapter 6
Programming
Languages

By:
J. Brookshear
Modified by:
Yacoub Sabatin, BEng, MSc

17
Chapter 6: Programming
Languages

6.1 Historical Perspective


6.2 Traditional Programming Concepts
6.3 Procedural Units
6.4 Language Implementation
6.5 Object Oriented Programming
6.6 Programming Concurrent Activities
6.7 Declarative Programming

35
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.1 Generations of


programming languages

36
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

18
Second-generation:
Assembly language

A mnemonic system for representing programs


Mnemonic names for op-codes
Names for all registers
Identifiers = descriptive names for memory
locations, chosen by the programmer
The machine language considered as the first-
generation, ex. Instruction Set (similar to
whats in chapter 2)
37
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Assembly language characteristics

One-to-one correspondence between machine


instructions and assembly instructions
Programmer must think like the machine
Inherently machine-dependent
Converted to machine language by a program
called an assembler

38
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

19
Assembly language example

Machine language Assembly language

156C LD R5, Price


166D LD R6, ShippingCharge
5056 ADDI R0, R5 R6
30CE ST R0, TotalCost
C000 HLT

39
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Third generation language


Uses high-level primitives
Similar to our pseudo-code in Chapter 5
Machine independent (mostly)
Examples: C, Basic, FORTRAN, COBOL
Each primitive corresponds to a short sequence
of machine language instructions
Converted to machine language by a program
called a compiler
Some languages are interpreted i.e. require
Interpreter, ex. Basic.
40
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

20
Figure 6.2 The evolution of
programming paradigms

41
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.3 A function for checkbook


balancing constructed from simpler
functions

42
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

21
Figure 6.4 The composition of a
typical imperative program or
program unit

43
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.5 Variable declarations


in C, C++, C#, and Java

44
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

22
Figure 6.6 A two-dimensional
array with two rows and nine
columns

45
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.7 Declaration of


heterogeneous array

46
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

23
Figure 6.8
Control
structures
and their
representations
in C, C++, C#,
and Java

47
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.9 The for loop structure


and its representation in C++, C#,
and Java

48
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

24
Figure 6.10 The flow of control
involving a procedure

49
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.11 The procedure


ProjectPopulation written in the
programming language C

50
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

25
Figure 6.12
Executing the
procedure Demo
and passing
parameters by
value

51
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.13
Executing the
procedure Demo
and passing
parameters by
reference

52
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

26
Figure 6.14 The function
CylinderVolume written in the
programming language C

53
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.21 The complete


program preparation process

54
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

27
Figure 6.15 The translation process

Lexical Analysis:
The process of recognizing which strings of symbols represent a single
entity.
Analyzer reads the source symbol by symbol & identify which group of
symbols are related.
Parser: Views program in terms of lexical units (tokens) rather
than individual symbols, to form a statement in the language.
Code Generation: Machine-language constructing to implement
the statements recognized by the parser.
55
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.16 A syntax diagram


of our if-then-else pseudocode
statement

56
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

28
Figure 6.17 Syntax diagrams
describing the structure of a
simple algebraic expression

57

Figure 6.18 The parse tree for


the string x + y x z based on the
syntax diagrams in Figure 6.17

58
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

29
Figure 6.19
Two distinct
parse trees for
the statement
if B1 then if B2
then S1 else S2

59
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.20 An object-oriented


approach to the translation
process

60
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

30
Objects and Classes

Object = active program unit containing both


data and procedures
Class = a template for all objects of the same
type (blueprint)

An Object is often called an instance of the class.

61
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Components of an object

Instance variable = variable within an object


Method = function or procedure within an
object
Can manipulate the objects instance variables
Constructor = special method to initialize a
new object instance

62
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

31
Encapsulation

Encapsulation = a way of restricting access to


the internal components of an object
Private
Public

63
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Additional object-oriented
concepts

Inheritance: allows new classes to be defined in


terms of previously defined classes
Polymorphism: allows method calls to be
interpreted by the object that receives the call

64
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

32
Figure 6.22 The structure of a
class describing a laser weapon in
a computer game

65
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.23 A class with a


constructor

66
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

33
Figure 6.24 Our LaserClass definition using
encapsulation as it would appear in a Java
or C# program

67
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Programming concurrent activities

Parallel or concurrent processing =


simultaneous execution of multiple processes
True concurrent processing requires multiple CPUs
Can be simulated using time-sharing with a single
CPU

68
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

34
Figure 6.25 Spawning processes

69
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Interaction between processes

Mutual exclusion = a method for ensuring that


data can be accessed by only one process at a
time
Monitor = a data item augmented with the
ability to control access to itself

70
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

35
Declarative programming

Resolution = combining two or more statements to


produce a new, logically equivalent statement
Example: (P OR Q) AND (R OR Q)
resolves to (P OR R)
Resolvent = a new statement deduced by resolution
Clause form = statement whose elementary components are
connected by the Boolean operation OR
Unification = assigning a value to a variable in a
statement

71
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Figure 6.26 Resolving the statements


(P OR Q) and (R OR Q) to produce
(P OR R)

72
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

36
Figure 6.27 Resolving the
statements (P OR Q), (R OR Q),
R, and P

73
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

Prolog

Fact = predicateName(arguments).
Example: parent(bill, mary).

Rule = conclusion :- premise.


:- means if
Example: wise(X) :- old(X).
Example: faster(X,Z) :- faster(X,Y), faster(Y,Z).

All statements must be fact or rules.

74
2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved

37

Вам также может понравиться