SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite Automata
Adel Al-Ofairi
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 

Tendances (20)

Superscalar Architecture_AIUB
Superscalar Architecture_AIUBSuperscalar Architecture_AIUB
Superscalar Architecture_AIUB
 
NP Complete Problems
NP Complete ProblemsNP Complete Problems
NP Complete Problems
 
parallel programming models
 parallel programming models parallel programming models
parallel programming models
 
SRAM DRAM
SRAM DRAMSRAM DRAM
SRAM DRAM
 
GRAPH COLORING AND ITS APPLICATIONS
GRAPH COLORING AND ITS APPLICATIONSGRAPH COLORING AND ITS APPLICATIONS
GRAPH COLORING AND ITS APPLICATIONS
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Divide&Conquer & Dynamic Programming
Divide&Conquer & Dynamic ProgrammingDivide&Conquer & Dynamic Programming
Divide&Conquer & Dynamic Programming
 
Lecture 16 memory bounded search
Lecture 16 memory bounded searchLecture 16 memory bounded search
Lecture 16 memory bounded search
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite Automata
 
Np completeness
Np completenessNp completeness
Np completeness
 
Matrix multiplication
Matrix multiplicationMatrix multiplication
Matrix multiplication
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
 
Regular Expressions To Finite Automata
Regular Expressions To Finite AutomataRegular Expressions To Finite Automata
Regular Expressions To Finite Automata
 
Rabin karp string matching algorithm
Rabin karp string matching algorithmRabin karp string matching algorithm
Rabin karp string matching algorithm
 
Greedy algorithm activity selection fractional
Greedy algorithm activity selection fractionalGreedy algorithm activity selection fractional
Greedy algorithm activity selection fractional
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Automata theory -- NFA and DFA construction
Automata theory -- NFA and DFA  constructionAutomata theory -- NFA and DFA  construction
Automata theory -- NFA and DFA construction
 

En vedette

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Binpacking firstfitalgorithm
Binpacking firstfitalgorithmBinpacking firstfitalgorithm
Binpacking firstfitalgorithm
jay_sean
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
Lincoln School
 

En vedette (8)

Master theorem
Master theoremMaster theorem
Master theorem
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Binpacking firstfitalgorithm
Binpacking firstfitalgorithmBinpacking firstfitalgorithm
Binpacking firstfitalgorithm
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 

Similaire à Analysis and design of algorithms part 4

Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
chidabdu
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notation
zukun
 

Similaire à Analysis and design of algorithms part 4 (20)

Approx
ApproxApprox
Approx
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
 
P, NP and NP-Complete, Theory of NP-Completeness V2
P, NP and NP-Complete, Theory of NP-Completeness V2P, NP and NP-Complete, Theory of NP-Completeness V2
P, NP and NP-Complete, Theory of NP-Completeness V2
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Approximation
ApproximationApproximation
Approximation
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Mit6 006 f11_quiz1
Mit6 006 f11_quiz1Mit6 006 f11_quiz1
Mit6 006 f11_quiz1
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 5
Unit 5Unit 5
Unit 5
 
Internship
InternshipInternship
Internship
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Teori pnp
Teori pnpTeori pnp
Teori pnp
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notation
 

Plus de Deepak John

introduction to computers
 introduction to computers introduction to computers
introduction to computers
Deepak John
 
Registers and counters
Registers and counters Registers and counters
Registers and counters
Deepak John
 
Module 2 logic gates
Module 2  logic gatesModule 2  logic gates
Module 2 logic gates
Deepak John
 

Plus de Deepak John (20)

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

Dernier

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

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.