This program solves the knight's tour problem on an N x N chessboard. The problem consists of finding a sequence of moves of a knight on a chessboard such that the knight visits every square exactly once.
The program is implemented in C and uses pthreads for parallel execution. The program uses a heuristic algorithm to find a solution to the problem.
To compile the program, run the following command in your terminal:
gcc -pthread -o warnsdorff_algorithm warnsdorff_algorithm.cTo run the program, execute the following command:
./warnsdorff_algorithm <N> <x> <y> [mode]where:
Nis the size of the chessboard (N x N). This argument is required.xandyare the starting position of the knight on the chessboard. These arguments are required.modeis optional and can be one of:singleors- Run in single-threaded mode onlymultiorm- Run in multi-threaded mode onlyboth(default) - Run both single-threaded and multi-threaded modes for performance comparison
For example, to solve the knight's tour problem on a 10x10 chessboard with the starting position at (0, 0), run the following command:
./warnsdorff_algorithm 10 0 0To run only in single-threaded mode:
./warnsdorff_algorithm 10 0 0 singleTo run only in multi-threaded mode:
./warnsdorff_algorithm 10 0 0 multiThe program uses a heuristic algorithm to find a solution to the problem. The algorithm works by choosing the next square to move to based on the number of unvisited squares that are reachable from that square. The square with the smallest number of reachable unvisited squares is chosen.
The program supports both single-threaded and multi-threaded execution modes:
Multi-threaded Mode:
- Creates four threads, each trying to find a solution starting from a different orientation
- Uses improved barrier synchronization with pthread condition variables for better thread coordination
- Implements thread-safe throughput tracking and early termination checks
- Uses mutex locks to ensure only one thread sets the solution status and outputs the path
Single-threaded Mode:
- Runs sequentially through all four orientations until a solution is found
- Provides baseline performance for comparison with multi-threaded execution
Performance Timing:
- Both modes include precise timing measurements using
gettimeofday() - Outputs start/end timestamps and total execution duration in milliseconds
- Enables direct performance comparison between single and multi-threaded approaches
The following graph shows a performance comparison between multi-threaded and single-threaded implementations of the Knight's Tour algorithm:
As shown in the graph, the multi-threaded approach consistently demonstrates significantly lower latency compared to the single-threaded approach across all tested chessboard sizes. The performance advantage of multi-threading is evident from the start and generally widens as the chessboard size increases.
The program now includes built-in timing capabilities that allow you to measure and compare performance between single-threaded and multi-threaded modes. When running in "both" mode, the program will:
- Execute the single-threaded version and measure its execution time
- Execute the multi-threaded version and measure its execution time
- Display timing results for both modes for easy comparison
This makes it easy to benchmark the performance improvements of the multi-threaded implementation on different chessboard sizes and starting positions.
