SlideShare a Scribd company logo
1 of 26
Unit 2
Algorithm
Things To Be Discussed In This Unit
• Program Design
• Concept and Definition
• Design of algorithm
• Characteristic of algorithm
• Big O notation
Ashim Lamichhane 2
Learning Objectives
• Understand the type of problem that will be covered in this class
• Recognize some problems for which sophisticated algorithms might not be
necessary.
• Question if your solution technique is an efficient one? Any room for
optimization?
Ashim Lamichhane 3
Program Design
• Involves taking the specification and designing solutions, the designer needs to
adopt a design strategy.
• Solution Strategy should work correctly in all conditions
• A large program should be divided into small modules and sub modules.
• Other important criteria by which a program can be judged are execution time and
storage requirement
Ashim Lamichhane 4
Algorithms
• The term ’algorithm’ refers to the sequence of instructions that must be followed
to solve a problem.
• Logical representation of the instructions which should be executed to perform a
meaningful task.
• Algorithms are generally created independent of underlying languages, i.e. an
algorithm can be implemented in more than one programming language.
Ashim Lamichhane 5
An algorithm has certain characteristics
• Each instruction should be unique and concise.
• Each instruction should be relative in nature and should not be repeated infinitely
• Repetition of same task(s) should be avoided.
• The result should be available to the user after algorithm terminates.
Ashim Lamichhane 6
NOTE:
• After an algorithm has been designed, its efficiency must be analyzed. i.e CPU time
and memory
• Memory space and running time should be taken care of.
• The importance of efficiency of an algorithm is in the correctness, i.e does it
always produce the correct result, and program complexity which considers
both the difficulty of implementing an algorithm along with its efficiency
Ashim Lamichhane 7
Properties of an Algorithm
• Input: A number of quantities are provided to an algorithm initially before the algorithm
begins. These quantities are inputs which are processed by the algorithm.
• Definiteness: Each step must be clear and unambiguous.
• Effectiveness: Each step must be carried out in finite time.
• Finiteness: Algorithms must terminate after finite time or step
• Output: An algorithm must have output.
• Correctness: Correct set of output values must be produced from the each set of inputs.
Ashim Lamichhane 8
Write an algorithm to find the greatest number among three numbers
Step 1: Read three numbers and store them in X, Y and Z
Step 2: Compare X and Y. if X is greater than Y then go to step 5 else go to step 3
Step 3: Compare Y and Z. if Y is greater than Z then print “Y is greatest” and go to step 7
otherwise go to step 4
Step 4: Print “Z is greatest” and go to step 7
Step 5: Compare X and Z. if X is greater than Z then print “X is greatest” and go to step 7
otherwise go to step 6
Step 6: Print “Z is greatest” and go to step 7
Step 7: Stop
Ashim Lamichhane 9
Different Approaches To Designing An Algorithm
• A complex system may be divided into smaller units called modules.
• The advantage of modularity is that it focuses on specific module.
• Modularity enhances design clarity, which in turn eases implementation,
debugging, testing, documenting and maintenance of the project.
• To design a hierarchy of a system there are two possible approaches
• Top-down approach
• Bottom-up approach
Ashim Lamichhane 10
Top-Down Approach
• How it is done?
• Identify the major components of the system,
• decompose them into their lower-level components and
• Iterate until the desired level of module complexity is achieved.
• It basically starts with the big picture. It breaks down from there into smaller
segments.
• Top-down design method takes the form of stepwise refinement.
Ashim Lamichhane 11
Bottom-Up Approach
• A bottom-up approach is the piecing together of systems to give rise to more
complex systems
• Bottom-up method works with layers of abstraction.
• Elements are then linked together to form larger subsystems, which then in turn
are linked, sometimes in many levels, until a complete top-level system is formed.
• This strategy often resembles a "seed" model, by which the beginnings are small
but eventually grow in complexity and completeness.
Ashim Lamichhane 12
Top-Down versus Bottom-Up Approach
• The top-down approach, however, is often useful way to better document a
design.
• The design activity should not be constrained to proceed according to a fixed
pattern but should be a blend of top-down and bottom-up approaches
Ashim Lamichhane 13
Complexity
• When we talk of complexity in context of computers, we call it computational
complexity.
• Computational complexity is a characterization of the time or space requirements
for solving a problem by a particular algorithm.
• Lesser the complexity better an algorithm.
Ashim Lamichhane 14
Complexity
• Given a particular problem, let ’n’ denote its size. The time required of a specific
algorithm for solving this problem is expressed by a function:
f : R->R
• Such that, f(n) is the largest amount of time needed by the algorithm to solve the
problem of size n.
• Function ‘f’ is usually called the time complexity function.
• Thus we conclude that the analysis of the program requires two main
considerations:
• Time Complexity (time required for completion)
• Space Complexity (memory required for completion)
Ashim Lamichhane 15
Time Complexity
• While measuring the time complexity of an algorithm, we concentrate on
developing only the frequency count for all key statements
(statements that are important and are the basic instructions of an algorithm)
• This is because, it is often difficult to get reliable timing figure because of clock
limitations and the multiprogramming or the sharing environment.
Ashim Lamichhane 16
Algorithm A
• In an algorithm A we may find that the statement a=a+1 is
independent and is not contained within any loop.
• Therefore, the number of times this shall be executed is 1.
• We say that the frequency count of an algorithm A is 1.
Ashim Lamichhane 17
Algorithm B
• In this algorithm, i.e. B, the key statement is the assignment operation a=a+1.
• Because this statement is contained within a loop, the number of times it is
executed is n, as the loop runs for n times.
• The frequency count for this algorithm is n.
Ashim Lamichhane 18
Algorithm C
• The frequency count for the statement a=a+1 is n2 as the inner loop runs n times,
each time the outer loop runs, the inner loop also runs for n times.
• If an algorithm perform f(n) basic operations when the size of its input is n, then its
total running time will be cf(n), where c is a constant that depends upon the
algorithm, on the way it is programmed, and on the way the
computer is used, but c does not depend on the size of the input
Ashim Lamichhane 19
Space Complexity
• Space complexity is essentially the number of memory cells which an algorithm needs.
• A good algorithm keeps this number as small as possible, too.
• There is often a time-space-tradeoff involved in a problem, that is, it cannot be solved with few
computing time and low memory consumption.
• Space complexity is measured with respect to the input size for a given instance of a problem.
Ashim Lamichhane 20
Naïve Algorithm Vs Efficient Algorithm
Ashim Lamichhane 21
Ex 1: Greatest Common Divisor
• DEFINITION:
• For integers, a and b, their greatest common divisor or gcd(a,b) is the largest integer d so
that d divides both a and b
• Compute GCD
• Input: Integers a,b ≥0
• Output: gcd(a,b)
• Ex: Run on large numbers like
gcd(3198848,1653264)
Ashim Lamichhane 22
Function NaiveGCD(a,b)
• PROBLEMS:
• Takes too much time to complete
• Not an optimal solution
Ashim Lamichhane 23
Best=0
For d from 1 to a+b:
if d/a and d/b:
best=d
return best
Function efficientGCD(a,b)
• Lemma
• Let a’ be the remainder when a is divided by b, then
gcd(a,b)=gcd(a’,b)=gcd(b,a’)
• So what is gcd(357,234)?
Ashim Lamichhane 24
If b=0:
return a
a’ = the remainder when a is divided by b
return efficientGCD(b,a’)
Example
Summary of naïve vs efficient algorithms
• Naïve algorithm is too slow
• The correct algorithm is much better
• Finding the correct algorithm requires knowing something interesting about the
problem
Ashim Lamichhane 25
Big O notation
• To be continued on next slide…
Ashim Lamichhane 26

More Related Content

What's hot

Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartRabin BK
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codeshermiraguilar
 
Notion of an algorithm
Notion of an algorithmNotion of an algorithm
Notion of an algorithmNisha Soms
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 

What's hot (20)

Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Big o notation
Big o notationBig o notation
Big o notation
 
4. algorithm
4. algorithm4. algorithm
4. algorithm
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Notion of an algorithm
Notion of an algorithmNotion of an algorithm
Notion of an algorithm
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 

Viewers also liked

Viewers also liked (20)

Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
 
Sorting
SortingSorting
Sorting
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Queues
QueuesQueues
Queues
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Linked List
Linked ListLinked List
Linked List
 
Searching
SearchingSearching
Searching
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Friedman two way analysis of variance by
Friedman two way analysis of  variance byFriedman two way analysis of  variance by
Friedman two way analysis of variance by
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
fundamental of information technology (2)
fundamental of information technology  (2)fundamental of information technology  (2)
fundamental of information technology (2)
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Sets and disjoint sets union123
Sets and disjoint sets union123Sets and disjoint sets union123
Sets and disjoint sets union123
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 

Similar to Algorithm Introduction

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
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxsatvikkushwaha1
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxSyed Zaid Irshad
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptTekle12
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfNayanChandak1
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer Ashim Lamichhane
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem slovingMani Kandan
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.pptALIZAIB KHAN
 

Similar to Algorithm Introduction (20)

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
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.ppt
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.ppt
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 

Recently uploaded

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Algorithm Introduction

  • 2. Things To Be Discussed In This Unit • Program Design • Concept and Definition • Design of algorithm • Characteristic of algorithm • Big O notation Ashim Lamichhane 2
  • 3. Learning Objectives • Understand the type of problem that will be covered in this class • Recognize some problems for which sophisticated algorithms might not be necessary. • Question if your solution technique is an efficient one? Any room for optimization? Ashim Lamichhane 3
  • 4. Program Design • Involves taking the specification and designing solutions, the designer needs to adopt a design strategy. • Solution Strategy should work correctly in all conditions • A large program should be divided into small modules and sub modules. • Other important criteria by which a program can be judged are execution time and storage requirement Ashim Lamichhane 4
  • 5. Algorithms • The term ’algorithm’ refers to the sequence of instructions that must be followed to solve a problem. • Logical representation of the instructions which should be executed to perform a meaningful task. • Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in more than one programming language. Ashim Lamichhane 5
  • 6. An algorithm has certain characteristics • Each instruction should be unique and concise. • Each instruction should be relative in nature and should not be repeated infinitely • Repetition of same task(s) should be avoided. • The result should be available to the user after algorithm terminates. Ashim Lamichhane 6
  • 7. NOTE: • After an algorithm has been designed, its efficiency must be analyzed. i.e CPU time and memory • Memory space and running time should be taken care of. • The importance of efficiency of an algorithm is in the correctness, i.e does it always produce the correct result, and program complexity which considers both the difficulty of implementing an algorithm along with its efficiency Ashim Lamichhane 7
  • 8. Properties of an Algorithm • Input: A number of quantities are provided to an algorithm initially before the algorithm begins. These quantities are inputs which are processed by the algorithm. • Definiteness: Each step must be clear and unambiguous. • Effectiveness: Each step must be carried out in finite time. • Finiteness: Algorithms must terminate after finite time or step • Output: An algorithm must have output. • Correctness: Correct set of output values must be produced from the each set of inputs. Ashim Lamichhane 8
  • 9. Write an algorithm to find the greatest number among three numbers Step 1: Read three numbers and store them in X, Y and Z Step 2: Compare X and Y. if X is greater than Y then go to step 5 else go to step 3 Step 3: Compare Y and Z. if Y is greater than Z then print “Y is greatest” and go to step 7 otherwise go to step 4 Step 4: Print “Z is greatest” and go to step 7 Step 5: Compare X and Z. if X is greater than Z then print “X is greatest” and go to step 7 otherwise go to step 6 Step 6: Print “Z is greatest” and go to step 7 Step 7: Stop Ashim Lamichhane 9
  • 10. Different Approaches To Designing An Algorithm • A complex system may be divided into smaller units called modules. • The advantage of modularity is that it focuses on specific module. • Modularity enhances design clarity, which in turn eases implementation, debugging, testing, documenting and maintenance of the project. • To design a hierarchy of a system there are two possible approaches • Top-down approach • Bottom-up approach Ashim Lamichhane 10
  • 11. Top-Down Approach • How it is done? • Identify the major components of the system, • decompose them into their lower-level components and • Iterate until the desired level of module complexity is achieved. • It basically starts with the big picture. It breaks down from there into smaller segments. • Top-down design method takes the form of stepwise refinement. Ashim Lamichhane 11
  • 12. Bottom-Up Approach • A bottom-up approach is the piecing together of systems to give rise to more complex systems • Bottom-up method works with layers of abstraction. • Elements are then linked together to form larger subsystems, which then in turn are linked, sometimes in many levels, until a complete top-level system is formed. • This strategy often resembles a "seed" model, by which the beginnings are small but eventually grow in complexity and completeness. Ashim Lamichhane 12
  • 13. Top-Down versus Bottom-Up Approach • The top-down approach, however, is often useful way to better document a design. • The design activity should not be constrained to proceed according to a fixed pattern but should be a blend of top-down and bottom-up approaches Ashim Lamichhane 13
  • 14. Complexity • When we talk of complexity in context of computers, we call it computational complexity. • Computational complexity is a characterization of the time or space requirements for solving a problem by a particular algorithm. • Lesser the complexity better an algorithm. Ashim Lamichhane 14
  • 15. Complexity • Given a particular problem, let ’n’ denote its size. The time required of a specific algorithm for solving this problem is expressed by a function: f : R->R • Such that, f(n) is the largest amount of time needed by the algorithm to solve the problem of size n. • Function ‘f’ is usually called the time complexity function. • Thus we conclude that the analysis of the program requires two main considerations: • Time Complexity (time required for completion) • Space Complexity (memory required for completion) Ashim Lamichhane 15
  • 16. Time Complexity • While measuring the time complexity of an algorithm, we concentrate on developing only the frequency count for all key statements (statements that are important and are the basic instructions of an algorithm) • This is because, it is often difficult to get reliable timing figure because of clock limitations and the multiprogramming or the sharing environment. Ashim Lamichhane 16
  • 17. Algorithm A • In an algorithm A we may find that the statement a=a+1 is independent and is not contained within any loop. • Therefore, the number of times this shall be executed is 1. • We say that the frequency count of an algorithm A is 1. Ashim Lamichhane 17
  • 18. Algorithm B • In this algorithm, i.e. B, the key statement is the assignment operation a=a+1. • Because this statement is contained within a loop, the number of times it is executed is n, as the loop runs for n times. • The frequency count for this algorithm is n. Ashim Lamichhane 18
  • 19. Algorithm C • The frequency count for the statement a=a+1 is n2 as the inner loop runs n times, each time the outer loop runs, the inner loop also runs for n times. • If an algorithm perform f(n) basic operations when the size of its input is n, then its total running time will be cf(n), where c is a constant that depends upon the algorithm, on the way it is programmed, and on the way the computer is used, but c does not depend on the size of the input Ashim Lamichhane 19
  • 20. Space Complexity • Space complexity is essentially the number of memory cells which an algorithm needs. • A good algorithm keeps this number as small as possible, too. • There is often a time-space-tradeoff involved in a problem, that is, it cannot be solved with few computing time and low memory consumption. • Space complexity is measured with respect to the input size for a given instance of a problem. Ashim Lamichhane 20
  • 21. Naïve Algorithm Vs Efficient Algorithm Ashim Lamichhane 21
  • 22. Ex 1: Greatest Common Divisor • DEFINITION: • For integers, a and b, their greatest common divisor or gcd(a,b) is the largest integer d so that d divides both a and b • Compute GCD • Input: Integers a,b ≥0 • Output: gcd(a,b) • Ex: Run on large numbers like gcd(3198848,1653264) Ashim Lamichhane 22
  • 23. Function NaiveGCD(a,b) • PROBLEMS: • Takes too much time to complete • Not an optimal solution Ashim Lamichhane 23 Best=0 For d from 1 to a+b: if d/a and d/b: best=d return best
  • 24. Function efficientGCD(a,b) • Lemma • Let a’ be the remainder when a is divided by b, then gcd(a,b)=gcd(a’,b)=gcd(b,a’) • So what is gcd(357,234)? Ashim Lamichhane 24 If b=0: return a a’ = the remainder when a is divided by b return efficientGCD(b,a’) Example
  • 25. Summary of naïve vs efficient algorithms • Naïve algorithm is too slow • The correct algorithm is much better • Finding the correct algorithm requires knowing something interesting about the problem Ashim Lamichhane 25
  • 26. Big O notation • To be continued on next slide… Ashim Lamichhane 26