Escolar Documentos
Profissional Documentos
Cultura Documentos
Day 3
Plan for Day-3
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:
ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 4
Technologies Ltd Version No: 2.0
Dynamic Programming – Matrix multiplication
(Contd…)
ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 5
Technologies Ltd Version No: 2.0
Dynamic Programming – Computing a Binomial
Coefficient
• 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 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
• Polynomial Problems
• NP Problems
• P vs NP
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
n 10 20 30 40
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
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
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
• 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
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
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
• 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
• Polynomial Problems
• NP Problems
• P vs. NP
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
ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 27
Technologies Ltd Version No: 2.0
Solving a Problem
• (i) Get a mathematical model
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
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?
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.
ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 32
Technologies Ltd Version No: 2.0
TSDS ( Cont.)
B D
ER/CORP/CRS/SE15/003
Copyright © 2004, Infosys 33
Technologies Ltd Version No: 2.0
TSDS (Cont.)
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
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?
• 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.
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.
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
• 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