Uploaded by SAGOR MAHMUD

Algorithm Assignment

advertisement
University of Liberal Arts Bangladesh
Topic: Report for Assignment.
Course Title: Algorithm
Course Code: CSE 2201
Section: 02
Submitted by
Name: Md Sagar Mahmud
ID: 193014031
Department: Computer Science and Engineering.
Submitted to
Dr. Md. Zulfiker Mahmud
Assistant Professor
Department of Computer Science and Engineering (CSE)
Date of Submission: 23-12-2023
Problem Statement:
The Traveling Salesperson Problem (TSP) is a classic combinatorial optimization problem that
involves finding the shortest possible route that visits each city in a given list exactly once and
returns to the starting city. It has applications in logistics, transportation, scheduling, and other
areas where efficient route planning is crucial.
Introduction:
This project implements a dynamic programming (DP) solution to the TSP in the C programming
language. DP is a powerful technique for solving optimization problems by breaking them down
into overlapping subproblems and storing intermediate results to avoid redundant computations.
Methodology:
1. Data Structures:
o
A 2D array cities to store the distance matrix between cities.
o
A 3D array memo to store computed minimum distances for subproblems, using
memoization to avoid redundant calculations.
2. Recursive Function tsp:
o
Base cases: If all cities are visited, return the distance back to the starting city.
o
Check for memoized results to avoid recalculations.
o
Recursively explore possible next cities, calculate their distances, and update the
minimum distance.
o
Store the minimum distance in the memoization array.
o
Return the minimum distance found for the current subproblem.
3. Input Function inputCities:
o
Prompts the user to enter the number of cities.
o
Reads the distance matrix from the user.
4. Main Function:
o
Calls inputCities to get the problem input.
o
Initializes the memoization array with -1s.
o
Calls tsp to start the recursive process.
o
Prints the minimum distance for the TSP solution.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_CITIES 10
int numCities;
int cities[MAX_CITIES][MAX_CITIES];
int memo[MAX_CITIES][1 << MAX_CITIES];
int min(int a, int b) {
return (a < b) ? a : b;
}
int tsp(int currentCity, int visited) {
if (visited == (1 << numCities) - 1) {
return cities[currentCity][0];
}
if (memo[currentCity][visited] != -1) {
return memo[currentCity][visited];
}
int minDistance = INT_MAX;
for (int i = 0; i < numCities; i++) {
if (!(visited & (1 << i))) {
int newDistance = cities[currentCity][i] + tsp(i, visited | (1 << i));
minDistance = min(minDistance, newDistance);
}
}
memo[currentCity][visited] = minDistance;
return minDistance;
}
void inputCities() {
printf("Enter the number of cities: ");
scanf("%d", &numCities);
printf("Enter the distance matrix (%dx%d):\n", numCities, numCities);
for (int i = 0; i < numCities; i++) {
for (int j = 0; j < numCities; j++) {
scanf("%d", &cities[i][j]);
}
}
}
int main() {
inputCities();
for (int i = 0; i < MAX_CITIES; i++) {
for (int j = 0; j < (1 << MAX_CITIES); j++) {
memo[i][j] = -1;
}
}
int minDistance = tsp(0, 1 << 0);
printf("Minimum distance for TSP: %d\n", minDistance);
return 0;
}
Output:
Discussion:

Time Complexity: O(n^2 * 2^n) due to the recursive exploration of subproblems and the
exponential size of the state space.

Space Complexity: O(n * 2^n) for the memoization array.

Practical Limitations: The exponential complexity limits this DP approach to relatively
small TSP instances (up to around 15-20 cities).

Alternative Approaches: For larger TSP instances, heuristics or approximation algorithms
are often used, such as simulated annealing, genetic algorithms, or branch and bound
techniques.
Conclusion:
This project successfully implemented a dynamic programming solution to the Traveling
Salesperson Problem in C. The code demonstrated the key concepts of DP, including recursive
problem decomposition, memoization, and base cases. The discussion highlighted the
computational complexity and practical limitations of this approach, as well as potential alternative
strategies for larger problem instances.
Download