Você está na página 1de 45

Analysis of Algorithms

Day 3
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)

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 2
Technologies Ltd Version No: 2.0
Dynamic Programming
• Dynamic Programming is a design principle which is used to solve problems
with 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 and
distinct various in Dynamic Programming they are overlapping.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 3
Technologies Ltd 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

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 4
Technologies Ltd 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
– ( M1 x ( M2 x M3 ) ) x M4 will need 10 x 1 x 100 = 1000 multiplications

• Total number of multiplications = 2200.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 5
Technologies Ltd Version No: 2.0
Dynamic Programming – Computing a Binomial
Coefficient

• The Binomial Coefficient, denoted by C(n,k), is the number of combinations of


k elements 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

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 6
Technologies Ltd Version No: 2.0
Dynamic Programming – Computing a Binomial
Coefficient (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)

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 7
Technologies Ltd Version No: 2.0
Dynamic Programming – Computing a Binomial
Coefficient (Contd…)
Computing a Binomial Coefficient:

1. Begin
2. For i = 0 to n do
2.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

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 8
Technologies Ltd Version No: 2.0
Analysis of Algorithms
Unit 5 - Intractable Problems
Intractable Problems

• Tractable Problems vs. Intractable Problems

• Polynomial Problems

• NP Problems

• P vs NP

• NP Complete and NP Hard Problems

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 10
Technologies Ltd Version No: 2.0
Tractable Problems vs. Intractable Problems
• An algorithm for a given problem is said to be a polynomial time algorithm if it’s
worst 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 time are
called Tractable Problems. These problems can run in a reasonable amount
of time for even very large amounts of input data.

• The set of all problems that cannot be solved in polynomial amount of time are
called Intractable Problems. It is of type O( kn). Intractable problems require
huge amounts of time for even modest input sizes.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 11
Technologies Ltd Version No: 2.0
Tractable Problems vs. Intractable Problems
(Contd…)

1 1 1 1 1

logn 3.3 4.3 4.9 5.3

n 10 20 30 40

nlogn 33 86 147 212

n2 100 400 900 1600

n3 1000 8000 27000 64000

2n 1024 1 million 1.1 billion 1.1 trillion

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 12
Technologies Ltd Version No: 2.0
Tractable Problems vs. Intractable Problems
(Contd…)

All problems

Intractable solutions

Tractable
solutions

Solvable
problems

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 13
Technologies Ltd 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 14
Technologies Ltd Version No: 2.0
P-Class problems
• Polynomial problems are the set of problems which have polynomial time
algorithms

• 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) -- Linear
O(n log n) -- Nearly linear
O(n2) -- Quadratic

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 15
Technologies Ltd Version No: 2.0
NP Problems (Contd…)
• NP problems are the set of problems which have nondeterministic polynomial
time algorithms

• A formal definition for the same is given below

The class of decision problems that can be solved in polynomial time by


nondeterministic algorithms is called the NP class or Nondeterministic
Polynomial problems.

• P ⊆ NP NP
Not known
• NP ⊆ P
P

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 16
Technologies Ltd 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 17
Technologies Ltd Version No: 2.0
P Vs NP (Contd…)- Non deterministic algorithm for
searching an element
start

Machine having special capability- will check all numbers at


one level and it will take one unit of time. Hence this will take
only 4 unit of time

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 18
Technologies Ltd 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 problem
in polynomial time on a deterministic machine

• A formal definition for the same is given below

A decision problem D is said to NP Complete if


1. If it belongs to NP class
2. Every problem in the NP class is polynomially reducible to D

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 19
Technologies Ltd 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).

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 20
Technologies Ltd 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 21
Technologies Ltd Version No: 2.0
NP Hard Problems
• A problem P is said to be a NP Hard problem if any problem (not necessarily in
NP) 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 problems
where in we need to find the optimal solution

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 22
Technologies Ltd 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

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 23
Technologies Ltd Version No: 2.0
Analysis of Algorithms

Case study
SDLC

Requirements
gathering
Design
Build
Test

Deployment
Maintenance

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 25
Technologies Ltd Version No: 2.0
Writing a computer program

Problem formulation
& specification
Design
implementation

Testing
Documentation

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 26
Technologies Ltd Version No: 2.0
Models

Formalize a problem

Solve this problem Yes


No Discover what is
Solution
with the existing known about this
Exists?
solution model.

Use the properties


to get a good
solution

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 27
Technologies Ltd Version No: 2.0
Solving a Problem
• (i) Get a mathematical model

• (ii) Construct an algorithm

• (iii) Analyse the Algorithm

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 28
Technologies Ltd 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 29
Technologies Ltd Version No: 2.0
Exercise

• Write a program which accepts a string as input and returns 1 if the number of
characters 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 30
Technologies Ltd 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?

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 31
Technologies Ltd 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 32
Technologies Ltd Version No: 2.0
TSDS ( Cont.)

B D

A The roads A,B,C are two way roads and


D is a one way.
Here there are 9 possible turns

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 33
Technologies Ltd Version No: 2.0
TSDS (Cont.)

Among the 9 possible turns certain turns can be done


simultaneously like AÆB and CÆD.

For simplicity, take AÆB as AB.

List of possible turns:


AB, AC, AD, BA, BC, BD, CA, CB, CD
Consider the Left drive as in our Indian Roads. C

B D

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 34
Technologies Ltd Version No: 2.0
Graph representation of incompatible turns

AB AC AD

BA BC BD
C

B D
CA CB CD

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 35
Technologies Ltd Version No: 2.0
Graph coloring

Graph coloring: the assignment of color to each


node such that no two nodes connected by an
edge have the same color.
Color Turns

Red AB, AC, AD, BC, CD

Blue BA, BD

Yellow CA, CB
B D

A
ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 36
Technologies Ltd Version No: 2.0
Graph coloring

AB AC AD

BA BC BD C

B D

CA CB CD
A

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 37
Technologies Ltd Version 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?

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 38
Technologies Ltd Version 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.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 39
Technologies Ltd Version No: 2.0
Analysis – Left / Right drive
• For the same junction the change of the drive will affect the number of
incompatible turns, but within the n* (n-1) turns.

• So, this won’t matter much

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 40
Technologies Ltd Version 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 turned
simultaneously)
• Do the above process with all possible combinations taking into consideration the nature
of the road (two/one way) and the drive (left/right).
• Its Brute force.
• Inefficient !

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 41
Technologies Ltd Version No: 2.0
Analysis – Graph Coloring
• Coloring the graph with a few colors as possible, belongs to the class of
problems called “ NP Complete problems”
• Exhaustive checking to be done
• Inefficient!
• How to solve this graph coloring problem?

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 42
Technologies Ltd Version 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 vertex
with the new color.

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 43
Technologies Ltd Version 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

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 44
Technologies Ltd Version No: 2.0
Thank You!

ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 45
Technologies Ltd Version No: 2.0

Você também pode gostar