Find the shortest path in a graph with dynamic weight - algorithm

I have the following scenario:
I want to find a flight between two cities: A and B. There is no direct flight from A to B; so, I need to find a connecting flight with the lowest cost.
In addition, the air ticket is not fixed. It depends on the time that I buy it; for example, the price will be cheaper if I buy it early.
Moreover, the time affects the flight too; for example, there is only one flight from C to D on May 31 at 7 AM. If the plane flies from A to C on May 31 at 8 AM, I miss the flight. For this reason, I represent the cities as vertices of a graph. The path AB exists if there is a valid flight from A to B. The weight will be the ticket fee.
Is there any idea or suggestion for my problem?
Thanks

I answered once a very similar question I am pretty sure same idea can be used here. The idea is to use a routing algorithm, designed for internet routers - which are dynamic and constantly changing. The algorithm designed for it is Distance Vector Routing Protocol.
The suggested implementation is basically a distributed version of Bellman-Ford algorithm, that modifies itself once there is a change on the weights of each edge in order to get the new optimal path.
Note that the algorithm has draw backs, mainly the count to infinity problem.

The usual way to deal with not being in the right place at the right time is to make the nodes represent a specific place at a specific time. Then a flight from C to D that departs on May 30 at 9PM and arrives May 31 at 7AM corresponds to an arc from node C_May30_9PM to D_May31_7AM. You also need arcs that correspond to waiting around, e.g., D_May31_7AM to D_May31_8AM.
I'm not sure there's much to say about purchasing tickets at the level of detail you've described.

Related

Group locations on map based on distance

I've given the following problem. A set of locations (e.x. around 200 soccer clubs) is spread over a map. I want to group the locations based on their distance to each other. The result should be a list of groups (around 10 to 20) so that the distance each soccer club has to drive to visit all other clubs within their group is minimized.
I'm pretty sure an algorithm exists already. I probably only need the "official" name of this problem.
Can anyone please help me ?
You're probably looking for Data Clustering Algorithms. Since you have an idea of the number of clusters, a simple algorithm is k-means clustering.
If you want to choose the maximum distance d at the outset (and then determine how few groups suffice to guarantee that no team needs to drive more than this distance to get to another team in their own group) then you can formulate the problem as a graph colouring problem: make a vertex for each team, and put an edge between two vertices whenever the distance between them exceeds d. The solution to a graph colouring problem assigns a "colour" (just a label) to each vertex so that (a) no two vertices connected by an edge are assigned the same colour and (b) the number of distinct colours used is minimal. (In other words, edges represent "conflicts", indicating that the two endpoints cannot belong to the same group.) So here, each colour corresponds to a group, which is guaranteed to consist only of teams that are all <= d from each other, and the solution will try to minimise the total number of groups. You might need to rerun with a few different values of d until you get a solution with acceptably few groups.
Note that this is an NP-hard problem, so it might take a long time to find an exact (minimal-group-count) solution. There are many heuristics that are much quicker and still do a decent job, though.

Distance between lots of points on a map

I have a 20,000 point array of gps locations.
They represent points on a forest path that need to be checked. I need to figure out how many km of forest path needs checking.
Group the points into routes.
Measure the shorted path of each route
Which algorithms should I consider and in which order.
Should I get the shortest path and break it up into routes or get the routes and then find the shorted path of each.
This solution asumes that you only have the points and don't know on which forest path th e points are, and in which order, etc.
I would try it this way:
1 connect each node with each other with a link, and as link weight use the distance (or better the number of seconds when going with 2km/h in meters in between the nodes: low speed asuming walking in the wood is slower then on a existimg forest road)
2 if the forest has diffuclties (mountains, vallley, river):
2a: ascent/descent: raise the link weight, using the altitudinal difference, look in outdoor planning resources, how many meters ascent has impact to travellling time. (300m could be one addionional hour as rough estimate)
2b: valley, river or other limits: either again raise the weight or remove the link if one cannot directly go from one point to the other. (e.g draw the valley as polygon and remove all links that cross the polygon)
Are there already paths/ forest roads in the wood?
Yes, draw them as links into the modell (graph), to use link weight, e,.g 5km/h walking speed.
Now you have a graph with nodes and the links with hopefully realistic link weight related to travelling speed between nodes.
Now use Shortes path (Dijkstras Algorithm) and travelling salesman algorithm.
If that all is to much work (could be some months for somebody with a degree in computer science) , plan it manually: draw a raster of 1000 x 1000m and let the human intelligence
do its job.
Since 20.000 points which have to be checked by walking, needs a high effort, it is addionally worth to evaluate automatic planning versus human experience. Try both variants and look which is more efficient.
(I think that people with outdoor experience when having a good map with countour lines and the check points on it, will do a better job, asuming preorganizing by point two quadrants asignement and quadrants to people.)
My other soulution:
This asumes you have more info which you did not have yet posted:
You probably have more info than just the coordinates of the points.
Who has created this points? In your graphic, they look as they are on a path.
Are they recorded while driving on that path with a vehicle? Then you have a time stamp, and therefore an order of points that are in sequnce, and thefore already are related to a path.
So the first step would be to assign the points to a path.
(You also could draw all forest paths known as vectors to a digital map and match the points to the neareast path automatically)
You need the paths when you cannot directly reach each node on a straight line in betwwen them (e.g driving by vehicle or walking in wood when river avoids direct straight line path)
Then once you have a graph with nodes on links, use a minimum spaning tree to calculate the sum of path lengths in kilomter.
For visting the points you often will have to return to a branch, so then a travelling salesman algorith will help to give the kilomters needed to visit all nodes.
The question seems to be similar to a constrained vehicle routing problem. You can try a heuristic for example the savings algorithmus: http://neo.lcc.uma.es/vrp/solution-methods/heuristics/savings-algorithms/.

Marauders dilemma algorithm

I'm making this repost after the earlier one here with more details.
PROBLEM :
The problem consists of a marauder who has to travel to different cities spread over a map. The starting location is known. Each city has a fixed loot associated with it. The aim of marauder is to travel across various nature of terrain. By nature of terrain, I mean there is a varied cost of travel between each pair of cities. He has to maximize the booty gained.
What we have done:
We have generated an adjacancy matrix (booty-path cost in place for each node) and then employed a heuristic analysis. It gave some output which is reasonable.
Now, the problem now is that each city has few or more vehicles in them, which can be bought (by paying) and can be used to travel. What vehicle does in actual is that it reduces the path cost. Once a vehicle is bought, it remains upto the time when next vehicle is bought. It is to upto to decide whether to buy the vehicle or not and how.
I need help at this point. How to integrate the idea of vehicle into what we already have? Plus, any further ideas which may help us to maximize the profit. I can post the code, if required. Thanks!
One way to do it would be to have a directed edge bearing the cost of the vehicle towards a duplicate graph with the reduced costs. You can even make it so that the reduction is finer than just a percentage if you want to.
The downside is that this will probably increase the size of the graph a lot (as many copies as you have different vehicles, plus the links between them), and if your heuristic is not optimal, you may have to modify it so that it considers the new edge positively.
It sounds as though beam search would suit this problem. Beam search uses a heuristic function H and a parameter k and works like this:
Initialize the set S to the initial game position.
Set T to the empty set.
For each game position in S, generate all possible successor positions to S after one move by the marauder. (A move being to loot, to purchase a vehicle, to move to an adjacent city, or whatever else a marauder can do.) Add each such successor position to the set T.
For each position p in T, evaluate H(p) for a heuristic function H. (The heuristic function can take into account the amount of loot, the possession of a vehicle, the number of remaining unlooted cities, and whatever else you think is relevant and easy to compute.)
If you've run out of search time, return the best-scoring position in T.
Otherwise, set S to the best-scoring k positions in T and go back to step 2.
The algorithm works well if you store T in the form of a heap with k elements.

Help on algorithm to place rooms on a limited space

I'm working on a small house design project and one of its most important parts is a section where the user can give some info about how he wants his rooms (for example, a house with 10 x 10 meters, having a 3x3 living room, a 3x3 kitchen, two 4 x 5 bedrooms, and a 4x2 bathroom), and then the program generates a map of the house according to the requeriments made.
For now, I'm not worried about drawing the map, just arranging the rooms in a way they don't overlap (yes, the output can be pretty ugly). I've already made some searches and found that what I want is very similar to the packing problem, which has some algorithms that handle this problem pretty well (although it's a NP-complete problem).
But then I had one more restriction: the user can specify "links" between rooms, for example, he may wish that a room must have a "door" to a bathroom, the living room to have a direct to the kitchen, etc (that is, the rooms must be placed side by side), and this is where the things get complicated.
I'm pretty sure that what I want configures a NP-problem, so I'm asking for tips to construct a good, but not necessarily optimal implementation. The idea I have is to use graphs to represent the relationship between rooms, but I can't find out how I can adapt the existent packing algorithms to fit this new restriction. Can anyone help me?
I don't have a full answer for you, but I do have a hint: Your connectivity constraints will form what is known as a planar graph (if they don't, the solution is impossible with a single-story house). Rooms in the final solution will correspond to areas enclosed by edges in the dual of the constraint graph, so all you need to do then is take said dual, and adjust the shape of its edges, without introducing intersections, to fit sizing constraints. Note that you will need to introduce a vertex to represent 'outside' in the constraint graph, and ensure it is not surrounded in the dual. You may also need to introduce additional edges in the constraint graph to ensure all the rooms are connected (and add rooms for hallways, etc).
You might find this interesting.
It's a grammar for constructing Palladian villas.
To apply something like that to your problem, I would have a way to construct one at random, and then be able to make random changes to it, and use a simulated annealing algorithm.

A special case of grouping coordinates

I'm trying to write a program to place students in cars for carpooling to an event. I have the addresses for each student, and can geocode each address to get coordinates (the addresses are close enough that I can simply use euclidean distances between coordinates.) Some of the students have cars and can drives others. How can I efficiently group students in cars? I know that grouping is usually done using algorithms like K-Mean, but I can only find algorithms to group N points into M arbitrary-sized groups. My groups are of a specific size and positioning. Where can I start? A simply greedy algorithm will ensure the first cars assigned have minimum pick-up distance, but the average will be high, I imagine.
Say that you are trying to minimize the total distance traveled. Clearly traveling salesman problem is a special instance of your problem so your problem is NP-hard. That puts us in the heuristics/approximation algorithms domain.
The problem also needs some more specification, for example howmany students can fit in a given car. Lets say, as many as you want.
How about you solve it as a minimum spanning tree rooted at the final destination. Then each student with the car is is responsible for collecting all its children nodes. So the total distance traveled in at most 2x the total length of spanning tree which is a 2x bound right there. Of course this is ridiculous 'coz the nodes next to root will be driving a mega bus instead of a car in this case.
So then you start playing the packing game where you try to fill the cars greedily.
I know this is not a solution, but this might help you specify the problem better.
This is an old question, but since I found it, others will as well.
Group students together by distance. Find the distance between all sets of two students. Start with the closest students and add them in a group, and continue adding until all students are in groups. If students are beyond a threshold distance, like 50 miles, don't combine them into a group (this will cause a few students to go solo). If students have different sized cars, stop adding them when the max car size has been reached between the students in the group (and whichever one you're trying to add).
Finding the optimal (you asked for efficient) solution would require a more defined problem, which it seems like you don't have. If you wanted to eliminate individual drivers though, taking the above solution and special casing the outliers, working them individually into groups and swapping people around adjacent groups to fit them in, could find a very strong solution.

Resources