0% found this document useful (0 votes)
11 views

design assingment

Uploaded by

alemugemechu44
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

design assingment

Uploaded by

alemugemechu44
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1. What is an Algorithm? Discuss its characteristics in detail.

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.

 Input − An algorithm should have 0 or more well-defined inputs.

 Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.

 Finiteness − Algorithms must terminate after a finite number of steps.

 Feasibility − Should be feasible with the available resources.

 Independent − An algorithm should have step-by-step directions, which should be independent


of any programming code.

2.What are the different ways to write an algorithm? Discuss in detail.

An algorithm can be written in following ways:

 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.

3) Differentiate between prior (theoretical) and posterior (empirical) analysis of


algorithms.

Efficiency of an algorithm can be analyzed at two different stages, before implementation and after
implementation. They are the following

 A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is


measured by assuming that all other factors, for example, processor speed, are constant and
have no effect on the implementation.
 A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is
implemented using programming language. This is then executed on target computer machine.
In this analysis, actual statistics like running time and space required, are collected.

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 of an algorithm refers to defining the mathematical boundation/framing of its


run-time performance. Using asymptotic analysis, we can very well conclude the best case, average
case, and worst case scenario 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.

Ω(g(n))=¿ { f (n): there exist positive constants c and n0 s.t. ¿} ¿ {}

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 −

Θ(g(n))=¿ {f (n): there exist positive constants c1 , c2 ,and n0 s.t. ¿ }¿ {}


5.Discus the following data structures in detail

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.

 fixed length (need preliminary reservation of memory)

 contiguous memory locations

 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.

 Singly linked list (next pointer)

Doubly linked list (next + previous point)

Dynamic length ,arbitrary memory locations,and access by following links

 Stacks

A stack of plates

Insertion/deletion can be done only at the top.

LIFO

Two operations (push and pop)

 Queues

A queue of customers waiting for services

Insertion/enqueue from the rear and deletion/dequeue from the front.

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.

 Undirected and directed graphs (digraphs).

 Complete, dense, and sparse graphs

 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

 n x n boolean matrix if |V| is n.

 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.

 The adjacency matrix of an undirected graph is symmetric.

 Adjacency linked lists

 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 .

It is a strategy that depends on “Just do it!”

Strengths

• wide applicability
• simplicity

• yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting,
searching, string matching) 

Weaknesses

• rarely yields efficient algorithms

• some brute-force algorithms are unacceptably slow

• not as constructive as some other design technique

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

Searching an element from a list of array algorithim

Time complexcity O(n)

Space Complexcity O(1)

Checking whether a set is a subset of another set or not algorthim.

def is_subset(set1, set2):

for elem in set1:

if elem not in set2:

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.

- Space complexity: O(1) as no extra space is used.

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.

You might also like