This project focuses on solving the classical Traveling Salesman Problem (TSP) using PostgreSQL. The goal is to find the cheapest route that visits all cities and returns to the starting point.
ex00
- ex00.sql: DDL for table creation with INSERTs of data; SQL DML statement
- Language: ANSI SQL
- SQL Syntax Pattern: Recursive Query
Please refer to the graph on the left. It represents four cities (a, b, c, and d) connected by arcs with associated costs (or taxation). The cost between cities (a,b) is equal to the cost between (b,a).
Your task is to create a table named "nodes" with the following structure: {point1, point2, cost}. Fill the table with data based on the given picture, considering both direct and reverse paths between two nodes.
Write a single SQL statement that returns all tours (paths) with the minimum traveling cost if we start from city "a". The tours should be sorted by total_cost and then by tour.
Example output data:
total_cost | tour
-----------+-----------------
80 | {a,b,d,c,a}
... | ...
ex01
- ex01.sql: SQL DML statement
- Language: ANSI SQL
- SQL Syntax Pattern: Recursive Query
In this exercise, you need to modify the SQL query from the previous exercise to include additional rows with the highest cost. The sample output data below demonstrates the expected result, sorted by total_cost and then by tour.
Example output data:
total_cost | tour
-----------+-----------------
80 | {a,b,d,c,a}
... | ...
95 | {a,d,c,b,a}