Assignment of DSA
Assignment of DSA
Assignment of DSA
Submitted to:
Dr. Muhammad Awais
Submitted by:
4203 M. Khalid Irfani
4213 Atif Hameed
4225 Qaiser Nadeem
4235 Munazza Ashraf
4243 Rameela Mushtaq
4255 Sahaab Tariq
Semester:
3rd (Morning .A)
Session:
2020-2024
Time Complexity
Time complexity is the computational complexity that describes the amount of computer
time it takes to run an algorithm. Time complexity is commonly estimated by counting
the number of elementary operations performed by the algorithm, supposing that each
elementary operation takes a fixed amount of time to perform. Thus, the amount of time
taken and the number of elementary operations performed by the algorithm are taken to
be related by a constant factor.
The amount of time taken by an algorithm is a function of the length of input only.
Here, the length of input indicates the number of operations to be performed by the
algorithm.
The time complexity is commonly expressed using big O notation, typically O(n), O(n
log n), O(na), O(2a),etc…, where n is the size in units of bits needed to represent the
input.
Worst-case complexity
Average-case complexity
Best-case complexity
Worst-case complexity:
Average-case complexity:
Best-case complexity:
6. Polynomial time
7. Exponential time_O(2^n)
8. Factorial time_O(n!)
Constant time – O (1):
An algorithm is said to have constant time with order O (1) when it is not dependent
on the input size n. Irrespective of the input size n, the runtime will always be the
same.
For Example :
int index = 5;
int item = list[index];
if (condition true) then
perform some operation that runs in constant time
else
perform some other operation that runs in constant time
for i = 1 to 100
for j = 1 to 200
Perform some operation that runs in constant time
An algorithm is said to have a linear time complexity when the running time
increases linearly with the length of the input. When the function involves checking
all the values in an input data, such function has Time complexity with this order
O(n).
For example:
A procedure that adds up all elements of a list requires time proportional to the length
of the list, if the adding time is constant, or, at least, bounded by a constant .
An algorithm is said to have a logarithmic time complexity when it reduces the size of
the input data in each step. This indicates that the number of operations is not the
same as the input size. The number of operations gets reduced as the input size
increases. Algorithms with Logarithmic time complexity are found in binary trees or
binary search functions.
For Example:
D (⌊ n/2⌋), where ⌊ ⌋ denotes the floor function. If w= D(⌊ n/2⌋) , then we are done. Else,
if w< D(⌊ n/2⌋), continue the search in the same way in the left half of the dictionary,
otherwise continue similarly with the right half of the dictionary. This algorithm is similar
to the method often used to find an entry in a paper dictionary.
An algorithm is said to have a non – linear time complexity where the running time
increases non-linearly (n^2) with the length of the input. Generally, nested loops
come under this time complexity order where for one loop takes O(n) and if the
function involves loop within a loop, then it goes for O(n)*O(n) = O(n^2) order.
. Polynomial time:
The selection sort sorting algorithm on n integers performs operations for some
constant A. Thus it runs in time and is a polynomial time algorithm.
All the basic arithmetic operations (addition, subtraction, multiplication, division, and
comparison) can be done in polynomial time.
Maximum matchings in graphs can be found in polynomial time.
Complexity Class P:
P is the complexity class containing decision problems which can be solved
by a deterministic Turing machine using a polynomial amount of
computation time, or polynomial time.
P is often taken to be the class of computational problems which are
"efficiently solvable" or "tractable“.
Problems that are solvable in theory, but cannot be solved in practice
P is known to contain many natural problems, including the decision
versions of linear programming, calculating the greatest common divisor,
and finding a maximum matching.
Complexity Class NP
NP ("Nondeterministic Polynomial time") is the set of decision problems
solvable in polynomial time on a nondeterministic Turing machine.
It is the set of problems that can be "verified" by a deterministic Turing
machine in polynomial time.
All the problems in this class have the property that their solutions can be
checked effectively.
This class contains many problems that people would like to be able to
Solve effectively, including n the Boolean satisfiability problem (SAT)
NP Complete
The NP complete problems are the most difficult problems in NP
("nondeterministic polynomial time") in the sense that they are the ones
most likely not to be in P.
If one could find a way to solve any NP complete problem quickly (in
polynomial time), then they could use that algorithm to solve all NP
problems quickly.
At present, all known algorithms for NP complete problems require time
that is super polynomial in the input size.
To solve an NP complete problem for any nontrivial problem size, generally
one of the following approaches is used:
Approximation
Probabilistic
Special case
NP-Hard :
A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y
is reducible to X in polynomial time.
NP-Hard problems are as hard as NP-Complete problems. NP-Hard
Problem need not be in NP class
Algorithm:
STEP 1: initialize a list L to contain one element 0.
STEP 2: for each i from 1 to n do
STEP 3: let Ui be a list containing all elements y in L, and all sums xi + y for all
y in L.
STEP 4: sort Ui in ascending order
STEP 5: make L empty
STEP 6: let y be the smallest element of Ui , add y to L for each element z of Ui
in increasing order do, if y + ε T/n < z ≤ T then, y = z
STEP 7: add z to L
STEP 8: return the largest element in L.
Difference between NP-Hard and NP-Complete
NP-hard NP-Complete
NP-Hard problems (say X) can be NP-Complete problems can be
solved if and only if there is a solved by a non-deterministic
NP Complete problem(say Y) Algorithm/Turing Machine in
that can be reducible into X in polynomial time
polynomial time
To solve this problem, it do not To solve this problem, it must be
have to be in NP. both NP and NP-hard problems.
Do not have to be a Decision It is exclusively a Decision
problem. problem.
Example: Halting problem, Example: Determine whether a
Vertex cover problem, etc. graph has a Hamiltonian cycle,
Determine whether a Boolean
formula is satisfiable or not,
Circuit-satisfiability problem, etc