GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Python - Backtracking

Python Data Structure Tutorial
  • Python - DS Home
  • Python - DS Introduction
  • Python - DS Environment
  • Python - Arrays
  • Python - Lists
  • Python - Tuples
  • Python - Dictionary
  • Python - 2-D Array
  • Python - Matrix
  • Python - Sets
  • Python - Maps
  • Python - Linked Lists
  • Python - Stack
  • Python - Queue
  • Python - Dequeue
  • Python - Advanced Linked list
  • Python - Hash Table
  • Python - Binary Tree
  • Python - Search Tree
  • Python - Heaps
  • Python - Graphs
  • Python - Algorithm Design
  • Python - Divide and Conquer
  • Python - Recursion
  • Python - Backtracking
  • Python - Sorting Algorithms
  • Python - Searching Algorithms
  • Python - Graph Algorithms
  • Python - Algorithm Analysis
  • Python - Big-O Notation
  • Python - Algorithm Classes
  • Python - Amortized Analysis
  • Python - Algorithm Justifications

Python Data Structures Useful Resources

  • Python - DS Quick Guide
  • Python - DS Useful Resources
  • Python - DS Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Python - Backtracking



Previous
Quiz
Next

Backtracking is a form of recursion. But it involves choosing only option out of any possibilities. We begin by choosing an option and backtrack from it, if we reach a state where we conclude that this specific option does not give the required solution. We repeat these steps by going across each available option until we get the desired solution.

Below is an example of finding all possible order of arrangements of a given set of letters. When we choose a pair we apply backtracking to verify if that exact pair has already been created or not. If not already created, the pair is added to the answer list else it is ignored.

Example

def permute(list, s):
   if list == 1:
      return s
   else:
      return [ 
         y + x
         for y in permute(1, s)
         for x in permute(list - 1, s)
      ]
print(permute(1, ["a","b","c"]))
print(permute(2, ["a","b","c"]))

Output

When the above code is executed, it produces the following result −

['a', 'b', 'c']
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.