1
Chapter 5
AlgorithmsAlgorithms
By:
J. BrookshearJ. Brookshear
Modified by:
Yacoub Sabatin, BEng, MScYacoub Sabatin, BEng, MSc
© 2005 Pearson Addison-Wesley. & 2014 Yacoub Sabatin. All rights reserved
2
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
3
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.
4
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
3
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
5
Figure 5.2 Folding a bird from a square piece of paper
© 2005 Pearson Addison-Wesley. & 2014 Yacoub Sabatin. All rights reserved
6
Figure 5.3 Origami primitives
4
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
7
Pseudocode primitives
• Assignment name� expression
• Conditional selection if condition then action
• Repeated execution while condition do activity
• Procedure procedure name (generic names)
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
8
Figure 5.4 The procedure Greetings in pseudocode
5
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
9
Figure 5.6 The sequential search algorithm in pseudocode
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
10
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.
6
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
11
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
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
12
Sample problem
• Person A is charged with the task of determining the ages of B’s three children.
• B tells A that the product of the children’s ages is 36.
• A replies that another clue is required.
• B tells A the sum of the children’s 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?
7
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
13
Figure 5.5
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
14
Figure 5.7 Components of repetitive control
8
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
15
Figure 5.8 The while loop structure
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
16
Figure 5.9 The repeat loop structure
9
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
17
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
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
18
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
10
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
19
Figure 5.12 Applying our strategy to search a list for the entry John
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
20
Figure 5.13 A first draft of the binary search technique
11
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
21
Figure 5.14 The binary search algorithm in pseudocode
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
22
Figure 5.15
12
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
23
Figure 5.16
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
24
Figure 5.17
13
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
25
Software efficiency
• Measured as number of instructions executed
• Θ notation for efficiency classes
– Example: insertion sort is Θ(n2)
• Best, worst, and average case
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
26
Figure 5.18 Applying the insertion sort in a worst-case situation
14
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
27
Figure 5.19 Graph of the worst-case analysis of the insertion sort algorithm
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
28
Figure 5.20 Graph of the worst-case analysis of the binary search algorithm
15
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
29
Software verification
• Proof of correctness
– Assertions
• Preconditions
• Loop invariants
• Testing
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
30
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?
16
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
31
Figure 5.21 Separating the chain using only three cuts
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
32
Figure 5.22 Solving the problem with only one cut
17
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
33
Figure 5.23 The assertions associated with a typical while structure
Chapter 6
Programming
Languages
By:
J. BrookshearJ. Brookshear
Modified by:
Yacoub Sabatin, BEng, MScYacoub Sabatin, BEng, MSc
18
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
35
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
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
36
Figure 6.1 Generations of programming languages
19
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
37
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
what’s in chapter 2)
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
38
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
20
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
39
Assembly language example
Machine language
156C
166D
5056
30CE
C000
Assembly language
LD R5, Price
LD R6, ShippingCharge
ADDI R0, R5 R6
ST R0, TotalCost
HLT
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
40
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.
21
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
41
Figure 6.2 The evolution of programming paradigms
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
42
Figure 6.3 A function for checkbook balancing constructed from simpler functions
22
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
43
Figure 6.4 The composition of a typical imperative program or program unit
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
44
Figure 6.5 Variable declarations in C, C++, C#, and Java
23
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
45
Figure 6.6 A two-dimensional array with two rows and nine columns
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
46
Figure 6.7 Declaration of heterogeneous array
24
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
47
Figure 6.8Control structures and their representations in C, C++, C#, and Java
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
48
Figure 6.9 The for loop structure and its representation in C++, C#, and Java
25
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
49
Figure 6.10 The flow of control involving a procedure
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
50
Figure 6.11 The procedure ProjectPopulation written in the programming language C
26
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
51
Figure 6.12Executing the procedure Demo and passing parameters by value
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
52
Figure 6.13Executing the procedure Demo and passing parameters by reference
27
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
53
Figure 6.14 The function CylinderVolume written in the programming language C
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
54
Figure 6.21 The complete program preparation process
28
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
55
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.
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
56
Figure 6.16 A syntax diagram of our if-then-else pseudocode statement
29
57
Figure 6.17 Syntax diagrams describing the structure of a simple algebraic expression
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
58
Figure 6.18 The parse tree for the string x + y x z based on the syntax diagrams in Figure 6.17
30
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
59
Figure 6.19Two distinct parse trees for the statement if B1 then if B2 then S1 else S2
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
60
Figure 6.20 An object-oriented approach to the translation process
31
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
61
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.
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
62
Components of an object
• Instance variable = variable within an object
• Method = function or procedure within an
object
– Can manipulate the object’s instance variables
• Constructor = special method to initialize a
new object instance
32
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
63
Encapsulation
• Encapsulation = a way of restricting access to
the internal components of an object
– Private
– Public
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
64
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
33
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
65
Figure 6.22 The structure of a class describing a laser weapon in a computer game
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
66
Figure 6.23 A class with a constructor
34
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
67
Figure 6.24 Our LaserClass definition using encapsulation as it would appear in a Java or C# program
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
68
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
35
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
69
Figure 6.25 Spawning processes
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
70
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
36
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
71
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
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
72
Figure 6.26 Resolving the statements (P OR Q) and (R OR ¬Q) to produce (P OR R)
37
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
73
Figure 6.27 Resolving the statements (P OR Q), (R OR ¬Q), ¬R, and ¬P
© 2005 Pearson Addison-Wesley. & 2010 Yacoub Sabatin. All rights reserved
74
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.