Topic 4 Notes
Topic 4 Notes
4.5 Pseudocode
It is used primarily as a rather short-term communication between members working on a specific
project - the code itself and other documents used for long-term archival purposes. Pseudocode
tends to be much more informal and a case of "whatever works". Some people choose to write
their pseudocode very much as though it were a true programming language with very formal
constructs Some people write their pseudocode almost like a free-verse description of what the
program needs to do.
Selection - Case 1
SEL: (test condition)
TRUE:
Statement(s) to be executed if test condition is TRUE
FALSE
Statement(s) to be executed if test condition is FALSE
Selection - Case 2
IF: (test condition)
Statement(s) to be executed if test condition is TRUE
ELSE:
Statement(s) to be executed if test condition is FALSE
Repetition - Case 1
LOOP:
WHILE: (test condition)
Statement(s) to be executed if test condition is TRUE
Repetition - Case 2
LOOP:
Statement(s) to be executed if test condition is TRUE
WHILE: (test condition)
4.6 Flowcharts
Flowcharts are a graphical means of representing an algorithm, as should be expected, they have
advantages and disadvantages compared to pseudocode.
The idea behind a flowchart is that it links together a series of blocks each of which perform some
specific task. Each of these tasks is represented by a block and has exactly one arrow leading to it
and, more importantly, one arrow exiting from it. This is key to the concept of a "structured
program".
The shape of the block may convey additional information about what is happening. For instance,
a rectangular block is frequently used to indicated that a computation is occurring while a slanted
parallelogram is used to indicate some type of input or output operation. The diversity of shapes
that can be used and what they mean is staggering - for instance a different shape can be used to
indicated output to a tape drive versus to a hard disk or to indicate output in text format verses
binary format. By using such highly specialized symbols, much of what is happening can be
conveyed by the symbols themselves. But the power of using these distinctions is generally only
useful to people that work with flowcharts continuously, professionally, and who are describing
very large and complex systems.
5. The hardware:
Backtracking algorithms
Based on a depth-first recursive search
A backtracking algorithm:
Tests to see if a solution has been found, and if so, returns it; otherwise
For each choice that can be made at this point,
Make that choice
Recur
If the recursion returns a solution, return it
If no choices remain, return
failure
Greedy algorithms
A “greedy algorithm” works well for optimization problems •Works in phases:
At each phase:
You take the best you can get right now, without regard for future consequences –You hope that
by choosing a local optimum at each step, you will end up at a global optimum
Example: Counting money
Suppose you want to count out a certain amount of money, using the fewest possible bills and
coins
A greedy algorithm would do this would be:
At each step, take the largest possible bill or coin that does not overshoot –Example: To make
$6.39, you can choose:
a $5 bill
a $1 bill, to make $6
a 25¢ coin, to make $6.25
A 10¢ coin, to make $6.35
four 1¢ coins, to make $6.39
For US money, the greedy algorithm always gives the optimum solution
A failure of the greedy algorithm
Randomized algorithms
A randomized algorithm uses a random number at least once during the computation to make a
decision
Example: In Quicksort, using a random number to choose a pivot –Example: Trying to factor a
large prime by choosing random numbers as possible divisors
Revision questions
1. What is the relationship between an algorithm and program?
2. Explain the process of developing and algorithm
3. What considerations should be taken into account when developing an algorithm
4. Describe the types of algorithms can be developed
5. Explain the importance of flowcharts in algorithm representation