design assingment
design assingment
An algorithm is a set of steps of operations to solve a problem performing calculation, data processing,
and automated reasoning tasks. An algorithm is an efficient method that can be expressed within finite
amount of time and space.
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and
their inputs/outputs should be clear and must lead to only one meaning.
Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.
Natural Language Representation: Use of Natural language in writing and algorithm can be
ambiguous and therefore algorithm may lack the characteristic of being definite.
Flowcharts: Graphical representation of algorithmic steps, flowcharts are not suitable to write
the solutions for complex problems.
Pseudocode: has an advantage of being easily converted into any programming language. A
Table of pseudocode conventions are given in textbook. Mostly used.
Efficiency of an algorithm can be analyzed at two different stages, before implementation and after
implementation. They are the following
We shall learn about a priori algorithm analysis. Algorithm analysis deals with the execution or running
time of various operations involved. The running time of an operation can be defined as the number of
computer instructions executed per operation.
4. What is asymptotic analysis of algorithm? Define with examples the commonly used
asymptotic notations to calculate the running time complexity of an algorithm.
Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to
work in a constant time. Other than the "input" all other factors are considered constant.
Asymptotic analysis refers to computing the running time of any operation in mathematical
units of computation. For example, the running time of one operation is computed as f(n) and
may be for another operation it is computed as g(n2). This means the first operation running
time will increase linearly with the increase in n and the running time of the second operation
will increase exponentially when n increases. Similarly, the running time of both operations will
be nearly the same if n is significantly small.
Asymptotic Analysis
Following are the commonly used asymptotic notations to calculate the running time complexity of an
algorithm.
Ο Notation
Ω Notation
θ Notation
Big-O Notation
Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant
multiple),
Examples:
10n is O(n2)
5n+20 is O(n)
Omega Notation, Ω
The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time.
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows −
a. Array
b. Linked List
c. Stack
d. Queue
e. Binary Tree and Binary Search Tree
f. Graph
Arrays
A sequence of n items of the same data type that are stored contiguously in computer
memory and made accessible by specifying a value of the array’s index.
direct access
Linked List
A sequence of zero or more nodes each containing two kinds of information: some data
and one or more links called pointers to other nodes of the linked list.
Stacks
A stack of plates
LIFO
Queues
FIFO
Binary tree a data structure in which a record is linked to two successor records,
usually referred to as the left branch when greater and the right when less than the
previous record.
A binary search tree (BST) is a binary tree where every node in the left subtree is less
than the root, and every node in the right subtree is of a value greater than the root. The
properties of a binary search tree are recursive: if we consider any node as a “root,”
these properties will remain true.
Graph
Formal definition
A graph G = <V, E> is defined by a pair of two sets: a finite set V of items called vertices
and a set E of vertex pairs called edges.
A graph with every pair of its vertices connected by an edge is called complete, K|V|
In Dense graph, the number of edges is close to the maximal number of edges.
Graph Representation
Adjacency matrix
The element on the ith row and jth column is 1 if there’s an edge from ith vertex to the
jth vertex; otherwise 0.
A collection of linked lists, one for each vertex, that contain all the vertices adjacent to
the list’s vertex.
6. Describe advantages and disadvantages of brute force approach for algorithm design
Brute Force
A straightforward approach, usually based directly on the problem’s statement and definitions of the
concepts involved .
Strengths
• wide applicability
• simplicity
• yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting,
searching, string matching)
Weaknesses
1. Write a brute force algorithm for the following problems and analyses the efficiency of
the algorithms
a. Searching an element from a list of array
b. Checking whether a set is a subset of another set or not
c. Selection sort
d. String matching problem
return False
return True
# Example usage
set1 = {1, 2, 3}
set2 = {3, 2, 1, 4, 5}
print(is_subset(set1, set2))
Efficiency analysis:
- Time complexity: O(n*m) where n is the size of the first set and m is the size of the second set.
Selection sort
Selection Sort Scan the array to find its smallest element and swap it with the first element. Then,
starting with the second element, scan the elements to the right of it to find the smallest among them
and swap it with the second elements. Generally, on pass i (0 £ i £ n-2), find the smallest element in
A[i..n-1] and swap it with A[i]: A[0] £ . . . £ A[i-1] | A[i], . . . , A[min], . . ., A[n-1]
String matching problem algorithm
Efficiency analysis:
- Time complexity:
O(n*m) where n is
the size of the first
set and m is the size
of the second set.
- Space complexity:
O(1) as no extra
space is used.