SlideShare a Scribd company logo
1 of 21
ALGORITHMS Algorithms
BACKTRACKING
Backtracking is a refinement of the brute force approach, which
systematically searches for a solution to a problem among all
available options. It does so by assuming that the solutions are
represented by vectors (v1,v2, ..., vm) of values and by traversing, in a
depth first manner, the domains of the vectors until the solutions are
found.
When invoked, the algorithm starts with an empty vector. At each
stage it extends the partial vector with a new value. Upon reaching a
partial vector (v1,v2, ..., vi) which can’t represent a partial solution, the
algorithm backtracks by removing the trailing value from the vector,
and then proceeds by trying to extend the vector with alternative
values.
ALGORITHM :
try(v1,v2, ..., vi){
IF (v1,v2, ..., vi) is a solution
THEN RETURN (v1,v2, ..., vi)
FOR each v DO
IF (v1,v2, ..., vi,v) is acceptable vector
THEN sol = try(v1,v2, ..., vi,v)
IF sol ≠ () THEN RETURN sol
END
END
RETURN ()
}
If Si is the domain of vi , then S1 × ... × Sm is the solution space of
the problem. The validity criteria used in checking for acceptable
vectors determines what portion of that space needs to be searched,
and so it also determines the resources required by the algorithm.
The traversal of the solution space can be represented by a depth-
first traversal of a tree. The tree itself is rarely entirely stored by the
algorithm in discourse; instead just a path toward a root is stored, to
enable the backtracking.
1. Traveling Salesperson
The problem assumes a set of n cities, and a salesperson which needs
to visit each city exactly once and return to the base city at the end.
The solution should provide a route of minimal length. The route (a,
b, d, c) is the shortest one for the following one, and its length is 51.
The traveling salesperson problem is an NP-hard problem, and so no
polynomial time algorithm is available for it. Given an instance G =
(V, E) the backtracking algorithm may search for a vector of cities (v1,
..., v|V|) which represents the best route.
The validity criteria may just check for number of cities in of the
routes, pruning out routes longer than |V|. In such a case, the
algorithm needs to investigate |V||V| vectors from the solution space.
On the other hand, the validity criteria may check for repetition of
cities, in which case the number of vectors reduces to |V |!.
THE N QUEENS PROBLEM
Consider a n by n chess board, and the problem of placing n queens
on the board without the queens threatening one another.
The solution space is {1, 2, 3, ..., n}n. The backtracking algorithm may
record the columns where the different queens are positioned. Trying
all vectors (p1, ..., pn) implies nn cases. Noticing that all the queens
must reside in different columns reduces the number of cases to n!.
For the latter case, the root of the traversal tree has degree n, the
children have degree n - 1, the grand children degree n - 2, and so
forth.
The solution space is {1, 2, 3, ..., n}n. The backtracking algorithm may
record the columns where the different queens are positioned. Trying
all vectors (p1, ..., pn) implies nn cases. Noticing that all the queens
must reside in different columns reduces the number of cases to n!.
For the latter case, the root of the traversal tree has degree n, the
children have degree n - 1, the grand children degree n - 2, and so
forth.
GENERATING PERMUTATIONS
A permutation can be obtained by selecting an element in the given
set and recursively permuting the remaining elements.
At each stage of the permutation process, the given set of elements
consists of two parts: a subset of values that already have been
processed, and a subset that still needs to be processed. This logical
separation can be physically realized by exchanging, in the ith step,
the ith value with the value being chosen at that stage. That
approaches leaves the first subset in the first i locations of the
outcome.
permute(i)
{
if i == N output A[N]
else
for j = i to N do
swap(A[i], A[j])
permute(i+1)
swap(A[i], A[j])
}

More Related Content

What's hot

Networks dijkstra's algorithm- pgsr
Networks  dijkstra's algorithm- pgsrNetworks  dijkstra's algorithm- pgsr
Networks dijkstra's algorithm- pgsr
Linawati Adiman
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
meisamstar
 
International Publication - (Calcolo)
International Publication - (Calcolo)International Publication - (Calcolo)
International Publication - (Calcolo)
Alberto Auricchio
 
A Biker Travels 60 Miles In 2
A Biker Travels 60 Miles In 2A Biker Travels 60 Miles In 2
A Biker Travels 60 Miles In 2
AdvancedAlgebra
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
gsp1294
 

What's hot (20)

Networks dijkstra's algorithm- pgsr
Networks  dijkstra's algorithm- pgsrNetworks  dijkstra's algorithm- pgsr
Networks dijkstra's algorithm- pgsr
 
Flat
FlatFlat
Flat
 
Pre-Cal 40S March 19, 2009
Pre-Cal 40S March 19, 2009Pre-Cal 40S March 19, 2009
Pre-Cal 40S March 19, 2009
 
25 String Matching
25 String Matching25 String Matching
25 String Matching
 
Fsa
FsaFsa
Fsa
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
implementation of travelling salesman problem with complexity ppt
implementation of travelling salesman problem with complexity pptimplementation of travelling salesman problem with complexity ppt
implementation of travelling salesman problem with complexity ppt
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
Matlab calculus
Matlab calculusMatlab calculus
Matlab calculus
 
MC0082 –Theory of Computer Science
MC0082 –Theory of Computer ScienceMC0082 –Theory of Computer Science
MC0082 –Theory of Computer Science
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
International Publication - (Calcolo)
International Publication - (Calcolo)International Publication - (Calcolo)
International Publication - (Calcolo)
 
A Biker Travels 60 Miles In 2
A Biker Travels 60 Miles In 2A Biker Travels 60 Miles In 2
A Biker Travels 60 Miles In 2
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
 
Self Stabilizing Depth-first Search
Self Stabilizing Depth-first SearchSelf Stabilizing Depth-first Search
Self Stabilizing Depth-first Search
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
 
isomorphism
 isomorphism isomorphism
isomorphism
 
Stochastic matrices
Stochastic matricesStochastic matrices
Stochastic matrices
 

Viewers also liked

0525 導入ゼミ 中込優介「ウォークラリーレポート」
0525 導入ゼミ 中込優介「ウォークラリーレポート」0525 導入ゼミ 中込優介「ウォークラリーレポート」
0525 導入ゼミ 中込優介「ウォークラリーレポート」
13n1072
 
Ttcn ingenierie protocoles-poly4
Ttcn ingenierie protocoles-poly4Ttcn ingenierie protocoles-poly4
Ttcn ingenierie protocoles-poly4
hemanth kumar sonti
 
Evaluation Question 1 - In what ways does your media product develop or chall...
Evaluation Question 1 - In what ways does your media product develop or chall...Evaluation Question 1 - In what ways does your media product develop or chall...
Evaluation Question 1 - In what ways does your media product develop or chall...
29556
 
CV_Martin_VESELY-20160309e
CV_Martin_VESELY-20160309eCV_Martin_VESELY-20160309e
CV_Martin_VESELY-20160309e
Martin Vesel
 

Viewers also liked (18)

17 грудня у білозерській зош і ііі ст
17 грудня у білозерській зош і ііі ст17 грудня у білозерській зош і ііі ст
17 грудня у білозерській зош і ііі ст
 
Proverbs 27a
Proverbs 27aProverbs 27a
Proverbs 27a
 
Slide share The Ultimate 'Train the Trainers' Service Excellence Learning a...
Slide share   The Ultimate 'Train the Trainers' Service Excellence Learning a...Slide share   The Ultimate 'Train the Trainers' Service Excellence Learning a...
Slide share The Ultimate 'Train the Trainers' Service Excellence Learning a...
 
Agile project management
Agile project managementAgile project management
Agile project management
 
Mophong
MophongMophong
Mophong
 
Enfermedades
Enfermedades Enfermedades
Enfermedades
 
Escasa comprension lectora
Escasa comprension lectoraEscasa comprension lectora
Escasa comprension lectora
 
Consumo colaborativo na era da Internet - A manifestação do mercado colaborat...
Consumo colaborativo na era da Internet - A manifestação do mercado colaborat...Consumo colaborativo na era da Internet - A manifestação do mercado colaborat...
Consumo colaborativo na era da Internet - A manifestação do mercado colaborat...
 
0525 導入ゼミ 中込優介「ウォークラリーレポート」
0525 導入ゼミ 中込優介「ウォークラリーレポート」0525 導入ゼミ 中込優介「ウォークラリーレポート」
0525 導入ゼミ 中込優介「ウォークラリーレポート」
 
Ttcn ingenierie protocoles-poly4
Ttcn ingenierie protocoles-poly4Ttcn ingenierie protocoles-poly4
Ttcn ingenierie protocoles-poly4
 
Dr Zubair Anwar CV
Dr Zubair Anwar CVDr Zubair Anwar CV
Dr Zubair Anwar CV
 
Catalogo enxebrebooks
Catalogo enxebrebooksCatalogo enxebrebooks
Catalogo enxebrebooks
 
Evaluation Question 1 - In what ways does your media product develop or chall...
Evaluation Question 1 - In what ways does your media product develop or chall...Evaluation Question 1 - In what ways does your media product develop or chall...
Evaluation Question 1 - In what ways does your media product develop or chall...
 
Coaching
CoachingCoaching
Coaching
 
CV_Martin_VESELY-20160309e
CV_Martin_VESELY-20160309eCV_Martin_VESELY-20160309e
CV_Martin_VESELY-20160309e
 
Sihma submission refugees_amendment_bill
Sihma submission refugees_amendment_billSihma submission refugees_amendment_bill
Sihma submission refugees_amendment_bill
 
Jurnal CDIMM_dec 2014_Europe Direct Maramures
Jurnal CDIMM_dec 2014_Europe Direct MaramuresJurnal CDIMM_dec 2014_Europe Direct Maramures
Jurnal CDIMM_dec 2014_Europe Direct Maramures
 
Invisible cloak!!
Invisible cloak!!Invisible cloak!!
Invisible cloak!!
 

Similar to Algo

Numerical
NumericalNumerical
Numerical
1821986
 

Similar to Algo (20)

Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 
Daa chapter11
Daa chapter11Daa chapter11
Daa chapter11
 
Brute force
Brute forceBrute force
Brute force
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
backtracking 8 Queen.pptx
backtracking 8 Queen.pptxbacktracking 8 Queen.pptx
backtracking 8 Queen.pptx
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
Numerical
NumericalNumerical
Numerical
 
Svm vs ls svm
Svm vs ls svmSvm vs ls svm
Svm vs ls svm
 
Parallel algorithm in linear algebra
Parallel algorithm in linear algebraParallel algorithm in linear algebra
Parallel algorithm in linear algebra
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Solution 3.
Solution 3.Solution 3.
Solution 3.
 
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
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Back tracking
Back trackingBack tracking
Back tracking
 

More from Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
Selection sort
Selection sortSelection sort
Selection sort
Raghu nath
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
Raghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
Raghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 

More from Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Perl
PerlPerl
Perl
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

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...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
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
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Algo

  • 2. BACKTRACKING Backtracking is a refinement of the brute force approach, which systematically searches for a solution to a problem among all available options. It does so by assuming that the solutions are represented by vectors (v1,v2, ..., vm) of values and by traversing, in a depth first manner, the domains of the vectors until the solutions are found. When invoked, the algorithm starts with an empty vector. At each stage it extends the partial vector with a new value. Upon reaching a partial vector (v1,v2, ..., vi) which can’t represent a partial solution, the algorithm backtracks by removing the trailing value from the vector, and then proceeds by trying to extend the vector with alternative values.
  • 3. ALGORITHM : try(v1,v2, ..., vi){ IF (v1,v2, ..., vi) is a solution THEN RETURN (v1,v2, ..., vi) FOR each v DO IF (v1,v2, ..., vi,v) is acceptable vector THEN sol = try(v1,v2, ..., vi,v) IF sol ≠ () THEN RETURN sol END END RETURN () }
  • 4. If Si is the domain of vi , then S1 × ... × Sm is the solution space of the problem. The validity criteria used in checking for acceptable vectors determines what portion of that space needs to be searched, and so it also determines the resources required by the algorithm. The traversal of the solution space can be represented by a depth- first traversal of a tree. The tree itself is rarely entirely stored by the algorithm in discourse; instead just a path toward a root is stored, to enable the backtracking.
  • 5.
  • 6. 1. Traveling Salesperson The problem assumes a set of n cities, and a salesperson which needs to visit each city exactly once and return to the base city at the end. The solution should provide a route of minimal length. The route (a, b, d, c) is the shortest one for the following one, and its length is 51.
  • 7.
  • 8. The traveling salesperson problem is an NP-hard problem, and so no polynomial time algorithm is available for it. Given an instance G = (V, E) the backtracking algorithm may search for a vector of cities (v1, ..., v|V|) which represents the best route. The validity criteria may just check for number of cities in of the routes, pruning out routes longer than |V|. In such a case, the algorithm needs to investigate |V||V| vectors from the solution space.
  • 9.
  • 10.
  • 11. On the other hand, the validity criteria may check for repetition of cities, in which case the number of vectors reduces to |V |!.
  • 12.
  • 13. THE N QUEENS PROBLEM Consider a n by n chess board, and the problem of placing n queens on the board without the queens threatening one another. The solution space is {1, 2, 3, ..., n}n. The backtracking algorithm may record the columns where the different queens are positioned. Trying all vectors (p1, ..., pn) implies nn cases. Noticing that all the queens must reside in different columns reduces the number of cases to n!. For the latter case, the root of the traversal tree has degree n, the children have degree n - 1, the grand children degree n - 2, and so forth.
  • 14.
  • 15. The solution space is {1, 2, 3, ..., n}n. The backtracking algorithm may record the columns where the different queens are positioned. Trying all vectors (p1, ..., pn) implies nn cases. Noticing that all the queens must reside in different columns reduces the number of cases to n!. For the latter case, the root of the traversal tree has degree n, the children have degree n - 1, the grand children degree n - 2, and so forth.
  • 16.
  • 17. GENERATING PERMUTATIONS A permutation can be obtained by selecting an element in the given set and recursively permuting the remaining elements.
  • 18.
  • 19. At each stage of the permutation process, the given set of elements consists of two parts: a subset of values that already have been processed, and a subset that still needs to be processed. This logical separation can be physically realized by exchanging, in the ith step, the ith value with the value being chosen at that stage. That approaches leaves the first subset in the first i locations of the outcome.
  • 20.
  • 21. permute(i) { if i == N output A[N] else for j = i to N do swap(A[i], A[j]) permute(i+1) swap(A[i], A[j]) }