SlideShare a Scribd company logo
Introduction to Algorithms
Dr. Kiran K
Associate Professor
Department of CSE
UVCE
Bengaluru, India.
What is an Algorithm ?
• A sequence of Unambiguous Instructions for Solving a Problem, i.e., for Obtaining
a required Output for any Legitimate Input in a Finite Amount of Time.
• A Finite Set of Instructions that, if followed, Accomplishes a particular Task.
• In addition, all algorithms must satisfy the following criteria:
1. Input :
2. Output :
3. Definiteness :
4. Finiteness :
5. Effectiveness :
• Abu Ja’far Mohammed Ibn Musa al Khowarizmi – Persian Mathematician (825 A.D)
Zero or More.
At Least One.
Clear and Unambiguous.
Terminates after a finite number of steps.
Every Instruction must be very Basic.
Why Study Algorithms ?
• Practical Standpoint:
 It is necessary to Know a Standard Set of Important Algorithms from Different
Areas of Computing.
 Design New Algorithms and Analyze their Efficiency.
• Theoretical Standpoint:
 Algorithmics (Study of Algorithms)
 Recognized as the Cornerstone of Computer Science.
 Relevant to Science, Business, and Technology, etc.
 Develops Analytical Skills.
Notion of an Algorithm
Examples Illustrating the Notion of Algorithm
Greatest Common Divisor (GCD) of Two Integers m and n
• Three Methods to solve illustrating the following important points:
 Non-Ambiguity.
 Range of Inputs.
 Representing the same algorithm in Several Different Ways.
 Several Algorithms for solving the Same Problem.
 Algorithms for the same problem can be based on Very Different Ideas and can
solve the problem with Dramatically Different Speeds.
Examples Illustrating the Notion of Algorithm…
1. Middle-School Procedure
Step 1: Find the Prime Factors of the First number, m.
Step 2: Find the Prime Factors of the Second number, n.
Step 3: Identify all the Common Factors in the two prime expansions found in
Step 1 and Step 2.
Step 4: Compute the Product of All the Common Factors and return it as the GCD.
GCD (60, 24):
Prime Factors of 60
Prime Factors of 24
Common Factors
Product of Common Factors
= 2 . 2 . 3 . 5
= 2 . 2 . 2 . 3
= 2, 2 and 3
= 2 * 2 * 3 → GCD (60, 24)
= 12
Examples Illustrating the Notion of Algorithm…
2. Euclid’s Algorithm – Euclid of Alexandria (3rd Century B.C)
GCD (60, 24):
GCD (60, 24)
GCD (24, 12)
n = 0
Euclid’s Algorithm for Computing GCD of m and n
Step 1: If n = 0, return the value of m as the GCD and stop;
otherwise, proceed to Step 2.
Step 2: Divide m by n and assign the value of the Remainder to r.
Step 3: Assign the value of n to m and the value of r to n.
Go to Step 1.
Algorithm Euclid(m, n)
while (n ≠ 0) do
r ← m mod n
m ← n
n ← r
Return m
= GCD (24, 60 mod 24)
= GCD (12, 24 mod 12)
→ GCD = 12
= GCD (24, 12)
= GCD (12, 0)
- GCD (m, n) = GCD (n, m mod n)
Examples Illustrating the Notion of Algorithm…
3. Consecutive Integer Checking Algorithm
Common Divisor cannot be Greater than the Smaller of the Two Numbers.
Consecutive Integer Checking Algorithm for Computing GCD of m and n
Step 1: Assign the value of min{m, n} to t
Step 2: Divide m by t. If the Remainder of this division is 0, go to Step 3;
otherwise, go to Step 4.
Step 3: Divide n by t. If the Remainder of this division is 0, return the
value of t as the GCD and stop; otherwise, proceed to Step 4.
Step 4: Decrease the value of t by 1. Go to Step 2.
Algorithm Euclid(m, n)
1 t ← min (m, n)
2 if (m % t = 0)
goto 3
else
goto 4
3 if (n % t = 0)
return t
else
goto 4
4 t ← t - 1
5 goto 2
Examples Illustrating the Notion of Algorithm…
GCD (60, 24):
Min (60, 24) = 24
60 % 24 ≠ 0
60 % 23 ≠ 0
60 % 22 ≠ 0
60 % 21 ≠ 0
60 % 20 = 0
60 % 19 ≠ 0
60 % 18 ≠ 0
60 % 17 ≠ 0
60 % 16 ≠ 0
60 % 15 = 0
60 % 14 ≠ 0
60 % 13 ≠ 0
60 % 12 = 0
24 % 20 ≠ 0
24 % 15 ≠ 0
24 % 12 = 0 → GCD = 12
Examples Illustrating the Notion of Algorithm…
Comparison:
Range of Inputs Ambiguity Speed
> = 1
> = 0
> = 1 -
-
Steps 1, 2 and 3 Moderate
Fast
Slow
Middle-School Procedure
Euclid’s Algorithm
Consecutive Integer Checking Algorithm
Sieve of Eratosthenes
• Invented in Ancient Greece (200 B.C)
• Generates Consecutive Prime Numbers not exceeding Integer n > 1.
Sieve of Eratosthenes for Generating Prime Numbers up to n
Step 1: Initialize a list of Prime Candidates with consecutive
integers from 2 to n.
Step 2: Eliminate all Multiples of 2.
Step 3: Eliminate all Multiples of the Next Integer Remaining in
the list.
Step 4: Continue in this fashion until no more numbers can be
Eliminated.
Step 5: The Remaining Integers are the Primes needed.
Algorithm Seive (n)
For (p ← 2 to n) do A[p] ← p
For (p ←2 to 𝑛 ) do
If (A[p] ≠ 0)
j ← p ∗ p
While ( j ≤ n) do
A[j ] ← 0
j ← j + p
i ← 0
For (p ← 2 to n) do
If (A[p] ≠ 0)
L[i] ← A[p]
i ← i + 1
Return L
Note:
If p is a number whose multiples are being eliminated, then the First
Multiple existing is p * p because all smaller multiples. 2p, . . , (p − 1) p
have been eliminated on earlier passes through the list.
→ p * p Not Greater than n → p < 𝑛
Examples Illustrating the Notion of Algorithm…
Generate Prime Numbers up to 25
Initialize List with Integers from 2 to 25
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
Consider the First Integer in the List - 2
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
Eliminate Multiples of 2
2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
Examples Illustrating the Notion of Algorithm…
2, 3, 5, 7, 9, 11, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 23, 24, 25.
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 24, 25.
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25.
All Multiples of 2 are Eliminated
The Next Integer Existing in the List - 3
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25.
Eliminate Multiples of 3
2, 3, 5, 7, 11, 13, 15, 17, 19, 21, 23, 25.
2, 3, 5, 7, 11, 13, 15, 17, 19, 21, 23, 25.
2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 25.
Examples Illustrating the Notion of Algorithm…
2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 25.
2, 3, 5, 7, 11, 13, 17, 19, 23, 25.
2, 3, 5, 7, 11, 13, 17, 19, 23, 25.
All Multiples of 3 are Eliminated
The Next Integer Existing in the List - 5
2, 3, 5, 7, 11, 13, 17, 19, 23, 25.
Eliminate Multiples of 5
2, 3, 5, 7, 11, 13, 17, 19, 23, .
The Next Integer Existing in the List - 7
7 * 7 = 49 is Greater than n. Hence Stop.
Remaining Elements: 2, 3, 5, 7, 11, 13, 17, 19, 23. → Prime Numbers up to 25
Fundamentals of Algorithmic Problem Solving
Algorithm Design and Analysis Process
Fundamentals of Algorithmic Problem Solving…
Understanding the Problem:
• Read the Problem’s Description carefully and Ask Questions in case of any Doubts.
• Do a Few Small Examples by Hand.
• Think about Special Cases, and Ask Questions again if needed.
• If the Problem is one of the Type that Arises in computing applications quite often:
 Use a known Algorithm to solve it. it helps to understand how such an algorithm works
and to know its strengths and weaknesses.
 If a algorithm is Not Readily Available, Design a New Algorithm.
• Specify exactly the Set of Instances the algorithm needs to handle.
 Instance – An Input the algorithm solves.
 If Not Specified Correctly, the algorithm may Crash on some Boundary Value.
Note:
A Correct Algorithm is not one that works most of the time, but
one that Works Correctly for All Legitimate Inputs.
Fundamentals of Algorithmic Problem Solving…
Ascertaining the Capabilities of the Computational Device:
• Random-Access Machines – John Von Neumann
 Instructions are Executed one after another, One Operation at a Time.
 Design Sequential Algorithms.
• Parallel Machines
 Can Execute Operations Concurrently, i.e., in Parallel.
 Design Parallel Algorithms.
• Memory and Speed:
 Scientific Purposes:
 No Need to worry
Algorithms are studied in terms Independent of specification parameters for a Particular Computer.
 Practical Purposes:
 Depends on the Problem
o In many situations need not worry.
o If Problems are Very Complex, or have to Process Huge Volumes of Data, or deal with applications
where the Time is Critical, Memory and Speed availability are Crucial.
Fundamentals of Algorithmic Problem Solving…
Choosing between Exact and Approximate Problem Solving:
• Solve a Problem Exactly – Exact Algorithm
• Solve a Problem Approximately – Approximation Algorithm
 Reasons for Developing Approximation Algorithm:
 Problems Cannot be Solved Exactly. Eg.: Extracting Square Roots, Solving Nonlinear
Equations, Evaluating Definite Integrals, etc.
 Available algorithms for solving a problem exactly can be Unacceptably Slow because
of the problem’s intrinsic complexity.
 An Approximation algorithm can be a Part of a More Sophisticated Algorithm that
solves a problem exactly.
Fundamentals of Algorithmic Problem Solving…
Algorithm Design Techniques:
• A General Approach to solving problems algorithmically.
• They provide Guidance for designing algorithms for New Problems.
• Make it possible to Classify Algorithms according to an Underlying Design Idea; therefore,
they can serve as a natural way to both Categorize and Study algorithms.
Designing an Algorithm and Data Structures:
• Designing an Algorithm – Challenging Task.
 Some Design Techniques can be Inapplicable to the problem in question.
 Several Techniques may be Combined - Hard to pinpoint algorithms as applications of
known design techniques.
 Particular Design Technique is Applicable - Requires a Nontrivial Ingenuity.
 Choosing among the general techniques and Applying them gets Easier With Practice.
Fundamentals of Algorithmic Problem Solving…
• Data Structures – Challenging Task.
 Choose data structures Appropriate for the Operations Performed by the algorithm.
Eg.: Sieve of Eratosthenes runs longer if linked list is used instead of an array.
 Some algorithm design techniques depend intimately on Structuring or Restructuring data
specifying a problem’s instance.
 Data structures remain crucially Important for both Design and Analysis of algorithms.
 Algorithms + Data Structures = Programs.
Methods of Specifying an Algorithm:
1. Natural Language:
 Important Skill one should develop.
 Inherent Ambiguity makes a succinct and clear description surprisingly Difficult.
Fundamentals of Algorithmic Problem Solving…
2. Pseudocode:
 Mixture of a Natural Language and Programming Language like constructs.
 More Precise than natural language.
 Usage often yields More Succinct algorithm descriptions.
 No Single Form leaving authors with a need to design their Own Dialects.
3. Flowchart:
 Collection of Connected Geometric Shapes containing descriptions of the algorithm’s steps.
 Dominant in the earlier days of computing.
 Inconvenient except for very simple algorithms.
4. Program:
 Written in a particular Computer Language.
 Considered as the Algorithm’s Implementation.
Fundamentals of Algorithmic Problem Solving…
Proving an Algorithm’s Correctness:
• Prove that the algorithm yields a Required Result for Every Legitimate Input in a finite
amount of time.
• For some algorithms, a proof of correctness is quite easy; for others, it can be quite complex.
• Common Technique – Mathematical Induction.
• For an Approximation Algorithm, show that the error produced by the algorithm does not
Exceed a Predefined Limit.
Note:
• Although Tracing the algorithm’s performance for a few specific inputs can be a very
worthwhile activity, it Cannot Prove the Algorithm’s Correctness Conclusively.
• In order to show that an algorithm is Incorrect, just One Instance of its input for which the
Algorithm Fails is Sufficient.
Fundamentals of Algorithmic Problem Solving…
Analyzing an Algorithm:
• Qualities an algorithm must possess:
1. Correctness
2. Efficiency
 Time Efficiency – How Fast the algorithm runs.
 Space Efficiency – How much Extra Memory an algorithm uses.
3. Simplicity
 Simpler algorithms are Easier to Understand and easier to Program.
 Resulting programs usually contain Fewer Bugs.
 Has Aesthetic appeal.
 Sometimes simpler algorithms are More Efficient than more complicated alternatives.
 Cannot be precisely Defined and Investigated with mathematical rigor.
Fundamentals of Algorithmic Problem Solving…
4. Generality
a) Generality of the Problem the algorithm Solves
 Sometimes it is Easier to design an algorithm for a problem posed in more general
terms.
Eg.: Determine whether two integers are Relatively Prime:
 Design an algorithm for a more general problem of computing the GCD of
two integers.
 Solve the former problem by checking whether the GCD is 1 or not.
 Sometimes designing a more general algorithm is Unnecessary or Difficult or even
Impossible
Eg.: Find the roots of a Quadratic Equation
 Cannot be generalized to handle Polynomials of Arbitrary Degrees.
Fundamentals of Algorithmic Problem Solving…
b) Generality of the Set of Inputs an algorithm Accepts.
 Design an algorithm that can handle a Set of Inputs that is Natural for the
problem at hand.
Eg.: (a) Finding GCD
 Excluding 1 as input is Unnatural.
(b) Find the roots of a Quadratic Equation
 Not implemented for Complex Coefficients unless this capability is
explicitly required.
Coding an Algorithm:
 Presents both a Peril and an Opportunity.
 Peril - Possibility of making the Transition from an algorithm to a program either
Incorrectly or very Inefficiently.
Modern compilers can be used in code optimization mode to avoid Inefficient implementation.
Fundamentals of Algorithmic Problem Solving…
 Unless the Correctness is proven with full Mathematical Rigor, the program cannot be
considered Correct.
 Practically the validity of programs is established by Testing.
 Provide Verification to check whether Inputs belong to the Specified Sets.
 Use Standard Tricks as computing a loop’s invariant outside the loop, collecting
common subexpressions, replacing expensive operations by cheap ones, and so on.
Important Problem Types
1. Sorting
2. Searching
3. String processing
4. Graph problems
5. Combinatorial problems
6. Geometric problems
7. Numerical problems
Important Problem Types…
1. Sorting
• Rearrange the items of a given list in Order.
Eg.: Numbers, Character Strings, Records, etc.
• Nature of the list items must Allow such an Ordering.
Eg.: Number – Value, Character Strings – Alphabet Order.
• Key - a piece of information used to Guide Sorting.
Eg.: Numbers – Number itself, Records – Student No., Name, etc.
• Sorting makes many Questions about the list Easier to answer. Eg:. Searching.
• Sorting is used as an Auxiliary Step in several important algorithms. Eg.: Geometric
algorithms and Data Compression.
• There are a few good sorting algorithms that sort an arbitrary array of size n using
about n log2 n comparisons.
• No algorithm that sorts by key comparisons can do better than n log2 n comparisons.
Important Problem Types…
• Properties of Sorting Algorithms:
1. Stable
 Preserves the Relative Order of any two Equal Elements in its input.
 If an Input List contains Two Equal Elements in Positions i and j where i < j, then
in the Sorted List they have to be in positions i‘ and j’ respectively, such that i’ < j’.
Eg.: Sort a list of students alphabetically and sort it according to student GPA:
A stable algorithm will yield a list in which students with the same GPA will
still be sorted alphabetically.
 Algorithms that can Exchange Keys located far apart are Not Stable, but they
usually work faster.
2. In-Place
 Does not require Extra Memory, except, possibly, for a few memory units.
Important Problem Types…
2. Searching
• Finding a given value, called a search Key, in a given set.
• Range from the Straightforward Sequential Search to a spectacularly Efficient but
limited Binary Search and algorithms based on representing the underlying set in a
different form.
3. String Processing
• String - A Sequence of Characters from an alphabet.
Eg.: Text Strings – Letters, Numbers and Special Characters.
Bit Stings – Zeros and Ones.
Gene Sequences
• String Matching - Searching for a given Word in a Text.
Important Problem Types…
4. Graph Problems
• Graph – Collection of Points called Vertices, some of which are connected by Line
Segments called Edges.
 Graphs Applications - Transportation, Communication, Social and Economic
Networks, Project Scheduling, and Games.
• Graph Algorithms - Graph-Traversal, Shortest-Path, Topological Sorting, etc.
• Some graph problems are computationally very hard. Eg.: Graph-Coloring Problem,
Travelling Salesman Problem, etc.
5. Combinatorial Problems
• Require to find a combinatorial object such as a Permutation, Combination or Subset
that satisfies certain constraints.
• May also be required to have some Additional Property such as a maximum value or a
minimum cost.
Important Problem Types…
• Most Difficult problems in computing, from both a theoretical and practical standpoint
because:
 The Number typically Grows Extremely Fast with a problem’s size, reaching
unimaginable magnitudes even for moderate-sized instances.
 There are No Known Algorithms for solving most such problems exactly in an
acceptable amount of time
6. Geometric Problems
• Geometric algorithms deal with geometric objects such as Points, Lines, and
Polygons.
• Geometric algorithms applications – Computer Graphics, Robotics, and Tomography.
7. Numerical Problems
• Involve mathematical objects of Continuous Nature. Eg.: Solving equations and
systems of equations, computing definite integrals, evaluating functions, etc.
Important Problem Types…
7. Numerical Problems
• Involve mathematical objects of Continuous Nature. Eg.: Solving equations and
systems of equations, computing definite integrals, evaluating functions, etc.
• Play a critical role in many Scientific and Engineering applications.
• Majority of such mathematical problems can be solved only Approximately.
• Typically require manipulating Real Numbers, which can be represented in a
computer only approximately.
• A large number of arithmetic operations performed on approximately represented
numbers can lead to an Accumulation of the Round-off Error to a point where it can
drastically distort an output.
References:
• Anany Levitin, Introduction to the Design and Analysis of Algorithms, 3rd Edition,
2012, Pearson Education.
• Ellis Horowitz, Sartaj Sahni, and Sanguthevar Rajasekaran, Computer Algorithms,
1997, Computer Science Press.
Ad

More Related Content

What's hot (20)

Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
Dakshitha Dissanayaka
 
1.2. introduction to automata theory
1.2. introduction to automata theory1.2. introduction to automata theory
1.2. introduction to automata theory
Sampath Kumar S
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
swapnac12
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
sangeetha s
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
DataminingTools Inc
 
Ch10 Recursion
Ch10 RecursionCh10 Recursion
Ch10 Recursion
leminhvuong
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
ssuserb7c8b8
 
Eclat algorithm in association rule mining
Eclat algorithm in association rule miningEclat algorithm in association rule mining
Eclat algorithm in association rule mining
Deepa Jeya
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Shweta Sahu
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Shaista Qadir
 
Linear Search
Linear SearchLinear Search
Linear Search
SWATHIR72
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
vikas dhakane
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introduction
Mohamed Gad
 
14 Skip Lists
14 Skip Lists14 Skip Lists
14 Skip Lists
Andres Mendez-Vazquez
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Dr Shashikant Athawale
 
1.2. introduction to automata theory
1.2. introduction to automata theory1.2. introduction to automata theory
1.2. introduction to automata theory
Sampath Kumar S
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
swapnac12
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
sangeetha s
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
ssuserb7c8b8
 
Eclat algorithm in association rule mining
Eclat algorithm in association rule miningEclat algorithm in association rule mining
Eclat algorithm in association rule mining
Deepa Jeya
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Shaista Qadir
 
Linear Search
Linear SearchLinear Search
Linear Search
SWATHIR72
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
vikas dhakane
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introduction
Mohamed Gad
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 

Similar to Introduction to Algorithm Design and Analysis.pdf (20)

Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
Low Ying Hao
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu
 
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptxBCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
VENKATESHBHAT25
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
GOWTHAMR721887
 
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
daa_unit THIS IS GNDFJG SDGSGS SFDF .pptdaa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
DrKBManwade
 
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
 
Notion of Algorithms.pdf
Notion of Algorithms.pdfNotion of Algorithms.pdf
Notion of Algorithms.pdf
ShivareddyGangam
 
Analysis Framework, Asymptotic Notations
Analysis Framework, Asymptotic NotationsAnalysis Framework, Asymptotic Notations
Analysis Framework, Asymptotic Notations
DrSMeenakshiSundaram1
 
1.introduction analysis and design of algorithms(1).pptx
1.introduction  analysis and design of algorithms(1).pptx1.introduction  analysis and design of algorithms(1).pptx
1.introduction analysis and design of algorithms(1).pptx
2021ismadhuprasadrna
 
Presentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptxPresentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptx
rameshmanoj733
 
Design and Analysis of Algorithms ppt by K. Adi
Design and Analysis of Algorithms ppt by K. AdiDesign and Analysis of Algorithms ppt by K. Adi
Design and Analysis of Algorithms ppt by K. Adi
Prof. Dr. K. Adisesha
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
ADA_Module 1_MN.pptx- Analysis and design of AlgorithmsADA_Module 1_MN.pptx- Analysis and design of Algorithms
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
madhu614742
 
Chapter one
Chapter oneChapter one
Chapter one
mihiretu kassaye
 
Unit i
Unit iUnit i
Unit i
Gunasundari Selvaraj
 
Unit i
Unit iUnit i
Unit i
GunasundariSelvaraj
 
IntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptxIntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptx
prasanna220904
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
jinalgoti
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
B.Kirron Reddi
 
ADA complete notes
ADA complete notesADA complete notes
ADA complete notes
Vinay Kumar C
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
Low Ying Hao
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu
 
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptxBCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
BCS401 ADA Module 1 PPT 2024-25 IV SEM.pptx
VENKATESHBHAT25
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
GOWTHAMR721887
 
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
daa_unit THIS IS GNDFJG SDGSGS SFDF .pptdaa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
DrKBManwade
 
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
 
Analysis Framework, Asymptotic Notations
Analysis Framework, Asymptotic NotationsAnalysis Framework, Asymptotic Notations
Analysis Framework, Asymptotic Notations
DrSMeenakshiSundaram1
 
1.introduction analysis and design of algorithms(1).pptx
1.introduction  analysis and design of algorithms(1).pptx1.introduction  analysis and design of algorithms(1).pptx
1.introduction analysis and design of algorithms(1).pptx
2021ismadhuprasadrna
 
Presentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptxPresentation_23953_Content_Document_20240906040454PM.pptx
Presentation_23953_Content_Document_20240906040454PM.pptx
rameshmanoj733
 
Design and Analysis of Algorithms ppt by K. Adi
Design and Analysis of Algorithms ppt by K. AdiDesign and Analysis of Algorithms ppt by K. Adi
Design and Analysis of Algorithms ppt by K. Adi
Prof. Dr. K. Adisesha
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
ADA_Module 1_MN.pptx- Analysis and design of AlgorithmsADA_Module 1_MN.pptx- Analysis and design of Algorithms
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
madhu614742
 
IntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptxIntroductionToAlgo_v1_1709293290768 (2).pptx
IntroductionToAlgo_v1_1709293290768 (2).pptx
prasanna220904
 
Ad

More from Kiran K (8)

String Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmString Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Kiran K
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
Kiran K
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
Kiran K
 
Naive string matching algorithm
Naive string matching algorithmNaive string matching algorithm
Naive string matching algorithm
Kiran K
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
Kiran K
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 
Bellman ford
Bellman fordBellman ford
Bellman ford
Kiran K
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Kiran K
 
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmString Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Kiran K
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
Kiran K
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
Kiran K
 
Naive string matching algorithm
Naive string matching algorithmNaive string matching algorithm
Naive string matching algorithm
Kiran K
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
Kiran K
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 
Bellman ford
Bellman fordBellman ford
Bellman ford
Kiran K
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Kiran K
 
Ad

Recently uploaded (20)

RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
"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
 
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
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
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
 
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
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
"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
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
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
 
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
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 

Introduction to Algorithm Design and Analysis.pdf

  • 1. Introduction to Algorithms Dr. Kiran K Associate Professor Department of CSE UVCE Bengaluru, India.
  • 2. What is an Algorithm ? • A sequence of Unambiguous Instructions for Solving a Problem, i.e., for Obtaining a required Output for any Legitimate Input in a Finite Amount of Time. • A Finite Set of Instructions that, if followed, Accomplishes a particular Task. • In addition, all algorithms must satisfy the following criteria: 1. Input : 2. Output : 3. Definiteness : 4. Finiteness : 5. Effectiveness : • Abu Ja’far Mohammed Ibn Musa al Khowarizmi – Persian Mathematician (825 A.D) Zero or More. At Least One. Clear and Unambiguous. Terminates after a finite number of steps. Every Instruction must be very Basic.
  • 3. Why Study Algorithms ? • Practical Standpoint:  It is necessary to Know a Standard Set of Important Algorithms from Different Areas of Computing.  Design New Algorithms and Analyze their Efficiency. • Theoretical Standpoint:  Algorithmics (Study of Algorithms)  Recognized as the Cornerstone of Computer Science.  Relevant to Science, Business, and Technology, etc.  Develops Analytical Skills.
  • 4. Notion of an Algorithm
  • 5. Examples Illustrating the Notion of Algorithm Greatest Common Divisor (GCD) of Two Integers m and n • Three Methods to solve illustrating the following important points:  Non-Ambiguity.  Range of Inputs.  Representing the same algorithm in Several Different Ways.  Several Algorithms for solving the Same Problem.  Algorithms for the same problem can be based on Very Different Ideas and can solve the problem with Dramatically Different Speeds.
  • 6. Examples Illustrating the Notion of Algorithm… 1. Middle-School Procedure Step 1: Find the Prime Factors of the First number, m. Step 2: Find the Prime Factors of the Second number, n. Step 3: Identify all the Common Factors in the two prime expansions found in Step 1 and Step 2. Step 4: Compute the Product of All the Common Factors and return it as the GCD. GCD (60, 24): Prime Factors of 60 Prime Factors of 24 Common Factors Product of Common Factors = 2 . 2 . 3 . 5 = 2 . 2 . 2 . 3 = 2, 2 and 3 = 2 * 2 * 3 → GCD (60, 24) = 12
  • 7. Examples Illustrating the Notion of Algorithm… 2. Euclid’s Algorithm – Euclid of Alexandria (3rd Century B.C) GCD (60, 24): GCD (60, 24) GCD (24, 12) n = 0 Euclid’s Algorithm for Computing GCD of m and n Step 1: If n = 0, return the value of m as the GCD and stop; otherwise, proceed to Step 2. Step 2: Divide m by n and assign the value of the Remainder to r. Step 3: Assign the value of n to m and the value of r to n. Go to Step 1. Algorithm Euclid(m, n) while (n ≠ 0) do r ← m mod n m ← n n ← r Return m = GCD (24, 60 mod 24) = GCD (12, 24 mod 12) → GCD = 12 = GCD (24, 12) = GCD (12, 0) - GCD (m, n) = GCD (n, m mod n)
  • 8. Examples Illustrating the Notion of Algorithm… 3. Consecutive Integer Checking Algorithm Common Divisor cannot be Greater than the Smaller of the Two Numbers. Consecutive Integer Checking Algorithm for Computing GCD of m and n Step 1: Assign the value of min{m, n} to t Step 2: Divide m by t. If the Remainder of this division is 0, go to Step 3; otherwise, go to Step 4. Step 3: Divide n by t. If the Remainder of this division is 0, return the value of t as the GCD and stop; otherwise, proceed to Step 4. Step 4: Decrease the value of t by 1. Go to Step 2. Algorithm Euclid(m, n) 1 t ← min (m, n) 2 if (m % t = 0) goto 3 else goto 4 3 if (n % t = 0) return t else goto 4 4 t ← t - 1 5 goto 2
  • 9. Examples Illustrating the Notion of Algorithm… GCD (60, 24): Min (60, 24) = 24 60 % 24 ≠ 0 60 % 23 ≠ 0 60 % 22 ≠ 0 60 % 21 ≠ 0 60 % 20 = 0 60 % 19 ≠ 0 60 % 18 ≠ 0 60 % 17 ≠ 0 60 % 16 ≠ 0 60 % 15 = 0 60 % 14 ≠ 0 60 % 13 ≠ 0 60 % 12 = 0 24 % 20 ≠ 0 24 % 15 ≠ 0 24 % 12 = 0 → GCD = 12
  • 10. Examples Illustrating the Notion of Algorithm… Comparison: Range of Inputs Ambiguity Speed > = 1 > = 0 > = 1 - - Steps 1, 2 and 3 Moderate Fast Slow Middle-School Procedure Euclid’s Algorithm Consecutive Integer Checking Algorithm
  • 11. Sieve of Eratosthenes • Invented in Ancient Greece (200 B.C) • Generates Consecutive Prime Numbers not exceeding Integer n > 1. Sieve of Eratosthenes for Generating Prime Numbers up to n Step 1: Initialize a list of Prime Candidates with consecutive integers from 2 to n. Step 2: Eliminate all Multiples of 2. Step 3: Eliminate all Multiples of the Next Integer Remaining in the list. Step 4: Continue in this fashion until no more numbers can be Eliminated. Step 5: The Remaining Integers are the Primes needed. Algorithm Seive (n) For (p ← 2 to n) do A[p] ← p For (p ←2 to 𝑛 ) do If (A[p] ≠ 0) j ← p ∗ p While ( j ≤ n) do A[j ] ← 0 j ← j + p i ← 0 For (p ← 2 to n) do If (A[p] ≠ 0) L[i] ← A[p] i ← i + 1 Return L Note: If p is a number whose multiples are being eliminated, then the First Multiple existing is p * p because all smaller multiples. 2p, . . , (p − 1) p have been eliminated on earlier passes through the list. → p * p Not Greater than n → p < 𝑛
  • 12. Examples Illustrating the Notion of Algorithm… Generate Prime Numbers up to 25 Initialize List with Integers from 2 to 25 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. Consider the First Integer in the List - 2 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. Eliminate Multiples of 2 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.
  • 13. Examples Illustrating the Notion of Algorithm… 2, 3, 5, 7, 9, 11, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 23, 24, 25. 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 24, 25. 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25. All Multiples of 2 are Eliminated The Next Integer Existing in the List - 3 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25. Eliminate Multiples of 3 2, 3, 5, 7, 11, 13, 15, 17, 19, 21, 23, 25. 2, 3, 5, 7, 11, 13, 15, 17, 19, 21, 23, 25. 2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 25.
  • 14. Examples Illustrating the Notion of Algorithm… 2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 25. 2, 3, 5, 7, 11, 13, 17, 19, 23, 25. 2, 3, 5, 7, 11, 13, 17, 19, 23, 25. All Multiples of 3 are Eliminated The Next Integer Existing in the List - 5 2, 3, 5, 7, 11, 13, 17, 19, 23, 25. Eliminate Multiples of 5 2, 3, 5, 7, 11, 13, 17, 19, 23, . The Next Integer Existing in the List - 7 7 * 7 = 49 is Greater than n. Hence Stop. Remaining Elements: 2, 3, 5, 7, 11, 13, 17, 19, 23. → Prime Numbers up to 25
  • 15. Fundamentals of Algorithmic Problem Solving Algorithm Design and Analysis Process
  • 16. Fundamentals of Algorithmic Problem Solving… Understanding the Problem: • Read the Problem’s Description carefully and Ask Questions in case of any Doubts. • Do a Few Small Examples by Hand. • Think about Special Cases, and Ask Questions again if needed. • If the Problem is one of the Type that Arises in computing applications quite often:  Use a known Algorithm to solve it. it helps to understand how such an algorithm works and to know its strengths and weaknesses.  If a algorithm is Not Readily Available, Design a New Algorithm. • Specify exactly the Set of Instances the algorithm needs to handle.  Instance – An Input the algorithm solves.  If Not Specified Correctly, the algorithm may Crash on some Boundary Value. Note: A Correct Algorithm is not one that works most of the time, but one that Works Correctly for All Legitimate Inputs.
  • 17. Fundamentals of Algorithmic Problem Solving… Ascertaining the Capabilities of the Computational Device: • Random-Access Machines – John Von Neumann  Instructions are Executed one after another, One Operation at a Time.  Design Sequential Algorithms. • Parallel Machines  Can Execute Operations Concurrently, i.e., in Parallel.  Design Parallel Algorithms. • Memory and Speed:  Scientific Purposes:  No Need to worry Algorithms are studied in terms Independent of specification parameters for a Particular Computer.  Practical Purposes:  Depends on the Problem o In many situations need not worry. o If Problems are Very Complex, or have to Process Huge Volumes of Data, or deal with applications where the Time is Critical, Memory and Speed availability are Crucial.
  • 18. Fundamentals of Algorithmic Problem Solving… Choosing between Exact and Approximate Problem Solving: • Solve a Problem Exactly – Exact Algorithm • Solve a Problem Approximately – Approximation Algorithm  Reasons for Developing Approximation Algorithm:  Problems Cannot be Solved Exactly. Eg.: Extracting Square Roots, Solving Nonlinear Equations, Evaluating Definite Integrals, etc.  Available algorithms for solving a problem exactly can be Unacceptably Slow because of the problem’s intrinsic complexity.  An Approximation algorithm can be a Part of a More Sophisticated Algorithm that solves a problem exactly.
  • 19. Fundamentals of Algorithmic Problem Solving… Algorithm Design Techniques: • A General Approach to solving problems algorithmically. • They provide Guidance for designing algorithms for New Problems. • Make it possible to Classify Algorithms according to an Underlying Design Idea; therefore, they can serve as a natural way to both Categorize and Study algorithms. Designing an Algorithm and Data Structures: • Designing an Algorithm – Challenging Task.  Some Design Techniques can be Inapplicable to the problem in question.  Several Techniques may be Combined - Hard to pinpoint algorithms as applications of known design techniques.  Particular Design Technique is Applicable - Requires a Nontrivial Ingenuity.  Choosing among the general techniques and Applying them gets Easier With Practice.
  • 20. Fundamentals of Algorithmic Problem Solving… • Data Structures – Challenging Task.  Choose data structures Appropriate for the Operations Performed by the algorithm. Eg.: Sieve of Eratosthenes runs longer if linked list is used instead of an array.  Some algorithm design techniques depend intimately on Structuring or Restructuring data specifying a problem’s instance.  Data structures remain crucially Important for both Design and Analysis of algorithms.  Algorithms + Data Structures = Programs. Methods of Specifying an Algorithm: 1. Natural Language:  Important Skill one should develop.  Inherent Ambiguity makes a succinct and clear description surprisingly Difficult.
  • 21. Fundamentals of Algorithmic Problem Solving… 2. Pseudocode:  Mixture of a Natural Language and Programming Language like constructs.  More Precise than natural language.  Usage often yields More Succinct algorithm descriptions.  No Single Form leaving authors with a need to design their Own Dialects. 3. Flowchart:  Collection of Connected Geometric Shapes containing descriptions of the algorithm’s steps.  Dominant in the earlier days of computing.  Inconvenient except for very simple algorithms. 4. Program:  Written in a particular Computer Language.  Considered as the Algorithm’s Implementation.
  • 22. Fundamentals of Algorithmic Problem Solving… Proving an Algorithm’s Correctness: • Prove that the algorithm yields a Required Result for Every Legitimate Input in a finite amount of time. • For some algorithms, a proof of correctness is quite easy; for others, it can be quite complex. • Common Technique – Mathematical Induction. • For an Approximation Algorithm, show that the error produced by the algorithm does not Exceed a Predefined Limit. Note: • Although Tracing the algorithm’s performance for a few specific inputs can be a very worthwhile activity, it Cannot Prove the Algorithm’s Correctness Conclusively. • In order to show that an algorithm is Incorrect, just One Instance of its input for which the Algorithm Fails is Sufficient.
  • 23. Fundamentals of Algorithmic Problem Solving… Analyzing an Algorithm: • Qualities an algorithm must possess: 1. Correctness 2. Efficiency  Time Efficiency – How Fast the algorithm runs.  Space Efficiency – How much Extra Memory an algorithm uses. 3. Simplicity  Simpler algorithms are Easier to Understand and easier to Program.  Resulting programs usually contain Fewer Bugs.  Has Aesthetic appeal.  Sometimes simpler algorithms are More Efficient than more complicated alternatives.  Cannot be precisely Defined and Investigated with mathematical rigor.
  • 24. Fundamentals of Algorithmic Problem Solving… 4. Generality a) Generality of the Problem the algorithm Solves  Sometimes it is Easier to design an algorithm for a problem posed in more general terms. Eg.: Determine whether two integers are Relatively Prime:  Design an algorithm for a more general problem of computing the GCD of two integers.  Solve the former problem by checking whether the GCD is 1 or not.  Sometimes designing a more general algorithm is Unnecessary or Difficult or even Impossible Eg.: Find the roots of a Quadratic Equation  Cannot be generalized to handle Polynomials of Arbitrary Degrees.
  • 25. Fundamentals of Algorithmic Problem Solving… b) Generality of the Set of Inputs an algorithm Accepts.  Design an algorithm that can handle a Set of Inputs that is Natural for the problem at hand. Eg.: (a) Finding GCD  Excluding 1 as input is Unnatural. (b) Find the roots of a Quadratic Equation  Not implemented for Complex Coefficients unless this capability is explicitly required. Coding an Algorithm:  Presents both a Peril and an Opportunity.  Peril - Possibility of making the Transition from an algorithm to a program either Incorrectly or very Inefficiently. Modern compilers can be used in code optimization mode to avoid Inefficient implementation.
  • 26. Fundamentals of Algorithmic Problem Solving…  Unless the Correctness is proven with full Mathematical Rigor, the program cannot be considered Correct.  Practically the validity of programs is established by Testing.  Provide Verification to check whether Inputs belong to the Specified Sets.  Use Standard Tricks as computing a loop’s invariant outside the loop, collecting common subexpressions, replacing expensive operations by cheap ones, and so on.
  • 27. Important Problem Types 1. Sorting 2. Searching 3. String processing 4. Graph problems 5. Combinatorial problems 6. Geometric problems 7. Numerical problems
  • 28. Important Problem Types… 1. Sorting • Rearrange the items of a given list in Order. Eg.: Numbers, Character Strings, Records, etc. • Nature of the list items must Allow such an Ordering. Eg.: Number – Value, Character Strings – Alphabet Order. • Key - a piece of information used to Guide Sorting. Eg.: Numbers – Number itself, Records – Student No., Name, etc. • Sorting makes many Questions about the list Easier to answer. Eg:. Searching. • Sorting is used as an Auxiliary Step in several important algorithms. Eg.: Geometric algorithms and Data Compression. • There are a few good sorting algorithms that sort an arbitrary array of size n using about n log2 n comparisons. • No algorithm that sorts by key comparisons can do better than n log2 n comparisons.
  • 29. Important Problem Types… • Properties of Sorting Algorithms: 1. Stable  Preserves the Relative Order of any two Equal Elements in its input.  If an Input List contains Two Equal Elements in Positions i and j where i < j, then in the Sorted List they have to be in positions i‘ and j’ respectively, such that i’ < j’. Eg.: Sort a list of students alphabetically and sort it according to student GPA: A stable algorithm will yield a list in which students with the same GPA will still be sorted alphabetically.  Algorithms that can Exchange Keys located far apart are Not Stable, but they usually work faster. 2. In-Place  Does not require Extra Memory, except, possibly, for a few memory units.
  • 30. Important Problem Types… 2. Searching • Finding a given value, called a search Key, in a given set. • Range from the Straightforward Sequential Search to a spectacularly Efficient but limited Binary Search and algorithms based on representing the underlying set in a different form. 3. String Processing • String - A Sequence of Characters from an alphabet. Eg.: Text Strings – Letters, Numbers and Special Characters. Bit Stings – Zeros and Ones. Gene Sequences • String Matching - Searching for a given Word in a Text.
  • 31. Important Problem Types… 4. Graph Problems • Graph – Collection of Points called Vertices, some of which are connected by Line Segments called Edges.  Graphs Applications - Transportation, Communication, Social and Economic Networks, Project Scheduling, and Games. • Graph Algorithms - Graph-Traversal, Shortest-Path, Topological Sorting, etc. • Some graph problems are computationally very hard. Eg.: Graph-Coloring Problem, Travelling Salesman Problem, etc. 5. Combinatorial Problems • Require to find a combinatorial object such as a Permutation, Combination or Subset that satisfies certain constraints. • May also be required to have some Additional Property such as a maximum value or a minimum cost.
  • 32. Important Problem Types… • Most Difficult problems in computing, from both a theoretical and practical standpoint because:  The Number typically Grows Extremely Fast with a problem’s size, reaching unimaginable magnitudes even for moderate-sized instances.  There are No Known Algorithms for solving most such problems exactly in an acceptable amount of time 6. Geometric Problems • Geometric algorithms deal with geometric objects such as Points, Lines, and Polygons. • Geometric algorithms applications – Computer Graphics, Robotics, and Tomography. 7. Numerical Problems • Involve mathematical objects of Continuous Nature. Eg.: Solving equations and systems of equations, computing definite integrals, evaluating functions, etc.
  • 33. Important Problem Types… 7. Numerical Problems • Involve mathematical objects of Continuous Nature. Eg.: Solving equations and systems of equations, computing definite integrals, evaluating functions, etc. • Play a critical role in many Scientific and Engineering applications. • Majority of such mathematical problems can be solved only Approximately. • Typically require manipulating Real Numbers, which can be represented in a computer only approximately. • A large number of arithmetic operations performed on approximately represented numbers can lead to an Accumulation of the Round-off Error to a point where it can drastically distort an output.
  • 34. References: • Anany Levitin, Introduction to the Design and Analysis of Algorithms, 3rd Edition, 2012, Pearson Education. • Ellis Horowitz, Sartaj Sahni, and Sanguthevar Rajasekaran, Computer Algorithms, 1997, Computer Science Press.