Algo Lecture01
Algo Lecture01
Agriculture, Multan
CS-408
Design and Analysis of Algorithm
Please be on time
(better: before time)
Learning Goals for Today
• Course objectives and outcomes
9
Examples
• Addition
• Conversion from decimal to binary
• The process of boiling an egg
• The process of mailing a letter
• Sorting
• Searching
10
Algorithm Applications
A large variety of problems in computer science, mathematics and other disciplines
depend on the use of algorithms for their solutions. The broad categories of
applications types are:
• •Searching Algorithms(Linear and non-linear)
• •Sorting Algorithms(Elementary and Advanced)
• •Strings Processing( Pattern matching, Parsing, Compression, Cryptography)
• •Optimization Algorithms(Shortest routes, minimum cost)
• •Geometric Algorithms( Triangulation, Convex Hull)
• •Image Processing( Compression, Matching, Conversion)
• •Data Mining Algorithms( Clustering, Cleansing, Rules mining)
• •Mathematical Algorithms(Random number generator, matrix operations, FFT, etc)
• Problem Domains
11
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
12
What is Algorithm?
• A computer algorithm is a detailed step-by-step method
for solving a problem by using a computer.
Input output
Algorithm
Three Requirements:
1. Sequence is:
a. Precise
b. Consists of a limited number of steps
Determine the 4
sequence of cities
such that the
traveling distance is 6
minimized 19
The Brute Force Strategy
• A strategy in which all possible combinations are
examined and the best among them is selected
21
Pseudo Code
• Language that is typically used for writing
algorithms
23
Start or stop
Process
Input or output
Flowchart
Elements Decision
Flow line
Connector
Off-page connector 24
Sequences
A sequence of instructions that are executed
in the precise order they are written in:
statement block 1
statement block 1
statement block 2 statement block 2
statement block 3
statement block 3
25
Conditionals
Select between alternate courses of action
depending upon the evaluation of a condition
If ( condition = true )
statement block 1 True False
Else condition
statement block 2
End if statement statement
block 1 block 2
26
Loops
Loop through a set of statements as long as a
condition is true
statement True
condition
block
False
27
We will now present the algorithm for a
problem whose solution is familiar to us
1. Pseudo code
2. Flowchart
3. Actual code
28
Problem Statement
Convert a decimal number into binary
29
Convert 75 to Binary
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
2 1 0
0 1
1001011 30
Solution in Pseudo Code
1. Let the decimal number be an integer x, x > 0
2. Let the binary equivalent be an empty string y
3. Repeat while x > 0 {
Determine the quotient & remainder of x
÷2
y = CONCATENATE( remainder, y )
x = quotient
}
4. Print y
31
5. Stop
Q: Is this the only possible algorithm for
converting a decimal number into a binary
representation?
32
Tips on Writing Good Pseudo Code
• Use indention for improved clarity
• Do not put “code” in pseudo code – make your
pseudo code language independent
• Don’t write pseudo code for yourself – write it
in an unambiguous fashion so that anyone with
a reasonable knowledge can understand and
implement it
• Be consistent
• Prefer formulas over English language
descriptions 33
Start Flowchart of Decimal
to Binary Conversion
Get x
Print y
x is the decimal number
Stop y is the binary equivalent
34
1. Does the flowchart depict the “correct”
algorithm?
2. What do we mean by “correct”, or better
yet, what do we check for
“correctness”?
3. One way is to check the algorithm for a
variety of inputs
4. Does it perform satisfactorily for:
• x=0?
• negative numbers?
• numbers with fractional parts? 35
Decimal to Binary Conversion in C
language
36
Another Example: Sorting
Sort the following objects w.r.t. their heights
37
Expected Result
38
Strategy
There are many strategies for solving this
problem. We demonstrate a simple one:
Swap it with the one next to it if they are in the wrong order
Keep on repeating until you reach the last object in the list
39
Back to the Objects to be Sorted
40
Q: Is the list sorted?
A: No
41
Sorting: Step A1
42
Sorting: Step A1
Swap? Yes
43
Sorting: Step A2
44
Sorting: Step A2
Swap? Yes
45
Sorting: Step A3
46
Sorting: Step A3
Swap? No
47
Sorting: After Step A7
48
Q: Is the list sorted?
A: No
49
Sorting: Step B1
50
Sorting: Step B1
Swap? Yes
51
Sorting: Step B2
52
Sorting: Step B2
Swap? No
53
Sorting: After Step B7
54
Q: Is the list sorted?
A: No
55
Sorting: Step C1
56
Sorting: Step C1
Swap? No
57
Sorting: After Step C7
58
Q: Is the list sorted?
A: Yes
59
STOP
60
Pros and Cons of Flowcharts (1)
• The process of writing an algorithm in the form
of a flowchart is just too cumbersome(bulky)
61
Pros and Cons of Flowcharts (2)
• The good thing about flowcharts is that their
symbols are quite intuitive and almost
universally understood
62
Pros and Cons of Pseudo Code (1)
• Quite suitable for SW development as it is
closer in form to real code
64
Difference between Algorithm, Flowchart
and Pseudo Code
• An algorithm is a sequence of instructions used
to solve a particular problem
• Flowchart and Pseudo code are tools to
document and represent the algorithm.
• In other words, an algorithm can be
represented using a flowchart or a pseudo
code.
65
In Today’s Lecture, We …
Became familiar with the concept of algorithms:
– What they are?
– What is their use?
– What do they consist of?
– What are the techniques used for representing
them?
• In particular, we looked at the building blocks that
are used in all algorithms
• We also discussed the pseudo code and flowcharts
for particular problems
• In addition, we outlined the pros and cons of those
two techniques 66