0% found this document useful (0 votes)
5 views

DS Intro Algorithms (1) SLM

Uploaded by

m.biswas2014.mb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DS Intro Algorithms (1) SLM

Uploaded by

m.biswas2014.mb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Data Structures

and
Algorithms
Program Development

1. Understand the problem


2. Represent your solution (your algorithm)
 Pseudocode
 Flowchart

3. Implement the algorithm in a program


4. Test and debug your program

2
Algorithm
How to
prepare
tea ??

Relate with
algorithm…
How to prepare Maggi
noodles ??

Relate with algorithm…

1. Take one and a half cup of Water in a pan.


2. Heat the pan on medium flame.
3. When the Water comes to boil, add the Maggi to the pan.
4. Cover it with a lid for a minute.
5. After a minute, uncover the lid and add the tastemaker to
the pan.
6. Mix it well, Without breaking the Noodles.
7. Just when all of your Water is boiled, switch off the flame.
8. Enjoy the hot Maggi.
What is an Algorithm?
(Formal Definition)

• An algorithm is a finite sequence of unambiguous


instructions to solve a particular problem.
• In addition, all algorithms must satisfy the following
criteria:
– Input: Zero or more quantities are externally supplied.
– Output: At least one quantity is produced.
– Definiteness: Each instruction is clear and unambiguous.
– Finiteness: algorithm terminates after a finite number of steps.
– Correctness: Algorithms must produce correct result.
– Effectiveness: every instruction must be basic i.e. simple
instruction
Algorithm design and analysis process
Algorithm specification

• An algorithm can be specified in:

1. Simple English
2. Graphical representation like flow chart
3. Programming language like c/c++ / java
4. Combination of above methods.
Control Structures

All languages have


3 important control
structures
that we can use
in our algorithms
Control Structures
Structures that control how the program “flows” or operates,
and in what order:

Sequence

Decision Making

Looping

10
“Weekly Pay” Example

Create a program to calculate the


weekly pay of an hourly employee

What is the input, process, and output?

Input: pay rate and number of hours


Process: multiply pay rate by number of hours
Output: weekly pay

11
Step A: Pseudocode
Start with a plain English description, then…
1. Display “Number of hours worked: ”
2. Get the hours
3. Display “Amount paid per hour: ”
4. Get the rate
5. Compute pay = hours * rate
6. Display “The pay is $” , pay

12
Flowchart Symbols

Start
Start Symbol Input/Output

End
End Symbol Decision Symbol

Data Processing Symbol Flow Control Arrows

13
Step B: Flowchart

Start Get the rate

Display “Number
pay = hours * rate
of hours worked: ”

Display “The pay


Get the hours is $” , pay

Display “Amount
paid per hour: ” End
14
Sequence
One step after another, with no branches

Already wrote one for “Weekly Pay” problem

What are some real life examples?

 Dialing a phone number


 Purchasing and paying for groceries

15
Decision Making
Selecting one choice from many based
on a specific reason or condition

If something is true, do A … if it’s not, do B

What are some real life examples?

 Walking around campus (construction!)


 Choosing where to eat lunch

16
Decision Making: Pseudocode
Answer the question “Is a number positive?”

Start with a plain English description

1. Display “Enter the number: ”


2. Get the number (call it num)
3. If num > 0
4. Display “It is positive”
5. Else
6. Display “It is negative”

17
Decision Making: Flowchart

Display “Enter Get the


Start the number: ” number

TRUE FALSE
num > 0
Display Display
“It is positive” “It is negative”

End
18
Looping
Doing something over and over again

Used in combination with decision making


Otherwise we loop forever
This is called an “infinite loop”
What are some real life examples?

 Doing homework problem sets


 Walking up steps

19
Looping: Pseudocode
Write an algorithm that counts from 1-20

Start with a plain English description

1. Set num = 1
2. While num <= 20
3. Display num
4. num = num + 1
5. (End loop)

20
Looping: Flowchart

Start num = 1

There’s an error in this


flowchart… do you see it?

TRUE Display
num >= 20 num
num = num + 1

FALSE
End
21
Looping: Flowchart

Start num = 1

TRUE Display
num <= 20 num
num = num + 1

FALSE
End
22
Performance Analysis
• Space complexity
– Space Complexity of an algorithm is total space taken by
the algorithm with respect to the input size.
– Space complexity includes both Auxiliary space and space
used by input.

• Time complexity
Time Complexity

• Execution time or run-time of the program


is refereed as its time complexity
• This is the sum of the time taken to
execute all instructions in the program.

• But, we count only the number of steps in


the program.
• How to count?
– Two ways
Method-1
• Introduce a count variable
• Increment count for every operation
Method-2
• Count the steps per execution for every
line of code
• Note the frequency of execution of
every line and find the total steps.
Method-2
Trade-off
• One has to make a compromise and to exchange
computing time for memory consumption or vice
versa, depending on application.
Analysis Framework

• Space complexity
• Time complexity

• Measuring an Input’s Size


– run longer on larger inputs
• Units for Measuring Running time
– Basic operation
• Order of Growth
Orders of Growth

• Order of growth of an algorithm is a way of stating how execution


time or memory occupied by it changes with the input size.
• Why emphasis on large input sizes?
• Because for large values of n, it is the function's order of growth
that counts.

You might also like