Module 1 Lecture Notes
Module 1 Lecture Notes
Prepared by
DR. HARIVINOD N
Associate Professor,
Dept. of Computer Science and Engineering,
SJEC Mangaluru
Course website:
www.harivinod.in
Lecture Notes | 18CS42 – DAA | Module 1: Introduction
Module-1: Introduction
Contents
1.1 Introduction
1.1.1 What is an Algorithm?
Algorithm: An algorithm is a finite sequence of unambiguous instructions to solve a particular
problem.
Input. Zero or more quantities are externally supplied.
Output. At least one quantity is produced.
Definiteness. Each instruction is clear and unambiguous. It must be perfectly clear what
should be done.
Finiteness. If we trace out the instruction of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
Effectiveness. Every instruction must be very basic so that it can be carried out, in
principle, by a person using only pencil and paper. It is not enough that each operation be
definite as in criterion c; it also must be feasible.
1.1.2. Algorithm Specification
An algorithm can be specified in
1) Simple English
2) Graphical representation like flow chart
3) Programming language like c++/java
4) Combination of above methods.
Unless n is extremely large or very small, the formula can give a reasonable estimate of the
algorithm's running time. It is for these reasons that the efficiency analysis framework ignores
1multiplicative constants and concentrates on the count's order of growth to within a constant
multiple for large-size inputs.
Orders of Growth
Why this emphasis on the count's order of growth for large input sizes? Because for large values
of n, it is the function's order of growth that counts: just look at table which contains values of
a few functions particularly important for analysis of algorithms.
Table: Values of several functions important for analysis of algorithms
Algorithms that require an exponential number of operations are practical for solving only
problems of very small sizes.
Here fixed component depends on the size of a, b and c. Also instance characteristics Sp = 0
Example-2: Let us consider the algorithm to find sum of array. For the algorithm given here
the problem instances are characterized by n, the number of elements to be summed. The space
needed by a[ ] depends on n. So the space complexity can be written as; Ssum(n) ≥ (n+3); n for
a[ ], One each for n, i and s.
Method-2: Determine the step count of an algorithm by building a table in which we list the
total number of steps contributed by each statement.
Example: An example is shown below. The code will find the sum of n numbers.
The above method is both excessively difficult and, usually unnecessary. The thing to do is to
identify the most important operation of the algorithm, called the basic operation, the
operation contributing the most to the total running time, and compute the number of times the
basic operation is executed.
1.2.3 Time-Space tradeoff
There is often a time-space-tradeoff involved in a problem, that is, it cannot be solved with
few computing time and low memory consumption. One has to make a compromise and to
exchange computing time for memory consumption or vice versa, depending on which
algorithm one chooses and how one parameterizes it.
Informally, O(g(n)) is the set of all functions with a lower or same order of growth as g(n).
Note that the definition gives us a lot of freedom in choosing specific valuesfor constants c and
n0.
1
Examples: 𝑛 𝜖 𝑂(𝑛2 ), 100𝑛 + 5 𝜖 𝑂(𝑛2 ), 𝑛(𝑛 − 1)𝜖𝑂(𝑛2 )
2
Strategies to prove Big-O: Sometimes the easiest way to prove that f(n) = O(g(n)) is to take c
to be the sum of the positive coefficients of f(n). We can usually ignore the negative
coefficients.
Here is an example of the formal proof that n3 ∈ Ω(n2) : n3 ≥ n2 for all n ≥ 0, i.e., we can
select c = 1 and n0 = 0.
Example:
Example: n2 + 5n + 7 = Θ(n2)
Theorem: If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}).
(The analogous assertions are true for the Ω and Ө notations as well.)
Proof: The proof extends to orders of growth the following simple fact about four arbitrary
real numbers a1, b1, a2, b2: if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}.
Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 and some nonnegative integer n1
such that t1(n) ≤ c1g1(n) for all n ≥ n1.
Similarly, since t2(n) ∈ O(g2(n)), t2(n) ≤ c2g2(n) for all n ≥ n2.
Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so that we can use both
inequalities. Adding them yields the following:
t1(n) + t2(n) ≤ c1g1(n) + c2g2(n)
≤ c3 g1(n) + c3g2(n) = c3[g1(n) + g2(n)]
≤ c32 max{g1(n), g2(n)}.
Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants c and n0 required by the O
definition being 2c3 = 2 max{c1, c2} and max{n1, n2}, respectively.
3.4. Little Oh
The function f(n)= o(g(n)) [ i.e f of n is a little oh of g of n ] if and only if
𝑓(𝑛)
lim =0
𝑛→∞ 𝑔(𝑛)
Example:
Here comparison is the basic operation. Note that number of comparisions will be same for all
arrays of size n. Therefore, no need to distinguish worst, best and average cases. Total number
of basic operations (comparison) are,
Example-2: To check whether all the elements in the given array are distinct
Here basic operation is comparison. The maximum no. of comparisons happen in the worst
case. (i.e. all the elements in the array are distinct and algorithms return true).
𝟏
Other than the worst case, the total comparisons areless than 𝒏𝟐 . For example if the first two
𝟐
elements of the array are equal, only one comparison is computed. So in general C(n) =
O(n2)
Example-3: To perform matrix multiplication
Example-1
….
Example-2: Tower of Hanoi puzzle. In this puzzle, There are n disks of different sizes that
canslide onto any of three pegs. Initially, all the disks are on the first peg in order ofsize, the
largest on the bottom and the smallest on top. The goal is to move all thedisks to the third peg,
using the second one as an auxiliary, if necessary. We canmove only one disk at a time, and it
is forbidden to place a larger disk on top of asmaller one.The problem has an elegant recursive
solution, which is illustrated in Figure.
1. If n = 1, we move the single disk directly from the source peg to the destination peg.
2. To move n>1 disks from peg 1 to peg 3 (with peg 2 as auxiliary),
o we first move recursively n-1 disks from peg 1 to peg 2 (with peg 3 as auxiliary),
o then move the largest disk directly from peg 1 to peg 3, and,
o finally, move recursively n-1 disks from peg 2 to peg 3 (using peg 1 as auxiliary).
We have the following recurrence relation for the number of moves M(n):
The pattern of the first three sums on the left suggests that the next one will be
24M(n − 4) + 23 + 22 + 2 + 1, and generally, after i substitutions, we get
Since the initial condition is specified for n = 1, which is achieved for i = n-1, weget the
following formula for the solution to recurrence,
The standard approach to solving such a recurrence is to solve it only for n = 2k and then take
advantage of the theorem called the smoothness rule which claims that under very broad
assumptions the order of growth observed for n = 2k gives a correct answer about the order of
growth for all values of n.
of the active areas of current research involving computer scientists, economists, and social
scientists.
1.4.5. Combinatorial Problems
Generally speaking, combinatorial problems are the most difficult problems in computing,
from both a theoretical and practical standpoint. Their difficulty stems from the following facts.
First, the number of combinatorial objects typically grows extremely fast with a problem’s size,
reaching unimaginable magnitudes even for moderate-sized instances. Second, there are no
known algorithms for solving most such problems exactly in an acceptable amount of time.
A linked list is a sequence of zero or more elements called nodes, each containing two kinds
of information: some data and one or more links called pointers to other nodes of the linked
list. In a singly linked list, each node except the last one contains a single pointer to the next
element. Another extension is the structure called the doubly linked list, in which every node,
except the first and the last, contains pointers to both its successor and its predecessor.
A list is a finite sequence of data items, i.e., a collection of data items arranged in a certain
linear order. The basic operations performed on this data structure are searching for, inserting,
and deleting an element. Two special types of lists, stacks and queues, are particularly
important.
A stack is a list in which insertions and deletions can be done only at the end. This end is called
the top because a stack is usually visualized not horizontally but vertically—akin to a stack of
plates whose “operations” it mimics very closely.
A queue, on the other hand, is a list from which elements are deleted from one end of the
structure, called the front (this operation is called dequeue), and new elements are added to the
other end, called the rear (this operation is called enqueue). Consequently, a queue operates in
a “first-in–first-out” (FIFO) fashion—akin to a queue of customers served by a single teller in
a bank. Queues also have many important applications, including several algorithms for graph
problems.
Many important applications require selection of an item of the highest priority among a
dynamically changing set of candidates. A data structure that seeks to satisfy the needs of such
applications is called a priority queue. A priority queue is a collection of data items from a
totally ordered universe (most often, integer or real numbers). The principal operations on a
priority queue are finding its largest element, deleting its largest element, and adding a new
element.
1.5.2. Graphs
A graph is informally thought of as a collection of points in the plane called “vertices” or
nodes,” some of them connected by line segments called “edges” or “arcs.”A graph G is called
undirected if every edge in it is undirected. A graph whose every edge is directed is called
directed. Directed graphs are also called digraphs.
The graph depicted in Figure (a) has six vertices and seven undirected edges:
V = {a, b, c, d, e, f }, E = {(a, c), (a, d), (b, c), (b, f ), (c, e), (d, e), (e, f )}.
The digraph depicted in Figure 1.6b has six vertices and eight directed edges:
V = {a, b, c, d, e, f }, E = {(a, c), (b, c), (b, f ), (c, e), (d, a), (d, e), (e, c), (e, f )}.
Graph Representations- Graphs for computer algorithms are usually represented in one of
two ways: the adjacency matrix and adjacency lists.
The adjacency matrix of a graph with n vertices is an n x n boolean matrix with one row and
one column for each of the graph’s vertices, in which the element in the i th row and the jth
column is equal to 1 if there is an edge from the ith vertex to the jth vertex, and equal to 0 if
there is no such edge.
The adjacency lists of a graph or a digraph is a collection of linked lists, one for each vertex,
that contain all the vertices adjacent to the list’s vertex (i.e., all the vertices connected to it by
an edge).
Weighted Graphs: A weighted graph (or weighted digraph) is a graph (or digraph) with
numbers assigned to its edges. These numbers are called weights or costs.
Among the many properties of graphs, two are important for a great number of applications:
connectivity and acyclicity. Both are based on the notion of a path. A path from vertex u to
vertex v of a graph G can be defined as a sequence of adjacent (connected by an edge) vertices
that starts with u and ends with v.
A graph is said to be connected if for every pair of its vertices u and v there is a path from u to
v. Graphs with several connected components do happen in real-world applications. It is
important to know for many applications whether or not a graph under consideration has cycles.
A cycle is a path of a positive length that starts and ends at the same vertex and does not traverse
the same edge more than once.
1.5.3. Trees
A tree (more accurately, a free tree) is a connected acyclic graph. A graph that has no cycles
but is not necessarily connected is called a
forest: each of its connected components is a
tree. Trees have several important properties
other graphs do not have. In particular, the
number of edges in a tree is always one less
than the number of its vertices:|E| = |V|-1
Rooted Trees: Another very important
property of trees is the fact that for every two vertices in a tree, there always exists exactly one
simple path from one of these vertices to the other. This property makes it possible to select an
arbitrary vertex in a free tree and consider it as the root of the so-called rooted tree. A rooted
tree is usually depicted by placing its root on the top (level 0 of the tree), the vertices adjacent
to the root below it (level 1), the vertices two edges apart from the roots till below (level 2),
and so on.
Ordered Trees- An ordered tree is a rooted tree in which all the children of each vertex are
ordered. It is convenient to assume that in a tree’s diagram; all the children are ordered left to
right. A binary tree can be defined as an ordered tree in which every vertex has no more than
two children and each child is designated as either a left child or a right child of its parent; a
binary tree may also be empty.
If a number assigned to each parental vertex is
larger than all the numbers in its left subtree and
smaller than all the numbers in its right subtree.
Such trees are called binary search trees.
Binary trees and binary search trees have a wide
variety of applications in computer science.
collection of items that are not necessarily distinct. Note that if a set is represented by a list,
depending on the application at hand, it might be worth maintaining the list in a sorted order.
Dictionary: In computing, the operations we need to perform for a set or a multiset most often
are searching for a given item, adding a new item, and deleting an item from the collection. A
data structure that implements these three operations is called the dictionary. An efficient
implementation of a dictionary has to strike a compromise between the efficiency of searching
and the efficiencies of the other two operations. They range from an unsophisticated use of
arrays (sorted or not) to much more sophisticated techniques such as hashing and balanced
search trees.
A number of applications in computing require a dynamic partition of so men-element set into
a collection of disjoint subsets. After being initialized as a collection of n one-element subsets,
the collection is subjected to a sequence of intermixed union and search operations. This
problem is called the set union problem.
*****