Best route calculation algorithm

Asked

Viewed 3,468 times

9

I’m doing a logging system that addresses the issue of routes/routing. Where I should choose the best route to travel.

Initially I am only doing some tests and testing advanced algorithms, what I am doing seems to be something illogical, but it will meet my need.

Come on, let’s go: I have a bank of registered locations. These locations can make borders with several other locations. For each location is informed the distance in KM.

Based on the registered locations, the system has an option in which the user informs two points (origin and destination) and at the end the system informs the route for the vehicle to reach its given destination considering the shortest way.

I already have some cities, for example, Cidadea, Cidadeb, Cidadec, Cidaded, cidadee.

For example,

de CidadeA pra cidadeB são 5 km.
de CidadeB para cidadeC são 7 km.
de CidadeC para cidadeE são 22 km.
de CidadeC para cidadeD são 8 km.
e assim como posso ter outras cidades.

And I chose (two comboboxes) to go from the cityA to cityeB, so he has to bring me all the routes, that is, all the tickets to get to the city D in the most "fast" way, by the distance, then he has to bring the points, so

CidadeA - > CidadeB
CidadeB - > Cidade C...
e assim sucessivamente.

The user informs the source and destination, the system must calculate displaying point by point where it must pass.

I tried to cross joins and analyze, I tried to give go inside until I find, but it’s something that seems infinite, even thinking too much becomes difficult to assemble. Does anyone know a way, famous algorithms that do this? I’m checking out the A* algorithms, etc, but they still haven’t helped me.

Friends, here is the structure of the tables: Table Location and table Distances.

    use master
go
CREATE DATABASE Rota
go
USE Rota
GO

CREATE TABLE [dbo].[Localidade](
    [cod_cidade] [int] IDENTITY(1,1) NOT NULL,
    [nome] [varchar](50) NULL,
 CONSTRAINT [PK_Localidade] PRIMARY KEY CLUSTERED 
(
    [cod_cidade] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


CREATE TABLE [dbo].[DistanciaCidades](
    [cod_cidade1] [int] NOT NULL,
    [cod_cidade2] [int] NOT NULL,
    [distancia] [int] NULL,
 CONSTRAINT [PK_DistanciaCidades] PRIMARY KEY CLUSTERED 
(
    [cod_cidade1] ASC,
    [cod_cidade2] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
  • 1

    If you read English, I strongly recommend the wikipedia article on the Travelling salesman problem. The Portuguese version doesn’t look so reliable.

  • Your question seems very logical yes, I just didn’t understand your "kms", are you by chance using a structure in the database of n:m? I know it seems irrelevant to ask this, but the structure of the bank in my view seems to be analyzed as well

2 answers

6

This is a problem of minimum path, unidirectional believe (the distance from A to B is the same distance from B to A) and no negative weight (the distance is always greater than or equal to zero). Considering that the distance between cities does not change frequently (except for the opening of new roads, etc.) and that cities are not commonly registered/unsubscribed, it may be worth using an algorithm that calculates the shortest paths between all pairs. So all you need to do is consult the result, quickly, without having to recalculate it.

There are several applicable algorithms, each with characteristics specific to the level of complexity [asymptotic], domain restrictions, etc., choose one that meets your particular needs. In the absence of specific requirements, dijkstra’s algorithm seems to be the simplest and most efficient, at least for the most common applications.

My first suggestion is to implement (or search for a ready implementation) outside the bank, in the same application layer. If you want to do everything in the bank, there is an implementation route stored Procedure (MS SQL CLR) which may be of interest to you. In the rest of this answer, I will outline how to implement by hand, not necessarily in the best or most efficient way, okay?

A simple implementation would consist of the following: Create a temporary table, with columns (cidade, valor, anterior, visitado). Initially, fill this table with your cities, marking them all as "not visited", the former as null and the value as "infinite" (or simply a very large number). Then perform the following steps:

  1. Mark the initial city value as zero (because the distance from A to A is zero);
  2. While your destination [or all cities] has not yet been visited, do:
    1. Select the city X not visited with the lowest value;
    2. Select All Cities Y not visited that are adjacent to it (i.e. reachable from it);
    3. Add up the distance between X and Y with the value of X, and compare with the value of Y; if it is smaller, update the value of Y and the countryside anterior of Y;
    4. Mark the city X as "visited".

At the end, you will have for each city a value corresponding to the minimum distance between A and her, and a anterior telling from which city one arrives at it by walking that distance. To find the exact path, start from B and follow the anterior until you get into A.

  • Good afternoon friend. Thank you. With the table structure I have, is it possible to do this the same way? thank you as well select city c? my destination is the city d, how will I manage to catch the city c? hugging

  • @user17245 Yes, your table DistanciaCidades has all relevant information for mounting your graph: (cod_cidade1, cod_cidade2 e distancia), corresponding to (vértice 1, vértice 2, peso da aresta). But I suggest doing this outside the bank, and if possible use an implementation already ready, see the updated response. P.S. I changed the names from C and D to X and Y, not to be confused with your example.

4

From what I understand, you have the shortest path problem and then you can use the algorithm of Dijkstra to find the optimal solution in polynomial time. To implement, the ideal would be to get the city list and its distances from the database and assemble a data structure to run the algorithm and not try to run it directly in the BD.

  • The shortest way is not always the fastest, think of a lifeguard in the sand and a drowning in its diagonal , swimming in a straight line would not be the fastest but running through the sand a straight line almost at the perpendicular of the drowned and then yes swimming.

  • Research genetic algorithms and ant simulations (Dorigo).

  • In the case of the question, the information he possesses are cities and the distances (in Km) between them. Speed for it is the shortest distance travelled ( ... all tickets to reach the city D the "fastest" way, by distance, ...). I didn’t understand the lifeguard’s example.

  • @Motta is still a minimum path case. The difference is that the heaviness of moving on water is much greater than the weight of moving on land. Putting the right weights, a minimum path algorithm would arrive at the same strategy that you suggested, if it were really the best of course (if it takes a huge displacement on land to gain a few meters in water, then it no longer pays...).

  • @robot_s http://static.nautil.us/2904_b8b9c74ac526fffbeb2d39ab038d1cd7.png

  • Just one comment , the algorithms cited are the best known and implemented.

  • @Motta read again what you had said. You are considering that the speed on the earth (when walking through the sand) and swimming are different. In his case the vehicles always walk with the same speed, that is, it is only the distance he considers to define the "fastest". If he wants to consider other costs such as toll, road condition and vehicle type, then he will have another problem.

  • But that weighs, but it was just an example of the shortest way x quickest difference. An interesting discussion this we raised.

  • 1

    Well, I can’t comment on the answer above. But Thorup’s algorithm is only "efficient" for a variant of the minimum path problem (which has the integer weights). If the weights are decimal, Dijkstra’s is still more efficient. The cost to assemble the data structure used in Thorup’s algorithm is only worth it when it has many vertices - in the presentation it only exceeds the Dijkstra from the 3 thousand vertices.

  • Another one point. "Does it make a difference if the distance is 150,000m or 150,000.2m?" Who should define the importance of this is the author of the question (that he even, in the example, uses integer values in km).

  • Again, Dijkstra solves the problem for both whole weights and decimals, Thurop does not. Therefore, since you pointed out that the Dijkstra was less efficient you should at least indicate which situations this occurs. Based on the slides you placed yourself, the experiment the author did shows from how many vertices Thurop’s algorithm surpasses Dijkstra’s. Regarding the number of edges of the author’s problem, this may vary, especially if the cities are in a metropolitan region, for example. For directed graph just use Dijkstra’s with Fibonacci heap.

  • good morning friends! thank you so much for the suggestions. I am researching and testing all. with the table structure I put together, what would be a way to adapt to these complex algorithms? I embrace

  • @robot_s Okay, I’ve removed the Thorup reference from my answer. By the way, if you have better suggestions, you can always [Dit] your response to include them, if it is in your interest of course (P.S. I will clear my comments here, because this discussion is already too extensive).

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.