SlideShare a Scribd company logo
1 of 73
Muhammad Nawaz Shareef University of
Agriculture, Multan
CS-701
Advanced Analysis of Algorithms
Instructor : Dr. Abdul Razzaq
Email: abdul.razzaq@mnsuam.edu.pk
Lecture 01
Welcome to
CS-701
Please be on time
(better: before time)
Introduction to Algorithm
Analysis
Learning Goals for Today
• Course objectives and outcomes
To become familiar with the concept of algorithms:
– What they are?
– What is their use?
– What do they consist of?
– What are the techniques used for representing them?
• In particular, we will look at the building blocks that are used
in all algorithms
• We will also discuss the pseudo code and flowcharts for
particular problems
• In addition, we will outline the pros and cons of those two
Course objective
Major objective of this course is:
• Design and analysis of modern algorithms
• Different variants
• Accuracy
• Efficiency
• Comparing efficiencies
• Motivation thinking new algorithms
• Advanced designing techniques
• Real world problems will be taken as examples
• To create feelings about usefulness of this course
Course Outcomes
At the end of the course, you should be able to….
• describe the major modern algorithms and selected techniques that are
essential to today’s computers.
• decide on the suitability of a specific algorithm design technique for a given
problem.
• Analyze average and worst-case running times
• Argue and prove correctness of algorithms
• Use of graph theory in problems solving
• apply the algorithms and design techniques to solve problems, and
mathematically evaluate the quality of the solutions
Course Contents
The following major topics will be covered in this
semester:
Introduction to Algorithm Analysis
• Mathematical Preliminaries
• Asymptotic Analysis
• Recurrences
• Sorting Algorithm Analysis
• Graph Algorithms
• Dynamic Programming
• String Processing Algorithms
• Theory of NP-Completeness
• Approximation Algorithms
7
Solving Problems (1)
When faced with a problem:
1. We first clearly define the problem
2. Think of possible solutions
3. Select the one that we think is the best
under the prevailing circumstances
4. And then apply that solution
5. If the solution woks as desired, fine; else
we go back to step 2
8
Solving Problems (2)
• It is quite common to first solve a problem
for a particular case
• Then for another
• And, possibly another
• And watch for patterns and trends that
emerge
• And to use the knowledge form those
patterns and trends in coming up with a
general solution
9
Solving Problems (3)
• It helps if we have experienced that problem or
similar ones before
• Generally, there are many ways of solving a
given problem; the best problem-solvers
come-up with the most appropriate solution
more often than not!
• The process that can be used to solve a
problem is termed as the “algorithm”
10
al.go.rithm
Sequence of steps
that can be taken to solve a given problem
steps
sequence
11
Examples
• Addition
• Conversion from decimal to binary
• The process of boiling an egg
• The process of mailing a letter
• Sorting
• Searching
12
Algorithm Applications
A large variety of problems in computer science, mathematics and other disciplines
depend on the use of algorithms for their solutions. The broad categories of
applications types are:
• •Searching Algorithms(Linear and non-linear)
• •Sorting Algorithms(Elementary and Advanced)
• •Strings Processing( Pattern matching, Parsing, Compression, Cryptography)
• •Optimization Algorithms(Shortest routes, minimum cost)
• •Geometric Algorithms( Triangulation, Convex Hull)
• •Image Processing( Compression, Matching, Conversion)
• •Data Mining Algorithms( Clustering, Cleansing, Rules mining)
• •Mathematical Algorithms(Random number generator, matrix operations, FFT, etc)
• Problem Domains
13
Let us write down the algorithm for
a problem that is familiar to us
Converting a decimal number into binary
14
Convert 75 to Binary
75
2
37 1
2
18 1
2
9 0
2
4 1
2
2 0
2
1 0
2
0 1
1001011
remainder
15
Algorithm for Decimal-to-Binary Conversion
1. Write the decimal number
2. Divide by 2; write quotient and remainder
3. Repeat step 2 on the quotient; keep on
repeating until the quotient becomes zero
4. Write all remainder digits in the reverse order
(last remainder first) to form the final result
16
Points to Note:
1. The process consists of repeated application
of simple steps
2. All steps are unambiguous (clearly defined)
3. We are capable of doing all those steps
4. Only a limited no. of steps needs to be taken
5. Once all those steps are taken according to
the prescribed sequence, the required result
will be found
6. Moreover, the process will stop at that point
17
Algorithm (Better Definition)
1st Definition:
Sequence of steps that can be taken
to solve a problem
Better Definition:
A precise sequence of a limited
number of unambiguous, executable
steps that terminates in the form of a
solution
• A computer algorithm is a detailed step-by-step method
for solving a problem by using a computer.
• An Algorithm is well defined computational procedure that
takes some value, or set of values, as input and produces
some value, or set of values as output.
• More generally, an Algorithm is any well defined
computational procedure that takes collection of elements
as input and produces a collection of elements as output.
Algorithm
Input output
What is Algorithm?
19
Three Requirements:
1. Sequence is:
a. Precise
b. Consists of a limited number of steps
2. Each step is:
a. Unambiguous
b. Executable
3. The sequence of steps terminates in
the form of a solution
20
Why Algorithms are Useful?
• Once we find an algorithm for solving a
problem, we do not need to re-discover it the
next time we are faced with that problem
• Once an algorithm is known, the task of
solving the problem reduces to following
(almost blindly and without thinking) the
instructions precisely
• All the knowledge required for solving the
problem is present in the algorithm
21
Analysis of Algorithms
• Analysis in the context of algorithms is concerned with
predicting the resources that requires:
– Computational time
– Memory
– Logic functions
• However, Time – generally measured in terms of the
number of steps required to execute an algorithm - is
the resource of most interest
• By analyzing several candidate algorithms, the most
efficient one(s) can be identified
• Brute Force
– Straightforward, naive approach
– Mostly expensive
• Divide-and-Conquer
– Divide into smaller sub-problems
• Iterative Improvement
– Improve one change at a time
• Decrease-and-Conquer
– Decrease instance size
• Transform-and-Conquer
– Modify problem first and then solve it
• Space and Time Tradeoffs
– Use more space now to save time later
Important Designing Techniques
23
Greedy Algorithm
• An algorithm that always takes the best
immediate, or local solution while finding an
answer
• Greedy algorithms may find the overall or
globally optimal solution for some optimization
problems, but may find less-than-optimal
solutions for some instances of other problems
• KEY ADVANTAGE: Greedy algorithms are
usually faster, since they don't consider the
details of possible alternatives
24
Deterministic Algorithm
• An algorithm whose behavior can be completely
predicted from the inputs
• That is, each time a certain set of input is
presented, the algorithm gives the same results
as any other time the set of input is presented
25
Heuristic
Not guaranteed to be optimal or perfect, but sufficient for the immediate goals.
• A procedure that usually, but not always, works
or that gives nearly the right answer
• Some problems, such as the traveling
salesman problem, take far too long to compute
an exact, optimal solution. A few good
heuristics have been devised that are fast and
find a near-optimal solution more often than not
• Is a heuristic, an algorithm? Yes? No? Why?
26
The Traveling Salesman Problem
A salesman needs
to visit each of the n
cities one after the
other and wants to
finish the trip where
it was started
Determine the
sequence of cities
such that the
traveling distance is
minimized
2
6
5
4
3
1
A possible sequence for n = 6
27
The Brute Force Strategy
• A strategy in which all possible combinations
are examined and the best among them is
selected
• What is the problem with this approach?
A: Doesn’t scale well with the size of the
problem
• How many possible city sequences for n=6? For
n=60? For n=600?
28
Algorithm Representation
• We have said enough about algorithms – their
definition, their types, etc.
• But, how do we actually represent them?
• Generally, SW developers represent them in
one of three forms:
– Pseudo code
– Flowcharts
– Actual code
29
Pseudo Code
• Language that is typically used for writing
algorithms
• Similar to a programming language, but not as
rigid
• The method of expression most suitable for a
given situation is used:
– At times, plain English
– At others, a programming language like syntax
30
Flowchart
• A graphical representation of a process (e.g. an
algorithm), in which graphic objects are used to
indicate the steps & decisions that are taken as
the process moves along from start to finish
• Individual steps are represented by boxes and
other shapes on the flowchart, with arrows
between those shapes indicating the order in
which the steps are taken
31
Start or stop
Process
Input or output
Connector
Decision
Flow line
Off-page connector
Flowchart
Elements
32
Sequences
A sequence of instructions that are executed
in the precise order they are written in:
statement block 1
statement block 2
statement block 3
statement block 1
statement block 2
statement block 3
33
Conditionals
Select between alternate courses of action
depending upon the evaluation of a condition
If ( condition = true )
statement block 1
Else
statement block 2
End if statement
block 1
condition
True False
statement
block 2
34
Loops
Loop through a set of statements as long as a
condition is true
Loop while ( condition = true )
statement block
End Loop
condition
True
False
statement
block
35
We will now present the algorithm for a
problem whose solution is familiar to us
We will first go through the problem
statement and then present the algorithm
in three different formats:
1. Pseudo code
2. Flowchart
3. Actual code
36
Problem Statement
Convert a decimal number into binary
37
Convert 75 to Binary
75
2
37 1
2
18 1
2
9 0
2
4 1
2
2 0
2
1 0
2
0 1
1001011
remainder
38
Solution in Pseudo Code
1. Let the decimal number be an integer x, x > 0
2. Let the binary equivalent be an empty string y
3. Repeat while x > 0 {
Determine the quotient & remainder of x Ă· 2
y = CONCATENATE( remainder, y )
x = quotient
}
4. Print y
5. Stop
39
Q: Is this the only possible algorithm for
converting a decimal number into a binary
representation?
If not, then is this the best?
In terms of speed?
In terms of memory requirement?
In terms of ease of implementation?
You must ask these questions after
writing any algorithm!
40
Tips on Writing Good Pseudo Code
• Use indention for improved clarity
• Do not put “code” in pseudo code – make your
pseudo code language independent
• Don’t write pseudo code for yourself – write it
in an unambiguous fashion so that anyone with
a reasonable knowledge can understand and
implement it
• Be consistent
• Prefer formulas over English language
descriptions
41
Start
Find quotient
& remainder
of x Ă· 2
Get x
x>0 ?
Print y
Stop
y = CONC(remainder, x)
x = quotient
x is the decimal number
y is the binary equivalent
Flowchart of Decimal
to Binary Conversion
Yes
No
42
1. Does the flowchart depict the “correct”
algorithm?
2. What do we mean by “correct”, or better
yet, what do we check for “correctness”?
3. One way is to check the algorithm for a
variety of inputs
4. Does it perform satisfactorily for:
• x = 0 ?
• negative numbers?
• numbers with fractional parts?
43
Decimal to Binary Conversion in C
language
44
Another Example: Sorting
Sort the following objects w.r.t. their heights
45
Expected Result
46
Strategy
There are many strategies for solving this
problem. We demonstrate a simple one:
Repeat the following steps while the list is un-sorted:
Start with the first object in the list
Swap it with the one next to it if they are in the wrong order
Repeat the same with the next to the first object
Keep on repeating until you reach the last object in the list
47
Back to the Objects to be Sorted
48
Q: Is the list sorted?
A: No
49
Sorting: Step A1
50
Sorting: Step A1
Swap? Yes
51
Sorting: Step A2
52
Sorting: Step A2
Swap? Yes
53
Sorting: Step A3
54
Sorting: Step A3
Swap? No
55
Sorting: After Step A7
56
Q: Is the list sorted?
A: No
57
Sorting: Step B1
58
Sorting: Step B1
Swap? Yes
59
Sorting: Step B2
60
Sorting: Step B2
Swap? No
61
Sorting: After Step B7
62
Q: Is the list sorted?
A: No
63
Sorting: Step C1
64
Sorting: Step C1
Swap? No
65
Sorting: After Step C7
66
Q: Is the list sorted?
A: Yes
67
68
Pros and Cons of Flowcharts (1)
• The process of writing an algorithm in the form
of a flowchart is just too cumbersome(bulky)
• And then converting this graphical form into
code is not straight forward
• However, there is another kind of flowcharts –
called Structured Flowcharts – that may be
better suited for software developers
69
Pros and Cons of Flowcharts (2)
• The good thing about flowcharts is that their
symbols are quite intuitive and almost
universally understood
• Their graphical nature makes the process of
explaining an algorithm to one’s peers quite
straightforward
70
Pros and Cons of Pseudo Code (1)
• Quite suitable for SW development as it is
closer in form to real code
• One can write the pseudo code, then use it as a
starting point or outline for writing real code
• Many developers write the pseudo code first
and then incrementally comment each line out
while converting that line into real code
71
Pros and Cons of Pseudo Code (2)
• Pseudo code can be constructed quite quickly
as compared with a flowchart
• Unlike flowcharts, no standard rules exist for
writing pseudo code
72
Difference between Algorithm, Flowchart
and Pseudo Code
• An algorithm is a sequence of instructions used
to solve a particular problem
• Flowchart and Pseudo code are tools to
document and represent the algorithm.
• In other words, an algorithm can be
represented using a flowchart or a pseudo
code.
73
In Today’s Lecture, We …
Became familiar with the concept of algorithms:
– What they are?
– What is their use?
– What do they consist of?
– What are the techniques used for representing
them?
• In particular, we looked at the building blocks that
are used in all algorithms
• We also discussed the pseudo code and flowcharts
for particular problems
• In addition, we outlined the pros and cons of those
two techniques

More Related Content

Similar to Algo_Lecture01.pptx

Problem solving
Problem solvingProblem solving
Problem solvinghamza239523
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structureSelf-Employed
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdfMIT,Imphal
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computingjayavignesh86
 
PPS_Unit 1.pptx
PPS_Unit 1.pptxPPS_Unit 1.pptx
PPS_Unit 1.pptxKundanBhatkar
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)farazahmad005
 
Ic lecture6 architecture and algo
Ic lecture6 architecture and algoIc lecture6 architecture and algo
Ic lecture6 architecture and algoAttaullahRahimoon
 
Architecture Algorithm Definition
Architecture Algorithm DefinitionArchitecture Algorithm Definition
Architecture Algorithm DefinitionGaditek
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Concept of Algorithm.pptx
Concept of Algorithm.pptxConcept of Algorithm.pptx
Concept of Algorithm.pptxElProfesor14
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.pptALIZAIB KHAN
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptxASVKVinayak
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm IntroductionAshim Lamichhane
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++Online
 

Similar to Algo_Lecture01.pptx (20)

Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Problem solving
Problem solvingProblem solving
Problem solving
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
Algo
AlgoAlgo
Algo
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computing
 
PPS_Unit 1.pptx
PPS_Unit 1.pptxPPS_Unit 1.pptx
PPS_Unit 1.pptx
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
Ic lecture6 architecture and algo
Ic lecture6 architecture and algoIc lecture6 architecture and algo
Ic lecture6 architecture and algo
 
Architecture Algorithm Definition
Architecture Algorithm DefinitionArchitecture Algorithm Definition
Architecture Algorithm Definition
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 
Concept of Algorithm.pptx
Concept of Algorithm.pptxConcept of Algorithm.pptx
Concept of Algorithm.pptx
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
 
Lecture01.ppt
Lecture01.pptLecture01.ppt
Lecture01.ppt
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++
 

More from ShaistaRiaz4

Lecture3(b).pdf
Lecture3(b).pdfLecture3(b).pdf
Lecture3(b).pdfShaistaRiaz4
 
Algorithms Analysis.pdf
Algorithms Analysis.pdfAlgorithms Analysis.pdf
Algorithms Analysis.pdfShaistaRiaz4
 
Case Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfCase Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfShaistaRiaz4
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).pptShaistaRiaz4
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.pptShaistaRiaz4
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).pptShaistaRiaz4
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.pptShaistaRiaz4
 
Bisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdfBisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdfShaistaRiaz4
 
MNS Lecture 1.pptx
MNS Lecture 1.pptxMNS Lecture 1.pptx
MNS Lecture 1.pptxShaistaRiaz4
 
Plan (2).pptx
Plan (2).pptxPlan (2).pptx
Plan (2).pptxShaistaRiaz4
 
Lecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdfLecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdfShaistaRiaz4
 
Lecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfLecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfShaistaRiaz4
 
oppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptxoppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptxShaistaRiaz4
 
Development Education.pptx
Development Education.pptxDevelopment Education.pptx
Development Education.pptxShaistaRiaz4
 
WISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.pptWISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.pptShaistaRiaz4
 
Summary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptxSummary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptxShaistaRiaz4
 
MH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.pptMH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.pptShaistaRiaz4
 
Intro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.pptIntro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.pptShaistaRiaz4
 
Coping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.pptCoping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.pptShaistaRiaz4
 
Intellectual_development.ppt
Intellectual_development.pptIntellectual_development.ppt
Intellectual_development.pptShaistaRiaz4
 

More from ShaistaRiaz4 (20)

Lecture3(b).pdf
Lecture3(b).pdfLecture3(b).pdf
Lecture3(b).pdf
 
Algorithms Analysis.pdf
Algorithms Analysis.pdfAlgorithms Analysis.pdf
Algorithms Analysis.pdf
 
Case Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfCase Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdf
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.ppt
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.ppt
 
Bisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdfBisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdf
 
MNS Lecture 1.pptx
MNS Lecture 1.pptxMNS Lecture 1.pptx
MNS Lecture 1.pptx
 
Plan (2).pptx
Plan (2).pptxPlan (2).pptx
Plan (2).pptx
 
Lecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdfLecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdf
 
Lecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfLecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdf
 
oppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptxoppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptx
 
Development Education.pptx
Development Education.pptxDevelopment Education.pptx
Development Education.pptx
 
WISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.pptWISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.ppt
 
Summary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptxSummary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptx
 
MH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.pptMH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.ppt
 
Intro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.pptIntro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.ppt
 
Coping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.pptCoping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.ppt
 
Intellectual_development.ppt
Intellectual_development.pptIntellectual_development.ppt
Intellectual_development.ppt
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

Algo_Lecture01.pptx

  • 1. Muhammad Nawaz Shareef University of Agriculture, Multan CS-701 Advanced Analysis of Algorithms Instructor : Dr. Abdul Razzaq Email: abdul.razzaq@mnsuam.edu.pk
  • 2. Lecture 01 Welcome to CS-701 Please be on time (better: before time) Introduction to Algorithm Analysis
  • 3. Learning Goals for Today • Course objectives and outcomes To become familiar with the concept of algorithms: – What they are? – What is their use? – What do they consist of? – What are the techniques used for representing them? • In particular, we will look at the building blocks that are used in all algorithms • We will also discuss the pseudo code and flowcharts for particular problems • In addition, we will outline the pros and cons of those two
  • 4. Course objective Major objective of this course is: • Design and analysis of modern algorithms • Different variants • Accuracy • Efficiency • Comparing efficiencies • Motivation thinking new algorithms • Advanced designing techniques • Real world problems will be taken as examples • To create feelings about usefulness of this course
  • 5. Course Outcomes At the end of the course, you should be able to…. • describe the major modern algorithms and selected techniques that are essential to today’s computers. • decide on the suitability of a specific algorithm design technique for a given problem. • Analyze average and worst-case running times • Argue and prove correctness of algorithms • Use of graph theory in problems solving • apply the algorithms and design techniques to solve problems, and mathematically evaluate the quality of the solutions
  • 6. Course Contents The following major topics will be covered in this semester: Introduction to Algorithm Analysis • Mathematical Preliminaries • Asymptotic Analysis • Recurrences • Sorting Algorithm Analysis • Graph Algorithms • Dynamic Programming • String Processing Algorithms • Theory of NP-Completeness • Approximation Algorithms
  • 7. 7 Solving Problems (1) When faced with a problem: 1. We first clearly define the problem 2. Think of possible solutions 3. Select the one that we think is the best under the prevailing circumstances 4. And then apply that solution 5. If the solution woks as desired, fine; else we go back to step 2
  • 8. 8 Solving Problems (2) • It is quite common to first solve a problem for a particular case • Then for another • And, possibly another • And watch for patterns and trends that emerge • And to use the knowledge form those patterns and trends in coming up with a general solution
  • 9. 9 Solving Problems (3) • It helps if we have experienced that problem or similar ones before • Generally, there are many ways of solving a given problem; the best problem-solvers come-up with the most appropriate solution more often than not! • The process that can be used to solve a problem is termed as the “algorithm”
  • 10. 10 al.go.rithm Sequence of steps that can be taken to solve a given problem steps sequence
  • 11. 11 Examples • Addition • Conversion from decimal to binary • The process of boiling an egg • The process of mailing a letter • Sorting • Searching
  • 12. 12 Algorithm Applications A large variety of problems in computer science, mathematics and other disciplines depend on the use of algorithms for their solutions. The broad categories of applications types are: • •Searching Algorithms(Linear and non-linear) • •Sorting Algorithms(Elementary and Advanced) • •Strings Processing( Pattern matching, Parsing, Compression, Cryptography) • •Optimization Algorithms(Shortest routes, minimum cost) • •Geometric Algorithms( Triangulation, Convex Hull) • •Image Processing( Compression, Matching, Conversion) • •Data Mining Algorithms( Clustering, Cleansing, Rules mining) • •Mathematical Algorithms(Random number generator, matrix operations, FFT, etc) • Problem Domains
  • 13. 13 Let us write down the algorithm for a problem that is familiar to us Converting a decimal number into binary
  • 14. 14 Convert 75 to Binary 75 2 37 1 2 18 1 2 9 0 2 4 1 2 2 0 2 1 0 2 0 1 1001011 remainder
  • 15. 15 Algorithm for Decimal-to-Binary Conversion 1. Write the decimal number 2. Divide by 2; write quotient and remainder 3. Repeat step 2 on the quotient; keep on repeating until the quotient becomes zero 4. Write all remainder digits in the reverse order (last remainder first) to form the final result
  • 16. 16 Points to Note: 1. The process consists of repeated application of simple steps 2. All steps are unambiguous (clearly defined) 3. We are capable of doing all those steps 4. Only a limited no. of steps needs to be taken 5. Once all those steps are taken according to the prescribed sequence, the required result will be found 6. Moreover, the process will stop at that point
  • 17. 17 Algorithm (Better Definition) 1st Definition: Sequence of steps that can be taken to solve a problem Better Definition: A precise sequence of a limited number of unambiguous, executable steps that terminates in the form of a solution
  • 18. • A computer algorithm is a detailed step-by-step method for solving a problem by using a computer. • An Algorithm is well defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output. • More generally, an Algorithm is any well defined computational procedure that takes collection of elements as input and produces a collection of elements as output. Algorithm Input output What is Algorithm?
  • 19. 19 Three Requirements: 1. Sequence is: a. Precise b. Consists of a limited number of steps 2. Each step is: a. Unambiguous b. Executable 3. The sequence of steps terminates in the form of a solution
  • 20. 20 Why Algorithms are Useful? • Once we find an algorithm for solving a problem, we do not need to re-discover it the next time we are faced with that problem • Once an algorithm is known, the task of solving the problem reduces to following (almost blindly and without thinking) the instructions precisely • All the knowledge required for solving the problem is present in the algorithm
  • 21. 21 Analysis of Algorithms • Analysis in the context of algorithms is concerned with predicting the resources that requires: – Computational time – Memory – Logic functions • However, Time – generally measured in terms of the number of steps required to execute an algorithm - is the resource of most interest • By analyzing several candidate algorithms, the most efficient one(s) can be identified
  • 22. • Brute Force – Straightforward, naive approach – Mostly expensive • Divide-and-Conquer – Divide into smaller sub-problems • Iterative Improvement – Improve one change at a time • Decrease-and-Conquer – Decrease instance size • Transform-and-Conquer – Modify problem first and then solve it • Space and Time Tradeoffs – Use more space now to save time later Important Designing Techniques
  • 23. 23 Greedy Algorithm • An algorithm that always takes the best immediate, or local solution while finding an answer • Greedy algorithms may find the overall or globally optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems • KEY ADVANTAGE: Greedy algorithms are usually faster, since they don't consider the details of possible alternatives
  • 24. 24 Deterministic Algorithm • An algorithm whose behavior can be completely predicted from the inputs • That is, each time a certain set of input is presented, the algorithm gives the same results as any other time the set of input is presented
  • 25. 25 Heuristic Not guaranteed to be optimal or perfect, but sufficient for the immediate goals. • A procedure that usually, but not always, works or that gives nearly the right answer • Some problems, such as the traveling salesman problem, take far too long to compute an exact, optimal solution. A few good heuristics have been devised that are fast and find a near-optimal solution more often than not • Is a heuristic, an algorithm? Yes? No? Why?
  • 26. 26 The Traveling Salesman Problem A salesman needs to visit each of the n cities one after the other and wants to finish the trip where it was started Determine the sequence of cities such that the traveling distance is minimized 2 6 5 4 3 1 A possible sequence for n = 6
  • 27. 27 The Brute Force Strategy • A strategy in which all possible combinations are examined and the best among them is selected • What is the problem with this approach? A: Doesn’t scale well with the size of the problem • How many possible city sequences for n=6? For n=60? For n=600?
  • 28. 28 Algorithm Representation • We have said enough about algorithms – their definition, their types, etc. • But, how do we actually represent them? • Generally, SW developers represent them in one of three forms: – Pseudo code – Flowcharts – Actual code
  • 29. 29 Pseudo Code • Language that is typically used for writing algorithms • Similar to a programming language, but not as rigid • The method of expression most suitable for a given situation is used: – At times, plain English – At others, a programming language like syntax
  • 30. 30 Flowchart • A graphical representation of a process (e.g. an algorithm), in which graphic objects are used to indicate the steps & decisions that are taken as the process moves along from start to finish • Individual steps are represented by boxes and other shapes on the flowchart, with arrows between those shapes indicating the order in which the steps are taken
  • 31. 31 Start or stop Process Input or output Connector Decision Flow line Off-page connector Flowchart Elements
  • 32. 32 Sequences A sequence of instructions that are executed in the precise order they are written in: statement block 1 statement block 2 statement block 3 statement block 1 statement block 2 statement block 3
  • 33. 33 Conditionals Select between alternate courses of action depending upon the evaluation of a condition If ( condition = true ) statement block 1 Else statement block 2 End if statement block 1 condition True False statement block 2
  • 34. 34 Loops Loop through a set of statements as long as a condition is true Loop while ( condition = true ) statement block End Loop condition True False statement block
  • 35. 35 We will now present the algorithm for a problem whose solution is familiar to us We will first go through the problem statement and then present the algorithm in three different formats: 1. Pseudo code 2. Flowchart 3. Actual code
  • 36. 36 Problem Statement Convert a decimal number into binary
  • 37. 37 Convert 75 to Binary 75 2 37 1 2 18 1 2 9 0 2 4 1 2 2 0 2 1 0 2 0 1 1001011 remainder
  • 38. 38 Solution in Pseudo Code 1. Let the decimal number be an integer x, x > 0 2. Let the binary equivalent be an empty string y 3. Repeat while x > 0 { Determine the quotient & remainder of x Ă· 2 y = CONCATENATE( remainder, y ) x = quotient } 4. Print y 5. Stop
  • 39. 39 Q: Is this the only possible algorithm for converting a decimal number into a binary representation? If not, then is this the best? In terms of speed? In terms of memory requirement? In terms of ease of implementation? You must ask these questions after writing any algorithm!
  • 40. 40 Tips on Writing Good Pseudo Code • Use indention for improved clarity • Do not put “code” in pseudo code – make your pseudo code language independent • Don’t write pseudo code for yourself – write it in an unambiguous fashion so that anyone with a reasonable knowledge can understand and implement it • Be consistent • Prefer formulas over English language descriptions
  • 41. 41 Start Find quotient & remainder of x Ă· 2 Get x x>0 ? Print y Stop y = CONC(remainder, x) x = quotient x is the decimal number y is the binary equivalent Flowchart of Decimal to Binary Conversion Yes No
  • 42. 42 1. Does the flowchart depict the “correct” algorithm? 2. What do we mean by “correct”, or better yet, what do we check for “correctness”? 3. One way is to check the algorithm for a variety of inputs 4. Does it perform satisfactorily for: • x = 0 ? • negative numbers? • numbers with fractional parts?
  • 43. 43 Decimal to Binary Conversion in C language
  • 44. 44 Another Example: Sorting Sort the following objects w.r.t. their heights
  • 46. 46 Strategy There are many strategies for solving this problem. We demonstrate a simple one: Repeat the following steps while the list is un-sorted: Start with the first object in the list Swap it with the one next to it if they are in the wrong order Repeat the same with the next to the first object Keep on repeating until you reach the last object in the list
  • 47. 47 Back to the Objects to be Sorted
  • 48. 48 Q: Is the list sorted? A: No
  • 56. 56 Q: Is the list sorted? A: No
  • 62. 62 Q: Is the list sorted? A: No
  • 66. 66 Q: Is the list sorted? A: Yes
  • 67. 67
  • 68. 68 Pros and Cons of Flowcharts (1) • The process of writing an algorithm in the form of a flowchart is just too cumbersome(bulky) • And then converting this graphical form into code is not straight forward • However, there is another kind of flowcharts – called Structured Flowcharts – that may be better suited for software developers
  • 69. 69 Pros and Cons of Flowcharts (2) • The good thing about flowcharts is that their symbols are quite intuitive and almost universally understood • Their graphical nature makes the process of explaining an algorithm to one’s peers quite straightforward
  • 70. 70 Pros and Cons of Pseudo Code (1) • Quite suitable for SW development as it is closer in form to real code • One can write the pseudo code, then use it as a starting point or outline for writing real code • Many developers write the pseudo code first and then incrementally comment each line out while converting that line into real code
  • 71. 71 Pros and Cons of Pseudo Code (2) • Pseudo code can be constructed quite quickly as compared with a flowchart • Unlike flowcharts, no standard rules exist for writing pseudo code
  • 72. 72 Difference between Algorithm, Flowchart and Pseudo Code • An algorithm is a sequence of instructions used to solve a particular problem • Flowchart and Pseudo code are tools to document and represent the algorithm. • In other words, an algorithm can be represented using a flowchart or a pseudo code.
  • 73. 73 In Today’s Lecture, We … Became familiar with the concept of algorithms: – What they are? – What is their use? – What do they consist of? – What are the techniques used for representing them? • In particular, we looked at the building blocks that are used in all algorithms • We also discussed the pseudo code and flowcharts for particular problems • In addition, we outlined the pros and cons of those two techniques