Josephus problem
recursive algorithm
algorithm explanation
problem solving
computer science

Explanation for recursive implementation of Josephus problem

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

The Josephus problem is a theoretical problem related to a certain elimination game, based on an ancient story about Josephus, a Jewish historian. This problem can be solved using various methods, and one of the most intuitive solutions is through recursive implementation. In this article, we'll delve into a recursive approach to solving the Josephus problem, detailing the technical aspects and providing examples to enhance understanding.

The Problem Statement

Consider `n` people standing in a circle, numbered from 1 to `n`. Starting from a specific position, every `k-th` person is eliminated from the circle. The process is repeated until only one person remains. The task is to determine the position of the last person remaining.

Recursive Solution Explanation

Base Case

The base case of the recursion defines when the problem is trivially solvable. For the Josephus problem, the base case occurs when there is only one person (`n = 1`). In this situation, no person is eliminated, and the survivor is obviously the one person left:

J(1,k)=0J(1, k) = 0

This means that the position of the survivor is `0` in 0-based indexing.

Recursive Case

To solve the problem recursively for `n` people, we need to relate it to the solution of the problem with `n-1` people. The core idea is that eliminating every `k-th` person from a group of `n` people reduces the problem to eliminating every `k-th` person from `n-1` people. The trick is to adjust the position due to the circular nature of the circle:

J(n,k)=(J(n1,k)+k)modnJ(n, k) = (J(n-1, k) + k) \mod n

Here's how the recursive logic works:

  1. Calculate the Josephus position for `n-1` people.
  2. Shift this position because the circle has shifted by moving `k` positions.
  3. Apply modulo `n` to wrap around the circle if necessary.

Complete Recursive Function

A simple recursive function in Python looks like this:

  • Different counting directions.
  • Varying steps `k` depending on certain conditions or person attributes.
  • Network topology design to ensure fail-safe connectivity.
  • Memory and resource management in operating systems through circular buffering techniques.
  • Cryptographic algorithms that use similar elimination tactics for key generation or verification.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.