0% found this document useful (0 votes)
14 views34 pages

week 2 lec 3

Uploaded by

abdullahzahidhp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views34 pages

week 2 lec 3

Uploaded by

abdullahzahidhp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

1

025

Wednesday,
nalysis of Algorithm
Lahore Garrison University
January 1, 2
CSC354-Design and Analysis of Algorithms
Week-2 Lecture-3
Semester-# Fall 2019
Prepared by:

A
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: [email protected]
 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 Wednesday


Analysis of Algorithm , January 1
, 2025
3
Previous Lecture Summary

 Information On Algorithm
 What is Algorithm?
 Types of Algorithms
 Examples of Algorithm

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
4
Today’s Lecture Summary

 Algorithm Analysis
 Efficiency Measurement
 Algorithm Correctness

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
5
Algorithm Analysis

While designing an algorithm we have to take care of two aspects:


1. Algorithm Correctness: Algorithm must give desired output within
finite time.
2. Algorithm Efficiency : Algorithms must use minimum resources while
executing on a machine.

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
6
Algorithm Correctness

How to verify that algorithm is correct?

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
7
Algorithm Correctness

How to verify that algorithm is correct?


 Empirical method: Correctness can be verified by testing different
instances of the problem

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
8
Algorithm Correctness

How to verify that algorithm is correct?


 Empirical method: Correctness can be verified by testing different
instances of the problem
 Advantage:
 Simple
 Easy to apply
 Disadvantage:
 It doesn't guarantee the correctness

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
9
Algorithm Correctness

How to verify that algorithm is correct?


 Formal method: Mathematically it is proved that algorithm will
produce the required output for all the instances of the problem within
finite time.
 Advantage:
 guarantees the correctness
 Disadvantage:
 Difficult
 Not possible for complex algorithms

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
10
Basic Notions

 Preconditions and Postconditions


 Algorithm State
 Assertions
 Annotation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
11
Preconditions and Postconditions

 Preconditions: Properties that must be satisfied by the input

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
12
Preconditions and Postconditions

 Preconditions: Properties that must be satisfied by the input


 Postconditions: Properties that must be satisfied by the result

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
13
Preconditions and Postconditions

 Preconditions: Properties that must be satisfied by the input


 Postconditions: Properties that must be satisfied by the result
 Example: Find the minimum, m, of a non-empty array, x[1..n]

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
14
Preconditions and Postconditions

 Preconditions: Properties that must be satisfied by the input


 Postconditions: Properties that must be satisfied by the result
 Example: Find the minimum, m, of a non-empty array, x[1..n]

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
15

 Partial correctness verification: prove that if an algorithm


terminates it leads to postcondition starting from precondition.
 Intermediate steps in correctness verification:
 analyze the algorithm state
 the effect of each processing step on the algorithm state

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
16
Basic Notions

 Preconditions and Postconditions


 Algorithm State
 Assertions
 Annotation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
17
Algorithm State

 Algorithm state: set of values corresponding to all variables used in the algorithm

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
18
Algorithm State

 Algorithm state: set of values corresponding to all variables used in the algorithm
 During the execution of an algorithm its state changes (since the variables change
their values)

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
19
Algorithm State

 Algorithm state: set of values corresponding to all variables used in the algorithm
 During the execution of an algorithm its state changes (since the variables change
their values)
 The algorithm is correct if at the end of the algorithm its state implies the
postconditions

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
20
Algorithm State

 Algorithm state: set of values corresponding to all variables used in the algorithm
 During the execution of an algorithm its state changes (since the variables change
their values)
 The algorithm is correct if at the end of the algorithm its state implies the
postconditions

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
21
Algorithm State

 Algorithm state: set of values corresponding to all variables used in the algorithm
 During the execution of an algorithm its state changes (since the variables change
their values)
 The algorithm is correct if at the end of the algorithm its state implies the
postconditions

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
22
Algorithm State

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
23
Algorithm State

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
24
Basic steps in correctness
verification

 Identify the preconditions and postconditions


 Annotate the algorithm with assertions concerning its state such that
 the preconditions are satisfied
 the final assertion implies the postconditions
 Prove that by each processing step one arrives from the previous
assertion to the next assertion

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
25
Some Notation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
26
Example: Assertions and Annotation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
27
Example: Assertions and Annotation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
28

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
29

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
30
Example: Assertions and Annotation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
31
Example: Assertions and Annotation

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
32

 FURTHER DISCUSSION

NEXT WEEK …………….

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
33

Q&A

Lahore Garrison University Wednesday


Analysis of Algorithm , January 1
, 2025
34
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 Wednesday


Analysis of Algorithm , January 1
, 2025

You might also like