SlideShare a Scribd company logo
Introduction to Algorithms
Chapter 1: The Role of Algorithms in
Computing
Computational problems
 A computational problem specifies an
input-output relationship
 What does the input look like?
 What should the output be for each input?
2
 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
Algorithms
 A tool for solving a well-specified
computational problem
Algorithm
Input Output
3
 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
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.
4
 Written in a pseudo code which can be
implemented in the language of programmer’s
choice.
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
5
 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.
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
6
 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
The Problem-solving Process
Problem
specification
Analysis
Design
7
Algorithm
Program
Executable
(solution)
Implementation
Compilation
From Algorithms to Programs
Problem
Algorithm
Algorithm: A sequence
of instructions describing
how to do a task (or
process)
8
C++ Program
C++ Program
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.
9
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.
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
10
 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.
Components of an Algorithm
 Variables and values
 Instructions
 Sequences
 A series of instructions
Procedures
11
 Procedures
 A named sequence of instructions
 we also use the following words to refer to a
“Procedure” :
 Sub-routine
 Module
 Function
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
12
 Repetitions
 Also known as iteration or loop
 Documentation
 Records what the algorithm does
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
min = T[1]
13
 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
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
14
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
Euclid’s GCD Algorithm
GCD(x, y)
{
while (y != 0)
{
15
t = mod(x, y)
x = y
y = t
}
Output x
}
Euclid’s GCD Algorithm – sample run
while (y!=0) {
int temp = x%y;
x = y;
y = temp;
}
Example: Computing GCD(72,120)
16
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
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
17
 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
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
18
 Suppose
 Computer A execute one billion (109) instructions per
second
 Computer B execute ten million (107) instructions per
second
 So computer A is 100 times faster than computer B
 Assume that
 c1 = 2 and c2 = 50
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
19
50 . 10 . lg(10 ) 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
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.
20
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.
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]
21
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 attributes 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]).
Pseudo-code Example
22
Ad

More Related Content

Similar to CP4151 Advanced data structures and algorithms (20)

complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
pasinduneshan
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
NishaS88
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
ashish bansal
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
TechVision8
 
Design and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptxDesign and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptx
DhanushreeAN1
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Introduction to Design and Analysis of Algorithms
Introduction to Design and Analysis of AlgorithmsIntroduction to Design and Analysis of Algorithms
Introduction to Design and Analysis of Algorithms
ssusered62011
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
MemMem25
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
Natarajan Angappan
 
Presentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptxPresentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptx
rameshmanoj733
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
DeepakYadav656387
 
Performance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic NotationsPerformance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic Notations
DrSMeenakshiSundaram1
 
IntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptxIntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptx
prasanna220904
 
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
rajesshs31r
 
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
rajesshs31r
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
pasinduneshan
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
NishaS88
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
TechVision8
 
Design and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptxDesign and analysis of algorithms Module-I.pptx
Design and analysis of algorithms Module-I.pptx
DhanushreeAN1
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Introduction to Design and Analysis of Algorithms
Introduction to Design and Analysis of AlgorithmsIntroduction to Design and Analysis of Algorithms
Introduction to Design and Analysis of Algorithms
ssusered62011
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
MemMem25
 
Presentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptxPresentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptx
rameshmanoj733
 
Performance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic NotationsPerformance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic Notations
DrSMeenakshiSundaram1
 
IntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptxIntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptx
prasanna220904
 
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
rajesshs31r
 
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
rajesshs31r
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 

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
Sheba41
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
MapReduce.pptx
MapReduce.pptxMapReduce.pptx
MapReduce.pptx
Sheba41
 
pig.ppt
pig.pptpig.ppt
pig.ppt
Sheba41
 
HadoooIO.ppt
HadoooIO.pptHadoooIO.ppt
HadoooIO.ppt
Sheba41
 
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
Sheba41
 
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
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.ppt
Sheba41
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
MapReduce.pptx
MapReduce.pptxMapReduce.pptx
MapReduce.pptx
Sheba41
 
HadoooIO.ppt
HadoooIO.pptHadoooIO.ppt
HadoooIO.ppt
Sheba41
 
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
Sheba41
 
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
Sheba41
 
Ad

Recently uploaded (20)

How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Ad

CP4151 Advanced data structures and algorithms

  • 1. 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? 2  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 Algorithm Input Output 3  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. 4  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 5  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 6  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. From Algorithms to Programs Problem Algorithm Algorithm: A sequence of instructions describing how to do a task (or process) 8 C++ Program C++ Program
  • 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. 9 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 10  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 11  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 12  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 min = T[1] 13  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 14 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) { 15 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) 16 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 17  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 18  Suppose  Computer A execute one billion (109) instructions per second  Computer B execute 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 19 50 . 10 . lg(10 ) 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. 20 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] 21 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 attributes 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]).