Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • The 11 Best Fans to Buy Before It Gets Hot Again (2026)
    • A look at Dylan Patel’s SemiAnalysis, an AI newsletter and research firm that expects $100M+ in 2026 revenue from subscriptions and AI supply chain research (Abram Brown/The Information)
    • ‘Euphoria’ Season 3 Release Schedule: When Does Episode 2 Come Out?
    • Francis Bacon and the Scientific Method
    • Proxy-Pointer RAG: Structure Meets Scale at 100% Accuracy with Smarter Retrieval
    • Sulfur lava exoplanet L 98-59 d defies classification
    • Hisense U7SG TV Review (2026): Better Design, Great Value
    • Google is in talks with Marvell Technology to develop a memory processing unit that works alongside TPUs, and a new TPU for running AI models (Qianer Liu/The Information)
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Sunday, April 19
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»A Gentle Introduction to Backtracking
    Artificial Intelligence

    A Gentle Introduction to Backtracking

    Editor Times FeaturedBy Editor Times FeaturedJuly 1, 2025No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    is a flexible method for exploring the answer area of varied varieties of information science issues and incrementally establishing candidate options – a bit like navigating a maze. On this article, we are going to briefly go over the idea of backtracking earlier than diving into a few intuitive, hands-on examples coded in Python.

    Observe: All instance code snippets within the following sections have been created by the writer of this text.

    Conceptual Overview

    At a excessive degree, the backtracking method includes a step-by-step exploration of the answer area of a computational drawback (normally an issue that may be framed as certainly one of constraint satisfaction or combinatorial optimization). At every step within the exploration, we proceed alongside completely different paths, checking that the issue constraints are glad as we go alongside.

    If we come across a sound answer throughout our exploration, we make an observation of it. At this level, we will finish the search if our drawback solely requires us to search out one legitimate answer. If the issue calls for locating a number of (or all) doable options, we will proceed to discover extensions of the beforehand found answer.

    Nonetheless, if at any level the issue constraints are violated, we backtrack; this implies going again to the final level in our search the place a partial answer had been constructed (and the place legitimate options nonetheless appeared doable), and persevering with our search alongside a special path from there. This forward-and-backward technique of exploration might be continued as wanted till the whole answer area is explored and all legitimate options are explored.

    Palms-On Examples

    Fixing a Sudoku

    A Sudoku puzzle is a basic instance of a constraint satisfaction drawback with sensible purposes in numerous fields starting from operations research to cryptography. The usual model of the puzzle consists of a 9-by-9 grid, fabricated from 9 non-overlapping 3-by-3 sub-grids (or blocks). Within the beginning configuration of the puzzle, a number of the 81 cells within the grid are prefilled with digits starting from 1 to 9. To finish the puzzle, the remaining cells should be full of digits from 1 to 9 whereas adhering to the next constraints: no row, column, or 3-by-3 block could comprise a reproduction digit.

    The Python code under reveals easy methods to implement a Sudoku solver utilizing backtracking, together with a comfort perform for pretty-printing the grid. Observe that the solver expects empty cells to be denoted (or initialized) with zeros.

    from copy import deepcopy
    
    def is_valid(board, row, col, num):
        # Test if num will not be within the present row or column
        for i in vary(9):
            if board[row][i] == num or board[i][col] == num:
                return False
        # Test if num will not be within the 3-by-3 block
        start_row, start_col = 3 * (row // 3), 3 * (col // 3)
        for i in vary(start_row, start_row + 3):
            for j in vary(start_col, start_col + 3):
                if board[i][j] == num:
                    return False
        return True
    
    def find_empty_cell(board):
        # Discover the subsequent empty cell (denoted by 0)
        # Return (row, col) or None if puzzle is full
        for row in vary(9):
            for col in vary(9):
                if board[row][col] == 0:
                    return row, col
        return None
    
    def solve_board(board):
        empty = find_empty_cell(board)
        if not empty:
            return True  # Solved
        row, col = empty
        for num in vary(1, 10):
            if is_valid(board, row, col, num):
                board[row][col] = num
                if solve_board(board):
                    return True
                board[row][col] = 0  # Backtrack
        return False
    
    def solve_sudoku(start_state):
        board_copy = deepcopy(start_state)  # Keep away from overwriting unique puzzle
        if solve_board(board_copy):
            return board_copy
        else:
            elevate ValueError("No answer exists for the given Sudoku puzzle")
    
    def print_board(board):
        for i, row in enumerate(board):
            if i > 0 and that i % 3 == 0:
                print("-" * 21)
            for j, num in enumerate(row):
                if j > 0 and j % 3 == 0:
                    print("|", finish=" ")
                print(num if num != 0 else ".", finish=" ")
            print()

    Now, suppose we enter a Sudoku puzzle, initializing empty cells with zeros, and run the solver as follows:

    puzzle = [
        [5, 0, 0, 0, 3, 0, 0, 0, 7],
        [0, 0, 0, 4, 2, 7, 0, 0, 0],
        [0, 2, 0, 0, 6, 0, 0, 4, 0],
        [0, 1, 0, 0, 9, 0, 0, 2, 0],
        [0, 7, 0, 0, 0, 0, 0, 5, 0],
        [4, 0, 6, 0, 0, 0, 7, 0, 1],
        [0, 4, 2, 0, 7, 0, 6, 1, 0],
        [0, 0, 0, 0, 4, 0, 0, 0, 0],
        [7, 0, 0, 9, 5, 6, 0, 0, 2],
    ]
    
    answer = solve_sudoku(puzzle)
    print_board(answer)

    The solver will produce the next answer inside milliseconds:

    5 6 4 | 1 3 9 | 2 8 7 
    1 9 8 | 4 2 7 | 5 6 3 
    3 2 7 | 8 6 5 | 1 4 9 
    ---------------------
    8 1 5 | 7 9 4 | 3 2 6 
    2 7 9 | 6 1 3 | 8 5 4 
    4 3 6 | 5 8 2 | 7 9 1 
    ---------------------
    9 4 2 | 3 7 8 | 6 1 5 
    6 5 3 | 2 4 1 | 9 7 8 
    7 8 1 | 9 5 6 | 4 3 2

    Cracking a Math Olympiad Downside

    Math Olympiads are competitions for pre-university college students and include powerful math issues that should be solved underneath timed situations with out using calculators. Since systematically exploring the total answer area for such issues is often not possible, profitable answer approaches are inclined to depend on analytical reasoning and mathematical ingenuity, exploiting express and implicit constraints gleaned from the issue assertion to streamline the search of the answer area. Some issues should do with constraint satisfaction and combinatorial optimization, which we additionally come throughout in information science issues in trade (e.g., checking whether or not a path to a given vacation spot exists, discovering all doable paths to a vacation spot, discovering the shortest path to a vacation spot). Thus, even when a intelligent mathematical answer method exists for a particular Olympiad drawback, it may be instructive to research different generalizable approaches (like backtracking) that exploit the facility of at the moment’s computer systems and can be utilized to unravel a broad vary of comparable issues in observe.

    For instance, contemplate the following problem that appeared in Spherical 1 of the British Mathematical Olympiad in November 2018: “A listing of 5 two-digit optimistic integers is written in growing order on a blackboard. Every of the 5 integers is a a number of of three, and every digit 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 seems precisely as soon as on the blackboard. In what number of methods can this be completed? Observe {that a} two-digit quantity can not start with the digit 0.”

    Because it occurs, the answer to the above drawback is 288. The video under explains an answer method that cleverly exploits some key express and implicit options of the precise drawback assertion (e.g., the answer should be offered as an ordered record, and a quantity is a a number of of three if the sum of its digits can be a a number of of three).

    The Python code under reveals how backtracking can be utilized to unravel the issue:

    def is_valid_combination(numbers):
        # Checks if every digit from 0-9 seems precisely as soon as in an inventory of numbers
        digits = set()
        for quantity in numbers:
            digits.replace(str(quantity))
        return len(digits) == 10
    
    def find_combinations():
        multiples_of_3 = [i for i in range(12, 100) 
                            if i % 3 == 0 and '0' not in str(i)[0]]
        valid_combinations = []
        def backtrack(begin, path):
            if len(path) == 5:
                if is_valid_combination(path):
                    valid_combinations.append(tuple(path))
                return
            for i in vary(begin, len(multiples_of_3)):
                backtrack(i + 1, path + [multiples_of_3[i]])
        backtrack(0, [])
        return valid_combinations
        
    print(f"Answer: {len(find_combinations())} methods")

    The perform is_valid_combination() specifies the important thing constraint that should maintain for every legitimate 5-number record found through the exploration of the search area. The record multiples_of_3 options the candidate numbers which will seem in a sound 5-number record. The perform find_combinations() applies backtracking to effectively check out all distinctive 5-number combos from multiples_of_3.

    The perform is_valid_combination() and the record comprehension used to generate multiples_of_3 might be modified to unravel a broad vary of comparable issues.

    Past Backtracking

    As we’ve got seen, backtracking is an easy but highly effective method for fixing various kinds of constraint satisfaction and combinatorial optimization issues. But, different methods akin to depth-first search (DFS) and dynamic programming (DP) additionally exist and should look comparable on the floor – so when does it make sense to make use of backtracking as an alternative of those different methods?

    Backtracking might be regarded as a extra strategic type of DFS, during which constraint checking is a core function of every determination step, and invalid paths might be deserted early. In the meantime, DP could also be used for issues that exhibit two properties: overlapping subproblems and an optimum substructure. An issue has overlapping subproblems if the identical subproblems should be solved a number of occasions whereas fixing the bigger drawback; storing and reusing the outcomes of the recurring subproblems (e.g., utilizing memoization) is a key function of DP. Moreover, an issue has an optimum substructure if an optimum answer to the issue might be constructed by constructing on optimum options to its subproblems.

    Now, contemplate the N-Queens Downside, which seems at easy methods to place N queens on an N-by-N chessboard, such that no two queens can assault one another; it is a basic drawback that has purposes in a number of real-world eventualities the place discovering options with out conflicts is essential (e.g., useful resource allocation, scheduling, circuit design, and path planning for robots). The N-Queens drawback doesn’t inherently exhibit overlapping subproblems or an optimum substructure, since subproblems could not essentially should be solved repeatedly to unravel the general drawback, and the location of queens in a single a part of the board doesn’t assure an optimum placement for the whole board. The inherent complexity of the N-Queens Downside thus makes it much less appropriate for exploiting the strengths of DP, whereas backtracking aligns extra naturally with the issue’s construction.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    Proxy-Pointer RAG: Structure Meets Scale at 100% Accuracy with Smarter Retrieval

    April 19, 2026

    Dreaming in Cubes | Towards Data Science

    April 19, 2026

    AI Agents Need Their Own Desk, and Git Worktrees Give Them One

    April 18, 2026

    Your RAG System Retrieves the Right Data — But Still Produces Wrong Answers. Here’s Why (and How to Fix It).

    April 18, 2026

    Europe Warns of a Next-Gen Cyber Threat

    April 18, 2026

    How to Learn Python for Data Science Fast in 2026 (Without Wasting Time)

    April 18, 2026

    Comments are closed.

    Editors Picks

    The 11 Best Fans to Buy Before It Gets Hot Again (2026)

    April 19, 2026

    A look at Dylan Patel’s SemiAnalysis, an AI newsletter and research firm that expects $100M+ in 2026 revenue from subscriptions and AI supply chain research (Abram Brown/The Information)

    April 19, 2026

    ‘Euphoria’ Season 3 Release Schedule: When Does Episode 2 Come Out?

    April 19, 2026

    Francis Bacon and the Scientific Method

    April 19, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    This Ultimate Y2K Sci-Fi Movie Made Virtual Reality Seem Almost Too Real

    May 31, 2025

    Naya Connect modular keyboard review and features

    January 25, 2026

    Private data including criminal records stolen in Legal Aid hack

    May 19, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.