SlideShare a Scribd company logo
1 of 22
Introduction to Algorithms
Chapter 1: The Role of Algorithms in
Computing
2
Computational problems
 A computational problem specifies an
input-output relationship
 What does the input look like?
 What should the output be for each input?
 Example:
 Input: an integer number n
 Output: Is the number prime?
 Example:
 Input: A list of names of people
 Output: The same list sorted alphabetically
3
Algorithms
 A tool for solving a well-specified
computational problem
 Algorithms must be:
 Correct: For each input produce an appropriate output
 Efficient: run as quickly as possible, and use as little
memory as possible – more about this later
Algorithm
Input Output
4
Algorithms Cont.
 A well-defined computational procedure that
takes some value, or set of values, as input and
produces some value, or set of values, as output.
 Written in a pseudo code which can be
implemented in the language of programmer’s
choice.
5
Correct and incorrect algorithms
 Algorithm is correct if, for every input instance, it ends
with the correct output. We say that a correct algorithm
solves the given computational problem.
 An incorrect algorithm might not end at all on some input
instances, or it might end with an answer other than the
desired one.
 We shall be concerned only with correct algorithms.
6
Problems and Algorithms
 We need to solve a computational problem
 “Convert a weight in pounds to Kg”
 An algorithm specifies how to solve it, e.g.:
 1. Read weight-in-pounds
 2. Calculate weight-in-Kg = weight-in-pounds *
0.455
 3. Print weight-in-Kg
 A computer program is a computer-
executable description of an algorithm
7
The Problem-solving Process
Problem
specification
Algorithm
Program
Executable
(solution)
Analysis
Design
Implementation
Compilation
8
From Algorithms to Programs
Problem
C++ Program
Algorithm: A sequence
of instructions describing
how to do a task (or
process)
9
Practical Examples
 Internet and Networks
􀂄 The need to access large amount of information with the shortest
time.
􀂄 Problems of finding the best routs for the data to travel.
􀂄 Algorithms for searching this large amount of data to quickly find
the pages on which particular information resides.
 Electronic Commerce
􀂄 The ability of keeping the information (credit card numbers,
passwords, bank statements) private, safe, and secure.
􀂄 Algorithms involves encryption/decryption techniques.
10
Hard problems
 We can identify the Efficiency of an algorithm
from its speed (how long does the algorithm take
to produce the result).
 Some problems have unknown efficient solution.
 These problems are called NP-complete
problems.
 If we can show that the problem is NP-complete,
we can spend our time developing an efficient
algorithm that gives a good, but not the best
possible solution.
11
Components of an Algorithm
 Variables and values
 Instructions
 Sequences
 A series of instructions
 Procedures
 A named sequence of instructions
 we also use the following words to refer to a
“Procedure” :
 Sub-routine
 Module
 Function
12
Components of an Algorithm Cont.
 Selections
 An instruction that decides which of two possible
sequences is executed
 The decision is based on true/false condition
 Repetitions
 Also known as iteration or loop
 Documentation
 Records what the algorithm does
13
A Simple Algorithm
 INPUT: a sequence of n numbers
 T is an array of n elements
 T[1], T[2], …, T[n]
 OUTPUT: the smallest number among them
 Performance of this algorithm is a function of n
min = T[1]
for i = 2 to n do
{
if T[i] < min
min = T[i]
}
Output min
14
Greatest Common Divisor
 The first algorithm “invented” in history was Euclid’s
algorithm for finding the greatest common divisor
(GCD) of two natural numbers
 Definition: The GCD of two natural numbers x, y is
the largest integer j that divides both (without
remainder). i.e. mod(j, x)=0, mod(j, y)=0, and j is the
largest integer with this property.
 The GCD Problem:
 Input: natural numbers x, y
 Output: GCD(x,y) – their GCD
15
Euclid’s GCD Algorithm
GCD(x, y)
{
while (y != 0)
{
t = mod(x, y)
x = y
y = t
}
Output x
}
16
Euclid’s GCD Algorithm – sample run
while (y!=0) {
int temp = x%y;
x = y;
y = temp;
}
Example: Computing GCD(72,120)
temp x y
After 0 rounds -- 72 120
After 1 round 72 120 72
After 2 rounds 48 72 48
After 3 rounds 24 48 24
After 4 rounds 0 24 0
Output: 24
17
Algorithm Efficiency
 Consider two sort algorithms
 Insertion sort
 takes c1n2 to sort n items
 where c1 is a constant that does not depends on n
 it takes time roughly proportional to n2
 Merge Sort
 takes c2 n lg(n) to sort n items
 where c2 is also a constant that does not depends on n
 lg(n) stands for log2 (n)
 it takes time roughly proportional to n lg(n)
 Insertion sort usually has a smaller constant factor than
merge sort
 so that, c1 < c2
 Merge sort is faster than insertion sort for large input sizes
18
Algorithm Efficiency Cont.
 Consider now:
 A faster computer A running insertion sort against
 A slower computer B running merge sort
 Both must sort an array of one million numbers
 Suppose
 Computer A executes one billion (109) instructions per
second
 Computer B executes ten million (107) instructions per
second
 So computer A is 100 times faster than computer B
 Assume that
 c1 = 2 and c2 = 50
19
Algorithm Efficiency Cont.
 To sort one million numbers
 Computer A takes
2 . (106)2 instructions
109 instructions/second
= 2000 seconds
 Computer B takes
50 . 106 . lg(106) instructions
107 instructions/second
 100 seconds
 By using algorithm whose running time grows more slowly,
Computer B runs 20 times faster than Computer A
 For ten million numbers
 Insertion sort takes  2.3 days
 Merge sort takes  20 minutes
20
Pseudo-code conventions
Algorithms are typically written in pseudo-code that is similar to C/C++
and JAVA.
 Pseudo-code differs from real code with:
 It is not typically concerned with issues of software
engineering.
 Issues of data abstraction, and error handling are often
ignored.
 Indentation indicates block structure.
 The symbol "▹" indicates that the remainder of the line is a
comment.
 A multiple assignment of the form i ← j ← e assigns to both
variables i and j the value of expression e; it should be treated as
equivalent to the assignment j ← e followed by the assignment i ← j.
21
Pseudo-code conventions
 Variables ( such as i, j, and key) are local to the given procedure.
We shall not us global variables without explicit indication.
 Array elements are accessed by specifying the array name
followed by the index in square brackets. For example, A[i]
indicates the ith element of the array A. The notation “…" is used
to indicate a range of values within an array. Thus, A[1…j]
indicates the sub-array of A consisting of the j elements A[1],
A[2], . . . , A[j].
 A particular attribute is accessed using the attributes name
followed by the name of its object in square brackets.
 For example, we treat an array as an object with the attribute
length indicating how many elements it contains(length[A]).
22
Pseudo-code Example

More Related Content

Similar to CP4151 ADSA unit1 Advanced Data Structures and Algorithms

DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxJeevaMCSEKIOT
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
AOA Week 01.ppt
AOA Week 01.pptAOA Week 01.ppt
AOA Week 01.pptINAM352782
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notesProf. Dr. K. Adisesha
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 

Similar to CP4151 ADSA unit1 Advanced Data Structures and Algorithms (20)

DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptx
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Chapter one
Chapter oneChapter one
Chapter one
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
AOA Week 01.ppt
AOA Week 01.pptAOA Week 01.ppt
AOA Week 01.ppt
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
01-algo.ppt
01-algo.ppt01-algo.ppt
01-algo.ppt
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 

More from Sheba41

Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptSheba41
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfSheba41
 
MapReduce.pptx
MapReduce.pptxMapReduce.pptx
MapReduce.pptxSheba41
 
HadoooIO.ppt
HadoooIO.pptHadoooIO.ppt
HadoooIO.pptSheba41
 
Unit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdfUnit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdfSheba41
 
Unit-5 Time series data Analysis.pptx
Unit-5 Time series data Analysis.pptxUnit-5 Time series data Analysis.pptx
Unit-5 Time series data Analysis.pptxSheba41
 

More from Sheba41 (7)

Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
MapReduce.pptx
MapReduce.pptxMapReduce.pptx
MapReduce.pptx
 
pig.ppt
pig.pptpig.ppt
pig.ppt
 
HadoooIO.ppt
HadoooIO.pptHadoooIO.ppt
HadoooIO.ppt
 
Unit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdfUnit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdf
 
Unit-5 Time series data Analysis.pptx
Unit-5 Time series data Analysis.pptxUnit-5 Time series data Analysis.pptx
Unit-5 Time series data Analysis.pptx
 

Recently uploaded

Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 

Recently uploaded (20)

Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 

CP4151 ADSA unit1 Advanced Data Structures and Algorithms

  • 1. Introduction to Algorithms Chapter 1: The Role of Algorithms in Computing
  • 2. 2 Computational problems  A computational problem specifies an input-output relationship  What does the input look like?  What should the output be for each input?  Example:  Input: an integer number n  Output: Is the number prime?  Example:  Input: A list of names of people  Output: The same list sorted alphabetically
  • 3. 3 Algorithms  A tool for solving a well-specified computational problem  Algorithms must be:  Correct: For each input produce an appropriate output  Efficient: run as quickly as possible, and use as little memory as possible – more about this later Algorithm Input Output
  • 4. 4 Algorithms Cont.  A well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.  Written in a pseudo code which can be implemented in the language of programmer’s choice.
  • 5. 5 Correct and incorrect algorithms  Algorithm is correct if, for every input instance, it ends with the correct output. We say that a correct algorithm solves the given computational problem.  An incorrect algorithm might not end at all on some input instances, or it might end with an answer other than the desired one.  We shall be concerned only with correct algorithms.
  • 6. 6 Problems and Algorithms  We need to solve a computational problem  “Convert a weight in pounds to Kg”  An algorithm specifies how to solve it, e.g.:  1. Read weight-in-pounds  2. Calculate weight-in-Kg = weight-in-pounds * 0.455  3. Print weight-in-Kg  A computer program is a computer- executable description of an algorithm
  • 8. 8 From Algorithms to Programs Problem C++ Program Algorithm: A sequence of instructions describing how to do a task (or process)
  • 9. 9 Practical Examples  Internet and Networks 􀂄 The need to access large amount of information with the shortest time. 􀂄 Problems of finding the best routs for the data to travel. 􀂄 Algorithms for searching this large amount of data to quickly find the pages on which particular information resides.  Electronic Commerce 􀂄 The ability of keeping the information (credit card numbers, passwords, bank statements) private, safe, and secure. 􀂄 Algorithms involves encryption/decryption techniques.
  • 10. 10 Hard problems  We can identify the Efficiency of an algorithm from its speed (how long does the algorithm take to produce the result).  Some problems have unknown efficient solution.  These problems are called NP-complete problems.  If we can show that the problem is NP-complete, we can spend our time developing an efficient algorithm that gives a good, but not the best possible solution.
  • 11. 11 Components of an Algorithm  Variables and values  Instructions  Sequences  A series of instructions  Procedures  A named sequence of instructions  we also use the following words to refer to a “Procedure” :  Sub-routine  Module  Function
  • 12. 12 Components of an Algorithm Cont.  Selections  An instruction that decides which of two possible sequences is executed  The decision is based on true/false condition  Repetitions  Also known as iteration or loop  Documentation  Records what the algorithm does
  • 13. 13 A Simple Algorithm  INPUT: a sequence of n numbers  T is an array of n elements  T[1], T[2], …, T[n]  OUTPUT: the smallest number among them  Performance of this algorithm is a function of n min = T[1] for i = 2 to n do { if T[i] < min min = T[i] } Output min
  • 14. 14 Greatest Common Divisor  The first algorithm “invented” in history was Euclid’s algorithm for finding the greatest common divisor (GCD) of two natural numbers  Definition: The GCD of two natural numbers x, y is the largest integer j that divides both (without remainder). i.e. mod(j, x)=0, mod(j, y)=0, and j is the largest integer with this property.  The GCD Problem:  Input: natural numbers x, y  Output: GCD(x,y) – their GCD
  • 15. 15 Euclid’s GCD Algorithm GCD(x, y) { while (y != 0) { t = mod(x, y) x = y y = t } Output x }
  • 16. 16 Euclid’s GCD Algorithm – sample run while (y!=0) { int temp = x%y; x = y; y = temp; } Example: Computing GCD(72,120) temp x y After 0 rounds -- 72 120 After 1 round 72 120 72 After 2 rounds 48 72 48 After 3 rounds 24 48 24 After 4 rounds 0 24 0 Output: 24
  • 17. 17 Algorithm Efficiency  Consider two sort algorithms  Insertion sort  takes c1n2 to sort n items  where c1 is a constant that does not depends on n  it takes time roughly proportional to n2  Merge Sort  takes c2 n lg(n) to sort n items  where c2 is also a constant that does not depends on n  lg(n) stands for log2 (n)  it takes time roughly proportional to n lg(n)  Insertion sort usually has a smaller constant factor than merge sort  so that, c1 < c2  Merge sort is faster than insertion sort for large input sizes
  • 18. 18 Algorithm Efficiency Cont.  Consider now:  A faster computer A running insertion sort against  A slower computer B running merge sort  Both must sort an array of one million numbers  Suppose  Computer A executes one billion (109) instructions per second  Computer B executes ten million (107) instructions per second  So computer A is 100 times faster than computer B  Assume that  c1 = 2 and c2 = 50
  • 19. 19 Algorithm Efficiency Cont.  To sort one million numbers  Computer A takes 2 . (106)2 instructions 109 instructions/second = 2000 seconds  Computer B takes 50 . 106 . lg(106) instructions 107 instructions/second  100 seconds  By using algorithm whose running time grows more slowly, Computer B runs 20 times faster than Computer A  For ten million numbers  Insertion sort takes  2.3 days  Merge sort takes  20 minutes
  • 20. 20 Pseudo-code conventions Algorithms are typically written in pseudo-code that is similar to C/C++ and JAVA.  Pseudo-code differs from real code with:  It is not typically concerned with issues of software engineering.  Issues of data abstraction, and error handling are often ignored.  Indentation indicates block structure.  The symbol "▹" indicates that the remainder of the line is a comment.  A multiple assignment of the form i ← j ← e assigns to both variables i and j the value of expression e; it should be treated as equivalent to the assignment j ← e followed by the assignment i ← j.
  • 21. 21 Pseudo-code conventions  Variables ( such as i, j, and key) are local to the given procedure. We shall not us global variables without explicit indication.  Array elements are accessed by specifying the array name followed by the index in square brackets. For example, A[i] indicates the ith element of the array A. The notation “…" is used to indicate a range of values within an array. Thus, A[1…j] indicates the sub-array of A consisting of the j elements A[1], A[2], . . . , A[j].  A particular attribute is accessed using the attributes name followed by the name of its object in square brackets.  For example, we treat an array as an object with the attribute length indicating how many elements it contains(length[A]).