SlideShare a Scribd company logo
Unit 2
Algorithm
Things To Be Discussed In This Unit
• Program Design
• Concept and Definition
• Design of algorithm
• Characteristic of algorithm
• Big O notation
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?
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
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.
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.
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
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.
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
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
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.
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.
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
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.
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)
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.
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.
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.
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
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.
20
Naïve Algorithm Vs Efficient Algorithm
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)
22
Function NaiveGCD(a,b)
• PROBLEMS:
• Takes too much time to complete
• Not an optimal solution
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)?
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
25
Big O notation
• To be continued on next slide…
26

More Related Content

What's hot

C programming Ms. Pranoti Doke
C programming Ms. Pranoti DokeC programming Ms. Pranoti Doke
C programming Ms. Pranoti Doke
Pranoti Doke
 
iFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature ImplementationsiFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature Implementations
Shinpei Hayashi
 
Python - Functions - Azure Jupyter Notebooks
Python - Functions - Azure Jupyter NotebooksPython - Functions - Azure Jupyter Notebooks
Python - Functions - Azure Jupyter Notebooks
Adri Jovin
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Sem1 plt xp_03
Sem1 plt xp_03Sem1 plt xp_03
Sem1 plt xp_03
Niit Care
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with C
gpsoft_sk
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.Proto
Joel Falcou
 
Boost.Dispatch
Boost.DispatchBoost.Dispatch
Boost.Dispatch
Joel Falcou
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
Emertxe Information Technologies Pvt Ltd
 
Visualizing Stakeholder Concerns with Anchored Map
Visualizing Stakeholder Concerns with Anchored MapVisualizing Stakeholder Concerns with Anchored Map
Visualizing Stakeholder Concerns with Anchored Map
Takanori Ugai
 
HDR Defence - Software Abstractions for Parallel Architectures
HDR Defence - Software Abstractions for Parallel ArchitecturesHDR Defence - Software Abstractions for Parallel Architectures
HDR Defence - Software Abstractions for Parallel Architectures
Joel Falcou
 
(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures
Joel Falcou
 
Session 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesSession 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesNirav Desai
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
kim.mens
 
Guiding Identification of Missing Scenarios for Dynamic Feature Location
Guiding Identification of Missing Scenarios for Dynamic Feature LocationGuiding Identification of Missing Scenarios for Dynamic Feature Location
Guiding Identification of Missing Scenarios for Dynamic Feature Location
Shinpei Hayashi
 

What's hot (20)

Unit 3
Unit 3Unit 3
Unit 3
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
C programming Ms. Pranoti Doke
C programming Ms. Pranoti DokeC programming Ms. Pranoti Doke
C programming Ms. Pranoti Doke
 
Unit 2
Unit 2Unit 2
Unit 2
 
iFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature ImplementationsiFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature Implementations
 
Python - Functions - Azure Jupyter Notebooks
Python - Functions - Azure Jupyter NotebooksPython - Functions - Azure Jupyter Notebooks
Python - Functions - Azure Jupyter Notebooks
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Sem1 plt xp_03
Sem1 plt xp_03Sem1 plt xp_03
Sem1 plt xp_03
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with C
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.Proto
 
Boost.Dispatch
Boost.DispatchBoost.Dispatch
Boost.Dispatch
 
Krml203
Krml203Krml203
Krml203
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
Visualizing Stakeholder Concerns with Anchored Map
Visualizing Stakeholder Concerns with Anchored MapVisualizing Stakeholder Concerns with Anchored Map
Visualizing Stakeholder Concerns with Anchored Map
 
HDR Defence - Software Abstractions for Parallel Architectures
HDR Defence - Software Abstractions for Parallel ArchitecturesHDR Defence - Software Abstractions for Parallel Architectures
HDR Defence - Software Abstractions for Parallel Architectures
 
(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures
 
Session 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesSession 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfaces
 
Functions
FunctionsFunctions
Functions
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
 
Guiding Identification of Missing Scenarios for Dynamic Feature Location
Guiding Identification of Missing Scenarios for Dynamic Feature LocationGuiding Identification of Missing Scenarios for Dynamic Feature Location
Guiding Identification of Missing Scenarios for Dynamic Feature Location
 

Similar to Algorithm and C code related to data structure

Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
Ashim Lamichhane
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
jinkhatima
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
Bulbul Agrawal
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
satvikkushwaha1
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
Mani Kandan
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.ppt
Tekle12
 
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
Tekle12
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
NayanChandak1
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
RahikAhmed1
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
Dr. Pankaj Agarwal
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
MIT,Imphal
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
Appili Vamsi Krishna
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
ssuser8bddb2
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
Akhil Kaushik
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
Afaq Mansoor Khan
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)
praveena p
 

Similar to Algorithm and C code related to data structure (20)

Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
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
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
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 C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)
 

More from Self-Employed

Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Ds lec 5_chap4
Ds lec 5_chap4Ds lec 5_chap4
Ds lec 5_chap4
Self-Employed
 
Discrete mathematics counting and logic relation
Discrete mathematics counting and logic relationDiscrete mathematics counting and logic relation
Discrete mathematics counting and logic relation
Self-Employed
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
Self-Employed
 
2.2 inverse of a matrix
2.2 inverse of a matrix2.2 inverse of a matrix
2.2 inverse of a matrix
Self-Employed
 
8086 architecture
8086 architecture8086 architecture
8086 architecture
Self-Employed
 

More from Self-Employed (7)

Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Ds lec 5_chap4
Ds lec 5_chap4Ds lec 5_chap4
Ds lec 5_chap4
 
Discrete mathematics counting and logic relation
Discrete mathematics counting and logic relationDiscrete mathematics counting and logic relation
Discrete mathematics counting and logic relation
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
2.2 inverse of a matrix
2.2 inverse of a matrix2.2 inverse of a matrix
2.2 inverse of a matrix
 
8086 architecture
8086 architecture8086 architecture
8086 architecture
 

Recently uploaded

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

Algorithm and C code related to data structure

  • 2. Things To Be Discussed In This Unit • Program Design • Concept and Definition • Design of algorithm • Characteristic of algorithm • Big O notation 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? 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 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. 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. 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 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. 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 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 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. 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. 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 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. 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) 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. 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. 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. 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 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. 20
  • 21. Naïve Algorithm Vs Efficient Algorithm 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) 22
  • 23. Function NaiveGCD(a,b) • PROBLEMS: • Takes too much time to complete • Not an optimal solution 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)? 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 25
  • 26. Big O notation • To be continued on next slide… 26