Lec 17-18 Algorithms Updated
Lec 17-18 Algorithms Updated
Lecture
(Algorithms)
1
DEFINING THE PROBLEM
• A well-defined problem has five components:
• Take Cat across the river and leave him on the other
side. Take back the chicken.
7
SOLVING PROBLEM IS ITERATIVE PROCESS
• It is quite common to first solve a problem for a
particular/special case
• Then for another
• And, possibly another
• And watch for patterns and trends that emerge
• And to use the knowledge form those patterns
and trends in coming up with a general solution
8
• If you want to solve a problem, it helps if you have
experienced that problem or similar ones before.
• Better Definition:
A precise sequence of a limited number of
unambiguous, executable steps that terminates
in the form of a solution.
THREE REQUIREMENT
1. Sequence is:
a. Precise
b. Consists of limited number of steps
• Ockham’s Razor:
• When choosing among competing, successful solutions
to a problem, chose the one which is least complex.
– Greedy Algorithm
– Deterministic Algorithm
– Randomized Algorithm
– Brute Force Strategy
TRAVELING SALESMAN PROBLEM
• A salesman needs to visit each of the n cities.
120 C 70
B F
60
100 40 30
80 D 50
A G
90
70 110
E
• Semantics:
The concept embedded in an algorithm(the soul!)
• Syntax:
The actual representation of an algorithm(the
body!)
WARNING!
• An algorithm can be syntactically correct, yet
semantically incorrect- very dangerous
situation!
– Pseudo Code
– Flowcharts
– Actual Code
– Structured English
FLOWCHART
• A graphical representation of a process (e.g.
an algorithm), in which graphic objects are
used to indicate the steps & decisions that are
taken as the process moves along from start to
finish.
Start or stop
Process
Input or output
Flowchart
Elements Decision
Flow line
Connector
Off-page connector
PSEUDO CODE
• Language that is typically used for writing
algorithms.
Calculate Percentage
Calculate Percentage
Sum ← Num1 + Num2
Average ← Sum/2 Sum ← Num1 + Num2
Average ← Sum/2
CONDITIONALS
• Select between alternate courses of action
depending upon the evaluation of condition:
Pseudo Code Flowchart
True False
condition
If (condition = true)
statement block 1
Else
statement statement
statement block 2 block 1 block 2
End If
LOOPs
• Loop through a set of statements as long as a
condition is true:
Pseudo Code Flowchart
Loop while (condition=true)
Do statement block
End Loop
statement
True
block condition
False
EXAMPLE OF CONDITIONS
• Toss a coin, if the head will occur, then I will
not do my homework assignment. If the tail
will occur, then I will do my homework
assignment.
• Pseudo code
– Input Number1
– Input Number2
– Sum ← Number1 + Number2
– Average ← Sum/2
– Output Sum, Average
34
Flow Chart
START
Input
Num1, Num2
Sum ← Number1 +
Number2
Average ← Sum/2
Output Sum,
Average
STOP 35
EXAMPLES
• Students appears in an examination, which
consists of total 10 subjects. Each subject having
maximum marks of 100.
– The roll number of the student, his/her name, and the
marks obtained by him/her in various subjects, are
supplied as input data.
– We want to make a list of only those students who
have passed (obtained 50% or more marks) in the
examination.
– In the end, we also want to print out the total number
of students who have passed.
– The input data of all students is terminated by a trailer
record, which has sentinel value of 9999999 for Roll No.
Start Flowchart for the Exam Process
Count = 0
Read
Input
Add 1 to Count
Write
Is RollNo = No Add marks of all Output
999999? Subjects (Total) Yes
Yes No
Percentage = Is percentage
Write Count
Total / 10 = >50?
37
Stop
SOLUTION IN PSEUDO CODE
1) Set Count to zero
2) Read first student record
3) Repeat while RollNo is not equal to 9999999 {
Calculate Total Marks
Calculate Percentage = Total / 10
IF Percentage => 50
Write Output
Add 1 to Count
ENDIF
}
4) Write Count
5) Stop
EXAMPLES
• Draw a flowchart and write pseudo code that
reads three numbers, determine the largest
number and print the largest number with an
identifying message?
SOLUTION IN PSEUDO CODE
1) Let the number be integer and non-negative
2) Set the largest number MAX
2) Read input N1, N2, N3
3) IF(N1 > N2 && N1 > N3)
MAX = N1
ENDIF
IF(N2 > N1 && N2 > N3)
MAX = N2
ENDIF
ELSE
MAX = N3
4) Print “The largest number is: ” MAX
5) Stop
Flowchart of the Example
START
Read A, B, C
Yes If
No If No
A is larger B>A && B>C
A>B && A>C
Print A
Yes
B is larger C is larger
Print B Print C
STOP
EXAMPLE
• A company plans to pay bonus to their employees, the details are
given below:
– Those earning Rs. 30,000 or above are to be paid 20 percent of their salary.
– Those earning less than Rs. 30,000 are to be paid Rs. 1000
– The input records contain the employee number, name and salary of the
employees.
44
Pros and Cons of Pseudo Code (1)
• Quite suitable for SW development as it is closer in
form to real code
45
Pros and Cons of Pseudo Code (2)
• Pseudo code can be constructed quite quickly as
compared with a flowchart
46