What is crossover in genetic algorithms?

Key takeaways:

  • Crossover in genetic algorithms: Crossover is a genetic operator that combines genetic material from two parent solutions to create offspring, enabling the exploration of new solutions with traits from both parents.

  • Importance of crossover: Crossover is crucial for maintaining population diversity and improving optimization by combining beneficial features from different parents. It enhances both exploration and exploitation in genetic algorithms.

  • Types of crossover:

    • One-point crossover: A single crossover point is selected, and the tails of the parents are swapped to generate offspring.

    • Two-point crossover: Two crossover points are selected, and the segment between them is exchanged between parents to create new offspring.

  • Role in optimization problems: Crossover is an essential mechanism for generating high-quality solutions in optimization problems by promoting recombination and diversity throughout the genetic algorithm process.

In genetic algorithms, the crossover is a genetic operator that involves recombining genetic material between two parent individuals to generate new offspring.

During crossover, a portion of the genetic material of each parent is exchanged or combined to create a new individual with a combination of traits from both parents. The specific mechanism of crossover varies depending on the encoding scheme used to represent the individuals. Still, the genetic material is generally divided into segments that are exchanged between parents to create offspring.

Importance of crossover in genetic algorithms

Crossover is a key component of genetic algorithms, because it allows for creating new solutions that combine beneficial features from multiple parent solutions. In addition, the crossover can help maintain diversity in the population of solutions over the optimization process. Overall, the crossover is a powerful tool for enhancing genetic algorithms' exploration and exploitation capabilities. It's also essential for achieving high-quality solutions to many optimization problems.

There are two common variations of the crossover operator in genetic algorithms.

  • One-point crossover

  • Two-point crossover

One-point crossover

One-point crossover involves selecting a random point along the length of the parents, and exchanging the values on the tails of the parents results in new offspring.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Code example

Let's have a look at the one-point crossover code example below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Code explanation

  • Lines 2–3: We define the two parents as arrays parent1 and parent2.

  • Lines 5–12: The one_point_crossover function is defined to perform a one-point crossover operation between two parents at a specified crossover point.

  • Line 18: We call the one_point_crossover() with three parameters parent1, parent2, and crossover_point.

  • Lines 21–24: We print both parents and offspring.

Two-point crossover

Two-point crossover is similar to one-point crossover but, instead of selecting a single point, two points are chosen for the parents. The values between the two points are exchanged between the parents to get new offspring.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

If you want to learn more about genetic algorithms, read the Answer on genetic algorithm phases.

Code example

Let's have a look at the two-point crossover code example below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Code explanation

  • Lines 2–3: We define the two parents as arrays parent1 and parent2.

  • Lines 5–15: The two_point_crossover function is defined to perform a two-point crossover operation between two parents at specified crossover points.

  • Line 22: The two_point_crossover function is called with parent1, parent2, crossover_point1, and crossover_point2.

  • Lines 25–28: We print both parents and offspring.

If you want to solve problem about genetic algorithms, read the Answer on solving the 8 queen problem using genetic algorithm

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved