0% found this document useful (0 votes)
2 views35 pages

DDA Unit 1

The document provides an overview of algorithms, defining them as finite sets of instructions for solving specific problems. It outlines characteristics, advantages, and disadvantages of algorithms, along with methods for designing and analyzing them. Additionally, it introduces various problem types such as sorting, searching, and combinatorial problems, and discusses algorithm design techniques and fundamental data structures.

Uploaded by

sailajam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views35 pages

DDA Unit 1

The document provides an overview of algorithms, defining them as finite sets of instructions for solving specific problems. It outlines characteristics, advantages, and disadvantages of algorithms, along with methods for designing and analyzing them. Additionally, it introduces various problem types such as sorting, searching, and combinatorial problems, and discusses algorithm design techniques and fundamental data structures.

Uploaded by

sailajam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

The Design and

analysis of
Algorithms
:Unit 1
BCA 4th Sem
Sailaja
Algorithms A finite set of instruction that specifies a sequence of operation is
to be carried out in order to solve a specific problem or class of
problems is called an Algorithm.
❖ An algorithm can be defined as a finite set of steps, which has to be followed
while carrying out a particular problem. It is nothing but a process of
executing actions step by step.

❖ An algorithm is a distinct computational procedure that takes input as a set


of values and results in the output as a set of values by solving the problem.
More precisely, an algorithm is correct, if, for each input instance, it gets the
correct output and gets terminated.

❖ An algorithm unravels the computational problems to output the desired


result. An algorithm can be described by incorporating a natural language
such as English, Computer language, or a hardware language.
Characteristics of Algorithms
• Input: It should externally supply zero or more quantities.
• Output: It results in at least one quantity.
• Definiteness: Each instruction should be clear and ambiguous.
• Finiteness: An algorithm should terminate after executing a finite number of steps.
• Effectiveness: Every instruction should be fundamental to be carried out, in
principle, by a person using only pen and paper.
• Feasible: It must be feasible enough to produce each instruction.
• Flexibility: It must be flexible enough to carry out desired changes with no efforts.
• Efficient: The term efficiency is measured in terms of time and space required by an
algorithm to implement. Thus, an algorithm must ensure that it takes little time and
less memory space meeting the acceptable limit of development time.
• Independent: An algorithm must be language independent, which means that it
should mainly focus on the input and the procedure required to derive the output
instead of depending upon the language.
Advantages of an Algorithm Disadvantages of an Algorithm
•Effective Communication: Since it is •Developing algorithms for complex
written in a natural language like English, problems would be time-consuming and
it becomes easy to understand the step- difficult to understand.
by-step delineation of a solution to any
particular problem. •It is a challenging task to understand
•Easy Debugging: A well-designed complex logic through algorithms
algorithm facilitates easy debugging to
detect the logical errors that occurred
inside the program.
•Easy and Efficient Coding: An algorithm is
nothing but a blueprint of a program that
helps develop a program.
•Independent of Programming
Language: Since it is a language-
independent, it can be easily coded by
incorporating any high-level language.
Assignment Exercise
a.Write down driving directions for going from your College to your home
with the precision required by an algorithm.

b. Write down a recipe for cooking your favorite dish with the precision
required by an algorithm.

2. Design an algorithm for computing√nfor any positive integern.Be-


sides assignment and comparison, your algorithm may only use the
fourbasic arithmetical operations.
• 3 a)Find gcd(31415, 14142) by applying Euclid’s algorithm.
• b. Estimate how many times faster it will be to find gcd(31415,
14142)by Euclid’s algorithm compared with the algorithm based on
checking consecutive integers from min{m,n}down to gcd(m,n).
4.Prove the equality
gcd(m,n)= gcd(n,m mod n)for every pair of positive integers m and n

5. Locker doors There are n lockers in a hallway, numbered sequentially


from 1 to n. Initially, all the locker doors are closed. You make n passes by
the lockers, each time starting with locker
#1. On the ith pass, i = 1, 2,...,n, you toggle the door of every ith locker: if
the door is closed, you open it; if it is open, you close it. After the last
pass, which locker doors are open and which are closed? How many of
them are open?
Fundamentals
of Algorithmic
Problem Solving
• A sequence of steps
involved in designing and
analyzing an algorithm is
shown in the figure below.
Fundamentals of Algorithmic Problem
Solving
1. Understanding the Problem

2. Decision making

3. Methods of Specifying an Algorithm

4. Proving an Algorithm’s Correctness

5. Analyzing an Algorithm

6. Coding an Algorithm
1.Understanding the Problem
1. This is the first step in designing of algorithm.

2. Read the problem’s description carefully to understand the problem


statement completely.

3. Ask questions for clarifying the doubts about the problem.

4. Identify the problem types and use existing algorithm to find


solution.

5. Input (instance) to the problem and range of the input get fixed.
2.Decision making:The Decision making is done on the following:
a) Ascertaining the Capabilities of the Computational Device
In random-access machine (RAM), instructions are executed one
after another (The central assumption is that one operation at a
time). Accordingly, algorithms designed to be executed on such
machines are called sequential algorithms.
• →In some newer computers, operations are executed concurrently, i.e., in
parallel. Algorithms that take advantage of this capability are called parallel
algorithms.
• →Choice of computational devices like Processor and memory is mainly
based on space and time efficiency
b)Choosing between Exact and Approximate Problem Solving:
→The next principal decision is to choose between solving the
problem exactly or solving it approximately.

→An algorithm used to solve the problem exactly and produce


correct result is called an exact algorithm.

→If the problem is so complex and not able to get exact solution,
then we have to choose an algorithm called an approximation
algorithm. i.e., produces an Approximate answer. E.g., extracting
square roots, solving nonlinear equations, and evaluating definite
integrals.
c)Algorithm Design Techniques
• 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.

• Though Algorithms and Data Structures are independent, but they are
combined together to develop program. Hence the choice of proper data
structure is required before designing the algorithm.

• Implementation of algorithm is possible only with the help of


Algorithms and Data Structures

• Algorithmic strategy / technique / paradigm are a general approach by


which many problems can be solved algorithmically. E.g., Brute Force,
Divide and Conquer, Dynamic Programming, Greedy Technique and soon.
3) Methods of Specifying an Algorithm
• There are three ways to specify an algorithm.
They are:
a. Natural language
b. Pseudocode
c. Flowchart
Natural Language Pseudocode:
It is very simple and easy to specify an • Pseudocode is a mixture of a natural language
algorithm using natural language. But many and programming language constructs.
times specification of algorithm by using Pseudocode is usually more precise than
natural language is not clear and thereby we natural language.
get brief specification. • For Assignment operation left arrow “←”, for
Example: An algorithm to perform addition of comments two slashes “//”,if condition, for,
two numbers. while loops are used.
Step 1: Read the first number, say a.
Step 2: Read the first number, say b. ALGORITHM Sum(a,b)
Step 3: Add the above two numbers and //Problem Description: This algorithm performs addition
store the result in c. of two numbers
Step 4: Display the result from c. //Input: Two integers a and b
//Output: Addition of two integers
Such a specification creates difficulty while c←a+b
actually implementing it. Hence many return c
programmers prefer to have specification of This specification is more useful for
algorithm by means of Pseudocode. implementation of any language
4)Proving an Algorithm’s Correctness
• Once an algorithm has been specified then its correctness must be
proved.
• An algorithm must yield a required result for every legitimate input in
a finite amount of time.
• For Example, the correctness of Euclid’s algorithm for computing the
greatest common divisor stems from the correctness of the equality
gcd(m, n) = gcd(n, m mod n).
• 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.
• The notion of correctness for approximation algorithms is less
straightforward than it is for exact algorithms. The error produced by
the algorithm should not exceed a predefined limit.
5)Analyzing an Algorithm
• For an algorithm the most important is efficiency. In fact, there are two
kinds of algorithm efficiency.
They are:
❖ Time efficiency, indicating how fast the algorithm runs, and
❖ Space efficiency, indicating how much extra memory it uses.
The efficiency of an algorithm is determined by measuring both time
efficiency and space efficiency.
• So factors to analyze an algorithm are:
▪ Time efficiency of an algorithm
▪ Space efficiency of an algorithm
▪ Simplicity of an algorithm
▪ Generality of an algorithm
6)Coding an Algorithm
• The coding / implementation of an algorithm is done by a suitable
programming language like C, C++,JAVA.
• The transition from an algorithm to a program can be done either incorrectly
or very inefficiently. Implementing an algorithm correctly is necessary. The
Algorithm power should not reduce by in efficient implementation.
• Standard tricks like computing a loop’s invariant (an expression that does
not change its value) outside the loop, collecting common subexpressions,
replacing expensive operations by cheap ones, selection of programming
language and so on should be known to the programmer.
• Typically, such improvements can speed up a program only by a constant
factor, whereas a better algorithm can make a difference in running time by
orders of magnitude. But once an algorithm is selected, a 10–50% speedup
may be worth an effort.
• It is very essential to write an optimized code (efficient code) to reduce the
burden of compiler.
Problems and Puzzles
• 1. Old World puzzle A peasant finds himself on a riverbank with a
wolf, a goat, and a head of cabbage. He needs to transport all three to
the other side of the river in his boat. However, the boat has room for
only the peasant himself and one other item (either the wolf, the
goat, or the cabbage). In his absence, the wolf would eat the goat,
and the goat would eat the cabbage. Solve this problem for the
peasant or prove it has no solution. (Note: The peasant is a vegetarian
but does not like cabbage and hence can eat neither the goat nor the
cabbage to help him solve the problem. And it goes without saying
that the wolf is a protected species.)
• . New World puzzle There are four people who want to cross a rickety
bridge; they all begin on the same side. You have 17 minutes to get them
all across to the other side. It is night, and they have one flashlight. A
maximum of two people can cross the bridge at one time. Any party that
crosses, either one or two people, must have the flashlight with them. The
flashlight must be walked back and forth; it cannot be thrown, for example.
Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes,
person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must walk
together at the rate of the slower person’s pace. (Note: According to a
rumor on the Internet, interviewers at a well-known software company
located near Seattle have given this problem to interviewees.)
• Which of the following formulas can be considered an algorithm for
computing the area of a triangle whose side lengths are given positive
numbers a, b, and c? a. S = p(p − a)(p − b)(p − c), where p = (a + b + c)/2 b.
S = 1 2 bc sin A, where A is the angle between sides b and c c. S = 1 2 aha,
where ha is the height to base a.
• The Design And Analysis Of Algorithms Solution Manual (2012).pdf
Important Problem Types,
In this section, we are going to introduce the most important problem types:

✓ Sorting

✓ Searching

✓ String processing

✓ Graph problems

✓ Combinatorial problems

✓ Geometric problems

✓ Numerical problems
Sorting
• The sorting problem is to rearrange the elements of a given list in non
decreasing (ascending) or decreasing order (descending) order.
Examples of sorting algorithms
• Selection sort
• Bubble sort
• Insertion sort
• Merge sort
• Heap sort …
• Number of key comparisons is used to determine time complexity of
sorting algorithms
Two properties related to sorting algorithms
• Stability: A sorting algorithm is called stable if it preserves the relative order of
any two equal elements in its input.
A sorting algorithm is called stable if it preserves the relative order of any two
equal elements in its input. In other words, 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 , 20 Introduction respectively, such that i < j .
This property can be desirable if, for example, we have a list of students sorted
alphabetically and we want to 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.
• In place: A sorting algorithm is in place if it does not require extra memory,
except, possibly for a few memory units.
An algorithm is said to be in-place if it does not require extra memory, except,
possibly, for a few memory units. There are important sorting algorithms that are
in-place and those that are not.
Searching
• The searching problem : Find a given value, called a search key, in a
given set
• Sequential searching
• Binary searching…

• organizing very large data sets for efficient searching poses special
challenges with important implications for real-world applications
String Matching

• A string is a sequence of characters from an alphabet.


• Text strings: letters, numbers, and special characters.
• String matching: searching for a given word/pattern in a text.
• Text: I am a computer science graduate
• Pattern: compute

One particular problem—that of searching for a given word in a text—


has attracted special attention from researchers. They call it string
matching. Several algorithms that exploit the special nature of this type
of searching have been invented.
Graph Problems
• a graph can be thought of as a collection of points called vertices, some of
which are connected by line segments called edges.
• Graphs are an interesting subject to study, for both theoretical and practical
reasons. Graphs can be used for modeling a wide variety of applications,
including transportation, communication, social and economic networks,
project scheduling, and games.
• Studying different technical and social aspects of the Internet in particular is
one of the active areas of current research involving computer scientists,
economists, and social scientists
• graph algorithms include graph-traversal algorithms (how can one reach all
the points in a network?), shortest-path algorithms (what is the best route
between two cities?), and topological sorting for graphs with directed edges
(is a set of courses with their prerequisites consistent or self-contradictory?).
• The graph-coloring problem seeks to assign the smallest number of colors
to the vertices of a graph so that no two adjacent vertices are the same color.
Combinatorial problems
• These are problems for which it is required to generate permutations,
a combinations, or a subset that satisfies certain constraints.
• A desired combinatorial object may have an associated cost that
needs to be minimized or maximized
• In practical, the combinatorial problems are the most difficult
problems in computing.
• The traveling salesman problem and the graph coloring problem are
examples of combinatorial problems.
Geometric problems

• Geometric algorithms deal with geometric objects such as points,


lines, and
• polygons.
• Geometric algorithms are used in computer graphics, robotics
• etc. Examples: closest-pair problem and the convex-hull problem
Fundamental Data Structures
• https://www.studocu.com/in/document/srm-institute-of-science-
and-technology/design-and-analysis-of-algorithms/fundamental-
data-structures/27002963
The Design and analysis of
Algorithms
Unit 1 Chapter 2
Numerical problems

• Numerical problems are problems that involve computing definite


integrals,
• evaluating functions, mathematical equations, systems of equations,
and so
• on
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy