itc lec5-24+sep+2012
TRANSCRIPT
Introduction To Computer Science (ITC)
Instructor: Adeela [email protected], [email protected]
1
National University of Computer and Emerging Sciences, Islamabad
Lecture 05Introduction to Algorithms
24 September 2012
2
Algorithm
Algorithm is a step by step procedure for solving a problem
It represents a process that a computer carries out, in order to complete a well defined task
The objective of computer science is to solve problems by developing, analyzing, and
implementing algorithmic solutions
3
Al-Khwarizmi Principle
• All complex problems can be broken into simpler sub-problems.
• Solve a complex problem by breaking it down into smaller sub-problems and then solve them (in a specified order), one at a time.
• When all the steps are solved, the original problem itself has also been solved.
• This process is called Algorithm. (Originating from al-Khwārizmī)
4
Computer Programming
• Computer is a powerful tool• It is not intelligent!• In order to use computer to solve our problems,
we must tell it what we want done and the order in which we want it done.
• These instructions are called computer program. • Writing these instructions is called computer
programming.• The person giving these instructions is called a
computer programmer.
5
Computer Programming
• Analyze the problem
• Develop a sequence of instructions for solving the problem.
• Communicate it to the computer.
6
Phases of the software life cycle
• Requirement definition
• Analysis and design
• Coding
• Testing
• Implementation
• Maintenance
7
Problem Solving Techniques
• Ask questions
• Look for things that are similar
• Means-ends analysis
• Divide and Conquer
• Merging solutions
8
Ask Questions• Ask questions until you have developed a clear
understanding of the problem.• Who, What, Why, Where, When.
– What do I have to work with? (my data or input)
– How much data is there?
– What should my output look like?
– How many times is the process going to be repeated?
– What are the exceptions to the main course?
– What special error condition might come up?
9
Look for things that are Similar
• Do not reinvent the wheel!
• Draw Analogies
10
Means-Ends analysis
• Starting point and ending state are known.• You need to devise the transformation
function.• Ends are defined – you need to analyze
your means of getting between them.• Lahore to Islamabad
– What are the options?– Narrow down the options?– Figure out the details?
11
Divide and Conquer
• Same as the Al-khwarizmi Principle.
• Breakup the large problem into smaller units and then solve them one at a time.
• Building block approach.
12
Divide and Conquer
Hard Problem
Hard Sub-problemEasy Sub-problem Easy Sub-problem
Easy Sub-problemEasy Sub-problem
13
Merging Solution
• Sometimes merging two independent solutions solves the problem more efficiently?
• Calculate Average– Count values– Sum Values– Divide sum by count
• Alternative approach – calculate partial sum as you count
14
Problem Solving Techniques
• What is the unknown?– What is required?
• What are the data?– What is given?
• What is the condition?– By what condition the unknown is linked to the
data?
15
Conversion from Fahrenheit to Celsius
• Output– Temperature in Celsius (C)
• Inputs– Temperature in Fahrenheit (F)
• Process32)(F
9
5C
16
Calculate and print the average grade of 3 tests for the entire class
• Input– 3 test scores for each student
• output– Average of 3 tests for each student
• Process1. Get three scores2. Add them together3. Divide by three to get the average4. Print the average5. Repeat step 1 to 4 for next student6. Stop if there are no more students
17
A flowchart is a visual or graphical representation of an algorithm.
The flowchart employs a series of blocks and arrows, each of which represents a particular operation or step in the algorithm.
The arrows represent the sequence in which the operations are implemented.
Flow Charts
18
Flowcharts – Most Common Symbols
SymbolSymbol NameName FunctionFunction
TerminalTerminal Represents the beginning or Represents the beginning or end of a end of a program.program.Flow-lineFlow-line Represents the flow of logic.Represents the flow of logic.
ProcessProcess Represents calculations Represents calculations or dataor data
manipulation.manipulation.
Input/OutputInput/Output Represents inputs or Represents inputs or outputs of data outputs of data and information.and information.
DecisionDecision Represents a comparison, Represents a comparison, question, question, or decision that or decision that determines determines alternative paths alternative paths to be followed.to be followed.
19
Flowcharts – An Example Find the solution of a quadratic equation Ax2+Bx+C=0, given A, B and C.
START
INPUT A, B, C
CalculateR = SQRT(B2-4AC)
AA
AA
X1 = (-B+R)/(2A)X2 = (-B-R)/(2A)
PRINTA, B, C, X1, X2
END
20
Flow Charting
Expresses the flow of processing in a structured pictorial format.
Processing Steps
Input and Output Steps
Decision
Flow of
data
ConnectorsTerminator
21
Flow chart for Converting Fahrenheit into Celsius
Get temp. in ‘F’
Print ‘C’
Calculate )32(F
95
C
Stop
Begin
22
Add them together
Divide the result by three
More students?Yes
StopNo
Flow chart for calculating average of three scores
Get three scores
Print the average
23
STARTSTART
INPUT INPUT A, BA, B
Add A to B Add A to B and store in Cand store in C
OUTPUTOUTPUT CC
ENDEND
Comparison of Algorithm representations in Natural language, flowchart and Pseudo-code
Step 1: Begin the calculations
Step 2: Input two values A and B
Step 3: Add the values
Step 4: Display the result
Step 5: End the calculation
BEGIN Adder Input A and B C = A + B PRINT CEND Adder
Natural languageNatural language FlowchartFlowchart Pseudo-codePseudo-code
24
Algorithm Representation(Natural Languages)
• English or some other natural language.• Are not particularly good:
–too verbose–unstructured–too rich in interpretation (ambiguous)–imprecise
25
Algorithm Representation(Using Programming Language)
{
int I, m, Carry;
int a[100], b[100], c[100];
cin >> m;
for ( int j = 0 ; k <= m-1 ; j++ ) {
cin >> a[j];
cin >> b[j];
}
Carry = 0;
i = 0;
while ( i < m ) { …
26
Programming Languages
• Are not particularly good either
– Too many implementation details to worry
about
– Too rigid syntax
• Easy to lose sight of the real task
27
Pseudo-code
• We need a compromise between the two:
Pseudo-code• Computer scientists use pseudo-code to
express algorithms:– English like constructs (or other natural
language), but– modeled to look like statements in typical
programming languages.
28
Pseudo-code Primitives
Three basic kind of operations:• Sequential
– Computation ( Set … )– Input/Output ( Get ... / Print ... )
• Conditional– If … Else– If …
• Iterative / looping– Repeat ...– While ...
29
Performs a computation and stores the result.
Computation
Example:Example:
Set the value of Set the value of C to (A + B)C to (A + B)
Set the value of Set the value of locationlocation to 0 to 0
Set the value of Set the value of GPA to (sum / count)GPA to (sum / count)
General format:General format: Set the value of <variable> to <expression>
30
VariablesA variable is a named storage. - A value can be stored into it, overwriting
the previous value
- Its value can be copied ExamplesExamples::
Set the value of A to 3
The variable A holds the value 3 after its execution
Set the value of A to (A+1)
Same as: add 1 to the value of A ( A is now 4)
31
Not too Strict on Syntax
• Pseudo-code is kind of a programming language without a rigid syntax, for example we can write:– Set the value of A to (B+C)
• as – Set A to (B+C)
• or even:• Set the value of sum to 0• Set the value of GPA to 0
• as • Set sum and GPA to 0
32
Sequential Operations - Input/OutputSequential Operations - Input/Output
Outside worldOutside world
InputInput
OutputOutput
•The Computer needs to communicate with the outside world:
INPUT operations allow the computing agent to receive from the outside world data values to use in subsequent computations.OUTPUT operations allow the computing agent to communicate results of computations to the outside world.
33
InputGeneral format:
The computing agent (computer) suspends executions and waits for an input value.
Get a value for <variable>
34
Input - Examples
• Examples:– Get value for grade– Get values for N, M
• Can write:– Get value for N1
– ...– Get value for N100
• as– Get value for N1,..., N100
35
OutputGeneral format:
The computing agent (computer) displays the value of the variable(s).
Print the value of <variable>
Print the message, "<text>"
36
Output - Examples
• Examples:– Print the value of grade– Print the message, "Hello"
• Can write:– Print the value of N1
– ...– Print the value of N100
• as– Print the values of N1,..., N100
37
Example
• Write an algorithm to calculate the average of three numbers.
Steps Operations
1 Get values for N1, N2, and N3
2 Set the value of Average to (N1+N2+N3)/3
3 Print the value of Average
4 Stop
38
Conditional Operations
If <condition> then
operations for the then-part
Else
operations for the else-part
1. Evaluate <condition> expression to see whether it is true or false.
2. If true, then execute operations in then-part
3. Otherwise, execute operations in else-part.
39
Conditions, or Boolean Expressions
• A condition is one whose value is true or false, for example:
3 > 2 is greater than (true)
3 = 2 is equal to (false) A > 2 is true if A’s value is
greater than 2 (at the time this is executed), false otherwise.
40
E1 or E2E1 or E2
true if at least one of them is true; false true if at least one of them is true; false otherwise.otherwise.
E.g. 3 > 2 or 2 > 3 is true E.g. 3 > 2 or 2 > 3 is true
E1 and E2E1 and E2
true if both are true; false otherwise true if both are true; false otherwise
E.g. 3 > 2 and 2 > 3 is falseE.g. 3 > 2 and 2 > 3 is false
not Enot E
true if E is false, false if E is true true if E is false, false if E is true
Conditions may be compoundedConditions may be compounded
41
1. Get a value for A
2. If A = 0 then
3. Print the message, “The input is zero”
Else
4. Print the message, “The input is not zero”
ExampleExample
1. Get a value for grade
2. If grade < 1 or grade > 9 then
3. Print the message, “Invalid grade”
Else
4. Set the value of total to (grade + total)
42
Iterative Operation - While
While <condition> remains true do steps i to j
step i: operation
step i+1: operation
…
step j: operation1.1. Evaluate <condition>Evaluate <condition>2.2. If condition is true, execute steps i to j, If condition is true, execute steps i to j,
then go back to i.then go back to i.3. Otherwise, if condition is false,continue 3. Otherwise, if condition is false,continue
execution from step j+1.execution from step j+1.
43
1 Get a value for count
2 While count < 10 do
3 Set square to (count * count)
4 Print the values of count and square
5 Add 1 to count
6 Stop
Example
44
What happens when it gets executed?
If count starts with 7, we get printout
7 49
8 64
9 81
What if count starts with 11?
Nothing is printed, loop is executed 0 times.
While Loops
45
Set value of A equal to 1
While A > 0
Print message, “Enter an integer”
Get a value for A
End of the loop
Stop
What does the following algorithm do?What does the following algorithm do?
ExerciseExercise
46
Tracing an algorithmTracing an algorithm
The current values of algorithm variables at
various points during execution can be
known by tracing the algorithm with a table
called Trace Table
47
Trace TableTrace TableProblem: Determine the value of the variable x and y after the following algorithm is executedPseudocode:x = 5Y = 7If x = 5 then
y= 8else
y= 0if y = 7 then
x = 6else
x = 3if x = y then
y = 0
Nesting???
48
… … ContinuedContinued
834.3
834.2
854.1
853
752
-51
yXStep
No.
Trace table for algorithm
49
Find the phone number of a given Name in an (unsorted) list of names and their phone numbers
Names Phone numbers
N1 T1
N2 T2
…
N1000 T1000
Sequential Search: an Example
50
Sequential Search: an Example
123456789
1011
553614442563521463541236452361442563551123441155521364528975541258
Name to fin
d:
Smith, J
ohn
51
1. Get value for Name
2. Get values for N1,…,N1000
3. Get values for T1,…,T1000
4. If Name = N1 then print the value of T1
5. If Name = N2 then print the value of T2
…
1002. If Name = N999 then print the value of T999
1003. If Name = N1000 then print the value of 1000
1005. Stop
Sequential Search: 1st Attempt
52
Get values for Name, N1,…, N1000, T1,…, T1000
Set the value i to 1 and the value of Found to 0
While Found = 0 AND i <= 1000 do
If Name = Ni then
Print the value of Ti
Set the value of Found to 1
Else
Add 1 to the value of I
EndIF
End of While loop
Stop
Sequential Search: Using a Loop
53
Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4
The largest is 8 at location 3The largest is 8 at location 3
Idea (sketch): Go through the entire list, at each Idea (sketch): Go through the entire list, at each iteration find the largest-so-far and record its iteration find the largest-so-far and record its locationlocation
Given a list of variables A1, A2, …, An, find the Given a list of variables A1, A2, …, An, find the largest value and its (first) location largest value and its (first) location
Selection: Find the Largest Number
54
Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4
To begin with, To begin with, set set largest-so-farlargest-so-far to (the value of) to (the value of) A1A1set set locationlocation to 1to 1set set ii to 2 to 2
i
55
Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4
CompareCompare A1A1 and and A2A2 largest-so-farlargest-so-far still holds the value of still holds the value of A1A1set set ii to to ii+1+1
i
56
Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4
Compare Compare A1A1 and and A3A3largest-so-farlargest-so-far now holds the value of now holds the value of A3A3locationlocation is 3 is 3set set ii to to ii+1+1
i
57
Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4
Continue the similar process until Continue the similar process until ii = 8 = 8
i
58
Get a value for n, the size of the listGet values for A1, A2, …, An, the list to be searchedSet largest_so_far to A1 and set location to 1Set the value of i to 2While i is less or equal to n do If Ai > largest_so_far then Set the value of largest_so_far to Ai
Set the value of location to I EndIF
Add 1 to the value of iEnd of While loopPrint the values of largest_so_far and location
Selection: Find The Largest Number