100% (3) 100% found this document useful (3 votes) 8K views 405 pages Ada Textbook Skyward
The document outlines the fundamentals of algorithms, including their definitions, characteristics, and importance in problem-solving across various fields such as computer science, finance, and healthcare. It covers different algorithmic techniques such as brute force, divide and conquer, dynamic programming, and greedy methods, along with their applications and limitations. Additionally, it discusses the analysis of algorithm efficiency and the significance of data structures in algorithm design.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save ADA TEXTBOOK SKYWARD For Later
Total: 48 Hrs
Unit’ ; 2 a Durs|
Introduction: Algorithms, Fundamentals of Algorithmic Problem Solving, Important Problem Types,
Fundamental Data Structures.
Fundamentals of the Analysis of Algorithm Efficiency: The Analysis Framework, Asymptotic
Notations and Basic Efficiency Classes, Mathematical Analysis of Non-recursive and Recursive
Algorithms, Empirical Analysis of Algorithms
Unit=
Brute Force Method: Selection Sort and Bubble Sort, Sequential Search, Brute-Forcé
Exhaustive Search, Depth-First Search and Breadth-First Search.
-e String Matching,
Decrease and Conquer: Insertion Sort, Topological Sorting, Algorithms for Generating Combinatorial
Objects, Decrease-by-a-Constant-Factor Algorithms.
Divide and Conquer: Merge Sort, Quick Sort, Binary Tree Traversals and Related Properties,
Strassen’s Matrix Multiplication.
Space and Time Tradeoffs: Sorting by Counting, Input Enhancement in String Matching, Hashing.
Dynamic Programming: Binomial Coefficient, Principle of Optimality, Optimal Binary Search Trees,
Knapsack Problem and Memory Functions, Warshall’s and Floyd’s Algorithms.
Greedy Technique: Prim’s Algorithm, Kruskal’s Algorithm, Dijkstra’s Algorithm, Huffman Trees.
112 Hours]
Limitations of Algorithm Power: Lower-Bound Arguments, Decision Trees, P, NP and NP Complete
Problems.
Coping with the Limitations of Algorithm Power: Back Tracking: n Queens problem, Hamiltonian
Circuit Problem, Subset-Sum Problem. Branch-and-Bound: Assignment Problem, Knapsack Problem,
Traveling Salesman Problem.CONTENTS
tee Introduction to Algoritiim
1.1 Whatis an Algorithm?
1 -1.26]]
12
1.2 The Notion of the Algorithm ° 13
1.3. Characteristics of an Algorithm 13
1.4 What is Design and Analysis of Algorithms? Why We Need to Study? 14
1.5 Algorithm Examples 15
1.6 Fundamentals of Algorithm Problem Solving aa
1,7 Problem Types 114
1.8 Fundamental Data Structures 18
1.8.1 Classification of Data Structures 119
1.8.2 Primitive Data Structures 119
1.8.3 Non-Primitive Data Structures 120
Operations on Data Structures 124
Abstract Data Type (ADT) 124
Importance of Data Structures in Designing Algorithm 1.24
Review Questions 125
‘abier -2_ Fundamentals of the Analysis of Algorithm Eiliciency 24 - 2.42 a
2.1 The Analysis Framework 22
21.1 Space Complexity 22
21.2 Time Complexity 26
2.1.2.1 Measuring an Input's Size 27
2.1.2.2 Units for Measuring Running Time 27
2.13 Orders of Growth 211
21.4 Worst Case, Best Case and Average Case Efficiencies 23
2.2 Asymptotic Notations 215
2.2.1 Big O Notation (0) 2.16
2.2.2 Omega Notation (W) 219
2.2.3 Theta Notation(q) 2.20
2.2.4 Comparison of 0, W and q Notations 222
22.5 Using Limits for Comparing Orders of Growth ae
2.3. Basic Efficiency Classes ae
2.4 Whatis Iterative and Recursive Algorithms? =
2.5 Mathematical Analysis of Non-Recursive Algorithms a
25.1 General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms aa
2.5.2 Finding the Largest Element in an Array 228
2.5.3 Checking the Uniqueness in an Array
vi25.4 Matrix Multiplication 229
2.6 Mathematical Analysis of Recursive Algorithms 230
2.6.1 General Plan for Analysing the Time Efficiency of Recursive Algorithms 2a1
2.6.2 Factorial of a Number 2.31
2.6.3 Tower of Hanoi Problem 2.32
2.6.4 Fibonacci Series 234
2.7. Empirical Analysis of Algorithms 287
2.7. General Plan for the Empirical Analysis of Algorithm Time Efficiency 2.37
2.7.2 Advantages and Disadvantages of Empirical Analysis of Algorithms 2.39
2.7.3 Difference between Mathematical & Empirical Analysis 2.40
2.8. Review Questions 2.40
beoes Brute Force Method & Exhaustive Search 31-334]
3.1 Introduction 3.2
3.2 Brute Force Method 3.2
3.2.1 Selection Sort 3.3
3.2.2 Bubble Sort 3.9
3.2.3. Sequential Search or Linear Search 3.14
3.2.4 Brute-Force String Matching 3.16
3.2.5 Advantages and Disadvantages of Brute-Force Method 3.20
3.3 Exhaustive Search 3.20
3.3.1. Graph Traversals 3.21
3.3.2 Depth First Search (DFS) 3.21
3.3.3 Breadth First Search (BFS) 3.27
3.3.4 Advantages and Disadvantages of Exhaustive Search 3.32
3.4 Review Questions 3.32
Rene Decrease and Conquer at-434
44 Decrease-and-Conquer 42
42. Decrease-by-a-Constant 44
4.2.1 Insertion Sort 44
42.2 Topological Sorting 48
4.2.3 Algorithm for Generating Combinatorial Objects 4.16
4.2.3.1 Generating Permutations 4.16
4.2.3.2 Generating Subsets 4.19
4.3, Decrease-by-a-Constant-Factor 4.23
43. Binary Search 4.25
4.3.2 Fake-Coin Problem 4.28
43.3 Russian Peasant Multiplication 4.30
vii434 Josephus Problem 431
44. Advantages and Disadvantages of Decrease and Conquer Technique 4.32
45 Review Questions 433
54-532
5.1 Divide and Conquer Technique 52
5.2 Merge Sort Sa
53 Quick Sort 5.12
SA Binary Tree Traversal and Related Properties 5.20
5.4.1 Whatis Binary Tree? 5.20
5.4.2 Properties of Binary Tree 5.21
543 Binary Tree Traversals 5.22
5.5. Strassen's Matrix Multiplication 5.25
5.6 Advantages and Disadvantages of Divide and Conquer Technique 5.30
5.7 Review Questions 531
VA ‘Space and Time Tradeotis 61-630
apter - 6
6.1 Space and Time Tradeoffs 62
6.1.1 Precomputing or Input Enhancement 63
6.1.2 Prestructuring 63
6.2 Sorting by Counting 64
6.3 Input Enhancement in String Matching 6.10
63.1 Horspool’s Algorithm (Simplified Version of Boyer Moore Algorithm) 6.10
63.2. Boyer Moore Algorithm 644
6.4 Hashing 6.18
6.4.1 Hash Table 6.19
6.4.2 Hash Function dil
64.3 Hash Collision at
64.4 Choosing a Hash Function 623
64.5 Collision Resolution 625
6.4.5.1 Open Addressing 625
645.2 Collision Resolution by Chaining 630
6.4.6 Time and Space Complexity of Hashing 632
6.5 Review Questions 632
Baca Dynamic Programming 71-744
7.1 What is Dynamic Programming? by
7.2. Divide and Conquer Vs Dynamic Programming oa7.3. Applications of Dynamic Programming 73
74 ‘The Process of Solving Problems Using Dynamic Programming 74
75. Computing a Binomial Co-efficient 78
7.6 Principle of Optimality 79
7.7 Optimal Binary Search Tree 710
7.8 The Knapsack Problem 715
7.9 Memory Functions 7.21
7.10 Warshall’s Algorithm 7.26
7.11 Floyd's Algorithm (All Pairs Shortest Path Algorithm) 7.32
7.12. Advantages and Disadvantages of Dynamic Programming 7.40
7.13 Review Questions 741
Baaes Greedy Tectmique 81-838
8.1. Introduction to Greedy Technique a2
8.1.1 Characteristics of Greedy Technique 83
8.1.2 Control Abstraction For Greedy Method or General Procedure 83
8.1.3 Difference Between Dynamic Programming and Greedy Technique a4
8.14 Applications of Greedy Technique 84
8.2. Minimum Spanning Tree 85
8.2.1 Prim’s Algorithm 8.7
8.2.2. KRUSKAL'S Algorithm 8.12
8.2.3 Difference Between Prim’s and Kruskal’s Algorithm 8.17
8.3 Dijkstra’s Algorithm (Single Source Shortest Path Algorithm) 8.18
84 Huffman Tress and Huffman Codes 8.27
8.4.1 Data Compression 8.27
8.4.2 Fixed Length Codes 8.27
843 Variable Length Codes 8.28
8.44 Prefix Codes 8.29
8.4.5 Meaning of Huffman Trees and Huffman Codes 8.30
8.4.6 Huffman Algorithm 8.30
85. Advantages and Disadvantages of Greedy Technique 8.34
8.6 Review Questions 8.35
:
aes Limitations of Algorithm Power 94-9.20)
9.1 Introduction 92
9.2 Lower-bound Arguments 93
9.2.1 Trivial Lower Bounds 94
9.2.2 Information Theoretic Arguments 95
92.3 Adversary Arguments 9.6
ix9.24 Problem Reduction ah
9.3 Decision Trees 38
9.3.1 Decision Trees for Sorting Algorithms a9
9.3.2 Decision Trees for Searching a Sorted Array 942
94 P,NP and NP-Complete Problems 914
94.1 The Class P Problems eas:
94.2 The Class NP Problems 208
9.4.3 The Class NP Complete Problems 9.17
9.5. Review Questions 9.19
pier 19 CODING With the Limitation of Algorithm Power 104-1038
10.1 Introduction 102
10.2 Backtracking 10.2
10.2.1 General Backtracking Methods and Algorithms 104
10.2.2. N-Queens Problem 106
10.2.3 Hamiltonian Circuit Problem 10.13
10.2.4 Sum of Subsets 10.16
10.3 Branch and Bound 10.20
10.3.1 The Assignment Problem 10.21
10.3.2 Knapsack Problem 10.25
10.3.3 Travelling Sales-man Problem 10.31
10.4 Review Questions 1035
Anpenay nab Programs using ¢ At-a32
Rinses Tab Programs using Python Bi-B24
Bnsca« ode! Question Papers c-cd
Model Question Paper - 1
Model Question Paper - 2
Model Question Paper - 3a ead
CHAPTER
INTRODUCTION TO
ALGORITHM
q Contents >
+ What is an Algorithm?
+ The Notion of the Algorithm
Characteristics of an Algorithm
+ Whats Design and Analysis of Algorithms? Why We Need to Study?
¢ Algorithm Examples
+ Fundamentals of Algorithm Problem Solving
+ Problem Types
+ Fundamental Data Structures
* Classification of Data Structures = Primitive Data Structures
* Non-Primitive Data Structures * Operations on Data Structures
* Abstract Data Type (ADT)
* Importance of Data Structures in Designing Algorithm
+ Review Questions
Two ideas lie gleaming on the jeweler’s velvet. The first is the calculus, the second, the
algorithm. The calculus and the rich body of mathematical analysis to which it gave rise made
modern science possible; but it has been the algorithm that has made possible the modern
world.
—David Berlinski,
The Advent of the Algorithm, 2000Algorithms are a set of instructions or steps that are followed to solve a problem or complete a task,
Algorithms are an essential tool for solving problems and completing tasks in many different areas of
our lives, By breaking down complex problems into smaller components and developing step-by-step
instructions for addressing each one, we can achieve our goals more efficiently and effectively.
Algorithms have various applications in our daily lives and different industries.
Examples:
* Daily Life: Consider the simple task of preparing a meal. A recipe is essentially an algorithm,
It provides step-by-step instructions on how to prepare a dish, guiding the cook on what
ingredients to use, how to mix them, and how long to cook them. Following these steps in the
correct order results ina prepared meal.
+ GPS Navigation : GPS Navigation systems use algorithms to calculate the best routes from one
location to another. They analyze data such as distance, traffic conditions, and road closures to
Provide drivers with the fastest or most convenient path to their destination.
Finance Industry : Trading algorithms are widely used in stock markets. They analyze a
variety of factors such as price, timing, and volume to make decisions about when to buy or
sell stocks. These algorithms help to maximize profits and minimize losses,
Healthcare Industry: Algorithms play a crucial role in diagnosing diseases and deciding
on treatment plans. For example, an algorithm might analyze a patient's symptoms,
medical
history, and test results to recommend a course of action to the doctor.
Computer Science: Algorithms play a crucial role in solving problems and developing software
applications. For example, an algorithm might be used to sort data in a database or search for
specific information on the Internet, Programmers use algorithms to develop software that
can perform complex tasks such as image recognition or natural language processing,
What is an Algorithm?
Informally an algorithm is any well defined computational procedure that takes some values as “input” and
Broduces some value, or set of values, as “output”. An algorithm is thus a sequence af computational steps
that transform the input into the output.”
In simple terms we can say that
finite amount of time”,
fa Definitions: Algorithm
+ An algorithm is a sequence of unambiguous instructions for solving a problem. It is a step by step
procedure with the input to solve the problem in a finite amount of time to obtain the required output.
(oR)
“An algorithm isa step-by-step procedure for performing some tas!
+ ‘An algorithm is a sequence of unambiguous instruct) |
‘tructions for solving a probl ining |
Fequired output for any legitimate input in a finite @ a problem, ie,, for obtai
amount of time”The notion of an algorithm involves defining a sequence of unambiguous instructions for solving a
problem. These instructions can then be implemented in a computer program, which takes inputs
and produces outputs based on the algorithm's logic. The algorithm serves as the blueprint for the
program by providing a step-by-step guide for how to process the inputs and generate the desired
outputs.
Algorithm
shes
Computer )——>[ Output )
The Notion of the Algorithm.
Characteristics
ofan Algorithm
Versatility
or Flexibility,
1. Finiteness: An algorithm must always terminate after a number of steps. It means that an
algorithm cannot go on forever.
Example : An algorithm to calculate the sum of all the numbers in a list would iterate through
each number in the list once and then stop. It doesn't matter if the list has ten numbers or ten
million, the algorithm will always finish in a finite number of steps.jan and Analysis of Algorithms
2. Definiteness: Each step of the algorithm must be clearly defined and unambiguous.
Example : Consider an algorithm for preparing a coffee: the steps can’t just say “add some
coffee powder" or "add sugar”. They must specify exact measurements and times, like "add 2
spoons of coffee powder" and "add 1 spoon sugar”, so there is no ambiguity about what to do,
3. Input: An algorithm must take zero or more inputs, which are the values that the algorithm
operates on to produce the output.
Example : A GPS navigation algorithm takes as input the current location and the destination
location. Without these specific inputs, the algorithm would not be able to provide the correct
output.
4. Output: An algorithm produces at least one or more outputs. Output is the result of applying
the algorithm to the input(s).
Example : In GPS navigation, the output would be the step-by-step directions to reach the
destination from the current location. The output is directly dependent on the provided inputs.
5. Effectiveness: Each operation should be effective ie., the operation must be able to carryout
in finite amount of time.
Example : Consider an algorithm to sorta list of numbers in ascending order. In this algorithm,
operations might include comparing two numbers and swapping them if they're in the wrong
order. These are basic operations that must be completed in finite amount of time for an
algorithm to be effective.
6. Versatility or Flexibility: This feature represents the capacity ofan algorithm to solve a single
problem by different algorithm techniques.
Example : Sorting a list of numbers can be accomplished by various algorithms such as Bubble
Sort, Quick Sort, or Merge Sort. Each of these algorithms has its own strengths and weaknesses
and performs better under certain conditions. Another classic example is searching for an item
in alist. A simple linear search could be used or efficient binary search could be used if the list
is sorted.
ENCE CLT OPN ota te Yam ERAN
The design and analysis of algorithms is a fundamental aspect of computer science and computational
theory. It's concerned with creating efficient ways to solve computational problems and evaluate
their performance.
‘The study of algorithm design and analysis is crucial in computer science because it emphasizes not
just problem-solving, but optimizing the solution for maximum efficiency. This optimization can be
in terms of how fast the algorithm executes (time efficiency) and how much memory the algorithm
uses (space efficiency).
1, Design of Algorithms: This refers to the process of creating an algorithm (a set of well-
defined instructions) to solve a specific computational problem, It involves understanding
the problem, determining the computational steps required to solve it, and organizing these
steps in a logical manner: Designing an algorithm often requires a deep understanding of the
problem domain, and it may involve applying certain algorithm design strategies such as
divide-and-conquer, dynamic programming, greedy techniques, backtracking, etc.2. Analysis of Algorithms: Once an algorithm is designed, it's crucial to analyze it to determine
its efficiency and effectiveness. This involves evaluating how the algorithm performs in terms
of time and space complexity. Time complexity refers to the computational time required by
the algorithm to solve the problem, while space complexity refers to the memory used during
computation. An ideal algorithm is one that achieves the desired result with minimal time and
space usage.
me
Let's consider the task of creating a guest list for a party. We can use two different strategies can be used: a
linear approach and a more efficient categorized approach.
ign of Algorithm
Linear Approach Categorized Approach
This is like a simple algorithm where we go _| This is like a more organized and efficient
step by step without any specific order or algorithm where we divide the problem into
priority. smaller ones and tackle them systematically.
Divide your potential guests into
categories (friends, family, coworkers,
etc).
2. St jt for instance,
2, Continue with whoever comes to Er with ongieategon Ce
family) and list down all the people
mind next
1. Start with the first person who comes
to mind and write their name down.
you want to invite,
3. Keep doing this until we can't think of
anyone else to invite.
‘The Categorized Approach is generally more efficient. It ensures we don't forget anyone as it's easier to
remember people when we think about them in specific groups (friends, family, etc).
BE 4217's of Algorithm
Efficiency is a key consideration in the design and analysis of algorithms. For example, consider two students
Rekha and Srikanth who develop algorithms to find an element in an array of 100 elements. Although both
solutions correctly identify the intended element, their efficiencies differ.
1. Time Efficiency : Rekha's algorithm takes only 2 seconds to find an element, while Srikanth's
algorithm requires 5 seconds. This suggests that Rekha's algorithm has superior time efficiency, which
is important for completing tasks promptly without unnecessary delays.
2. Space Efficiency : Srikanth's algorithm uses only 500 bytes of memory compared to the 800 bytes
required by Rekha's algorithm. In this case, Srikanth’s algorithm is more space-efficient, meaning it
uses less memory to accomplish the same task. This is important for running algorithms on devices
with limited memory ot processing larger datasets without running out of memory.
Therefore, designing and analyzing algorithms is crucial for determining their efficiency in terms of time and
space complexity. It helps developers choose the most appropriate algorithm for a given problem based on its
efficiency and other factors .
3, Repeat the process for each category.
We will use the sixth feature of an algorithm, which involves solving a single problem using different
techniques. To illustrate the sixth feature of an algorithm, let's consider an example of finding thegreatest common divisor (GCD) of two non-negative and not-both-zero integers using various
methods or techniques.
Recall that the GCD of two non-negative, not-both-zero integers m and n is denoted by gcd(m, n) and
is defined as the largest integer that di
ides both m and n evenly.
Ce)
Euclid of Alexandria has outlines a most famous algorithm for finding the GCD and hence the name,
According to him, GCD of m and nis defined as:
GCD (n,m),
GCD (m,n) = m,
GCD(n,MOD(m,n)), otherwise
Here, MOD (m, n) is nothing “m modulo n" ie, the remainder obtained when dividing m by n. The
third part of the equality ie,, GCD (m, n) = GCD (n, MOD (m, n)) is the heart of the problem which is
applied repeatedly until MOD (m, n) becomes zero.
Find GCD(288,108)
GCD (288,108) = GCD (108, MOD (288, 108))
{CD (108, 72) = GCD (72, MOD (108, 72))
{CD (72, 36) = GCD (36, MOD (72, 36)
GCD (36, 0) = 36
METI i Alston for Comping 600 (msm
Step1: Check the second equality first in the defi
Otherwise go to step 2.
ion i.e, if n = 0, return the value of ‘m’ and stop.
Step2: Calculate MOD (m, n) and assign this value to a temporary variable ‘t.
Step 3:___Assign the value of ‘n’ to 'm’ and “to ‘n’.Go to step 1
Explanation:
Euclid’s algorithm is based on applying repeatedly the equality
GCD(m, n) = GCD(n, m mod n)
where m mod n is the remainder of the division of m by n, until m mod nis e
last value of mis also the greatest common divisor of the initial m and n,
For example, GCD(60, 24) can be computed as follows:
GCD(60, 24) = GCD(24, 60 mod 24) = GCD(24, 12) = GCD(12, 24 mod 12) = GCD (12,0) = 12
Euclid GED (m,n) +
// Computing the GCD (m, n)
// Input : Two non-negative, not-both-zero integers m, n
J/ Output : Greatest common divisor of m and n
qual to 0. Since GCD(m, 0) = m, thet
t=mmodn
men
net
+
return m
}
Analysis:
1. Euclid's Algorithm is an iterative process that repeatedly applies the same set of rules. The algorithm
reduces the problem of finding the GCD of two large numbers to that of finding the GCD of smaller
numbers, thus solving the problem more quickly than other methods. This makes it efficient as it
typically requires fewer steps than other methods.
2. One limitation of Eucli
's Algorithm is that i
requires both input numbers to be non-negative.
e Integer Checking Algorithm
This technique is based on the general definition of a GCD thata GCD is the largest integer that divides
both the numbers evenly. Obviously, such an integer cannot be greater than any of the two integers.
So GCD (im, n) < min {m, n}. Let us consider min {m, n} as a temporary variable ‘t’ ie., t= min (m,
n). Now we start checking whether ‘t’ divides both m and n. If it does, the GCD (m, n) is ‘t’ otherwise
decrease t by 1 and check once again. The process continues until ‘t’ reaches a number that divides
both mand n.
‘Consecutive Integer Checking Algorithmfor Computing GCD (m,n)
Step1: Assign the value of min {m,n} tot
Step2: Calculate MOD (m, t). Ifit is zero, go to step 3, else go to step 4.
Step3: Calculate MOD (n, t).Ifitis “zero’, return the value of t’ as answer and stop,
else go to step 4.
Step4: __Decrement't’ by 1 and go to step 2.
Find GCD (8, 6)
Step1: The smaller of 8 and 6 is 6, so tis assigned the value of 6. (t=6)
Step 2: 8 divided by 6 leaves a non zero remainder, so we move on to the step 4.
Step 4: tis then decreased by 1, changing its value from 6 to 5. Go to Step 2.
Step 2: 8 divided by 5 leaves a non zero remainder, so we move on to the step 4.
Step 4: tis then decreased by 1, changing its value from 5 to 4. Go to Step 2.
Step 2: 8 divided by 4 leaves a remainder 0, so we move on to the step 3.
Step 3: 6 divided by 4 leaves a non zero remainder, so we move on to the step 4.
Step4: __ tis then decreased by 1, changing its value from 4 to 3. Go to Step 2.step 4.
‘Step2: 8 divided by 8 leaves a non zero remainder, so we move on to the step
Step. tis then decreased by 1, changing its value from 3 to 3. Go to Step 2.
step 8 divided by 2 leaves a zero remainder, so we move on to the step 3.
Step 3: 6 divided by 2 leaves a zero remainder, The value oft becomes the GCD of 8 and 6
GCD(8.6) =
// Computing the GCD (m, n)
// Input : Two non-negative, not-both zero integers m, n.
// Output : Greatest common divisor of m and n.
{
t = min (m, n)
while (t # @) do
{
t= mmodt
if (t = 0)
{
t=nmodt
aif (t @)
{
return t
}
}
tet-1
#
return t
}
‘Analysis
The Consecutive Integer Checking Algorithm for computing the Greatest Common Divisor (GCD) of two
numbers (m, n) is simple to understand but it can be
highly inefficient, especially for large numbers. The
major drawbacks include:
1. This algorithm checks each integer one at a time to see if it divides both m and n without leaving a
remainder. Many ofthese checks are redundant. For example, ifa number is not divisible by 2, there's
no need to check if t's divisible by 4, 6, 8, etc,
2. Inefficient for large numbers.
Euclid Algorithm would be far more effcientas they reduce the problem size faster and don't involve checking
every integer.
The third technique is the one that is more familiar to the mi
middle ~ e the
name. In this method, the prime factors of the i flndand tented
common terms and multiplying them gives the GCD,om Find GCD(288,108)
‘The prime factors of 288 = 25 x 3? 2) 108 2) 268
‘The prime factors of 108 = 2? x 3? 254 2f144
Now, the common terms are 2? and 3? 3|_27 2) 72
‘The GCD of 288 and 108 = 2?x 37= 4% 9=36 3L_9 2 36
3 2 18
a3
3
Middle-School Procedure for Computing GCD(m, n) :
Find the prime factors of m
Find the prime factors of n
Identify the common prime factors of the above two step factors.
Step 4: Compute the product of the common factors of step 3 and return the result
which is the GCD.
Analysis:
‘The Middle-School Procedure for finding the GCD can be quite complex to implement as an
algorithm, especially when it comes to the task of finding the prime factors of a number. This is
where the Sieve of Eratosthenes comes into play.
It's important to remember that calculating prime factorizations for large numbers can be a time-
consuming process. The other algorithms like the Euclidean Algorithm are preferred as they are
more efficient.
‘The Sieve of Eratosthenes is an ancient algorithm that can generate all primes up to a given limit. This
can be useful for the Middle-School Procedure as it can help with the prime factorization part of the
process,
The Sieve of Eratosthenes works by iteratively marking the multiples of each prime number starting
from 2. The multiples are in fact the numbers which are not prime. By eliminating these multiples, the
algorithm is left with the prime numbers.
This generates all of the prime numbers less than or equal to n. Start by writing the numbers from
2,3,4...ninaline. Then keep repeating the following process until all the elements or numbers less
than or equal to square root of n (sqrt(n)) have been crossed out or circled.
Circle the next number which is neither circled nor crossed out, and cross out all other multiples of
that number which are in the list (some of these are probably already crossed out). The numbers
which are not crossed out when the process terminates are all of the primes between 2 to n.Ue yaiamemmm meson
Tess than or equal to 36.
TT ret ovr the Prime Number
ers ess than or equal to 36. Le, when n = 36
Analyst
To find out the prime num!
7TeLepLs fe f74+ 3 9 | 10
ar a2 pas [4 | as | 36 | 12 18 | 19
30 | 2122 | 23 | 24 | 25 | 26 27_|_28
39 | 30 | 31 | 32 | 33 | 341 35 36
ntioned earlier until all the elements
‘The square root of 36 Is 6 and therefore we have to repeat the process mo
are crossed out or circled less than or equal to 6.
Consider a first number 2, which is not yet circled or crossed out. Circle the number 2 and cross-out all the
4, 6, 8, 10 «in. 36.
multiples of 2 except 21
@| 3 5 7 9
11 13 15, 17 19
21 23 25 27
29 31 33 35
out nor circled. ie., number 3. Now, circle the number 3
Consider the next number which is neither crossed
9, 12, 15, «33, 36). If some of them are already crossed
and cross outall the multiples of 3 except 3.i¢, (6,
‘out, then no need to cross them again.
@|@)* 5 7
1 13 7 19
23 E
29 31 5
neither crossed out nor circled. ie., number 5. Now, circle the number 5
35.
Consider the next number whict
and cross out the multiple of § except 5. ie., 10,
@|®@
1 13 7 19.
23
29 31
Now, we cannot consider any number since all the numbers
mbe
ee from 2 to Sqrt (36) are crossed out or circled. So
‘The numbers which not crossed out are all of the prime numbers between 2 to 36.
213)5}7/ 411] 13 | 17 | 19] 23 | 29] 31Designing an algorithm involves creating a step-by-step guide to solve a specific problem. Algorithms
are strategies that manipulate data and solve problems efficien tly. A sequence of steps one typically
goes through in designing and analyzing an algorithm are given below. This sequence of actions is
crucial in developing an algorithm that not only solves the problem but efficiently.
1, Understanding the Problem
2, Ascertaining the Capabilities of the Computational Device
3. Designing an Algorithm and Data Structures
4, Proving Algorithm Correctness
5, Analysing an Algorithm
6. Coding an Algorithm
Ascertaining the Capabilities of the Computational Device
y
Designing an Algorithm and Data Structures
Providing Algorithm Correctness |
Analysing an Algorithm
Coding an Algorithm
1. Understanding the Problem :
‘To design an algorithm, itis essential to have a clear understanding of the problem thatneeds to
be solved. This involves understanding what the algorithm needs to do and what the expected
output should be. It's similar to understanding the rules of a any game before starting to play.
Example : Ifan algorithm is required to sorta list of numbers in ascending order, itis necessary
to understand what sorting means and how it works, Sorting is the process of arranging items
in a specific order, such as alphabetical or numerical order. In this case, we want to sort a
list of numbers in ascending order, which means arranging them from smallest to largest. By
understanding this concept, an algorithm can be created that takes an unsorted list of numbers
as an input and returns a sorted list in ascending order as an output.
2. Ascertaining the Capabilities of the Computational Device :
After gaining a complete understanding of a problem, it is necessary to determine the
capabilities of the computational device that the algorithm is intended to run on. This step
involves various activities.(a) Understanding Computational Capabilities: Understanding the nature of the device
on which an algorithm will run is critical.
Example : Consider two different types of computers: one is based on the Random-
Access Machine (RAM) model, common in many traditional computing environments,
This model assumes that instructions are executed one after another, sequentially.
Another type of computer could be a modern parallel processing system, which allows
multiple operations to execute concurrently. An algorithm designed for the RAM model
may not take full advantage of the parallel processing system's capabilities.
(b) Considering Speed and Memory: The specific speed and memory of the machine can
significantly impact the design of an algorithm.
Example: Suppose a machine is slow and has limited memory. In that case, an algorithm
designed to process a large dataset (such as analysing Aadhar details of entire nation)
might be inefficient or impractical. However, if the machine is powerful with ample
memory, this could allow for more complex and resource-intensive algorithms.
(©) Choosing between Exact and Approximate Problem Solving: The nature of the
problem also influences the choice of algorithm design. Based on the nature of the
problem, we can either choose Exact Algorithm or Approximation Algorithm.
Example : A problem like calculating the square root of a number might be solved using
an exact algorithm A problem like weather prediction model could provide a sufficiently
accurate forecast without needing an exact calculation. Some problems are inherently
complex and would take an excessively long time to solve exactly, like the traveling
salesman problem, where the goal is to find the shortest possible route that visits a set
of cities and returns to the original city. In this case, approximation algorithms could
provide a good-enough solution in a reasonable time-frame.
(d) Choosing Algorithm Design Techniques : Choosing the right algorithm design
techniques is crucial in problem-solving. There are instances where problems are
new or unknown which require the development of a unique algorithm from scratch.
However, there are also many problems where previously developed strategies or
proved approaches can be utilized for saving time and resources.
An algorithm design technique (or “strategy” or “paradigm’) is a general approach to
solving problems algorithmically that is applicable to a variety of problems from different
areas of computing.
Examples : Divide and Conquer, Greedy Algorithms, Dynamic Programming,
Backtracking, Branch and Bound, etc. Each technique has its unique characteristics,
advantages, and drawbacks, so knowing how and when to use them is a big part of
solving problems with algorithms.
3. Designing an Algorithm and Data Structures :
Designing an algorithm involves determining the actual steps required to solve a problem. This
process includes selecting appropriate data structures, such as arrays, lists, or trees, based 00
the nature of the operations that need to be performed. é ‘Example : If an algorithm needs to store a list of numbers and perform sorting operations, an
array data structure might be ideal because it allows for efficient sorting.
Methods for Specifying the Algorithm : Once the algorithm has been designed, it must be
specified or described in a way that others can understand. This specification is essential
because it enables other programmers to implement the algorithm correctly. The common
ways of specifying an algorithm is through Pseudocode or Flowcharts.
1, Pseudocode is a human-friendly way of describing an algorithm using a combination
of natural language and programming language constructs. It is not tied to any specific
programming language and can be easily understood by programmers with different
backgrounds.
2. Flowcharts are another method of specifying algorithms that use graphical symbols to
represent different steps in the algorithm. Flowcharts are useful for visualizing complex
algorithms and can help identify potential issues before implementation.
. Proving an Algorithm’s Correctness :
‘The correctness of an algorithm signifies that it provides the desired output for every valid input
within a finite amount of time. After the specification of an algorithm, an important phase is the
proofofits correctness. This processassuresthatthealgorithm producesanexpected outcome for
all suitable inputs within a finite amount of time. A common technique for proving correctness
is to use mathematical induction because an algorithm's iterations provide a natural sequence
of steps needed for such proofs.
Example : Euclid’s algorithm for computing the greatest common divisor can be proven
correct by showing that GCD(m, n) = GCD(n, m mod n), which requires a proof. Additionally,
it can be observed that the second integer gets smaller on every iteration of the algorithm
and the algorithm stops when the second integer becomes 0. We can conclude that Euclid's
algorithm is correct by using mathematical induction to prove these observations hold true for
all legitimate inputs,
. Analyzing the Algorithm :
Analyzing an algorithm means checking how well it performs. This involves looking at two
things:
(a) Time Efficiency: This refers to how quickly an algorithm can execute and deliver the
desired outcome. A good algorithm should be quick and not take too much time to finish.
Example : The Bubble Sort algorithm is known for its simplicity but it's not time-
efficient when it comes to large data sets. Quick Sortis a sorting algorithm that performs
significantly faster with large data sets, making it more time-efficient in such cases.
(b) Space Efficiency: This is about how much memory or space the algorithm needs to do its
work, A good algorithm should not require too much extra memory.
Example : The Merge Sort algorithm is time-efficient but not space-efficient because it
requires extra space proportional to the size of the input data. Heap Sort performs sorting
without needing additional memory and hence it more space-efficient than Merge Sort.Along with efficiency, it's also important that an algorithm should be simple and general,
(@) Simplicity means that the algorithm isn't complex or hard to understand. It should be
simple and easy to understand.
Example : [f two algorithms can sort the list equally well, but one method is easier to
understand and explain, then that simpler algorithm might be the better choice.
(b) Generality in algorithms means flexibility and adaptability to handle different Problems
or different types of data. If an algorithm is more general, then more problems it can
solve or the more types of data it can handle.
Example : If we write an algorithm to sort numbers in ascending order, then the same
sorting algorithm could be used to sort names, real numbers, or even a list of objects,
6. Coding an Algorithm :
The final step involves translatin
ig the algorithm into a programming language. Converting
an algorithm into computer code
is what we call "coding an algorithm’. The transition from
algorithm to code must maintain the algorithm's correctness and efficiency. This can sometimes
be challenging. One big challenge is ensuring the code works exactly like the algorithm intends,
without any mistakes or inefficiencies,
Each step of the coding process needs
implementation. For example, when im
different data sets is needed to
to be tested and debugged to ensure proper
plementing a sorting algorithm, extensive testing with
ensure it consistently produces the correct output.
Example : An example of coding an algorithm is implementing the bubble sort algorithm ina
Programming language like Python, Java,
C++ et
Design and analysis of algorithms is not re
problem can be applied to any problem reg:
field or another field. The import problem ty )w. These problem
‘ypes will be discussed in subsequent chapters,
* Sorting
‘stricted to specific problem types. The steps to solve a
ardless of its complexity or whether it i
is in the computer
‘pes in computer science are given belo
* Searching
* Combinatorial Problems
* Geometric Problems
String Processing
* Graph Problems
+ Numerical Problems
We can see so many
merit list, roll numbers eteN
‘There are different methods that are used to sort the data, They can be divided in to two
categories: Internal sorting and External sorting. If the data that is to be sorted is in main
memory, itis called Internal Sorting and if tis stored on the secondary storage devices, itis
called External Sorting,
There are different techniques and hence different algorithms for sorting. Before we start
designing the different algorithmic techniques it is very important to know how they behave
in every situation. The following parameters help in knowing the best:
(a) Execution Time:
output from the
preferred.
Execution of the program is the time required for attaining the sorted
time of input submission, The technique with less execution time is
(b) Space: The space required for the variable also matters in constructing the program or
algorithm
(© Coding Time: The time required to develo
P a sorting technique including the definition,
designing, and execution is called the codi
ing time.
Suppose if the data is in small quantity, the:
n there is noneed to search for best sorting technique
and we can take any simpler sorting tech
nique.
Examples of Sorting Algorithms :
* Bubble Sort + Insertion Sort
* Merge Sort * Quick Sort
+ Radix Sort
Searching Problems:
Searching refers to finding for an item in any list
in data processing, Searchin
* Selection Sort
* Heap Sort
of entries. It is one of the common operation
1g an employee details from the database or searching a telephone
‘number from the telephone directory are few of the daily life instances,
Searching should be efficient and
hiThe factors such as time efficiency, memory usage, and data structure type is important for the
selection of seraching algorithms.
Examples of Searching Algorithms :
* Linear Search + Binary Search
* Hash Search + Depth-First Search
* Breadth-First Search.
. String Processing Problems:
Strings are sequences of characters and String processing problems involve manipulating
strings to perform various operations such as searching, matching, and editing, There are
many kinds of strings like text made up of letters and numbers or even sequences of DNA
which have four different "letters": A, C, G, and T.
String processing is a big part of computer science, and it's used in all sorts of fields, from
creating computer software to understanding DNA sequences in biology.
One widely used problem in string processing is string matching, which is the task of finding
a particular word or sequence within a larger string or text. Many algorithms have been
developed to solve this problem efficiently. A popular one is the Boyer-Moore algorithm, which
is really smart and efficient. It doesn't go through each letter one by one but jumps ahead to
save time, making the search faster.
Solving string processing problems often requires developing efficient algorithms that
can handle large datasets and complex patterns. These algorithms can be based on various
techniques such as regular expressions, finite automata, and dynamic programming.
Examples of String Processing Problems:
+ Searching for a particular word or pattern in a text file,
+ Replacing all instances of one word with another in a text file.
Counting the number of occurrences ofa particular character or substring in a string.
Matching DNA sequences to identify genetic mutations,
* Parsing and analyzing programming languages
. Combinatorial Problems:
Combinatorial problems are a type of problem in computer science and mathematics that
involve counting or generating combinations or permutations of objects. These problems
often arise in various fields, including computer science, statistics, and engineering.
Combinatorial problems can be broadly classified into two categories: enumeration problems
and optimization problems.
(a) Enumeration problems involve counting the number of possible arrangements or
combinations of objects.
Example : An example of an enumeration problem is finding all subsets ofa set, Let's Say
we havea set{1, 2,3). The subsets are 0), {1), {2), {3}, {1,2}, (1, 3},{2,3},and {1,2, 3}, Foraset
with ‘n’ elements, there are 2* subsets. This problem's complexity grows exponentially
with the size of the set.‘umning to the Origin), PAHS hat satity
rapidly with the size he probler
no known algorithms that can fing ay
Problems can be
‘Olved eff
Problem, which im
ing thy
Solved using algorithms 1nd the si
Solving combinatorial Proble: i
Mis often requires develo it
large datasets and complex constraints. th hms cameo aon
Such as dynamic progra
thms that can handle
‘ ws® algorithms can be based
mming, backtracki
Examples of Combinato
n various techniques
N& and branch-and-bouna methods,
rial problems ;
* Traveling Salesman Problem * Shortest Path Problem
‘ing all possible subsets of a Set * Generating all possible Permutations of a set
* Selecting k objects from n objects,
Graph Problems:
Graph problems involve analyzing and manipulating 8raphs, which are mathematical
structures consisting of nodes (also called vertices) connected by ¢
model a wide range of real-world syste
‘dges. Graphs can be used to
‘cs, transportation networks,
as depth-first search, breadth-first search,
Examples of Graph Problems:
1. Shortest Path Problem:
Problem is commonly use
locations.
‘inding the shortest path between
two nodes in a graph. This
d in navigation systems to find the
fastest route between two
‘ ny
i : Finding the minimum set of edges that connects a
Minimum Spanning Tree Problem: Finding mu set of edges that comer
des in a graph. This problem is commonly used in
node: i
i locations. vit
See ee «des ina graph such that no two adjace1
ii : Assigning colors to nox tu pone
Cae ae sicgenpee Tha problem is commonly used in scheduling prol
same color. ; ised
ers nas mod to be assigned to resources without con! ony vy te
ae logical sortingis a technique used to 01 tapes na ron
f-dlenploeienl Sones Deh a for every directed edge from no
i DAG) su
acyclic graph (1comes before node B in the ordering. This technique is commonly used in scheduling
problems where tasks need to be performed in a specific order without conflicts.
5. Geometric Problems:
Geometric problems involve analyzing and manipulating geometric objects such as points,
tines, and polygons. These problems can arise ina wide range of applications such as computer
graphics, robotics, and tomography.
Examples of Geometric Problems:
1. Convex Hull Problem: Given a set of points in the plane, find the smallest convex polygon
that contains all the points. This problem is commonly used in computer graphics to
render 3D objects.
2. Nearest Neighbor Problem: Given a set of points in the plane, find the closest point to
a given query point. This problem is commonly used in location-based services to find
nearby places or people.
6. Numerical Problem:
Numerical problems involve computations with numbers, This could include tasks like finding
roots of equations, performing matrix operations, or solving differential equations. Various
numerical methods and algorithms are used to solve these types of problems. The majority of
these problems can be solved only approximately. But these problems play a vital role in many
scientific and engineering applications. So the algorithms for this type of problems should be
ore sophisticated and more focus is required while designing these algorithms.
Examples of Numerical Problems :
« Finding Roots of Equations
+ Performing Matrix Operations,
Solving Differential Equations
PELE McioatCaatt
Data structure and algorithms are two of the most important aspects of computer scienc
structures allow us to organize and store data, while algorithms allow us to process that data in
a meaningful way. Data structures are a key component in designing and analyzing algorithms. 4
ing related data items so that they can be manipulated
data structure is a way of organizing and stori
efficiently. The choice of data structure can have a significant impact on the performance of at
algorithm, both in terms of time and space complexity.
Data structures can be simple or complex depending on the problem at hand. They ¢
data types such as integers or characters, or they can be structures that contain other
For example, a two-dimensional array, which can be thought of as an array of arrays,
an contain basi
data structures
is often used €
implement matrices.
Choosing the right data structure for a given problem can have a significant imp
performance. For example, using an array instead of a linked list for frequent insertions or del
act on algorithm
jetions