Upload
dangkhue
View
266
Download
2
Embed Size (px)
Citation preview
COMP 151 – Introduction to Algorithms 1
Introduction to Algorithms
Dr. Saleh Al-Hatali
COMP 151
Introduction to Algorithms
COMP 151 - Introduction to Algorithms 1
What is an Algorithm?
• A typical programming task can be divided into two phases:
• Problem solving phase
– Produces an ordered sequence of steps that describes the solution of problem
– This sequence of steps is called an algorithm
• Implementation phase
– This phase implements the program in some programming language
COMP 151 - Introduction to Algorithms 2
COMP 151 – Introduction to Algorithms 2
What is an Algorithm?
• Algorithm
–A sequence of unambiguous instructions to solve a problem
–A well-defined procedure that takes some value(s) as input, process it and produces some value(s), as output
COMP 151 - Introduction to Algorithms 3
What is an Algorithm?
“Computer”
Problem
Algorithm
Input Output
COMP 151 - Introduction to Algorithms 4
COMP 151 – Introduction to Algorithms 3
What is an Algorithm?
• Hence algorithm has three parts:
–Input
–Processing or transformation
–Output
COMP 151 - Introduction to Algorithms 5
Properties of an Algorithm• Finiteness
– Terminates in finite number of steps
• Definiteness– Each step must be clearly and carefully
specified
• Input– Valid inputs clearly specified
• Output– Can be proved to produce the correct output
given a valid input
• Effectiveness– Steps must be simple and atomic
COMP 151 - Introduction to Algorithms 6
COMP 151 – Introduction to Algorithms 4
Steps in Problem Solving
• First produce a general algorithm (one can use pseudocode)
• Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language.
COMP 151 - Introduction to Algorithms 7
What is Pseudocode?
• Pseudocode
– Artificial, informal language used to develop algorithms
– Similar to everyday English
– Understood by programmers
• Not executed on computers
– Used to think out a program before coding
• Easy to convert into C++ program
COMP 151 - Introduction to Algorithms 8
COMP 151 – Introduction to Algorithms 5
• Assignment
name = expression
set name = expression
name ���� expression
• Example
RemainingFunds = CheckingBalance +
SavingsBalance
Pseudocode Primitives
COMP 151 - Introduction to Algorithms 9
• Conditional selection
if (condition) then
activity
• Example
if (sales have decreased) then
lower the price by 5%
Pseudocode Primitives (continued)
COMP 151 - Introduction to Algorithms 10
COMP 151 – Introduction to Algorithms 6
• Conditional selection
if (condition) then
activity
else
activity
end if
• Example
if (year is leap year) then
daily total = total / 366
else
daily total = total / 365
end if
Pseudocode Primitives (continued)
COMP 151 - Introduction to Algorithms 11
• Repeated execution
while (condition)
body
• Examplewhile (tickets remain to be sold)
sell a ticket
Pseudocode Primitives (continued)
COMP 151 - Introduction to Algorithms 12
COMP 151 – Introduction to Algorithms 7
• Indentation shows nested conditions
if (not raining) then
if (temperature == hot) then
go swimming
else
play golf
else
watch television
Pseudocode Primitives (continued)
COMP 151 - Introduction to Algorithms 13
Pseudocode Example
�Write an algorithm to read the
final mark of a certain course
and determine if the student
passes or fails the course.
COMP 151 - Introduction to Algorithms 14
COMP 151 – Introduction to Algorithms 8
Pseudocode Example
Pseudocode:
Input the final mark
if mark is below 60
Print “FAIL”
else
Print “PASS”
COMP 151 - Introduction to Algorithms 15
Pseudocode Example
Detailed Algorithm:
Step 1: Input MARK
Step 2: if (MARK < 60) then
Print “FAIL”
else
Print “PASS”
endif
COMP 151 - Introduction to Algorithms 16
COMP 151 – Introduction to Algorithms 9
What is a Flowchart?
• A graphical representation of the sequence of operations in an information system or program
• A graphical way to express an algorithm
–“A picture is worth a thousand words”
COMP 151 - Introduction to Algorithms 17
Flowchart Example
“PASS”
Step 1: Input MARK
Step 2: if (MARK < 60) then
Print “FAIL”
else
Print “PASS”
endif
START
Input
MARK
IS
MARK<60
“FAIL”
STOP
YesNo
COMP 151 - Introduction to Algorithms 18
COMP 151 – Introduction to Algorithms 10
Flow Chart Symbols
� There are no “one” definition for flowcharting symbols
� There are some guidelines for commonly used symbols
� Just be sure that when using these symbols you are consistent with their meaning
COMP 151 - Introduction to Algorithms 19
Basic Flowchart Shapes and Definitions
Start / End
The start or end of a workflow
Process
Process or action
Connector
Used to connect one part
of a flowchart to another
Decision
Decision point in a process or
workflow
Input / Output
Data: Inputs to, and outputs
from, a process
COMP 151 - Introduction to Algorithms 20
COMP 151 – Introduction to Algorithms 11
Flow Chart Symbols
The Start/Stop Symbol is used at the
beginning and end of a flowchart
Start
COMP 151 - Introduction to Algorithms 21
Stop
Flow Chart Symbols
The Process Symbol represents any
process, function, or action and is the
most frequently used symbol in
flowcharting
C � A + B
COMP 151 - Introduction to Algorithms 22
COMP 151 – Introduction to Algorithms 12
Flow Chart Symbols
The Input/Output Symbol represents data
that is available for input or resulting from
processing (i.e. customer database
records)
Display SUM
COMP 151 - Introduction to Algorithms 23
Flow Chart Symbols
The Decision Symbol is a junction where a
decision must be made. A single entry
may have any number of alternative
solutions, but only one can be chosen
A > B?
COMP 151 - Introduction to Algorithms 24
NoYes
COMP 151 – Introduction to Algorithms 13
Flow Chart Symbols
The Connector Symbol represents the exit
to, or entry from, another part of the same
flow chart. It is usually used to break a
flow line that will be continued elsewhere.
COMP 151 - Introduction to Algorithms 25
General Rules for Flowcharts
• All boxes of the flowchart are connected with Arrows (Not lines).
• Flowchart symbols have an entry point on the top of the symbol with no other entry points. The exit point for all flowchart symbols is on the bottom except for the Decision symbol.
• The Decision symbol has two exit points; these can be on the sides or the bottom and one side.
COMP 151 - Introduction to Algorithms 26
COMP 151 – Introduction to Algorithms 14
Various Structures of Algorithm
• Sequence (also known as Order or Process)
• Decision (also known as Selection)
• Repetition (also known Iteration or Looping)
COMP 151 - Introduction to Algorithms 27
The Sequence Structure
• This is the default structure of programs. Flow will transfer from start to end in sequence unless encountered by selection or repetitionstructures.
• Is also known as Process.
COMP 151 - Introduction to Algorithms 28
COMP 151 – Introduction to Algorithms 15
The Sequence Structure
• Example: Write an algorithm to add two numbers and display their sum
Start
Step 1: Read num1
Step 2: Read num2
Step 3: sum ���� num1 + num2
Step 4: Display sum
Stop
COMP 151 - Introduction to Algorithms 29
The Sequence Structure
COMP 151 - Introduction to Algorithms 30
STOP
START
Inputnum1, num2
sum ←←←← num1 + num2
Displaysum
COMP 151 – Introduction to Algorithms 16
The Sequence Structure
Start
Read
Num1
Sum���� Num1 + Num2
Read
Num2
Display
Sum
Stop
Another Way:
1. begin
2. get num1
3. get num2
4. set sum � num1 + num2
5. display sum
6. end
COMP 151 - Introduction to Algorithms 31
Exercises
Write the pseudocode and draw the
flowchart to find and display the
following:
1.The difference of two numbers.
2.Both the sum and difference of two
numbers.
3.To read the first and last names and display
a greeting, such as “Hello Ahmed Al-
Rashdi”
COMP 151 - Introduction to Algorithms 32
COMP 151 – Introduction to Algorithms 17
The Selection Structure
• Decision (Selection)
Instructions are written based on some conditions. If the particular condition or conditions are satisfied then the program flow is in one route. Otherwise, it follows another route. ie, based on conditions the selection of program flow is jumped into some ways.
COMP 151 - Introduction to Algorithms 33
The Selection Structure
• This structure is also known as if-else structure
• Example:If A > B then
print A
else
print B
endif
COMP 151 - Introduction to Algorithms 34
isA > B
Print BPrint A
Yes No
COMP 151 – Introduction to Algorithms 18
Relational Operators
Relational Operators
Operator Description> Greater than
< Less than
= Equal to
≥≥≥≥ Greater than or equal to
≤≤≤≤ Less than or equal to
≠≠≠≠ Not equal to
COMP 151 - Introduction to Algorithms 35
The Selection Structure
Example 1:
A=1, B=2, C=3, D=4
if A < B then
C = 5
Example 2:
if A > B then
D = 4
1 A
B
C
D
2
3
4
COMP 151 - Introduction to Algorithms 36
COMP 151 – Introduction to Algorithms 19
The Selection Structure
Example 3:
if C < D then
B = B + 1
else
B = B - 1
1 A
B
C
D
2
3
4
COMP 151 - Introduction to Algorithms 37
The Selection Structure
• Example: Write an algorithm to compare two numbers and print the smallest numberStart
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX ���� VALUE1
else
MAX ���� VALUE2
endif
Step 3: Print “The largest value is”, MAX
Stop
COMP 151 - Introduction to Algorithms 38
COMP 151 – Introduction to Algorithms 20
The Selection Structure
COMP 151 - Introduction to Algorithms 39
MAX ←←←← VALUE1
Print“The largest value is”, MAX
STOP
Yes No
START
InputVALUE1, VALUE2
MAX ←←←← VALUE2
is
VALUE1>VALUE2
The Selection Structure
num1 <
num2?
Start
Read
num1
Read
num2
Display
num1 as min
Stop
Display
num2 as min
Yes
No
Another Way:
1. begin
2. get num1
3. get num2
4. if num1 < num2 then
5. display num1 as smaller
6. else
7. display num2 as smaller
8. end if
9. end
COMP 151 - Introduction to Algorithms 40
COMP 151 – Introduction to Algorithms 21
Exercises
Write the pseudocode and draw the
flowchart to find and display the
following:
1.The largest of two numbers.
2.The largest of three numbers.
3.The median of three numbers.
4.The average of three numbers.
COMP 151 - Introduction to Algorithms 41
Exercises
Write the pseudocode and draw the
flowchart that reads a number and
determines the following:
1.If the number is negative or positive.
2.If the number is odd or even.
3.If the number is divisible by 5.
COMP 151 - Introduction to Algorithms 42
COMP 151 – Introduction to Algorithms 22
The Repetition Structure
• Repetition (Looping)
In some programs, the same steps are repeated for a number of times. In that case, the looping structure helps us to do the work.
Basically, we will study while loops, do...while loops and for loops structure. Based on some condition or conditions, the iteration is worked out.
COMP 151 - Introduction to Algorithms 43
The Repetition Structure
COMP 151 - Introduction to Algorithms 44
COMP 151 – Introduction to Algorithms 23
The Repetition Structure
COMP 151 - Introduction to Algorithms 45
The Repetition Structure
• Example: Write the algorithm and draw the flowchart for a program that reads the name of 30 students in the class and displays them on the screen
COMP 151 - Introduction to Algorithms 46
COMP 151 – Introduction to Algorithms 24
The Repetition Structure
• Pseudocode:Start
Step 1: Set the number of students=1
Step 2: Repeat until number of students > 30
Step 3: Read the Name of the student
Step 4: Display the Name of the student
Step 5: Increase the number of students by 1
Step 6: End Repeat
Stop
COMP 151 - Introduction to Algorithms 47
The Repetition Structure
COMP 151 - Introduction to Algorithms 48
COMP 151 – Introduction to Algorithms 25
count = 5?
Start
Read
Num
Display
sum as total
Stop
Yes
No
count���� 0
sum���� 0
count���� count + 1
sum���� sum + Num
Write the pseudocode and draw the
flowchart to sum five numbers input
by the user:
1. begin
2. set count�0
3. set sum�0
4. repeat until count=5
5. get num
6. set count�count+1
7. set sum�sum+num
8. end repeat
9. display sum as the total
10.end
COMP 151 - Introduction to Algorithms 49
The Repetition Structure
Exercises
Write the pseudocode and draw the
flowchart to find and display the
following:
1.The sum of N numbers.
2.The average of N numbers.
3.Entering and summing numbers untill
‘-1’ is entered.
4.The average of the first 20 numbers.
COMP 151 - Introduction to Algorithms 50