01 Introduction to analysis of Algorithms.pptxssuser586772
An algorithm is a set of steps of operations to solve a problem performing calculation, data processing, and automated reasoning tasks.
An algorithm is an efficient method that can be expressed within finite amount of Time and space.
The important aspects of algorithm design include creating an efficient algorithm to solve a problem in an efficient way using minimum time and space.
To solve a problem, different approaches can be followed. Some of them can be efficient with respect to time consumption, whereas other approaches may be memory efficient. To Evaluate An Algorithm we have to Satisfy the following Criteria:
INPUT: The Algorithm should be given zero or more input.
OUTPUT: At least one quantity is produced. For each input the algorithm produced value from specific task.
DEFINITENESS: Each instruction is clear and unambiguous.
FINITENESS: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps.
EFFECTIVENESS: Every instruction must very basic so that it can be carried out, in principle, by a person using only pencil & paper.Algorithm : Systematic logical approach which is a well-defined, step-by-step procedure that allows a computer to solve a problem.
Pseudocode : It is a simpler version of a programming code in plain English which uses short phrases to write code for a program before it is implemented in a specific programming language.
Program : It is exact code written for problem following all the rules of the programming language.Algorithm can be described (Represent) in four ways.
Natural language like English:
When this way is chooses, care should be taken, we
should ensure that each & every statement is definite.
(no ambiguity)
2. Graphic representation called flowchart:
This method will work well when the algorithm is small& simple.
3. Pseudo-code Method:
In this method, we should typically describe algorithms as
program, which resembles language like Pascal & Algol
(Algorithmic Language).
4.Programming Language:
we have to use programming language to write algorithms like
C, C++,JAVA etc.Comments begin with // and continue until the end of line.
Blocks are indicated with matching braces { and }.
An identifier begins with a letter. The data types of variables are not explicitly declared.
node= record
{
data type 1 data 1;
data type n data n;
node *link;
}
4. There are two Boolean values TRUE and FALSE.
Logical Operators
AND, OR, NOT
Relational Operators
<, <=,>,>=, =, !=
1.How to create an algorithm: To create an algorithm we have following design technique
a) Divide & Conquer
b) Greedy method
c) Dynamic Programming
d) Branch & Bound
e) Backtracking
2.How to validate an algorithm:
Once an algorithm is created it is necessary to show that it computes the correct Pr
The document discusses algorithms and their analysis. It begins by defining an algorithm and listing requirements like being unambiguous and finite. It describes writing algorithms using pseudocode or flowcharts and proving their correctness. The document then discusses analyzing algorithms by measuring their time and space efficiency using orders of growth. It explains analyzing best, worst, and average cases and counting basic operations. Finally, it provides examples of analyzing simple algorithms involving if statements and loops.
The document provides an overview of algorithms, including definitions, types, characteristics, and analysis. It begins with step-by-step algorithms to add two numbers and describes the difference between algorithms and pseudocode. It then covers algorithm design approaches, characteristics, classification based on implementation and logic, and analysis methods like a priori and posteriori. The document emphasizes that algorithm analysis estimates resource needs like time and space complexity based on input size.
Design and Analysis of Algorithm ppt for unit onessuserb7c8b8
The document outlines an algorithms course, including course details, objectives, and an introduction. The course code is 10211CS202 and name is Design and Analysis of Algorithms. It has 4 credits and meets for 6 hours per week. The course aims to teach fundamental techniques for effective problem solving, analyzing algorithm performance, and designing efficient algorithms. It covers topics like sorting, searching, and graph algorithms.
This document discusses algorithms including:
- An algorithm is a set of instructions to accomplish a task, with inputs and outputs.
- Characteristics of algorithms include being definite, finite, unambiguous, and efficient.
- The design process involves understanding the problem, capabilities of the device, choosing exact or approximate solutions, selecting a data structure, and techniques like divide-and-conquer.
- Algorithms can be specified through languages, pseudocode, or flowcharts and must be proven correct. Their analysis considers time and space complexity.
This document provides an overview of a lecture on designing and analyzing computer algorithms. It discusses key concepts like what an algorithm and program are, common algorithm design techniques like divide-and-conquer and greedy methods, and how to analyze algorithms' time and space complexity. The goals of analyzing algorithms are to understand their behavior, improve efficiency, and determine whether problems can be solved within a reasonable time frame.
This document provides an introduction to algorithms including definitions, characteristics, and the design process. It defines an algorithm as a finite set of unambiguous instructions to solve a problem. Key points:
- Algorithms must have input, output, be definitive, finite, and effective.
- The design process includes understanding the problem, developing a solution algorithm, proving correctness, analyzing efficiency, and coding.
- Examples of algorithm types are approximate, probabilistic, infinite, and heuristic.
- Pseudocode is commonly used to specify algorithms more clearly than natural language alone.
This document discusses algorithms and their analysis. It defines an algorithm as a set of unambiguous instructions to solve a problem with inputs and outputs. Good algorithms have well-defined steps, inputs, outputs, and terminate in a finite number of steps. Common algorithm analysis methods include calculating time and space complexity using asymptotic notations like Big-O. Pseudocode and flowcharts are commonly used to represent algorithms. Asymptotic analysis determines an algorithm's best, average, and worst case running times.
Here are some content ideas for your Panaflex sign that are both funny and professional:
Funny Options
1. "Warning: Workshop Ahead - Brain Cells at Work"
2. "Get Ready to Geek Out: [Workshop Name]"
3. "Unleash Your Inner [Topic] Guru"
Professional Options
1. "Expert Insights: [Workshop Name]"
2. "Elevate Your Skills: [Workshop Name]"
3. "Learn from the Best: [Workshop Name]"
Combining Both
1. "Serious Learning, Minimal Seriousness: [Workshop Name]"
2. "The [Topic] Extravaganza: Learn, Laugh, Repeat"
3. "Where Passion Meets Expertise: [Workshop Name]"
Feel free to pick the one that suits your workshop's tone and style!
This document outlines the syllabus for the subject "Design and Analysis of Algorithms" for the 3rd year 1st semester students of the Computer Science and Engineering department with specialization in Cyber Security at CMR Engineering College.
The syllabus is divided into 5 units which cover topics like algorithm analysis, asymptotic notations, algorithm design techniques like divide and conquer, dynamic programming, greedy algorithms etc. It also discusses NP-hard and NP-complete problems. The document provides the textbook and references for the subject. It further includes introductions to different units explaining key concepts like algorithms, properties of algorithms, ways to represent algorithms, need for algorithm analysis etc.
Introduction to Design Algorithm And Analysis.pptBhargaviDalal4
This document contains the syllabus for the subject "Design and Analysis of Algorithms" for the 3rd year 1st semester students of CMR Engineering College. It includes 5 units - Introduction, Disjoint Sets and Backtracking, Dynamic Programming and Greedy Methods, Branch and Bound, and NP-Hard and NP-Complete problems. The introduction covers topics like algorithm complexity analysis and divide and conquer algorithms. The syllabus outlines core algorithms topics and applications like binary search, quicksort, dynamic programming, shortest paths, knapsack etc. that will be covered in the course.
The document introduces algorithms and their role in computing. It defines computational problems as relationships between inputs and outputs. Algorithms are tools used to solve well-specified computational problems and must be correct and efficient. Examples of algorithms are given for common problems like determining if a number is prime or sorting a list alphabetically. Key aspects of algorithms like variables, instructions, selection, repetition and documentation are outlined. Examples are provided of analyzing algorithm efficiency based on runtime. Pseudo-code conventions for writing algorithms are also presented.
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsSheba41
The document introduces algorithms and their role in computing. It defines computational problems as relationships between inputs and outputs. Algorithms are tools used to solve well-specified computational problems and must be correct and efficient. Examples of algorithms are provided, along with their components like variables, instructions, selections, and repetitions. The document also discusses algorithm efficiency and provides examples comparing the efficiencies of different sorting algorithms. Pseudo-code conventions for writing algorithms are also outlined.
The document discusses algorithms and flowcharts. It defines an algorithm as a finite set of steps to solve a problem and notes that algorithms can be expressed in various ways, including pseudocode and flowcharts. Pseudocode uses a language similar to programming but without specific syntax, making it readable by programmers familiar with different languages. A flowchart provides a graphical representation of an algorithm's logical flow. The document provides examples of algorithms expressed in pseudocode and represented through flowcharts, such as finding the average of two numbers and calculating the largest of several inputs. It also discusses common flowchart structures like sequence, selection, and iteration.
This document provides an overview of algorithms. It begins by discussing the origins and evolution of the term "algorithm" from its Arabic roots in the 9th century to its modern meaning of a well-defined computational procedure. The document then defines algorithms and their key characteristics such as being precise, unambiguous, and terminating after a finite number of steps. Common algorithm design techniques like divide-and-conquer, greedy algorithms, and dynamic programming are introduced. Examples of merge sort and finding the maximum element in a list are used to illustrate algorithm concepts.
The document discusses algorithms, including their definition, common types of algorithms, properties of algorithms, and how to write algorithms. It provides an example algorithm to add two numbers and explains how to analyze algorithms for efficiency in terms of time and space complexity. Time complexity represents the running time of an algorithm, while space complexity represents the memory required.
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
This document discusses the design and analysis of algorithms. It begins with defining what an algorithm is - a well-defined computational procedure that takes inputs and produces outputs. It describes analyzing algorithms to determine their efficiency and comparing different algorithms that solve the same problem. The document outlines steps for designing algorithms, including understanding the problem, deciding a solution approach, designing the algorithm, proving correctness, and analyzing and coding it. It discusses using mathematical techniques like asymptotic analysis and Big O notation to analyze algorithms independently of implementations or inputs. The importance of analysis is also covered.
This document discusses the design and analysis of algorithms. It begins with defining what an algorithm is - a well-defined computational procedure that takes inputs and produces outputs. It describes analyzing algorithms to determine their efficiency and comparing different algorithms that solve the same problem. The document outlines steps for designing algorithms, including understanding the problem, deciding a solution approach, designing the algorithm, proving correctness, and analyzing and coding it. It discusses using mathematical techniques like asymptotic analysis and Big O notation to analyze algorithms independently of implementations or data. The importance of analyzing algorithms and techniques like divide-and-conquer are also covered.
Download Link Below 👇
https://techblogs.cc/dl/
ChimeraTool is a powerful and professional mobile repairing tool for Windows. If you are already involved in mobile repair, then there is no need for you to be told “what ChimeraTool is” and “how it works?”
Ad
More Related Content
Similar to Design and analysis of algorithms Module-I.pptx (20)
This document discusses algorithms and their analysis. It defines an algorithm as a set of unambiguous instructions to solve a problem with inputs and outputs. Good algorithms have well-defined steps, inputs, outputs, and terminate in a finite number of steps. Common algorithm analysis methods include calculating time and space complexity using asymptotic notations like Big-O. Pseudocode and flowcharts are commonly used to represent algorithms. Asymptotic analysis determines an algorithm's best, average, and worst case running times.
Here are some content ideas for your Panaflex sign that are both funny and professional:
Funny Options
1. "Warning: Workshop Ahead - Brain Cells at Work"
2. "Get Ready to Geek Out: [Workshop Name]"
3. "Unleash Your Inner [Topic] Guru"
Professional Options
1. "Expert Insights: [Workshop Name]"
2. "Elevate Your Skills: [Workshop Name]"
3. "Learn from the Best: [Workshop Name]"
Combining Both
1. "Serious Learning, Minimal Seriousness: [Workshop Name]"
2. "The [Topic] Extravaganza: Learn, Laugh, Repeat"
3. "Where Passion Meets Expertise: [Workshop Name]"
Feel free to pick the one that suits your workshop's tone and style!
This document outlines the syllabus for the subject "Design and Analysis of Algorithms" for the 3rd year 1st semester students of the Computer Science and Engineering department with specialization in Cyber Security at CMR Engineering College.
The syllabus is divided into 5 units which cover topics like algorithm analysis, asymptotic notations, algorithm design techniques like divide and conquer, dynamic programming, greedy algorithms etc. It also discusses NP-hard and NP-complete problems. The document provides the textbook and references for the subject. It further includes introductions to different units explaining key concepts like algorithms, properties of algorithms, ways to represent algorithms, need for algorithm analysis etc.
Introduction to Design Algorithm And Analysis.pptBhargaviDalal4
This document contains the syllabus for the subject "Design and Analysis of Algorithms" for the 3rd year 1st semester students of CMR Engineering College. It includes 5 units - Introduction, Disjoint Sets and Backtracking, Dynamic Programming and Greedy Methods, Branch and Bound, and NP-Hard and NP-Complete problems. The introduction covers topics like algorithm complexity analysis and divide and conquer algorithms. The syllabus outlines core algorithms topics and applications like binary search, quicksort, dynamic programming, shortest paths, knapsack etc. that will be covered in the course.
The document introduces algorithms and their role in computing. It defines computational problems as relationships between inputs and outputs. Algorithms are tools used to solve well-specified computational problems and must be correct and efficient. Examples of algorithms are given for common problems like determining if a number is prime or sorting a list alphabetically. Key aspects of algorithms like variables, instructions, selection, repetition and documentation are outlined. Examples are provided of analyzing algorithm efficiency based on runtime. Pseudo-code conventions for writing algorithms are also presented.
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsSheba41
The document introduces algorithms and their role in computing. It defines computational problems as relationships between inputs and outputs. Algorithms are tools used to solve well-specified computational problems and must be correct and efficient. Examples of algorithms are provided, along with their components like variables, instructions, selections, and repetitions. The document also discusses algorithm efficiency and provides examples comparing the efficiencies of different sorting algorithms. Pseudo-code conventions for writing algorithms are also outlined.
The document discusses algorithms and flowcharts. It defines an algorithm as a finite set of steps to solve a problem and notes that algorithms can be expressed in various ways, including pseudocode and flowcharts. Pseudocode uses a language similar to programming but without specific syntax, making it readable by programmers familiar with different languages. A flowchart provides a graphical representation of an algorithm's logical flow. The document provides examples of algorithms expressed in pseudocode and represented through flowcharts, such as finding the average of two numbers and calculating the largest of several inputs. It also discusses common flowchart structures like sequence, selection, and iteration.
This document provides an overview of algorithms. It begins by discussing the origins and evolution of the term "algorithm" from its Arabic roots in the 9th century to its modern meaning of a well-defined computational procedure. The document then defines algorithms and their key characteristics such as being precise, unambiguous, and terminating after a finite number of steps. Common algorithm design techniques like divide-and-conquer, greedy algorithms, and dynamic programming are introduced. Examples of merge sort and finding the maximum element in a list are used to illustrate algorithm concepts.
The document discusses algorithms, including their definition, common types of algorithms, properties of algorithms, and how to write algorithms. It provides an example algorithm to add two numbers and explains how to analyze algorithms for efficiency in terms of time and space complexity. Time complexity represents the running time of an algorithm, while space complexity represents the memory required.
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
This document discusses the design and analysis of algorithms. It begins with defining what an algorithm is - a well-defined computational procedure that takes inputs and produces outputs. It describes analyzing algorithms to determine their efficiency and comparing different algorithms that solve the same problem. The document outlines steps for designing algorithms, including understanding the problem, deciding a solution approach, designing the algorithm, proving correctness, and analyzing and coding it. It discusses using mathematical techniques like asymptotic analysis and Big O notation to analyze algorithms independently of implementations or inputs. The importance of analysis is also covered.
This document discusses the design and analysis of algorithms. It begins with defining what an algorithm is - a well-defined computational procedure that takes inputs and produces outputs. It describes analyzing algorithms to determine their efficiency and comparing different algorithms that solve the same problem. The document outlines steps for designing algorithms, including understanding the problem, deciding a solution approach, designing the algorithm, proving correctness, and analyzing and coding it. It discusses using mathematical techniques like asymptotic analysis and Big O notation to analyze algorithms independently of implementations or data. The importance of analyzing algorithms and techniques like divide-and-conquer are also covered.
Download Link Below 👇
https://techblogs.cc/dl/
ChimeraTool is a powerful and professional mobile repairing tool for Windows. If you are already involved in mobile repair, then there is no need for you to be told “what ChimeraTool is” and “how it works?”
An updated content measurement model - Elle Geraghty Content Strategy.pdfElle Geraghty
To figure out if a content person is a junior, mid or senior, I always look at their ability to effectively measure their content work. Making content is one thing, but making content that performs is something else entirely.
Internet Download Manager Crack Patch Latest IDM Free DownloadDesigner
Download Link Below 👇
https://techblogs.cc/dl/
A premium Windows tool that maximizes download speeds and manages downloads efficiently. Internet Download Manager (IDM) is a tool to increase download speeds by up to 10 times, resume or schedule downloads and download streaming videos.
Museums today are moving away from just showing objects to creating fun, hands-on experiences where visitors can touch, play, and learn. They design exhibits based on who will visit, use stories to make people feel connected, and add technology like virtual reality to make things even more exciting. Museums also make sure everyone, including people with disabilities, can enjoy the exhibits. They use games, teamwork, and regular feedback to keep improving, so visiting a museum becomes a fun and memorable adventure for everyone. Visit our website for more info.:https://www.peachprime.in/services/museum-exhibit-design/
Venngage’s AI Infographic Generator helps you bring clarity to your content by turning plain text into visually compelling infographics—automatically. Designed with marketers and communicators in mind, it’s built to simplify complex messaging and speed up your workflow.
With just a short prompt, the AI understands your intent and creates a structured infographic that fits your content type—whether you’re outlining a process, comparing features, or mapping out a timeline. The tool selects fonts, icons, layouts, and visual hierarchy for you, taking care of the heavy lifting in seconds.
Once generated, you can fully customize the design to align with your brand—tweak colors, edit text, replace visuals, and adjust layout elements using a simple drag-and-drop editor. The tool supports different infographic styles, making it ideal for internal reports, client presentations, blog content, and social media graphics.
What makes it valuable for marketers?
- Eliminate bottlenecks in the content creation process
- Get campaign visuals and data summaries out faster
- Repackage written content into visuals that perform better on social and email
- Communicate insights clearly with clients and internal teams
Whether you're on a tight deadline or just want to simplify your storytelling, Venngage’s AI helps you create high-impact visuals without relying on a designer or starting from scratch.
Generate AI Infographics here: https://venngage.com/ai-tools/infographic-generator
This portfolio showcases a curated selection of academic, professional, and conceptual architectural projects, highlighting a comprehensive design approach that merges aesthetics, functionality, and contextual sensitivity. Each project demonstrates proficiency in design development, technical detailing, and presentation, along with strong command over industry-standard software tools.From large-scale urban planning proposals to intimate interior spaces, the portfolio reflects a commitment to sustainability, innovation, and user-centered design. The work embodies a balance between conceptual clarity and technical precision, aiming to address real-world challenges through thoughtful architectural solutions.Included are detailed drawings, 3D visualizations, physical models, and process sketches that collectively represent a holistic design process.Throughout the portfolio, you'll find,Conceptual Development: Idea generation, site studies, and early-stage diagrams that lay the foundation for each project.
Architectural Drawings: Detailed plans, sections, elevations, and construction details showcasing clarity and precision.
3D Visualizations and Renderings: Realistic and conceptual renderings that communicate spatial atmosphere and design intent.
Physical Models and Process Work: Documentation of physical prototypes and iterative design processes that illustrate hands-on exploration.
Technical and Software Proficiency: Demonstration of skills in software such as AutoCAD, Rhino, Revit, SketchUp, Adobe Creative Suite, Lumion, and others.
3. AGENDA
What is an Algorithm?
Formal and Informal Definition
Properties of an Algorithm
Fundamentals Of Algorithmic Problem Solving
Example
Assignment
4. • An algorithm is a step by step procedure for solving the given
problem/task.
• An algorithm is independent of any programming language and
machine.
What is an algorithm?
5. Algorithms are used in our day to day activities
Example:
What is an algorithm?
6. Informal definition
An Algorithm is any well-defined computational procedure that takes some
value or set of values as input and produces a set of values or some value as output.
Thus algorithm is a sequence of computational steps that transforms the input into
the output.
7. Formal definition
An algorithm is a sequence of unambiguous instructions for solving a problem,
i.e., for obtaining a required output for any legitimate input in a finite amount of time.
This definition can be illustrated by a simple diagram:
10. INPUT :
An algorithm must have zero or more but must be finite number of inputs.
Example of zero input algorithm. Print the ASCII code of each of the
letter in the alphabet of the computer system.
OUTPUT :
An algorithm must have at-least one desirable outcome, i.e., output.
Properties of an algorithm
11. DEFINITENESS:
• Each instruction should be clear and unambiguous.
• It must be perfectly clear what should be done.
• Example Directions which are ambiguous
– “add 6 or 7 to x”
– compute x/0
• It is not clear which of the 2 possibilities should be done
Properties of an algorithm
12. FINITENESS:
• The algorithm should terminate after a finite number of steps in all the
cases, if we trace the algorithm.
• The time for termination should be reasonably short
Properties of an algorithm
13. EFFECTIVENESS:
• Every instruction must very basic so that it can be carried out, in principle, by a
person using only pencil & paper in a finite amount of time.
• Example -Effective
– Performing arithmetic on integers
• Example of not effectiveness.
Properties of an algorithm
14. Fundamentals Of AlgorithmicProblemSolving
Algorithms are procedural solutions to problems. These solutions are not answers
but specific instructions for getting answers.
The following diagram briefly illustrates the sequence of steps one typically goes
through in designing and analyzing an algorithm.
16. Example
Write an algorithm to add two integer numbers entered by user.
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read integer values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
20. Recap…
What is an Algorithm?
Formal and Informal Definition
Properties of an Algorithm
Fundamentals Of Algorithmic Problem Solving
Example
Assignment
23. Understand the problem
Before designing an algorithm the most important thing is to understand the problem
given.
Asking questions, doing a few examples by hand, thinking about special cases, etc.
An Input to an algorithm specifies an instance of the problem the algorithm that it solves.
Important to specify exactly the range of instances the algorithm needs to handle.
Else it will work correctly for majority of inputs but crash on some ―boundary‖ value.
A correct algorithm is not one that works most of the time, but one that works correctly
for all legitimate inputs.
24. Decide on:
Ascertaining the capabilities of a computational device
The vast majority of algorithms in use today are still destined to be programmed for a computer closely
resembling the von Neumann machine—a computer architecture.
Von Neumann architectures are sequential and the algorithms implemented on them are called
sequential algorithms.
Algorithms which designed to be executed on parallel computers called parallel
algorithms.
For very complex algorithms concentrate on a machine with high speed and more memory where
time is critical.
25. Decide on:
Deciding on appropriate data structures
Algorithms may or may not demand ingenuity in representing their inputs.
Inputs are represented using various data structures.
Algorithm + data structures=program.
Algorithm Design Techniques and Methods of Specifying an Algorithm
An algorithm design technique (or ―strategy‖ or ―paradigm‖) is a general approach to solving
problems algorithmically that is applicable to a variety of problems from different areas of computing.
The different algorithm design techniques are: brute force approach, divide and conquer,
greedy method, decrease and conquer, dynamic programming, transform and conquer and back
tracking.
26. Decide on:
Methods of specifying an algorithm:
Using natural language, in this method ambiguity problem will be there.
The next 2 options are : pseudo code and flowchart.
Pseudo code: mix of natural and programming language. More precise than NL
Flow chart: method of expressing an algorithmby collectionof
connected geometric shapes containing descriptions of the algorithm‘s steps.
This representation technique has proved to be inconvenient for large problems.
Algorithm needs to be converted into a computer program written in a particular computer language.
Hence program as yet another way of specifying the algorithm, although it is preferable to consider it as
the algorithm‘s implementation.
27. Decide on:
Choosing between exact and approximate problem solving
For exact result->exact algorithm
For approximate result->approximation algorithm.
Examples of exact algorithms: Obtaining square roots for numbers and solving non-
linear equations.
An approximation algorithm can be a part of a more sophisticated algorithm that
solves a problem exactly.
32. Types of algorithm specification
Algorithm can be described in three ways.
Natural language like English: When this way is choosed
care should be taken, we should ensure that each & every
statement is definite and unambigious.
33. Types of algorithm specification
Graphic representation called flowchart: This method will
work well when the algorithm is small& simple.
34. Types of algorithm specification
Pseudo-code Method: In this method, we typically
describe algorithms as program, which resembles programming
language constructs
35. Pseudo-Code Conventions
Comments begin with // and continue until the end of line.
Blocks are indicated with matching braces { and }.
A compound statement (i.e.,a collection of simple statements)can be
represented as a block.
The body of a procedure also forms a block.
Statements are delimited by ;.
36. Pseudo-Code Conventions
An identifier begins with a letter. The data types of variables are not explicitly declared.
Compound data types can be formed with records.
Here is an example,
node = record
{
data type – 1 data-1;
. . .
data type – n data – n;
node * link;
}
Here link is a pointer to the record type node.
Individual data items of a record can be accessed with → and period (.)
37. Pseudo-Code Conventions
Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
There are two Boolean values TRUE and FALSE.
Logical Operators AND, OR, NOT
Relational Operators <, <=,>,>=, =, !=
Elements of multidimensional arrays are accessed using [ and ]. For
example, if A is a two dimensional array, the (i,j)th element of the array is
denoted as A[i , j]. Array indices start at zero.
38. Pseudo-Code Conventions
The following looping statements are employed:for, while and repeat-until
While Loop:
while < condition > do
{
<statement-1>
. . .
<statement-n>
}
39. Pseudo-Code Conventions
For Loop:
for variable: = value-1 to value-2 step step do
{
<statement-1>
. . .
<statement-n>
}
The clause “step step” is optional and taken as +1 if it does not occur,
step could either be positive or negative.
41. Pseudo-Code Conventions
A conditional statement has the following forms.
if <condition> then
<statement>
if <condition> then
<statement-1>
else
<statement-1>
42. Pseudo-Code Conventions
Input and output are done using the instructions read & write.
Algorithm, the heading takes the form,
Algorithm Name (Parameter lists)
where Name is the name of the procedure and
(<parameter list>) is a listing of the procedure parameters.
The body has one or more (simple or compound) statements enclosed within braces { and }.
An algorithm may or may not return any values.
Simple variables to procedures are passed by value.
Arrays and records are passed by reference.
An array name or a record name is treated as a pointer to the respective datatype.
43. Example
As an example, the following algorithm finds and returns the maximum
of n given numbers.
Algorithm Max(A , n)
// Purpose: To find Max in an array A[1:n]
//Input: A is an array of size n
//Output: Returns the max value in A[1:n]
{
Result := A[1];
for i:= 2 to n do
if A[i] > Result then
Result :=A[i];
return Result;
48. AGENDA
Need for Algorithm Analysis
Analysis Framework
Kinds of Efficiency
Measuring An Input Size
Units For Measuring Run Time
Orders Of Growth
Worst-Case, Best-Case, Average Case Efficiencies
Example
49. Needfor algorithm analysis
An algorithm is a clearly specified set of simple instructions to be followed to
solve a problem.
Three questions for algorithm analysis
What: What to analyze?
How: How to analyze?
Why: Why we need to do algorithm analysis?
Judge an algorithm is “good” or “bad”.
An algorithm with one year running time is hardly useful.
An algorithm that requires tens gigabytes of main memory is not
(currently) useful on most machines.
50. Types of efficiency
Time efficiency - Indicates how fast an algorithm in question runs, i.e.,
running time
Space efficiency - Deals with the extra space the algorithm requires.
51. How to analyze???
Measuring An Input Size
Units For Measuring Run Time
Orders Of Growth
Worst-Case, Best-Case, Average Case Efficiencies
52. Measuring An Input Size
An algorithm's efficiency is investigated as a function of some parameter ‘n’ indicating the
algorithm's input size.
Time Efficiency T(n) where ‘n’ is the input size
In most cases, selecting such a parameter is quite straightforward.
For example, What will be the size for problems of sorting list , searching, finding the list's
smallest element, and most other problems dealing with lists ??
# of elements in the list
For the problem of evaluating a polynomial p(x) of degree n???
it will be the polynomial's degree or the number of its coefficients, which is larger by one than its degree.
53. Measuring An Input Size
Computing the product of two n-by-n matrices.There are two natural measures of size for this
problem.
The matrix order n.
The total number of elements N in the matrices being multiplied.
The choice of an appropriate size metric can be influenced by operations of the algorithm in
question.
54. Measuring An Input Size
For example, how should we measure an input's size for a spell-checking algorithm?
If the algorithm examines individual characters of its input, then we should measure the size by
the number of characters;
If it works by processing words, we should count their number as the input size.
We should make a special note about measuring size of inputs for algorithms involving
properties of numbers (e.g., checking whether a given integer n is prime).
55. Units For Measuring Run Time
We can simply use some standard unit of time measurement-a second, a millisecond,
and so on-to measure the running time of a program implementing the algorithm.
There are obvious drawbacks to such an approach. They are
Dependence on the speed of a particular computer
Dependence on the quality of a program implementing the algorithm
The compiler used in generating the machine code
The difficulty of clocking the actual running time of the program.
56. Units For Measuring Run Time
We need to measure algorithm efficiency, we should have a metric that does not depend
on these extraneous factors.
One possible approach is to count the number of times each of the algorithm's operations
is executed. This approach is both difficult and unnecessary.
The main objective is to identify the most important operation of the algorithm, called the
basic operation, the operation contributing the most to the total running time, and
compute the number of times the basic operation is executed.
As a rule, it is not difficult to identify the basic operation of an algorithm.
57. Units For Measuring Run Time
Problem Input Size Basic Operation
Search for a key in a list of ‘n’
items
# of items in the list Key Comparison
Addition of 2 n x n matrices Dimensions of the matrices, n Addition
Polynomial Evaluation Order of the polynomial Multiplication
61. AGENDA
Analysis Framework
Kinds of Efficiency
Measuring An Input Size
Units For Measuring Run Time
Orders Of Growth
Basic Efficiency Classes
62. Types of efficiency
Time efficiency - Indicates how fast an algorithm in question runs, i.e.,
running time
Space efficiency - Deals with the extra space the algorithm requires.
63. How to analyze???
Measuring An Input Size
Units For Measuring Run Time
Orders Of Growth
Worst-Case, Best-Case, Average Case Efficiencies
64. Measuring An Input Size
An algorithm's efficiency is investigated as a function of some parameter ‘n’ indicating the
algorithm's input size.
Time Efficiency T(n) where ‘n’ is the input size
In most cases, selecting such a parameter is quite straightforward.
For example, What will be the size for problems of sorting list , searching, finding the list's
smallest element, and most other problems dealing with lists ??
# of elements in the list
65. Measuring An Input Size
Computing the product of two n-by-n matrices.There are two natural measures of size for this
problem.
The matrix order n.
The total number of elements N in the matrices being multiplied.
The choice of an appropriate size metric can be influenced by operations of the algorithm in
question.
66. Units For Measuring Run Time
We can simply use some standard unit of time measurement-a second, a millisecond,
and so on-to measure the running time of a program implementing the algorithm.
There are obvious drawbacks to such an approach. They are
Dependence on the speed of a particular computer
Dependence on the quality of a program implementing the algorithm
The compiler used in generating the machine code
The difficulty of clocking the actual running time of the program.
67. Units For Measuring Run Time
We need to measure algorithm efficiency, we should have a metric that does not depend
on these extraneous factors.
One possible approach is to count the number of times each of the algorithm's operations
is executed. This approach is both difficult and unnecessary.
The main objective is to identify the most important operation of the algorithm, called the
basic operation, the operation contributing the most to the total running time, and
compute the number of times the basic operation is executed.
As a rule, it is not difficult to identify the basic operation of an algorithm.
68. Units For Measuring Run Time
Example: Algorithm to find sum of n numbers
Step 1: Identify input size:
n – number of elements in the array
Step 2: Identify the Basic Operation: Addition
operation
sum <- sum+A[i]
Step 3: How many times the basic operation is
executed
For example we have 4 elements- it will be executed
for i=0,1,2,3 4 times,
Hence in general for n elements it will be executed n
times
n
69. Units For Measuring Run Time
Let cop be the execution time of an algorithm‘s basic operation on a
particular computer, and let C(n) be the number of times this operation
needs to be executed for this algorithm.
Then we can estimate the running time T (n) of a program
implementing this algorithm on that computer by the formula
T (n) ≈ copC(n).
Total number of steps for basic operation execution, C (n) = n
70. Orders of growth
Measuring the performance of an algorithm in relation with the input size is called order
of growth
Example:
76. Time Complexity- running time
We can estimate the running time T (n) of a program implementing the
algorithm on the computer by the formula
T (n) ≈ copC(n).
Where cop be the execution time of an algorithm‘s basic operation on a
particular computer, and C(n) be the number of times this operation needs
to be executed for this algorithm.
T (n) ≈ C(n).
77. Worst-Case, Best-Case, Average Case Efficiencies
Algorithm efficiency depends on the input size n.
And for some algorithms efficiency not only on input size but also on the specifics of
particular or type of input.
Here in this case, We have 3 types of efficiencies:
Best Case Efficiency
Worst Case Efficiency
Average Case Efficiency
78. Worst-Case, Best-Case, Average Case Efficiencies
Worst-case efficiency: Efficiency (number of times the basic operation will be
executed) for the worst case input of size n. i.e. The algorithm runs the longest
among all possible inputs of size n.
Best-case efficiency: Efficiency (number of times the basic operation will be executed)
for the best case input of size n. i.e. The algorithm runs the fastest among all possible
inputs of size n.
Average-case efficiency: Average time taken (number of times the basic operation will
be executed) to solve all the possible instances (random) of the input.
79. Example-sequential search
This is a straightforward algorithm that searches for a given item (some search key K)
in a list of n elements by checking successive elements of the list until either a match with
the search key is found or the list is exhausted.
Input size:
n no of elements in the list
Basic Operation:
Comparison Operation
Does the Basic Operation depends only on input size or also on type of input????
81. Example-sequential search
Worst case efficiency
The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n, which is an
input (or inputs) of size n for which the algorithm runs the longest among all possible inputs of that size.
In the worst case the input might be in such a way that there are no matching elements or the first
matching element happens to be the last one on the list, in this case the algorithm makes the largest number of
key comparisons among all possible inputs of size n:
Cworst (n) = n. Hence Tworst (n) = Cworst (n) = n.
The worst-case analysis provides very important information about an algorithm's efficiency by
bounding its running time from above.
In other words, it guarantees that for any instance of size n, the running time will not exceed Cworst (n)
82. Example-sequential search
best case efficiency
The best-case efficiency of an algorithm is its efficiency for the best-case input of
size n, which is an input (or inputs) of size n for which the algorithm runs the fastest among
all possible inputs of that size.
First, determine the kind of inputs for which the count C (n) will be the smallest among all
possible inputs of size n.
In sequential search, best-case inputs will be lists of size n with their first element equal
to a search key; accordingly, Cbest(n) = 1. Hence Tbest (n) = Cbest (n) = 1.
83. Example-sequential search
average case efficiency
Average time taken (number of times the basic operation will be executed) to solve all the
possible instances (random) of the input.
To analyze the algorithm's average-case efficiency, we must make some assumptions
about possible inputs of size n.
It involves dividing all instances of size n into several classes so that for each instance
of the class the number of times the algorithm's basic operation is executed is the same.
Then a probability distribution of inputs needs to be obtained or assumed so that the
expected value of the basic operation's count can then be derived.
84. Example-sequential search
average case efficiency
For sequential search: The average number of key comparisons Cavg(n) can be computed
as follows,
The standard assumptions are,
Let p be the probability of successful search.
In the case of a successful search, the probability of the first match occurring in the ith
position of the list is p/n for every i, where 1<=i<=n
The number of comparisons made by the algorithm in such a situation is obviously i.
In the case of an unsuccessful search, the number of comparisons is n with the
probability of such a search being (1 - p).
85. Example-sequential search
average case efficiency
Therefore Cavg(n) = Probability of succesful search + Probabiltiy of unsuccesful search
Cavg(n) = [1.p/n+2.p/n+3.p/n+…+i.p/n………n.p/n]+n.(1-p)
=p/n[1+2+3…+i+…..+n]+n(1-p) Example,
if p = 1 (i.e., the search must be successful), the
average number of key comparisons made by sequential
search is (n + 1) /2.
If p = 0 (i.e., the search must be unsuccessful), the
average number of key comparisons will be n because
the algorithm will inspect all n elements on all such
inputs.
87. ASYMPTOTICNOTATIONS
The efficiency analysis framework concentrates on the order of growth of an
algorithm‘s basic operation count as the principal indicator of the algorithm‘s
efficiency.
To compare and rank such orders of growth, computer scientists use three
notations: O (big oh), Ω(big omega), and Θ (big theta).
96. ASYMPTOTIC NOTATIONS
Informal Definition:
Informally, O(g(n)) is the set of all functions with a lower or same order of growth as g(n) (to within a
constant multiple, as n goes to infinity).
Examples:
The second notation, Ω (g(n)), stands for the set of all functions with a higher or same order of growth as g(n)
(to within a constant multiple, as n goes to infinity).
Examples:
Finally, Θ (g(n)) is the set of all functions that have the same order of growth as g(n).
104. Theorem: If t1 (n) ∈ O(g1 (n)) and t2 (n) ∈ O(g2 (n)), then t1 (n) + t2 (n) ∈ O(max{g1 (n), g2 (n)})
Proof:
For any four arbitrary real numbers a1 , b1 , a2 , b2 :
if a1 ≤ b1 and a2 ≤ b2 , then a1 + a2 ≤ 2 max{b1 , b2 }.
Since t1 (n) ∈ O(g1 (n)), there exist some positive constant c1 and some non negative integer
n1 such that
t1 (n) ≤ c1g1 (n) for all n ≥ n1 Eqn 1
Similarly, since t2 (n) ∈ O(g2 (n)),
t2 (n) ≤ c2g2(n) for all n ≥ n2 Eqn 2
Useful property involving the asymptotic notations
105. Let us denote c3= max{c1, c2} and consider n ≥ max{n1, n2} so that we can use both
inequalities.
Adding them i.e.., Eqn 1 and Eqn 2 yields the following:
t1 (n) + t2 (n) ≤ c1g1(n) + c2g2(n)
≤ c3 g1(n) + c3g2(n) = c3 [g1 (n) + g2 (n)]
≤ c32 max{g1(n), g2(n)}.
Hence, t1 (n) + t2 (n) ∈ O(max{g1 (n), g2 (n)}), with the constants c and n0 required by the
O(Big Oh) definition being 2c3 = 2 max{c1 , c2} and max{n1, n 2}, respectively.
Useful property involving the asymptotic notations
106. So what does this property imply for an algorithm that comprises two consecutively
executed parts?
It implies that the algorithm’s overall efficiency is determined by the part with a higher
order of growth, i.e., its least efficient part
Useful property involving the asymptotic notations
107. For example, we can check whether an array has equal elements by the following two-part
algorithm:
First, sort the array by applying some known sorting algorithm;
Second, scan the sorted array to check its consecutive elements for equality.
If, for example, a sorting algorithm used in the first part makes no more
than 1/2 n(n − 1) comparisons and hence its order of growth is O(n2)
While the second part makes no more than n − 1 comparisons (and hence is in O(n)), the
efficiency of the entire algorithm will be in O(max{n2, n}) = O(n2).
Useful property involving the asymptotic notations
115. AGENDA
Problems on Asymptotic Notations
Mathematical Analysis of Non Recursive Algorithms
General Plan
Example 1: Finding the largest element in a given array
Assignment
116. Problem 1: Consider f(n)=100n+5, Express f(n) using Big-Oh
Solution: It is given that f(n)=100n+5. Replacing 5 with n(so that next higher order term is obtained), we get
100n+n=101n and call it cg(n)
i.e., c(g(n))=101n
Now, the following constraint is satisfied:
f(n) ≤ cg(n) for all n ≥ n0
i.e., 100n+5 ≤ 101n for n ≥ 5
It is clear from above, that c=101, g(n)=n and n0 =5
So, by definition f(n) ∈ O(g(n)) i.e., f(n) ∈ O(n),
Problems on Asymptotic Notations
117. Problem 2: Prove that: 100n + 5 ∈ O(n2)
Solution:
Easiest way to prove that f(n) ∈ O(g(n)), we need to consider according to definition
f(n) ≤ cg(n), Here f(n)=100n+5, g(n)=n2 and to find c, is to take c to be the sum of the
positive coefficients of f(n)
Hence, 100n+5 ≤ 105n2 for all n ≥ 1
As per the Big-Oh definition, we have c=105 and n0 = 1
Hence proved, 100n + 5 ∈ O(n2)
Problems on Asymptotic Notations
118. Problem 3: Consider f(n)=100n+5, Express f(n) using Big-Omega
Solution: It is given that f(n)=100n+5. we consider c(g(n))=100n
Now, the following constraint is satisfied:
f(n) ≥ cg (n) for all n ≥ n0
i.e., 100n+5 ≥ 100n for n ≥ 0
It is clear from above, that c=100, g(n)=n and n0 =0 So, by definition f(n) ∈ (g(n)) i.e.,
f(n) ∈ (n)
Problems on Asymptotic Notations
119. General plan for analyzing efficiency of non-recursive algorithms:
Decide on parameter n indicating the input size of the algorithm.
Identify algorithm‘s basic operation.
Check whether the number of times the basic operation is executed depends only on the
input size n. If it also depends on the type of input, then investigate worst, average, and best
case efficiency separately.
Set up summation for C(n) reflecting the number of times the algorithm‘s basic operation is
executed.
Simplify summation using standard formulas and express C(n) using orders of growth.
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS
121. Analysis:
Step 1: Identify Input size ‘n’:
The number of elements = n (size of the array)
Step 2: Identify the basic operation:
Two operations can be considered to be as basic operation i.e.,
Comparison ::A[i]>maxval.
Assignment :: maxval←A[i].
Here the comparison statement is considered to be the basic operation of the algorithm.
Example 1: Finding the largest element in a given array
122. Analysis:
Step 3: Check whether the number of times the basic operation is executed depends only on
the input size n. If it also depends on the type of input then investigate worst, average, and
best case efficiency separately.
No best, worst, average cases- because the number of comparisons will be same for all
arrays of size n and it is not dependent on type of input.
Example 1: Finding the largest element in a given array
123. Analysis:
Step 4: Set up summation for C(n) reflecting the number of times the algorithm‘s basic
operation is executed.
Let C(n) denotes number of comparisons made.
Algorithm makes one comparison on each execution of the loop, which is repeated for
each value of the loop‘s variable i within the bound between 1 and n – 1.
Example 1: Finding the largest element in a given array
124. Analysis:
Step 5: Simplify summation using standard formulas and express C(n) using orders of
growth.
we have,
Example 1: Finding the largest element in a given array
Standard Formula to be used:
C(n)=
C(n) (n)
127. Recap…
Problems on Asymptotic Notations
Mathematical Analysis of Non Recursive Algorithms
General Plan
Example 1: Finding the largest element in a given array
Assignment
129. General plan for analyzing efficiency of non-recursive algorithms:
Decide on parameter n indicating the input size of the algorithm.
Identify algorithm‘s basic operation.
Check whether the number of times the basic operation is executed depends only on the
input size n. If it also depends on the type of input, then investigate worst, average, and best
case efficiency separately.
Set up summation for C(n) reflecting the number of times the algorithm‘s basic operation is
executed.
Simplify summation using standard formulas and express C(n) using orders of growth.
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS
130. Aim: Checks whether the elements in the array are distinct or not. It returns true if all the
elements in the array are distinct or otherwise false
Algorithm:
Example 2: Element uniqueness problem
131. Example 2: Element uniqueness problem
Analysis:
Step 1: Identify Input size ‘n’:
The number of elements = n (size of the array)
Step 2: Identify the basic operation:
Basic operation is identified as,
Comparison ::A[i]==A[j]
Here the comparison statement is considered to be the basic operation of the algorithm.
132. Example 2: Element uniqueness problem
Analysis:
Step 3: Check whether the number of times the basic operation is executed depends only on
the input size n. If it also depends on the type of input then investigate worst, average, and
best case efficiency separately.
Best, worst, average cases exist - because the number of comparisons will vary for the input
of size n based on type of input.
133. Example 2: Element uniqueness problem
Analysis:
Step 4: Set up summation for C(n) reflecting the number of times the algorithm‘s basic
operation is executed.
Here, we need to consider both best and worst case efficiency
Best Case Efficiency:
Best Case occurs when the Array with first two elements are the pair of equal elements
Let Cbest(n) denotes number of comparisons made in best case.
Algorithm makes exactly one comparison for the best case input
Therefore Cbest(n)=1 i.e., Cbest(n) (1)
134. Example 2: Element uniqueness problem
Analysis:
Worst Case Efficiency:
Worst case input is an array giving largest comparisons.
Array with no equal elements
Array with last two elements are the only pair of equal elements
Let Cworst (n) denotes number of comparisons in worst case
Algorithm makes one comparison for each repetition of the innermost loop i.e., for each
value of the loop‘s variable j between its limits i + 1 and n – 1; and this is repeated for
each value of the outer loop i.e, for each value of the loop‘s variable i between its limits 0
and n – 2.
135. Example 2: Element uniqueness problem
Analysis:
Worst Case Efficiency:
We need to set up the summation for Cworst (n)
136. Example 2: Element uniqueness problem
Analysis:
Step 5: Simplify summation using standard formulas and express Cworst (n) using orders of
growth.
we have,
Let us Consider
=n-1-i+1-1
=n-1-i [Obtained using the standard formula
137. Example 2: Element uniqueness problem
Analysis:
we have,
Cworst (n)= (n-1-0)+(n-1-1)+(n-1-2)+…………………+(n-1-(n-3))+(n-1-(n-2))
=(n-1)+(n-2)+(n-3)+…………………………+2+1
=1+2+3+…….+(n-3)+(n-2)+(n-1) [Using the formula 1+2+3+….+n=n(n+1)/2]
= =
Therefore Cworst (n) (n2)
143. AGENDA
Mathematical Analysis of Non Recursive Algorithms
General Plan
Example 3: Matrix Multiplication
Example 4: Find number of digits in binary representation
of a number
Assignment
144. General plan for analyzing efficiency of non-recursive algorithms:
Decide on parameter n indicating the input size of the algorithm.
Identify algorithm‘s basic operation.
Check whether the number of times the basic operation is executed depends only on the
input size n. If it also depends on the type of input, then investigate worst, average, and best
case efficiency separately.
Set up summation for C(n) reflecting the number of times the algorithm‘s basic operation is
executed.
Simplify summation using standard formulas and express C(n) using orders of growth.
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS
145. Aim: Given two n × n matrices A and B, find the time efficiency of the definition-based
algorithm for computing their product C = AB. By definition, C is an n × n matrix whose
elements are computed as the scalar (dot) products of the rows of matrix A and the columns
of matrix B:
Example 3: matrix multiplication
147. Example 3: matrix multiplication
Analysis:
Step 1: Identify Input size ‘n’:
The order of the matrix= n
Step 2: Identify the basic operation:
There are two arithmetical operations in the innermost loop —multiplication and addition—
that, in principle, can compete for designation as the algorithm‘s basic operation.
Actually, we do not have to choose between them, because on each repetition of the
innermost loop each of the two is executed exactly once.
So by counting one we automatically count the other.
Hence, first we consider multiplication as the basic operation.
148. Example 3: matrix multiplication
Analysis:
Step 3: Check whether the number of times the basic operation is executed depends only on
the input size n. If it also depends on the type of input then investigate worst, average, and
best case efficiency separately.
We can observe that the total number of multiplications M(n) executed by the algorithm.
depends only on the size of the input matrices, we do not have to investigate the worst-case,
average-case, and best-case efficiencies separately.
149. Example 3: matrix multiplication
Analysis:
Step 4: Set up summation for C(n) reflecting the number of times the algorithm‘s basic
operation is executed.
We can observe that there is just one multiplication executed on each repetition of the
algorithm‘s innermost loop, which is governed by the variable k ranging from the lower
bound 0 to the upper bound n − 1. Therefore, the number of multiplications made for every
pair of specific values of variables i and j is
Hence,
150. Example 3: matrix multiplication
Analysis:
Step 5: Simplify summation using standard formulas and express M (n) using orders of
growth.
we have,
Consider
Now,
Standard Formula to be used:
151. Example 3: matrix multiplication
Analysis:
Total running time:
Suppose if we take into account of addition; A(n) = n3
Total running time:
Hence T(n) (n3)
152. Example 4: finds the number of binary digits in the binary
representation of a positive decimal integer
153. Example 4: finds the number of binary digits in the binary
representation of a positive decimal integer
Example, Consider n=5, count=1
5>1 True count=2, n= 5/2=2.5=2
2>1 True count=3, n= 2/2=1=1
1>1 False
Hence count=3 (5=101 in base 2)
154. Example 4: finds the number of binary digits in the binary
representation of a positive decimal integer
Analysis:
Step 1: Identify Input size ‘n’:
The positive decimal integer = n
Step 2: Identify the basic operation:
First, notice that the most frequently executed operation here is not inside the while loop
but rather the comparison n > 1 that determines whether the loop‘s body will be executed.
Since the number of times the comparison will be executed is larger than the number of
repetitions of the loop‘s body by exactly 1.
155. Example 4: finds the number of binary digits in the binary
representation of a positive decimal integer
Analysis:
Step 3: Check whether the number of times the basic operation is executed depends only on
the input size n. If it also depends on the type of input then investigate worst, average, and
best case efficiency separately.
We can observe that the comparison statement depends only on input size not on type of input ,
we do not have to investigate the worst-case, average-case, and best-case efficiencies
separately.
156. Example 4: finds the number of binary digits in the binary
representation of a positive decimal integer
Analysis:
Step 4: Set up summation for C(n) reflecting the number of times the algorithm‘s basic
operation is executed.
A more significant feature of this example is the fact that the loop variable takes on only a
few values between its lower and upper limits;
Therefore, we have to use an alternative way of computing the number of times the body
of the loop is executed.
Since the value of n is about halved on each repetition of the loop, the answer should be
about log n
157. Example 4: finds the number of binary digits in the binary
representation of a positive decimal integer
Analysis:
Step 5: Simplify summation using standard formulas and express C (n) using orders of
growth.
We have, C(n) = number of times the comparison n>1 will be executed
We know that the number of times the body of the loop will be executed is log2 n
Therefore the number of times the comparison n>1 will be executed will be one larger
than the body of the loop
Therefore C(n)= log2 n+1, Hence C(n) (log2 n)
160. Recap….
Mathematical Analysis of Non Recursive Algorithms
General Plan
Example 3: Matrix Multiplication
Example 4: Find number of digits in binary representation
of a number
Assignment
162. General plan for analyzing efficiency of recursive algorithms:
Decide on parameter n indicating the input size of the algorithm.
Identify algorithm‘s basic operation.
Check whether the number of times the basic operation is executed depends only on the
input size n. If it also depends on the type of input, then investigate worst, average, and best
case efficiency separately.
Set up recurrence relation, with an appropriate initial condition, for C(n) reflecting the
number of times the algorithm‘s basic operation is executed.
Solve the recurrence relation using backward substitution method and express C(n)
using orders of growth.
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
163. Recurrence relations
A recurrence relation, T(n), is a recursive function of an integer variable n.
Like all recursive functions, it has one or more recursive cases and one or more base cases.
Example:
The portion of the definition that does not contain T is called the base case of the recurrence
relation; the portion that contains T is called the recurrent or recursive case.
Recurrence relations are useful for expressing the running times (i.e., the number of basic
operations executed) of recursive algorithms
164. Recurrence relations - solving using backward substitution method
Recurrence relation is solved using backward substitution method.
Inorder to get, a solution in terms of n
Example: T(n)=T(n-1)+1 for n>0, T(0)=0 for n=0
165. Example 1: factorial of a number
Definition:
n ! = 1 * 2 * … *(n-1) * n for n ≥ 1 and 0! = 1
Recursive definition of n!:
F(n) = F(n-1) * n for n ≥ 1 and F(0) = 1
166. Example 1: factorial of a number
Algorithm:
Algorithm Factorial(n)
//Purpose – Computes n! factorial recursively
//Input – A non negative integer n
//Output – The value of n!
{
if (n==0) // base case
return 1;
else
return Factorial(n-1)*n;
}
168. Example 1: factorial of a number
Analysis:
Input size: A non negative number = n
Basic operation: Multiplication
No best, worst, average cases.
Let M(n) denotes number of multiplications
169. Example 1: factorial of a number
Analysis:
Solve the recurrence relation using backward substitution method:
M(n)=M(n-1)+1 for n>0 and M(0) =0- initial condition
M(n) = M(n-1) + 1 substitute M(n-1)= M(n-2) + 1
= (M(n-2) + 1) + 1
= M(n-2) + 2 substitute M(n-2)= M(n-3) + 1
= (M(n-3) + 1) + 2
= M(n-3) + 3
170. Example 1: factorial of a number
Analysis:
Solve the recurrence relation using backward substitution method:
M(n) = M(n-3) + 3
…
The general formula for the above pattern for some ‘i’ is as follows:
M(n)= M(n-i) + i
By taking the advantage of the initial condition given i.e., M(0)=0, we equate n-i=0, then i=n
171. Example 1: factorial of a number
Analysis:
Solve the recurrence relation using backward substitution method:
We now substitute i=n in the patterns formula to get the ultimate result of the backward
substitutions.
M(n)=M(n-n)+n
M(n)= M(0) + n
M(n) = 0+n
M(n)=n
The number of multiplications to compute the factorial of ‘n’ is n where the time
complexity is linear.
176. General plan for analyzing efficiency of recursive algorithms:
Decide on parameter n indicating the input size of the algorithm.
Identify algorithm‘s basic operation.
Check whether the number of times the basic operation is executed depends only on the
input size n. If it also depends on the type of input, then investigate worst, average, and best
case efficiency separately.
Set up recurrence relation, with an appropriate initial condition, for C(n) reflecting the
number of times the algorithm‘s basic operation is executed.
Solve the recurrence relation using backward substitution method and express C(n)
using orders of growth.
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
177. Example 2: Tower of hanoi probelm
In this puzzle, we have n disks of different sizes
that can slide onto any of three pegs.
Initially, all the disks are on the first peg in order of
size, the largest on the bottom and the smallest on top.
The goal is to move all the disks to the third peg, using
the second one as an auxiliary, if necessary.
We can move only one disk at a time, and it is
forbidden to place a larger disk on top of a smaller one.
178. Example 2: Tower of hanoi probelm
The problem has an elegant recursive solution:
To move n>1 disks from peg 1 to peg 3 (with peg 2 as auxiliary):
o Step 1: We first move recursively n − 1 disks from peg 1 to peg 2 (with peg 3 as
auxiliary).
o Step 2: Then move the largest disk i.e., nth disk directly from peg 1 to peg 3.
o Step 3: And, finally, move recursively n − 1 disks from peg 2 to peg 3 (using peg 1 as
auxiliary).
Of course, if n = 1, we simply move the single disk directly from the source peg to the destination
peg.
180. Example 2: Tower of hanoi probelm
Algorithm TowerHanoi(n,src,aux,dest)
//Purpose: to Move n disks from source peg to destination peg recursively
//Input: ‘n’ disks on source peg in order of size, the largest on the bottom and the smallest on
top
//Output: ‘n’ disks on destination peg in order of size, the largest on the bottom and the
smallest on top
{
if (n = = 1) then
move disk from src to dest
else
TowerHanoi(n- 1, src, dest, aux) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, src,dest) // Step 3
}
181. Example 2: Tower of hanoi probelm
Analysis:
1.The number of disks n is the obvious choice for the input‘s size indicator.
2.Moving one disk is considered as the algorithm‘s basic operation.
3. Let us consider M(n) indicating the number of moves made for n disks.
Clearly, the number of moves M(n) depends on n i.e., input size only
4. We set the following recurrence equation for M(n):
M(n) = M(n-1)+ 1+M(n-1) for n>1,
Where M(n-1)= number of moves made to
transfer (n-1) disks from src to aux(using dest)
1 One Move to transfer nth disk from src to dest
Where M(n-1)= number of moves made to
transfer (n-1) disks from aux to dest(using src)
182. Example 2: Tower of hanoi probelm
Analysis:
4. We set the following recurrence equation for M(n):
M(n) = M(n-1)+ 1+M(n-1) for n>1,
The initial condition is: M(1) = 1 for n=1
Now, M(n) = 2M(n-1) + 1, M(1) = 1 and
183. Example 2: Tower of hanoi probelm
Analysis:
5. Solve the following recurrence equation for M(n) using backward substitution method:
M(n) = 2M(n-1) + 1
Substitute M(n − 1) = 2M(n − 2) + 1 in above eqn
M(n)= 2(2M(n-2) + 1) + 1
M(n)= 22*M(n-2) + 21 + 20
Substitute M(n − 2) = 2M(n − 3) + 1 in above eqn
M(n)= 22*(2M(n-3) + 1) + 21 + 20
M(n)= 23*M(n-3) + 22 + 21 + 20
= …
184. Example 2: Tower of hanoi probelm
Analysis:
5. Solve the following recurrence equation for M(n) using backward substitution method:
M(n)= 23*M(n-3) + 22 + 21 + 20
= …
The general formula for the above pattern for some ‘i’ is as follows:
M(n)= 2i*M(n-i) + 2i-1 +………..+ 22 + 21 + 20
By taking the advantage of the initial condition given i.e., M(1)=1, we equate n-i=1, then i=n-1
M(n)= 2n-1*M(n-(n-1)) + 2(n-1)-1 +………..+ 22 + 21 + 20
M(n)= 2n-1*M(n-n+1) + 2n-2 +………..+ 22 + 21 + 20
M(n)= 2n-1*M(1) + 2n-2 +………..+ 22 + 21 + 20
M(n)= 2n-1 + 2n-2 +………..+ 22 + 21 + 20
185. Example 2: Tower of hanoi probelm
Analysis:
5. Solve the following recurrence equation for M(n) using backward substitution method:
M(n)= 2n-1 + 2n-2 +………..+ 22 + 21 + 20
This is Geometric Progression Series with commom ratio r=2, a = 20 = 1,
with number of terms = n,
Wkt, Sum of Geometric Progression Series when r>1 =
Hence M(n)= 1(2n – 1)
(2-1)
M(n)= 2n – 1
Hence M(n) (2n) Exponentially Order of Growth
186. assignment
Solve the following recurrence relations:
x(n)=x(n-1)+5 for n>1, x(0)=0
f(n) =f(n-1)+2 for n>1 , f(0)=1
x(n)=2x(n-1)+1 for n>1, x(1)=1
F(n) = F(n-1) + n for n>0, F(0) = 0
X(n) = 3X(n-1) for n>1, X(1) = 4