Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

1 Travelling sales man problem

The Traveling Salesman Problem (TSP) is a classic problem in combinatorial optimization and graph
theory. In this problem, a salesman is given a list of cities and the distances between them. The objective
is to find the shortest possible route that visits each city exactly once and returns to the starting city.

Here's a brief overview of the Traveling Salesman Problem:

### Problem Statement:

Given a complete graph with a set of cities (nodes) and the distances between them (edges), find the
shortest possible route that visits each city exactly once and returns to the starting city.

### Approach to Solve TSP:

1. Brute Force: Calculate the distance for all permutations of city visits and choose the shortest route.
This approach becomes computationally expensive as the number of cities increases.

2. Dynamic Programming (DP): Use dynamic programming to solve smaller subproblems and build up to
find the optimal solution. The Held-Karp algorithm is a DP approach often used for TSP.

3. Heuristic Algorithms:

- Nearest Neighbor: Start at a random city and visit the nearest unvisited city until all cities are visited,
then return to the starting city. It's simple but may not provide the optimal solution.

- Genetic Algorithms: Inspired by natural selection, genetic algorithms iteratively improve a population
of solutions to converge on an optimal or near-optimal solution.

4. Optimization Techniques:

- 2-Opt: Swap two edges to improve the current route until no further improvements can be made.

- Lin-Kernighan Algorithm: Improves on 2-Opt by considering multiple edge exchanges for more
complex route optimization.

### Key Considerations:

- TSP is an NP-hard problem, meaning there is no known polynomial-time solution for the general case.

- The complexity grows factorially with the number of cities, making exact solutions inefficient for large
instances.

- Approximation algorithms and heuristics are commonly used to find near-optimal solutions within a
reasonable time frame.
### Applications of TSP:

- Logistics and transportation planning

- Circuit board drilling

- DNA sequencing

- Tour planning and sightseeing

In conclusion, the Traveling Salesman Problem is a fundamental combinatorial optimization problem


with various solution approaches. While finding an exact solution may be computationally intensive,
heuristic methods and optimization techniques help in efficiently approximating the optimal route for
visiting a set of cities exactly once and returning to the starting point.

2 Longest common subsequence problem.

The Longest Common Subsequence (LCS) problem is a fundamental dynamic programming problem in
computer science. It involves finding the longest subsequence that is present in two given sequences,
which can be strings or arrays.

### Problem Statement:

Given two sequences X and Y, the LCS problem seeks to find the longest common subsequence that is
present in both X and Y.

### Example:

Let's take two sequences:

X = "AGGTAB"

Y = "GXTXAYB"

The longest common subsequence between X and Y is "GTAB" with a length of 4.

### Approach to Solve the LCS Problem:

1. **Dynamic Programming**:

- Use a 2D array to store the lengths of LCS for different subsequences of X and Y.

- Apply dynamic programming to fill in the table based on whether characters match or not.

2. **Steps**:
- Create a 2D array DP with dimensions (m+1) x (n+1) where m is the length of X and n is the length of
Y.

- Initialize the base cases where the lengths of either sequence are 0.

- Iterate over X and Y, filling in the DP table based on whether the characters match or not.

- Use the DP table to backtrack and reconstruct the LCS.

3. **Complexity**:

- **Time Complexity**: O(mn) where m is the length of X and n is the length of Y.

- **Space Complexity**: O(mn) to store the DP table.

### Code Snippet for LCS Calculation in Python:

def longest_common_subsequence(X, Y):

m, n = len(X), len(Y)

L = [[0] * (n + 1) for i in range(m + 1)]

for i in range(1, m+1):

for j in range(1, n+1):

if X[i-1] == Y[j-1]:

L[i][j] = L[i-1][j-1] + 1

else:

L[i][j] = max(L[i-1][j], L[i][j-1])

lcs = ''

i, j = m, n

while i > 0 and j > 0:

if X[i-1] == Y[j-1]:

lcs = X[i-1] + lcs

i -= 1
j -= 1

elif L[i-1][j] > L[i][j-1]:

i -= 1

else:

j -= 1

return lcs, L[m][n]

# Example usage

X = "AGGTAB"

Y = "GXTXAYB"

lcs, length = longest_common_subsequence(X, Y)

print("Longest Common Subsequence: " + lcs)

print("Length of LCS: " + str(length))

This code snippet demonstrates how to find the longest common subsequence between two strings,
along with its length, using dynamic programming in Python.

If you need further clarification or have any specific questions regarding the LCS problem, feel free to
ask!

You might also like