bsse lecture 2
TRANSCRIPT
-
8/4/2019 BSSE Lecture 2
1/43
Fundamentals ofAlgorithmds
Fundamentals of Algorithms
LECTURE 02
IIT UST BANNU
-
8/4/2019 BSSE Lecture 2
2/43
Fundamentals ofAlgorithmds
Review of the Previous Lecture
What is computer Science?
Some Misconception about Computer Science.
What is an algorithm and how it is related toComputer Science?
Types of Operations in Algorithms.
Example of Algorithms in Real Life e.g. HumanGenome project, Robots etc.
-
8/4/2019 BSSE Lecture 2
3/43
Fundamentals ofAlgorithmds
The Parts of an Algorithm
It is worthwhile to think about these algorithmrelated terms because we can rely on thesewhen we write new algorithms. Common parts ofalgorithms include
1.Variables: Named Values 2.Parameters: Named Inputs 3.Conditionals: Handling Different
Conditions
4.Repetition 5.Subroutines: Named Helper Algorithms 6.Recursion: Helping Yourself
http://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.htmlhttp://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.html -
8/4/2019 BSSE Lecture 2
4/43
Fundamentals ofAlgorithmds
Variables: Named Values
As we write algorithms, we like to name things.
Sometimes we use long names, Sometimes, we use shorter names,
As we start to write more formal algorithms, we
may explicitly say that we're using names.
-
8/4/2019 BSSE Lecture 2
5/43
Fundamentals ofAlgorithmds
Parameters: Named Inputs
Many algorithms work on data that are passed tothe algorithm.
Similarly, an algorithm to find a name in thephone book might take the name and the phonebook as input values.
We call such input valuesparameters.
-
8/4/2019 BSSE Lecture 2
6/43
Fundamentals ofAlgorithmds
Conditionals: Handling Different
Conditions
At times, our algorithms have to account fordifferent conditions, doing different thingsdepending on those conditions.
We call such operations conditionals. Theytypically take either the form
-
8/4/2019 BSSE Lecture 2
7/43
Fundamentals ofAlgorithmds
Conditionals: Handling Different
Conditions
ifsome condition holdsthen do something
of the more complex form
ifsome condition holdsthen do somethingotherwisedo something else
At times, we need to decide between more than
two possibilities.
-
8/4/2019 BSSE Lecture 2
8/43
Fundamentals ofAlgorithmds
Repetition
At times, our algorithms require us to dosomething again and again.
. We call this technique repetition. Repetitiontakes many forms.
We might do work until we've reached a desiredstate.
-
8/4/2019 BSSE Lecture 2
9/43
Fundamentals ofAlgorithmds
Repetition
Repeat some actionuntil some condition holds
We might continue work as long as we're insome state.
repeat some actionwhile some condition holds
We might repeat an action a fixed number oftimes.
repeat some actionNtimesYou can probably think of other forms of
repetition.
-
8/4/2019 BSSE Lecture 2
10/43
Fundamentals ofAlgorithmds
Subroutines: Named Helper
Algorithms
Many algorithms require common actions fortheir operation.
We can write additional algorithms for thesecommon actions and use them as part of ourbroader algorithm.
We can also use them in other algorithms. Wecall these helper algorithms subroutines.
Just like functions in C++
-
8/4/2019 BSSE Lecture 2
11/43
Fundamentals ofAlgorithmds
Recursion: Helping Yourself
As strange as it may seem, we sometimes find ituseful to define an algorithm in terms of itself.
In particular, we can have an algorithm deal witha large or complex input by using itself as ahelper with a smaller or less complex input.
-
8/4/2019 BSSE Lecture 2
12/43
Fundamentals ofAlgorithmds
Recursion: Helping Yourself
Consider the following example
To make N peanut butter and jelly sandwiches
If N is 0 then conditional Celebrate, you're done!
Otherwise
Make one PBJ sandwich subroutine Make N-1 PBJ sandwiches recursion
-
8/4/2019 BSSE Lecture 2
13/43
Fundamentals ofAlgorithmds
Recursion: Helping Yourself
We call this technique recursion. Masteringrecursion is one of the key steps on your path tobecoming a computer scientist.
-
8/4/2019 BSSE Lecture 2
14/43
Fundamentals ofAlgorithmds
Characteristics of an algorithm
Let us try to present the scenario of a man brushing his ownteeth(natural denture) as an algorithm as follows.
Step 1. Take the brush
Step 2. Apply the paste
Step 3. Start brushing
Step 4. Rinse
Step 5. Wash
Step 6. Stop
-
8/4/2019 BSSE Lecture 2
15/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
An Algorithm should have the following fivecharacteristics:
1. Input
2. Output
3. Definiteness
4. Effectiveness 5. Termination
-
8/4/2019 BSSE Lecture 2
16/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
Finiteness:
It means that the algorithm terminates after afinite number of steps
-
8/4/2019 BSSE Lecture 2
17/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
Definiteness:
Should have well defined and unambiguouslyspecified steps.
-
8/4/2019 BSSE Lecture 2
18/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
Input:
The algorithm should be given valid inputs andclearly specified.
E.g. Student Record Search Algorithm
-
8/4/2019 BSSE Lecture 2
19/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
Output:
It means that it can be proved to produce thecorrect output given a valid input
-
8/4/2019 BSSE Lecture 2
20/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
Effectiveness:
steps are sufficiently simple and basic.
It should effectively solve the problem within
limited resources.
-
8/4/2019 BSSE Lecture 2
21/43
Fundamentals ofAlgorithmds
Characteristics of Algorithms
Termination:
It means that the algorithm should terminateafter completion of its task or failure (optional).
-
8/4/2019 BSSE Lecture 2
22/43
Fundamentals ofAlgorithmds
Ways of Expressing Algorithms
Pseudo code
Flowchart
-
8/4/2019 BSSE Lecture 2
23/43
Fundamentals ofAlgorithmds
Whats Pseudocode ?
Artificial and Informal language
Helps programmers to plan an algorithm Similar to everyday English
Not an actual programming language
-
8/4/2019 BSSE Lecture 2
24/43
Fundamentals ofAlgorithmds
E.g : Pseudocode
Read A, B
Calculate C = A*B
Display CStop
-
8/4/2019 BSSE Lecture 2
25/43
Fundamentals ofAlgorithmds
Comparative Between Flowchart vs
Pseudocode
Flowchart
A graphical way of writing pseudocode
Rounded rectangleterminal
Parallelograminput / output
Rectangleactions
Diamondsdecision / conditional
Circlesconnector
-
8/4/2019 BSSE Lecture 2
26/43
Fundamentals ofAlgorithmds
E.g : Flowchart
Start
Stop
Read ARead B
Display theResult C
Calculate ResutC=A*B
Start Terminal.
Program start
here
Stop Terminal
Program end
here
Input.
Enter values for
A and B
Process
Output
-
8/4/2019 BSSE Lecture 2
27/43
Fundamentals ofAlgorithmds
Comparative Between Flowchart vs
Pseudocode (..Cont.)
Pseudocode
No syntax ruleIndependent from anyprogramming language
Write in an ordinary language
Uses a structure resembling computer structure
No connector between pages
-
8/4/2019 BSSE Lecture 2
28/43
Fundamentals ofAlgorithmds
E.g : Pseudocode
Read A, BInput
Calculate C = A*B -Action
Display C - OutputStop -Terminal
-
8/4/2019 BSSE Lecture 2
29/43
Fundamentals ofAlgorithmds
Example 2 (Selection)
Read A, BIF A is less than B
BIG = B
SMALL = AELSE
BIG = A
SMALL = BWrite / Display BIG, SMALLStop
-
8/4/2019 BSSE Lecture 2
30/43
Fundamentals ofAlgorithmds
Example 2 (Selection)
Read A, B - InputIF A is less than B - Selection
BIG = B -Action
SMALL = A -ActionELSE - Selection
BIG = A - Action
SMALL = B -ActionWrite / Display BIG, SMALL - OutputStop -Terminal
-
8/4/2019 BSSE Lecture 2
31/43
Fundamentals ofAlgorithmds
Example 3 (Repetition)
Set count to zeroSet total to zero
Read number
WHILE ( not end-of-data )
increment count by 1
total = total + number
read number
IF ( count > 0 ) thenaverage = total / count
Display average
Stop
-
8/4/2019 BSSE Lecture 2
32/43
Fundamentals ofAlgorithmds
Advantages
Converting a pseudocode to a programminglanguage is much more easier than converting aflowchart.
As compared to flowchart, it is easier to modifya pseudocode of a program logic when programmodifications are necessary.
Easily modifiedImplements structured concepts
Done easily on Word Processor
-
8/4/2019 BSSE Lecture 2
33/43
Fundamentals ofAlgorithmds
Limitations
In the cases of pseudocode, a graphicrepresentation of program logic is not available.
There are no standard rules to follow for using apseudocode.
Different programmers use their own style of writingpseudocode; and hence,
Communication problem occurs due to lack ofstandardization.
-
8/4/2019 BSSE Lecture 2
34/43
Fundamentals ofAlgorithmds
Basic Algorithm Writing Techniques
-
8/4/2019 BSSE Lecture 2
35/43
Fundamentals ofAlgorithmds
Rules for Pseudocode
Write only one statement per line Capitalize initial keyword Indent to show hierarchy End multiline structures Keep statements language independent
-
8/4/2019 BSSE Lecture 2
36/43
Fundamentals ofAlgorithmds
One Statement Per Line
Each statement in pseudocode should express justone action for the computer. If the task list isproperly drawn, then in most cases each task willcorrespond to one line of pseudocode.
Task List
Read name, hours worked, rate of pay
Perform calculations
gross = hours worked * rate of pay
Write name, hours worked, gross
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
-
8/4/2019 BSSE Lecture 2
37/43
Fundamentals ofAlgorithmds
Capitalize Initial Keyword
In the example below note the words: READ and WRITE.These are just a few of the keywords to use, others include:READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
-
8/4/2019 BSSE Lecture 2
38/43
Fundamentals ofAlgorithmds
Indent to Show Hierarchy
Sequence:Keep statements in sequence all starting in the same column
Selection:Indent statements that fall inside selection structure, but not the keywords that form theselection Loop:
Indent statements that fall inside the loop but not keywords that form the loop
Each design structure uses a particular indentationpattern
READ name, grossPay, taxes
IF taxes > 0
net = grossPay taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
-
8/4/2019 BSSE Lecture 2
39/43
Fundamentals ofAlgorithmds
End Multiline Structures
See the IF/ELSE/ENDIF as constructed above,the ENDIF is in line with the IF.The same applies for WHILE/ENDWHILE etc
READ name, grossPay, taxes
IF taxes > 0
net = grossPaytaxes
ELSE
net = grossPay
ENDIF
WRITE name, net
-
8/4/2019 BSSE Lecture 2
40/43
Fundamentals ofAlgorithmds
Language Independence
Resist the urge to write in whatever language you are mostcomfortable with, in the long run you will save time.
Remember you are describing a logic plan to develop a
program, you are not programming!
-
8/4/2019 BSSE Lecture 2
41/43
Fundamentals ofAlgorithmds
The Selection Structureamount < 100
interestRate = .06 interestRate = .10
yes no
IF amount < 100
interestRate = .06
ELSE
Interest Rate = .10ENDIF
Pseudocode
-
8/4/2019 BSSE Lecture 2
42/43
Fundamentals ofAlgorithmds
The Looping StructureIn flowcharting one of the more confusing things is to separate selection
from looping.
This is because each structure use the diamond as their control symbol. Inpseudocode we avoid this by using specific keywords to designate
looping
WHILE/ENDWHILEREPEAT/UNTIL
Ti W iti G d P d C d
-
8/4/2019 BSSE Lecture 2
43/43
Fundamentals of
Tips on Writing Good Pseudo Code Use indention for improved clarity
Do not put code in pseudo code make yourpseudo code language independent
Dont write pseudo code for yourselfwrite it in anunambiguous fashion so that anyone with areasonable knowledge can understand and implementit
Be consistent
Prefer formulas over English language descriptions