AI Algorithms Learning Hub

Master the fundamentals of AI algorithms through interactive learning

A* Algorithm

Pathfinding algorithm that combines Dijkstra's and Best-First Search

BFS / DFS

Graph traversal algorithms for searching tree or graph data structures

Minimax Algorithm

Decision-making algorithm for two-player games

Alpha-Beta Pruning

Optimization of the Minimax algorithm

Genetic Algorithm

Nature-inspired optimization algorithm

Hill Climbing

Local search optimization algorithm

A* Algorithm

A* (pronounced "A-star") is a graph traversal and pathfinding algorithm that is often used in computer science due to its completeness, optimality, and reasonable time performance. The algorithm efficiently plots a walkable path between multiple nodes, or points, on the graph.

Key Components:

  • g(n) - the cost of the path from the start node to n
  • h(n) - the heuristic estimate of the cost from n to the goal
  • f(n) = g(n) + h(n) - the total estimated cost of the path through n
function aStar(start, goal):
    openSet = {start}
    cameFrom = {}
    gScore = {start: 0}
    fScore = {start: heuristic(start, goal)}

    while openSet is not empty:
        current = node in openSet with lowest fScore
        if current = goal:
            return reconstruct_path(cameFrom, current)
        

BFS / DFS Algorithms

BFS (Breadth-First Search) explores all neighbors before going deeper. DFS (Depth-First Search) explores as deep as possible before backtracking.

Applications:

  • Finding Shortest Path (BFS)
  • Maze Solving
  • Cycle Detection in Graphs
// BFS
function BFS(graph, start):
    queue = [start]
    visited = {start}

    while queue is not empty:
        node = queue.pop(0)
        for neighbor in graph[node]:
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append(neighbor)

// DFS
function DFS(graph, node, visited):
    visited.add(node)
    for neighbor in graph[node]:
        if neighbor not in visited:
            DFS(graph, neighbor, visited)
            

Minimax Algorithm

Minimax is a decision-making algorithm used in turn-based games to minimize the possible loss while maximizing potential gain.

Applications:

  • Chess, Tic-Tac-Toe, Checkers
  • AI Game Bots
        function minimax(node, depth, maximizingPlayer):
            if depth == 0 or node is terminal:
                return evaluate(node)
        
            if maximizingPlayer:
                maxEval = -infinity
                for child in node.children:
                    eval = minimax(child, depth - 1, false)
                    maxEval = max(maxEval, eval)
                return maxEval
            else:
                minEval = +infinity
                for child in node.children:
                    eval = minimax(child, depth - 1, true)
                    minEval = min(minEval, eval)
                return minEval
                                

Alpha-Beta Pruning

Optimization for Minimax to avoid unnecessary moves.

Parameters:

    function alphabeta(node, depth, alpha, beta, isMax):
        if depth == 0 or node is terminal:
            return heuristic(node)
        if isMax:
            value = -inf
            for child in node:
                value = max(value, alphabeta(child, depth-1, alpha, beta, false))
                alpha = max(alpha, value)
                if alpha >= beta:
                    break
        else:
            value = inf
            for child in node:
                value = min(value, alphabeta(child, depth-1, alpha, beta, true))
                beta = min(beta, value)
                if beta <= alpha:
                    break
        return value
                        

Genetic Algorithm

Optimization algorithm inspired by genetics.

Process:

    initialize population
    evaluate fitness
    while termination condition not met:
        select parents
        crossover parents
        mutate offspring
        evaluate new population
                        

Hill Climbing Algorithm

Iterative improvement algorithm for local search.

Steps:

    function hillClimbing(problem):
        current = initial solution
        loop:
            neighbor = best neighbor of current
            if neighbor is not better:
                return current
            current = neighbor