Skip to content

    Genetic Algorithms

    AI · Intermediate

    Definition

    Genetic algorithms evolve a population of solutions using selection, crossover and mutation.

    Optimize problems where gradients are unavailable.

    Diagram

    population→ select→ crossover→ mutate

    Flowchart

    Random populationScore fitnessSelect bestCrossoverMutateRepeat

    Example: Single-point crossover

    a, b, cut = '11111', '00000', 2
    print(a[:cut] + b[cut:], b[:cut] + a[cut:])

    Output: 11000 00111

    Key points

    • Mutation keeps diversity
    • No guarantee of global optimum

    Interview questions

    Why mutation?

    It explores new solutions and avoids getting stuck.

    More AI topics