Download - Analysis of algorithms-Day3
-
8/9/2019 Analysis of algorithms-Day3
1/45
Analysis of AlgorithmsDay 3
-
8/9/2019 Analysis of algorithms-Day3
2/45
Copyright 2004, InfosysTechnologies Ltd
2ER/CORP/CRS/SE15/003
Version No: 2.0
Plan for Day-3
Analysis of well known algorithms
Algorithm Design Techniques Dynamic Programming
Intractable problems
Deterministic Vs Non-Deterministic machines
P Vs NP
NP Complete
NP Hard
Case Study ( for self study)
-
8/9/2019 Analysis of algorithms-Day3
3/45
Copyright 2004, InfosysTechnologies Ltd
3ER/CORP/CRS/SE15/003
Version No: 2.0
Dynamic Programming
Dynamic Programming is a design principle which is used to solve problemswith overlapping sub problems
It solves the problem by combining the solutions for the sub problems
The difference between Dynamic Programming and Divide and Conquer is that
the sub problems in Divide and Conquer are considered to be disjoint anddistinct various in Dynamic Programming they are overlapping.
-
8/9/2019 Analysis of algorithms-Day3
4/45
Copyright 2004, InfosysTechnologies Ltd
4ER/CORP/CRS/SE15/003
Version No: 2.0
Dynamic Programming Matrix multiplication
Matrix Multiplication:
Consider the matrices M1 x M2 x M3 x M4 The corresponding dimensions are (10,20), (20,50), (50,1), (1,100) respectively
In recursion, the multiplication will be done in the following order:
M1 x ( M2 x ( M3 x M4 ) )
M3
x M4
will need 50 x 100 = 5000 multiplications
M2 x ( M3 x M4 ) will need 20 x 50 x 100 = 100000 multiplications
M1 x ( M2 x ( M3 x M4 ) ) will need 10 x 20 x 100 = 20000 multiplications
So total number of multiplications = 125000
-
8/9/2019 Analysis of algorithms-Day3
5/45
Copyright 2004, InfosysTechnologies Ltd
5ER/CORP/CRS/SE15/003
Version No: 2.0
Dynamic Programming Matrix multiplication
(Contd) The same matrix multiplication is done in the following order:
( M1 x ( M2 x M3 ) ) x M4 M2 x M3 will need 20 x 50 x 1 = 1000 multiplications
M1 x ( M2 x M3 ) will need 10 x 20 x 1 = 200 multiplications
( M 1 x ( M2 x M3 ) ) x M4 will need 10 x 1 x 100 = 1000 multiplications
Total number of multiplications = 2200.
-
8/9/2019 Analysis of algorithms-Day3
6/45
Copyright 2004, InfosysTechnologies Ltd
6ER/CORP/CRS/SE15/003
Version No: 2.0
Dynamic Programming Computing a BinomialCoefficient
The Binomial Coefficient, denoted by C(n,k), is the number of combinations of
kelements from a set of n elements.
The Binomial Coefficient is computed using the following formula
C(n,k) = C(n-1,k-1) + C(n-1,k) for all n > k > 0 and C(n,0) = C(n,n) = 1
-
8/9/2019 Analysis of algorithms-Day3
7/45
Copyright 2004, InfosysTechnologies Ltd
7ER/CORP/CRS/SE15/003
Version No: 2.0
Dynamic Programming Computing a BinomialCoefficient (Contd)
0 1 2 . . . k-1 k
0 1
1 1 1
2 1 2 1
.k 1 1
.
n-1 1 C(n-1,k-1) C(n-1,k)
n 1 C(n,k)
-
8/9/2019 Analysis of algorithms-Day3
8/45
Copyright 2004, InfosysTechnologies Ltd
8ER/CORP/CRS/SE15/003
Version No: 2.0
Dynamic Programming Computing a BinomialCoefficient (Contd)
Computing a Binomial Coefficient:
1. Begin
2. For i = 0 to n do2.1 For j = 0 to min(i,k) do
2.1.1 If j = 0 or j = k then C(i,j] = 1else C[i,j] = C[i-1, j-1] + C[i-1, j]
3. Output C[n,k]
4. End
-
8/9/2019 Analysis of algorithms-Day3
9/45
Analysis of AlgorithmsUnit 5 - Intractable Problems
-
8/9/2019 Analysis of algorithms-Day3
10/45
Copyright 2004, InfosysTechnologies Ltd
10ER/CORP/CRS/SE15/003
Version No: 2.0
Intractable Problems
Tractable Problems vs. Intractable Problems
Polynomial Problems
NP Problems
P vs NP
NP Complete and NP Hard Problems
-
8/9/2019 Analysis of algorithms-Day3
11/45
Copyright 2004, InfosysTechnologies Ltd
11ER/CORP/CRS/SE15/003
Version No: 2.0
Tractable Problems vs. Intractable Problems
An algorithm for a given problem is said to be a polynomial timealgorithm if itsworst case complexity belongs to O (nk) for a fixed integer k and an input size
of n.
The set of all problems that can be solved in polynomial amount of timearecalled Tractable Problems. These problems can run in a reasonable amountof time for even very large amounts of input data.
The set of all problems that cannot be solved in polynomial amount of time arecalled Intractable Problems. It is of type O( kn). Intractable problems requirehuge amounts of time for even modest input sizes.
-
8/9/2019 Analysis of algorithms-Day3
12/45
Copyright 2004, InfosysTechnologies Ltd
12ER/CORP/CRS/SE15/003
Version No: 2.0
Tractable Problems vs. Intractable Problems(Contd)
1.1 trillion1.1 billion1 million10242n
640002700080001000n3
1600900400100n2
2121478633nlogn
40302010n
5.34.94.33.3logn
11111
-
8/9/2019 Analysis of algorithms-Day3
13/45
Copyright 2004, InfosysTechnologies Ltd
13ER/CORP/CRS/SE15/003
Version No: 2.0
Tractable Problems vs. Intractable Problems(Contd)
All problems
Solvableproblems
Tractablesolutions
Intractable solutions
-
8/9/2019 Analysis of algorithms-Day3
14/45
Copyright 2004, InfosysTechnologies Ltd
14 ER/CORP/CRS/SE15/003
Version No: 2.0
Deterministic Vs Non-Deterministic machines
Deterministic machines:
Conventional Digital machines are Deterministic in nature.
Serialization of resource access
Non Deterministic machines: Hypothetical machine.
More than one job can be done in one unit of time.
-
8/9/2019 Analysis of algorithms-Day3
15/45
Copyright 2004, InfosysTechnologies Ltd
15 ER/CORP/CRS/SE15/003
Version No: 2.0
P-Class problems
Polynomial problems are the set of problems which have polynomial timealgorithms
A formal definition for the same is given below
The class of decision problems that can be solved in polynomial time by
deterministic algorithms is called the P class or Polynomial problems.
Polynomial problems
O(1) -- Constant
O(log n) -- Sub-linear
O(n
) -- LinearO(n log n) -- Nearly linear
O(n2) -- Quadratic
-
8/9/2019 Analysis of algorithms-Day3
16/45
Copyright 2004, InfosysTechnologies Ltd
16 ER/CORP/CRS/SE15/003
Version No: 2.0
NP Problems (Contd)
NP problems are the set of problems which have nondeterministic polynomialtime algorithms
A formal definition for the same is given below
P NP
NP P
The class of decision problems that can be solved in polynomial time by
nondeterministic algorithms is called the NP class or Nondeterministic
Polynomial problems.
Not known
NP
P
-
8/9/2019 Analysis of algorithms-Day3
17/45
Copyright 2004, InfosysTechnologies Ltd
17 ER/CORP/CRS/SE15/003
Version No: 2.0
P Vs NP Deterministic algorithm for searching an
element
d:depth
A binary tree
No of elements 2d+1
-1 Searching an element.
-
8/9/2019 Analysis of algorithms-Day3
18/45
Copyright 2004, InfosysTechnologies Ltd
18 ER/CORP/CRS/SE15/003
Version No: 2.0
P Vs NP (Contd)- Non deterministic algorithm for
searching an element start
Machine having special capability- will check all numbers atone level and it will take one unit of time. Hence this will takeonly 4 unit of time
-
8/9/2019 Analysis of algorithms-Day3
19/45
Copyright 2004, InfosysTechnologies Ltd
19 ER/CORP/CRS/SE15/003
Version No: 2.0
NP Complete problems
A NP Complete problem is one which belongs to the NP class and which has a
surprising property. Every problem in NP class can be reduced to this problemin polynomial time on a deterministic machine
A formal definition for the same is given below
A decision problem Dis said to NP Complete if
1. If it belongs to NP class2. Every problem in the NP class is polynomially reducible to D
-
8/9/2019 Analysis of algorithms-Day3
20/45
Copyright 2004, InfosysTechnologies Ltd
20 ER/CORP/CRS/SE15/003
Version No: 2.0
Examples for NP Complete Problems
Traveling Salesman Problem (TSP): Given n cities and a salesman who is
required to visit each city once, find such a route.
Printed Circuit Board Problem (PCB): Consider a PCB (Printed Circuit Board)
manufacturing unit that churns out several thousands of etched and drilled PC
boards of the same type every day. The final stage of the process is drilling. To
minimize the drilling time required for each board, we need to decide on the
sequence in which holes will be drilled (The drilling time can be cut down by as
much as 50% by sequencing the holes using sophisticated algorithms rather than
relying on an intuitive or visually judged sequence).
-
8/9/2019 Analysis of algorithms-Day3
21/45
Copyright 2004, InfosysTechnologies Ltd
21 ER/CORP/CRS/SE15/003
Version No: 2.0
Examples for NP Complete Problems ctd.,
Bin Packing Problem: Given a set of items, each with a certain weight and a set
of bins, each with a constant weight capacity. You are required to pack the items
using the minimum number of bins.
Knapsack Problem: Given a set of items, each with a certain weight and value
and a knapsack with a certain weight capacity. You are required to pack the
knapsack with items so as to maximize the value of the packed items.
Node Cover Problem: You are given a network that consists of a set nodes, and
a set of edges. An edge is a connection between two nodes. You are required to
find a minimal set of nodes S such that every other edge in this network is
connected to at least one node in S.
-
8/9/2019 Analysis of algorithms-Day3
22/45
Copyright 2004, InfosysTechnologies Ltd
22 ER/CORP/CRS/SE15/003
Version No: 2.0
NP Hard Problems
A problem Pis said to be a NP Hard problem if any problem (not necessarily inNP) is polynomially reducible to P
NP Hard problems are basically the optimization versions of the problems in
NP Complete class
The NP Hard problems are not mere yes/no problems. They are problemswhere in we need to find the optimal solution
-
8/9/2019 Analysis of algorithms-Day3
23/45
Copyright 2004, InfosysTechnologies Ltd
23 ER/CORP/CRS/SE15/003
Version No: 2.0
Summary of Unit-5
Tractable Problems vs. Intractable Problems
Need for polynomial time solutions
Polynomial Problems
Deterministic vs. Non Deterministic Algorithms
NP Problems
P vs. NP
NP Complete and NP Hard Problems
-
8/9/2019 Analysis of algorithms-Day3
24/45
Analysis of Algorithms
Case study
-
8/9/2019 Analysis of algorithms-Day3
25/45
Copyright 2004, InfosysTechnologies Ltd
25 ER/CORP/CRS/SE15/003
Version No: 2.0
SDLC
Requirementsgathering
Design
Build
Test
Deployment
Maintenance
-
8/9/2019 Analysis of algorithms-Day3
26/45
Copyright 2004, InfosysTechnologies Ltd
26 ER/CORP/CRS/SE15/003
Version No: 2.0
Writing a computer program
Problem formulation
& specification
implementation
Design
Testing
Documentation
-
8/9/2019 Analysis of algorithms-Day3
27/45
Copyright 2004, InfosysTechnologies Ltd
27 ER/CORP/CRS/SE15/003
Version No: 2.0
Models
Formalize a problem
Solve this problem
with the existingsolution
Discover what is
known about thismodel.
SolutionExists?
YesNo
Use the properties
to get a goodsolution
-
8/9/2019 Analysis of algorithms-Day3
28/45
Copyright 2004, InfosysTechnologies Ltd
28 ER/CORP/CRS/SE15/003
Version No: 2.0
Solving a Problem
(i) Get a mathematical model
(ii) Construct an algorithm
(iii) Analyse the Algorithm
-
8/9/2019 Analysis of algorithms-Day3
29/45
Copyright 2004, InfosysTechnologies Ltd
29 ER/CORP/CRS/SE15/003
Version No: 2.0
Analysis of algorithms
Help us in:
How to solve a problem?
How efficiently to solve the problem?
Converting the problem into a formal model will help in doing the so.
-
8/9/2019 Analysis of algorithms-Day3
30/45
Copyright 2004, InfosysTechnologies Ltd
30 ER/CORP/CRS/SE15/003
Version No: 2.0
Exercise
Write a program which accepts a string as input and returns 1 if the number ofcharacters in the string is even, else returns 0
Design an algorithm to solve the above problem.
Analyse the algorithm in terms of the resources required.
-
8/9/2019 Analysis of algorithms-Day3
31/45
Copyright 2004, InfosysTechnologies Ltd
31 ER/CORP/CRS/SE15/003
Version No: 2.0
Traffic signal design system
Are all the traffic signal systems alike?
If so, what is the generalised algorithm?
If not, how to design one?
-
8/9/2019 Analysis of algorithms-Day3
32/45
Copyright 2004, InfosysTechnologies Ltd
32 ER/CORP/CRS/SE15/003
Version No: 2.0
TSDS ( Cont.)
What is the input to TSDS?
(i) The intersection (junction) of roads.
(ii) Left / Right Drive.
Objective: To make the maximum
flow of traffic at the intersection
without any permissible collision.
-
8/9/2019 Analysis of algorithms-Day3
33/45
Copyright 2004, InfosysTechnologies Ltd
33 ER/CORP/CRS/SE15/003
Version No: 2.0
TSDS ( Cont.)
The roads A,B,C are two way roads and
D is a one way.
Here there are 9 possible turns
B
A
C
D
-
8/9/2019 Analysis of algorithms-Day3
34/45
Copyright 2004, InfosysTechnologies Ltd
34 ER/CORP/CRS/SE15/003
Version No: 2.0
TSDS (Cont.)
Among the 9 possible turns certain turns can be donesimultaneously like AB and CD.
For simplicity, take AB as AB.
List of possible turns:
AB, AC, AD, BA, BC, BD, CA, CB, CDConsider the Left drive as in our Indian Roads.
B
A
C
D
-
8/9/2019 Analysis of algorithms-Day3
35/45
Copyright 2004, InfosysTechnologies Ltd
35 ER/CORP/CRS/SE15/003Version No: 2.0
Graph representation of incompatible turns
AB
CDCBCA
BDBCBA
ADAC
B
A
C
D
-
8/9/2019 Analysis of algorithms-Day3
36/45
Copyright 2004, InfosysTechnologies Ltd
36 ER/CORP/CRS/SE15/003Version No: 2.0
Graph coloring
B
A
C
D
Graph coloring: the assignment of color to eachnode such that no two nodes connected by an
edge have the same color.
CA, CBYellow
BA, BDBlue
AB, AC, AD, BC, CDRed
TurnsColor
-
8/9/2019 Analysis of algorithms-Day3
37/45
-
8/9/2019 Analysis of algorithms-Day3
38/45
Copyright 2004, InfosysTechnologies Ltd
38 ER/CORP/CRS/SE15/003Version No: 2.0
Analysis
Where is the complexity of the problem Traffic Signal Design System lies?
Number of roads intersecting?
Right / Left drive?
Finding the incompatible turns?
Graph coloring?
-
8/9/2019 Analysis of algorithms-Day3
39/45
Copyright 2004, InfosysTechnologies Ltd
39 ER/CORP/CRS/SE15/003Version No: 2.0
Analysis Number of roads intersecting
If n roads are intersecting,
there will be n * (n-1) turns possible.
Complexity in Big oh notation: O(n2)It is polynomial in nature.
-
8/9/2019 Analysis of algorithms-Day3
40/45
Copyright 2004, InfosysTechnologies Ltd
40 ER/CORP/CRS/SE15/003Version No: 2.0
Analysis Left / Right drive
For the same junction the change of the drive will affect the number ofincompatible turns, but within the n* (n-1) turns.
So, this wont matter much
-
8/9/2019 Analysis of algorithms-Day3
41/45
Copyright 2004, InfosysTechnologies Ltd
41 ER/CORP/CRS/SE15/003Version No: 2.0
Analysis finding the incompatible turns
How to check for the incompatible turns?
Take two possible turns and check for the compatibility ( i.e. can be turnedsimultaneously)
Do the above process with all possible combinations taking into consideration the natureof the road (two/one way) and the drive (left/right).
Its Brute force.
Inefficient !
-
8/9/2019 Analysis of algorithms-Day3
42/45
-
8/9/2019 Analysis of algorithms-Day3
43/45
Copyright 2004, InfosysTechnologies Ltd
43 ER/CORP/CRS/SE15/003Version No: 2.0
Analysis Graph coloring
A reasonable technique that can be applied here is greedy technique
A possible algorithm is:
Step 1: select an uncolored vertex and color it with a new color.
Step 2: check the uncolored vertices. For each uncolored vertices check if
there is any edge to a new colored vertex. If not, then color the present vertexwith the new color.
-
8/9/2019 Analysis of algorithms-Day3
44/45
Copyright 2004, InfosysTechnologies Ltd
44 ER/CORP/CRS/SE15/003Version No: 2.0
Summary of Day-3
Analysis of well known algorithms (continued)
Intractable problems
Deterministic Vs Non-Deterministic machines
P Vs NP
NP Complete
NP Hard
Case Study
-
8/9/2019 Analysis of algorithms-Day3
45/45
Copyright 2004, InfosysTechnologies Ltd
45 ER/CORP/CRS/SE15/003Version No: 2.0
Thank You!