You are on page 1of 30

1

Lahore Garrison University


Algorithm
Friday, January
12, 2024

CSC354-Design and Analysis of Algorithms


Week-1 Lecture-2
Semester-# Fall 2019
Prepared by:

Awais Salman Qazi


2
Instructor Contact Details

 Name: Awais Salman Qazi


 Course Instructor: Design and Analysis of Algorithms
 Credit Hours: 3
 Office Location: CS-Faculty Office, 2nd Floor New Building
 Email: awais.salman@lgu.edu.pk
 Visiting Hours: Friday 8am-10am. Also after class. Students can also walk in if I am free
 phone calls are not allowed

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
3
Previous Lecture Summary

 History of Algorithm
 Introduction of Algorithm

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
4
Today’s Lecture Summary

 Some types of Algorithms


 Examples

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
5

Types of Algorithms

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
6
PROBABILISITIC ALGORITHM

In this algorithm, chosen values are used in such a way that the probability of chosen each value is
known and controlled.

e.g. Randomize Quick Sort

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
7
HEURISTIC ALGORITHM

In computer science, artificial intelligence, and mathematical optimization, a heuristic is a technique


designed for solving a problem more quickly when classic methods are too slow, or for finding an
approximate solution when classic methods fail to find any exact solution.

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
APPROXIMATE ALGORITHM
8
APPROXIMATE ALGORITHM

In this algorithm, answer is obtained that is as précised as required in decimal


notation. In other words it specifies the error we are willing to accept.

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
9
Example of Algorithm

Problem : Let a and b be strictly positive integers. Find the greatest common
divisor of a and b
Euclidean Method:
 Compute r, the reminder obtained by dividing a by b
 Replace a with b, b with r, and start the process again
 The process continue until one obtains a reminder equal to zero
 Then the previous reminder(which , obviously, is not zero) will be the gcd(a,b)

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
10

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
Where We're Going 11

 Learn general approaches to algorithm design


 Divide and conquer
 Greedy method
 Dynamic Programming
 Basic Search and Traversal Technique
 Graph Theory
 Linear Programming
 NP Problem

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
12
Some Application

 Study problems these techniques can be applied to


 sorting
 data retrieval
 network routing
 Games
 etc

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
13
What do we analyze about them?

 Correctness
 Does the input/output relation match algorithm requirement?

 Amount of work done


 Basic operations to do task finite amount of time

 Amount of space used


 Memory used

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
14
RAM model

 has one processor


 executes one instruction at a time
 each instruction takes "unit time“
 has fixed-size operands, and
 has fixed size storage (RAM and disk).

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
15

Lahore Garrison University Analysis of Al


gorithm
Friday, January
12, 2024
Example Of Algorithm
16
Few Classical Examples

Classical Multiplication Algorithms

• English
• American
• A la russe
• Divide and Conquer

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
17
Classic Multiplication

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
18
Multiplication

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
19
Multiplication

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
20
The Selection Problem

Which algorithm is better?

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
21
Which algorithm is better?

The algorithms are correct, but which is the best?


 Measure the running time (number of operations
needed).
 Measure the amount of memory used.
 Note that the running time of the algorithms
increase as the size of the input increases.

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
Running Time

 Most algorithms transform input objects into best case


average case
output objects. worst case
 The running time of an algorithm typically 120

grows with the input size. 100

Running Time
 Average case time is often difficult to 80

determine. 60

 We focus on the worst case running time. 40

 Easier to analyze 20

 Crucial to applications such as games, finance and 0


1000 2000 3000 4000
robotics
Input Size

Friday, January 12, 2024


Lahore Garrison University
Algorithm
Analysis of
22
Experimental Studies

 Write a program implementing the algorithm 9000

 Run the program with inputs of varying size and 8000

composition 7000

 6000
Use a method like System.currentTimeMillis() to get

Time (ms)
an accurate measure of the actual running time 5000

 4000
Plot the results
3000

2000

1000

0
0 50 100
Friday, January 12, 2024
Lahore Garrison University
Algorithm
Analysis of
23
Input Size
Friday, January 12, 2024
Lahore Garrison University
Algorithm
Analysis of
24
25
Limitations of Experiments

 It is necessary to implement the algorithm, which may be difficult


 Results may not be indicative of the running time on other inputs not included in
the experiment.
 In order to compare two algorithms, the same hardware and software
environments must be used

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
26
Theoretical Analysis

 Uses a high-level description of the algorithm instead of an implementation


 Characterizes running time as a function of the input size, n.
 Takes into account all possible inputs
 Allows us to evaluate the speed of an algorithm independent of the
hardware/software environment

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
27
Function of Growth rate

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
28

 FURTHER DISCUSSION

NEXT WEEK …………….

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
29

Q &A

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024
30
References

 These lecture notes were taken from following source:

 Introduction to Algorithms 2nd ,Cormen, Leiserson, Rivest and Stein, The


MIT Press, 2001.

Lahore Garrison University Analysis of Friday, Janua


Algorithm ry 12, 2024

You might also like