SlideShare a Scribd company logo
Analysis and Design of Algorithms
Deepak John
Department Of Computer Applications, SJCET-Pala
Types of ProblemsTypes of Problems
 Optimization problem: construct a solution that
maximizes or minimizes some objective functionmaximizes or minimizes some objective function
 Decision problem: A question that has two possible
answers, yes and no.
Examples:
•SHORTEST-PATH (optimization)
Given G, u,v, find a path from u to v with fewest edges.
•PATH (decision)
Given G, u,v, and k, whether exist a path from u to vGiven G, u,v, and k, whether exist a path from u to v
consisting of at most k edges.
The class PThe class P
 P is the set of decision problems that can be solved in polynomial
time .if the size of the input to the problem is n then the problemp p p
can be solved in time O(nk) for some constant k.
 Polynomially bounded
an algorithm is said to be polynomilly bounded if its worst case
complexity is bounded by a polynomial function of the input
i (if th i l i l h th t f h i t f isize(if there is a polynomial p such that for each input of size n
the algorithm terminates after at most p(n)steps)
 P: Polynomial algorithms - These include all the sorting P: Polynomial algorithms These include all the sorting
algorithms, with running times on the order of n, lg n,n lg n and n2.
Class NPClass NP
 Class NP: Problems that can be solved in a polynomial number
of steps by a nondeterministic polynomial algorithms.of steps by a nondeterministic polynomial algorithms.
 A nondeterministic algorithm: a two-stage procedure that takes
as its input an instance I of a decision problem and does the
following
1. “guessing” stage: An arbitrary string S is generated that can
b h h f l i f h i ibe thought of as a guess at a solution for the given instance
2. “verification” stage: A deterministic algorithm takes both I
and S as its input and check if S is a solution to instance Iand S as its input and check if S is a solution to instance I,
(outputs yes if s is a solution and outputs no or not halt at all
otherwise)
NP-CompleateNP Compleate
 is the class of problems that are the hardest problems in NP.
 example: example:
A hamiltonian cycle of an undirected graph is a simple
cycle that contains every vertex
The Relationship between P ,NP and NPC
Approximation algorithmsApproximation algorithms
 an algorithm which return solutions that are guaranteed to
be close to an optimal solution.be close to an optimal solution.
 We use performance ratio to measure the quality of an
approximation algorithm.
 We are going to find a Near-Optimal solution for a given
problem.
 We assume two hypothesis :
1. Each potential solution has a positive cost.
2. The problem may be either a maximization or a
minimization problem on the cost.
Performance ratio
 For minimization problem, the performance ratio of algorithm A is
defined as a number r such that for any instance I of the problem,
OPT(I) is the value of the optimal solution for instance I and
A(I) is the value of the solution returned by algorithm A on instance IA(I) is the value of the solution returned by algorithm A on instance I.
 For maximization problem, the performance ratio of algorithm A is
defined as a number r such that for any instance I of the problem,
OPT(I)/A(I)
is at most r (r≥1),
Bin Packing ProblemBin Packing Problem
 Pack a set of N BINS = {1, 2, …, n} items, each with size ti , i=1,
2,…,n, into identical bins, each with capacity C.
 the bin packing problem is to determine the minimum number of bins
to accommodate all items.
 Fi di th ti l l ti i NP h d bl Finding the optimal solution is a NP-hard problem.
 Minimize the number of bins without violating the capacity constraints
Online bin packing
 Items arrive one by one.y
 Each item must be assigned immediately to a bin, without
knowledge of any future items. Reassignment is not allowed.
 There exists no online bin packing algorithm that always finds an
optimal solution.
 Next Fit (NF) First Fit (FF) Best Fit (BF) Next Fit (NF), First Fit (FF), Best Fit (BF)
Offline bin packing
 All n items are known in advance, i.e. before they have to be packed., f y p
 Initially sort the items in decreasing order of size and assign the
largest items first.
 First Fit Decreasing (FFD) ,Best Fit Decreasing (BFD)
Consider items 3; 6; 2; 1; 5; 7; 2; 4; 1; 9: with bin size 10
Next FitNext Fit
 Put the current item in the current bin if it fits, otherwise in the next
bin.
 O(n)
 Ex:[3; 6] - [2; 1; 5] -[7; 2]- [4; 1] - [9]
First Fit
 Put the current item into the first bin it fits into.
O( 2) O(n2)
 Ex:[3; 6; 1] - [2; 5; 2; 1] - [7] - [4] - [9]
Best Fit
 Assign an arriving item to the bin in which it fits bestg g
 Performance of BF and FF is similar.
 Ex:[3;6;1]-[2;5;2;1]-[9]-[4]-[9]
Algorithm First-Fit
1: forAll objects i = 1, 2, . . . , n do
2: forAll bins j = 1, 2, . . . do
3: if Object i fits in bin j then
4: Pack object i in bin j4: Pack object i in bin j.
5: Break the loop and pack the next object.
6: end if
7: end for
8: if Object i did not fit in any available bin then
9 C bi d k bj i9: Create new bin and pack object i.
10: end if
11: end for11: end for
Consider items 3; 6; 2; 1; 5; 7; 2; 4; 1; 9: with bin size 10
First Fit DecreasingFirst Fit Decreasing
1. Sort the objects in decreasing order
2. Apply First Fit strategy to this sorted list.2. Apply First Fit strategy to this sorted list.
Ex:[9; 1] -[7; 3] -[6; 4] -[5; 2; 2; 1]
Best Fit Decreasingg
1. Sort the objects in decreasing order
2. Apply Best Fit strategy to this sorted list.
Graph Coloring ProblemGraph Coloring Problem
 Graph coloring is an assignment of colors to the vertices of a graph.
no two adjacent vertices have the same color
 Chromatic number: is the smallest number of colors with which it
can be colored.
 S ti l l i d id l i Sequential coloring and widgerson coloring
Sequential Graph coloring
1 Gi G (VE) ith ti U th i t {1 2 3 } t1. Given G=(V,E) with n vertices. Use the integers {1,2,3, …, n} to
represent colors. Start by assigning 0 to every vertex. Process the
vertices one at a time.
2. For each vertex, Vi, start by coloring Vi with the color 1.
3. Check the neighbors of Vi to see if any is colored 1. If not then go
t th t t Vi+1to the next vertex, Vi+1.
4. If there is a neighbor colored 1, recolor Vi with color 2, and
repeat the neighbor searchrepeat the neighbor search.
5. Repeat the previous step incrementing the color until we find a
color c that has not been used to color any of Vi’s neighbors.
Example for sequential coloring
Widgerson coloring
 Recursive Algorithm Recursive Algorithm
 A graph with maximum degree ∆ can be easily colored using ∆ +1
colors.
 Base Case: 2 Colorable Graphs
 Find the sub graph of the Neighborhood of a given vertex,
 recursively color this sub graph.
 At most 3√n colors for an n-colorable graph.
Algorithm
Travelling Salesman ProblemTravelling Salesman Problem
 Definition: Find a path through a weighted graph which starts and
ends at the same vertex, includes every other vertex exactly once,, y y ,
and minimizes the total cost of edges.
 TSP is classify as NP-complete problem, that means no
l i l l ith t t ithi t blpolynomial algorithm can guarantee to come within countable
times of the shortest tour.
 TSP : Find a tour of minimum cost (distance)f ( )
 Problem: To find a Hamiltonian cycle of minimal cost.
Nearest Neighbor
1 Pick a reference vertex to start at1. Pick a reference vertex to start at.
2. Walk to its nearest neighbor (i.e., along the shortest possible
edge).(If there is a tie, break it randomly.)
3. At each stage in your tour, walk to the nearest neighbor that
you have not already visited.
4 h h i i d ll i h i4. When you have visited all vertices, return to the starting vertex.
Shortest-Link
 Idea: Start in the middle Idea: Start in the middle.
1. Add the cheapest available edge to your tour.(If there is a tie,
break it randomly.)
2. Repeat until you have a Hamilton circuit.
3. Make sure you add exactly two edges at each vertex.
4. Don't close the circuit until all vertices are in it.

More Related Content

PPTX
data structures- back tracking
PPT
Np completeness
PDF
All pairs shortest path algorithm
PDF
ADA complete notes
PPT
Backtracking
PPTX
System protection in Operating System
PPT
BackTracking Algorithm: Technique and Examples
PPTX
Python Libraries and Modules
data structures- back tracking
Np completeness
All pairs shortest path algorithm
ADA complete notes
Backtracking
System protection in Operating System
BackTracking Algorithm: Technique and Examples
Python Libraries and Modules

What's hot (20)

PPTX
Presentation of daa on approximation algorithm and vertex cover problem
PPT
Parallel algorithms
PDF
I. Alpha-Beta Pruning in ai
PPTX
implementation of travelling salesman problem with complexity ppt
PPTX
Design and Analysis of Algorithms.pptx
PPTX
N queen problem
PPT
Lecture 8 dynamic programming
PPTX
Greedy algorithms
PPTX
0 1 knapsack using branch and bound
PDF
Design and analysis of algorithms
PPTX
Classification and prediction in data mining
PPT
9. Input Output in java
PPTX
Predicate logic
PPT
EULER AND FERMAT THEOREM
PPTX
Dynamic Programming
PPTX
Algorithm Using Divide And Conquer
PPTX
Tsp branch and-bound
PPT
5 csp
PPTX
Depth Buffer Method
PPT
Sum of subsets problem by backtracking 
Presentation of daa on approximation algorithm and vertex cover problem
Parallel algorithms
I. Alpha-Beta Pruning in ai
implementation of travelling salesman problem with complexity ppt
Design and Analysis of Algorithms.pptx
N queen problem
Lecture 8 dynamic programming
Greedy algorithms
0 1 knapsack using branch and bound
Design and analysis of algorithms
Classification and prediction in data mining
9. Input Output in java
Predicate logic
EULER AND FERMAT THEOREM
Dynamic Programming
Algorithm Using Divide And Conquer
Tsp branch and-bound
5 csp
Depth Buffer Method
Sum of subsets problem by backtracking 
Ad

Viewers also liked (8)

PDF
Master theorem
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPTX
Design and Analysis of Algorithms
PPT
Binpacking firstfitalgorithm
PPT
Divide and conquer
PPT
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
PPT
03 algorithm properties
PDF
Design & Analysis Of Algorithm
Master theorem
DESIGN AND ANALYSIS OF ALGORITHMS
Design and Analysis of Algorithms
Binpacking firstfitalgorithm
Divide and conquer
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
03 algorithm properties
Design & Analysis Of Algorithm
Ad

Similar to Analysis and design of algorithms part 4 (20)

PPT
Approx
PDF
Algorithm chapter 10
PDF
Daa notes 2
PPTX
Undecidable Problems and Approximation Algorithms
PPTX
Lower bound theory Np hard & Np completeness
PPTX
P, NP and NP-Complete, Theory of NP-Completeness V2
PPT
Confidence interval two tail tests-lower bounds upperbounds
PPTX
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
PPT
BCA Chapter 5 The Greedy Method Notes.ppt
PDF
Bt0080 fundamentals of algorithms2
PPTX
TRAVELING SALESMAN PROBLEM, Divide and Conquer
PDF
Anlysis and design of algorithms part 1
PPTX
Approximation
PDF
The Ultimate Dynamic Programming RoadMap | Tutort Academy
PDF
Learn Dynamic Programming Roadmap at Tutort Academy
PPT
dynamic programming Rod cutting class
PDF
Mit6 006 f11_quiz1
PPTX
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
Approx
Algorithm chapter 10
Daa notes 2
Undecidable Problems and Approximation Algorithms
Lower bound theory Np hard & Np completeness
P, NP and NP-Complete, Theory of NP-Completeness V2
Confidence interval two tail tests-lower bounds upperbounds
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
BCA Chapter 5 The Greedy Method Notes.ppt
Bt0080 fundamentals of algorithms2
TRAVELING SALESMAN PROBLEM, Divide and Conquer
Anlysis and design of algorithms part 1
Approximation
The Ultimate Dynamic Programming RoadMap | Tutort Academy
Learn Dynamic Programming Roadmap at Tutort Academy
dynamic programming Rod cutting class
Mit6 006 f11_quiz1
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE

More from Deepak John (20)

PDF
Network concepts and wi fi
PDF
Web browser week5 presentation
PDF
Information management
PDF
It security,malware,phishing,information theft
PDF
Email,contacts and calendar
PDF
Module 1 8086
PDF
Module 2 instruction set
PDF
introduction to computers
PDF
Registers and counters
PDF
Computer security module 4
PDF
Module 4 network and computer security
PDF
Network and computer security-
PDF
Computer security module 3
PDF
Module 4 registers and counters
PDF
Module 2 network and computer security
PDF
Computer security module 2
PDF
Computer security module 1
PDF
Network and Computer security
PDF
Combinational and sequential logic
PDF
Module 2 logic gates
Network concepts and wi fi
Web browser week5 presentation
Information management
It security,malware,phishing,information theft
Email,contacts and calendar
Module 1 8086
Module 2 instruction set
introduction to computers
Registers and counters
Computer security module 4
Module 4 network and computer security
Network and computer security-
Computer security module 3
Module 4 registers and counters
Module 2 network and computer security
Computer security module 2
Computer security module 1
Network and Computer security
Combinational and sequential logic
Module 2 logic gates

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Insiders guide to clinical Medicine.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RMMM.pdf make it easy to upload and study
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial disease of the cardiovascular and lymphatic systems
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
VCE English Exam - Section C Student Revision Booklet
Insiders guide to clinical Medicine.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf

Analysis and design of algorithms part 4

  • 1. Analysis and Design of Algorithms Deepak John Department Of Computer Applications, SJCET-Pala
  • 2. Types of ProblemsTypes of Problems  Optimization problem: construct a solution that maximizes or minimizes some objective functionmaximizes or minimizes some objective function  Decision problem: A question that has two possible answers, yes and no. Examples: •SHORTEST-PATH (optimization) Given G, u,v, find a path from u to v with fewest edges. •PATH (decision) Given G, u,v, and k, whether exist a path from u to vGiven G, u,v, and k, whether exist a path from u to v consisting of at most k edges.
  • 3. The class PThe class P  P is the set of decision problems that can be solved in polynomial time .if the size of the input to the problem is n then the problemp p p can be solved in time O(nk) for some constant k.  Polynomially bounded an algorithm is said to be polynomilly bounded if its worst case complexity is bounded by a polynomial function of the input i (if th i l i l h th t f h i t f isize(if there is a polynomial p such that for each input of size n the algorithm terminates after at most p(n)steps)  P: Polynomial algorithms - These include all the sorting P: Polynomial algorithms These include all the sorting algorithms, with running times on the order of n, lg n,n lg n and n2.
  • 4. Class NPClass NP  Class NP: Problems that can be solved in a polynomial number of steps by a nondeterministic polynomial algorithms.of steps by a nondeterministic polynomial algorithms.  A nondeterministic algorithm: a two-stage procedure that takes as its input an instance I of a decision problem and does the following 1. “guessing” stage: An arbitrary string S is generated that can b h h f l i f h i ibe thought of as a guess at a solution for the given instance 2. “verification” stage: A deterministic algorithm takes both I and S as its input and check if S is a solution to instance Iand S as its input and check if S is a solution to instance I, (outputs yes if s is a solution and outputs no or not halt at all otherwise)
  • 5. NP-CompleateNP Compleate  is the class of problems that are the hardest problems in NP.  example: example: A hamiltonian cycle of an undirected graph is a simple cycle that contains every vertex The Relationship between P ,NP and NPC
  • 6. Approximation algorithmsApproximation algorithms  an algorithm which return solutions that are guaranteed to be close to an optimal solution.be close to an optimal solution.  We use performance ratio to measure the quality of an approximation algorithm.  We are going to find a Near-Optimal solution for a given problem.  We assume two hypothesis : 1. Each potential solution has a positive cost. 2. The problem may be either a maximization or a minimization problem on the cost.
  • 7. Performance ratio  For minimization problem, the performance ratio of algorithm A is defined as a number r such that for any instance I of the problem, OPT(I) is the value of the optimal solution for instance I and A(I) is the value of the solution returned by algorithm A on instance IA(I) is the value of the solution returned by algorithm A on instance I.  For maximization problem, the performance ratio of algorithm A is defined as a number r such that for any instance I of the problem, OPT(I)/A(I) is at most r (r≥1),
  • 8. Bin Packing ProblemBin Packing Problem  Pack a set of N BINS = {1, 2, …, n} items, each with size ti , i=1, 2,…,n, into identical bins, each with capacity C.  the bin packing problem is to determine the minimum number of bins to accommodate all items.  Fi di th ti l l ti i NP h d bl Finding the optimal solution is a NP-hard problem.  Minimize the number of bins without violating the capacity constraints
  • 9. Online bin packing  Items arrive one by one.y  Each item must be assigned immediately to a bin, without knowledge of any future items. Reassignment is not allowed.  There exists no online bin packing algorithm that always finds an optimal solution.  Next Fit (NF) First Fit (FF) Best Fit (BF) Next Fit (NF), First Fit (FF), Best Fit (BF) Offline bin packing  All n items are known in advance, i.e. before they have to be packed., f y p  Initially sort the items in decreasing order of size and assign the largest items first.  First Fit Decreasing (FFD) ,Best Fit Decreasing (BFD)
  • 10. Consider items 3; 6; 2; 1; 5; 7; 2; 4; 1; 9: with bin size 10 Next FitNext Fit  Put the current item in the current bin if it fits, otherwise in the next bin.  O(n)  Ex:[3; 6] - [2; 1; 5] -[7; 2]- [4; 1] - [9] First Fit  Put the current item into the first bin it fits into. O( 2) O(n2)  Ex:[3; 6; 1] - [2; 5; 2; 1] - [7] - [4] - [9]
  • 11. Best Fit  Assign an arriving item to the bin in which it fits bestg g  Performance of BF and FF is similar.  Ex:[3;6;1]-[2;5;2;1]-[9]-[4]-[9]
  • 12. Algorithm First-Fit 1: forAll objects i = 1, 2, . . . , n do 2: forAll bins j = 1, 2, . . . do 3: if Object i fits in bin j then 4: Pack object i in bin j4: Pack object i in bin j. 5: Break the loop and pack the next object. 6: end if 7: end for 8: if Object i did not fit in any available bin then 9 C bi d k bj i9: Create new bin and pack object i. 10: end if 11: end for11: end for
  • 13. Consider items 3; 6; 2; 1; 5; 7; 2; 4; 1; 9: with bin size 10 First Fit DecreasingFirst Fit Decreasing 1. Sort the objects in decreasing order 2. Apply First Fit strategy to this sorted list.2. Apply First Fit strategy to this sorted list. Ex:[9; 1] -[7; 3] -[6; 4] -[5; 2; 2; 1] Best Fit Decreasingg 1. Sort the objects in decreasing order 2. Apply Best Fit strategy to this sorted list.
  • 14. Graph Coloring ProblemGraph Coloring Problem  Graph coloring is an assignment of colors to the vertices of a graph. no two adjacent vertices have the same color  Chromatic number: is the smallest number of colors with which it can be colored.  S ti l l i d id l i Sequential coloring and widgerson coloring
  • 15. Sequential Graph coloring 1 Gi G (VE) ith ti U th i t {1 2 3 } t1. Given G=(V,E) with n vertices. Use the integers {1,2,3, …, n} to represent colors. Start by assigning 0 to every vertex. Process the vertices one at a time. 2. For each vertex, Vi, start by coloring Vi with the color 1. 3. Check the neighbors of Vi to see if any is colored 1. If not then go t th t t Vi+1to the next vertex, Vi+1. 4. If there is a neighbor colored 1, recolor Vi with color 2, and repeat the neighbor searchrepeat the neighbor search. 5. Repeat the previous step incrementing the color until we find a color c that has not been used to color any of Vi’s neighbors.
  • 17. Widgerson coloring  Recursive Algorithm Recursive Algorithm  A graph with maximum degree ∆ can be easily colored using ∆ +1 colors.  Base Case: 2 Colorable Graphs  Find the sub graph of the Neighborhood of a given vertex,  recursively color this sub graph.  At most 3√n colors for an n-colorable graph.
  • 19. Travelling Salesman ProblemTravelling Salesman Problem  Definition: Find a path through a weighted graph which starts and ends at the same vertex, includes every other vertex exactly once,, y y , and minimizes the total cost of edges.  TSP is classify as NP-complete problem, that means no l i l l ith t t ithi t blpolynomial algorithm can guarantee to come within countable times of the shortest tour.  TSP : Find a tour of minimum cost (distance)f ( )  Problem: To find a Hamiltonian cycle of minimal cost.
  • 20. Nearest Neighbor 1 Pick a reference vertex to start at1. Pick a reference vertex to start at. 2. Walk to its nearest neighbor (i.e., along the shortest possible edge).(If there is a tie, break it randomly.) 3. At each stage in your tour, walk to the nearest neighbor that you have not already visited. 4 h h i i d ll i h i4. When you have visited all vertices, return to the starting vertex.
  • 21. Shortest-Link  Idea: Start in the middle Idea: Start in the middle. 1. Add the cheapest available edge to your tour.(If there is a tie, break it randomly.) 2. Repeat until you have a Hamilton circuit. 3. Make sure you add exactly two edges at each vertex. 4. Don't close the circuit until all vertices are in it.