In a connected graph, there are n points where hungry people are standing. Every hungry person wants to go one of the k restaurants in the graph. The travel distance should be within a 1 km distance for each person. A restaurant can fit no more than CEILING[n/k] guests.
Provided the points of these hungry people and the restaurants in the area, is there an efficient algorithm that runs in polynomial time that tells whether EVERY guest can be accommodated (as in True or False)?
This sort of reminds me of the Travelling Salesman problem so is it just a modified version of it?
This is an example of a matching problem on a bipartite graph. This is much easier to solve than the travelling salesman problem and can be done in O((n+k)^3).
There are two subproblems:
Find which people can reach each restaurant
Find how to match people to restaurants to avoid overflowing the constraints
Reaching Restaurants
It is possible to compute the shortest path between any pair of points in O(n^3) by using, for example, the Floyd-Warshall algorithm.
The allowed matchings are then the connections of distance less than 1 km.
Matching people to restaurants
This matching problem can be solved by constructing a graph and then solving for the maximum flow.
An appropriate graph is to have a source node, a sink node, and a node for each person and each restaurant.
Connect the source to each person with capacity 1
Connect each person to each restaurant within 1km with capacity 1
Connect each restaurant to the sink node with capacity Ceil(n/k).
Then compute the maximum flow through this graph. Every guest can be accomodated if and only if the maximum flow is n.
There are many algorithms to compute the maximum flow. One example would be push-relabel with complexity O(V^3) where V is the number of vertices (V=n+k+2 in this problem).
Related
Suppose you have "n" managers and "n" stores all located randomly across a geographic area. I need to be able to assign each manager to a store. The managers will travel daily from their homes to their assigned store. In general I'd like to minimize the daily distance travelled. This can be interpreted in two ways:
Minimize the average daily travel distance (which is also the same as minimizing the total travel distance.
Minimize the maximum travel distance for any single manager
Is this a known problem? Are there any obvious algorithms to solve it? It seems similar to the traveling salesman problem but it's not quite the same.
Both can be solved polynomially
I'll quickly cover both ways to define an optimal allocation as described in the question. Note that I won't make any assumption on the traveling times, such as triangle inequality. Of course such a property is likely to hold in practice, and there may be better algorithms that use these properties.
Minimize total distance
For this instance, we consider the managers and the stores to be a weighted complete bipartite graph. We then want a matching that minimizes the sum of the weights.
This is called the Balanced Assignment Problem, which is a specific case of minimum/maximum matching. Because the graph is bipartite, this can be solved polynomially. Wikipedia lists a couple of algorithms for solving this problem, most notably the Hungarian algorithm.
Minimize maximum distance
If we wish to minimize the maximum distance, we can find a solution through a binary search. Specifically, we binary search over the maximum distance and attempt to find a matching that does not violate this maximum distance.
For any given maximum distance x, we create the bipartite graph that has edges between manager M and store S if and only if d(M, S) < x. We then try to create a complete matching on this bipartite graph with any bipartite matching algorithm, and through success and failure complete the binary search for the smallest x that allows for matching, thus minimizing the maximum distance.
I am having some troubles solving a problem about Minimum Spanning Tree. So each node in a graph is a city, and is possible to have weight edges connecting two nodes, which is the cost of building a road between two cities. The problem is basically telling the minimum cost of building the roads and have all cities connected some way. I can easily solve this by using Prim or kruskal algorithm to solve this sub-problem of my biggest problem.
The tricky part comes now: Each city (node) can have an airport and each airport has a one time cost (if you decide to build it). You can travel between two cities using an airport if both cities have airports. Now I have to calculate the minimium cost of building roads AND airports in order to have all cities connected, but I am having difficulty representing the connections with the airports with the rest of the network. Can somebody help me here? Maybe I am completely wrong about using MST?
The only solution I came up is: For each city that has an aiport, I will connect that city with another cities that have airports. Also If the cost of building two airports is lower then building a road I take it in consideration. I run kruskal in order to get the cheapest edge, but If kruskal chooses an "airport" edge, I will add it to the spanning tree and then 0 the cost of both airports (if they havent been built in the past). I believe, that by doing this dynamic weight changes while runing kruskal, I am destroyng the idea of getting the minimum cost.
There are two possibilities:
1) The optimal solution does not use airports. In this case you can ignore airports and construct the minimum spanning tree as usual.
2) The optimal solution uses airports. In this case add a dummy node to the graph called "sky". The cost of building a road from any city to "sky" is the cost of building an airport. The cost of the optimal solution using airports is the cost of the minimum spanning tree in this ammended graph.
So try both options and pick the minimum cost.
There are N warehouses storing Q[i] quantity of an item on Day 1. On day 2, the requirements are for quantity Q'[i] in each warehouse. So basically item has to be moved between warehouses to fulfill the constraint. The distance between warehouses is known. What class of algorithms can solve this? Any pointers? The goal is to minimize the distance traveled in moving goods.
This is a classic problem which is solved with a min-cost max-flow algorithm. You augment your graph by adding two extra vertices: a source and a sink. From the source to all vertices of the original graph you add an edge with capacity equal to Q[i] and zero cost. From each vertex of the original graph you add an edge to the sink with capacity equal to Q'[i] and zero cost. For the edges between the vertices of the original graph you set the capacity to infinity and cost to the distance between the corresponding warehouses, and then compute the min-cost max-flow. The flow between the vertices of the original graph will tell you how many goods to transfer between those two warehouses.
Some links:
wikipedia article about min-cost max-flow
A very good presentation (they have a problem similar to yours there but not identical)
Here's a great article with the details of a very good implementation
There is a network of towns, connected by roads of various integer lengths.
A traveler wishes to travel in his car from one town to another. However, he does not want to minimize distance traveled; instead he wishes to minimize the petrol cost of the journey. Petrol can be bought in any city, however each city supplies petrol at various (integer) prices (hence why the shortest route is not necessarily the cheapest). 1 unit of petrol enables him to drive for 1 unit of distance.
His car can only hold so much petrol in the tank, and he can choose how many units of petrol to purchase at each city he travels through. Find the minimum petrol cost.
Does anyone know an efficient algorithm that could be used to solve this problem? Even the name of this type of problem would be useful so that I can research it myself! Obviously it's not quite the same as a shortest path problem. Any other tips appreciated!
EDIT - the actual problem I have states that there will be <1000 cities; <10000 roads; and the petrol tank capacity will be somewhere between 1 and 100.
You could solve this directly using Djikstra's algorithm if you are happy to increase the size of the graph.
Suppose your petrol tank could hold from 0 to 9 units of petrol.
The idea would be to split each town into 10 nodes, with node x for town t representing being at town t with x units of petrol in the tank.
You can then construct zero-cost edges on this expanded graph to represent travelling between different towns (using up petrol in the process so you would go from a level 8 node to a level 5 node if the distance was 3), and more edges to represent filling up the tank at each town with one unit of petrol (with cost depending on the town).
Then applying Djikstra should give the lowest cost path from the start to the end.
I think the question is: Is there a chance the petrol stuff makes the underlying traveling salesman problem computationally more feasible? If not, there is no efficient non-approximating algorithm.
Of course, you can find efficient solutions for edge cases, and there might be more edge cases with the petrol condition, as in, always take this city first because the petrol is so cheap.
I think you can solve this with dynamic programming. For each node, you save an array of tuples of petrol cost and the length of the path where you use that petrol, containing the optimal solution. Every step you loop trough all nodes and if there is a node you can go, which already has a solution, you loop trough all the nodes you can go to with a solution. You select the minimum cost, but note: you have to account for the petrol cost in the current node. All costs in the array that are higher than the cost in the current node, can instead be bought at the current node. Note that nodes which already have a solution should be recalculated, as the nodes you can go to from there could change. You start with the end node, setting the solution to an empty array (or one entry with cost and length 0). The final solution is to take the solution at the beginning and sum up every cost * length.
I'd try this:
Find the shortest route from start to destination. Dijkstra's algorithm is appropriate for this.
Find the minimum cost of petrol to travel this route. I'm not aware of any off-the-shelf algorithm for this, but unless there are many cities along the route even an exhaustive search shouldn't be computationally infeasible.
Find the next shortest route ...
Defining precise stopping criteria is a bit of a challenge, it might be best just to stop once the minimum cost found for a newly-tested route is greater than the minimum cost for a route already tested.
So, use 2 algorithms, one for each part of the problem.
This might be optimized suitably well using a Genetic Algorithm. Genetic Algorithms beat humans at some complex problems:
http://en.wikipedia.org/wiki/Genetic_algorithm
The gist of a Genetic Algorithm is:
Come up with a ranking function for candidate solutions
Come up with a pool of unique candidate solutions. Initialize it
with some randomly-generated possibilities. Maybe 10 or 100 or
1000...
Copy a candidate solution from the pool and perturb it in some way -
add a town, remove a town, add two towns, etc. This might improve
or worsen matters - your ranking function will help you tell. Which
one do you pick? Usually, you pick the best, but once in a while,
you intentionally pick one that's not to avoid getting stuck on a
local optimum.
Has the new solution already been ranked? If yes, junk it and go to
If no, continue...
Add the perturbed candidate back to the pool under its newly-calculated rank
Keep going at this (repeat from #3) until you feel you've done it long enough
Finally, select the answer with the best rank. It may not be
optimal, but it should be pretty good.
You could also formulate that as an integer linear programming (ILP) problem. The advantage is that there is a number of off-the-shelf solvers for this task and the complexity won't grow so fast as in the case of Peters solution with the size of the tank.
The variables in this particular problem will be the amounts of petrol purchased in any one town, the amount in the cars tank in any town on the way and actual roads taken.
The constraints will have to guarantee that the car spends the necessary fuel on every road and does not have less that 0 or more than MAX units of fuel in any town and that the roads constitute a path from A to B.
The objective will be the total cost of the fuel purchased.
The whole thing may look monstrous (ILP formulations often do), but it does not mean it cannot be solved in a reasonable time.
I have Graph with N nodes and edges with cost. (graph may be Complete but also can contain zero edges).
I want to find K trees in the graph (K < N) to ensure every node is visited and cost is the lowest possible.
Any recommendations what the best approach could be?
I tried to modify the problem to finding just single minimal spanning tree, but didn't succeeded.
Thank you for any hint!
EDIT
little detail, which can be significant. To cost is not related to crossing the edge. The cost is the price to BUILD such edge. Once edge is built, you can traverse it forward and backwards with no cost. The problem is not to "ride along all nodes", the problem is about "creating a net among all nodes". I am sorry for previous explanation
The story
Here is the story i have heard and trying to solve.
There is a city, without connection to electricity. Electrical company is able to connect just K houses with electricity. The other houses can be connected by dropping cables from already connected houses. But dropping this cable cost something. The goal is to choose which K houses will be connected directly to power plant and which houses will be connected with separate cables to ensure minimal cable cost and all houses coverage :)
As others have mentioned, this is NP hard. However, if you're willing to accept a good solution, you could use simulated annealing. For example, the traveling salesman problem is NP hard, yet near-optimal solutions can be found using simulated annealing, e.g. http://www.codeproject.com/Articles/26758/Simulated-Annealing-Solving-the-Travelling-Salesma
You are describing something like a cardinality constrained path cover. It's in the Traveling Salesman/ Vehicle routing family of problems and is NP-Hard. To create an algorithm you should ask
Are you only going to run it on small graphs.
Are you only going to run it on special cases of graphs which do have exact algorithms.
Can you live with a heuristic that solves the problem approximately.
Assume you can find a minimum spanning tree in O(V^2) using prim's algorithm.
For each vertex, find the minimum spanning tree with that vertex as the root.
This will be O(V^3) as you run the algorithm V times.
Sort these by total mass (sum of weights of their vertices) of the graph. This is O(V^2 lg V) which is consumed by the O(V^3) so essentially free in terms of order complexity.
Take the X least massive graphs - the roots of these are your "anchors" that are connected directly to the grid, as they are mostly likely to have the shortest paths. To determine which route it takes, you simply follow the path to root in each node in each tree and wire up whatever is the shortest. (This may be further optimized by sorting all paths to root and using only the shortest ones first. This will allow for optimizations on the next iterations. Finding path to root is O(V). Finding it for all V X times is O(V^2 * X). Because you would be doing this for every V, you're looking at O(V^3 * X). This is more than your biggest complexity, but I think the average case on these will be small, even if their worst case is large).
I cannot prove that this is the optimal solution. In fact, I am certain it is not. But when you consider an electrical grid of 100,000 homes, you can not consider (with any practical application) an NP hard solution. This gives you a very good solution in O(V^3 * X), which I imagine is going to give you a solution very close to optimal.
Looking at your story, I think that what you call a path can be a tree, which means that we don't have to worry about Hamiltonian circuits.
Looking at the proof of correctness of Prim's algorithm at http://en.wikipedia.org/wiki/Prim%27s_algorithm, consider taking a minimum spanning tree and removing the most expensive X-1 links. I think the proof there shows that the result has the same cost as the best possible answer to your problem: the only difference is that when you compare edges, you may find that the new edge join two separated components, but in this case you can maintain the number of separated components by removing an edge with cost at most that of the new edge.
So I think an answer for your problem is to take a minimum spanning tree and remove the X-1 most expensive links. This is certainly the case for X=1!
Here is attempt at solving this...
For X=1 I can calculate minimal spanning tree (MST) with Prim's algorithm from each node (this node is the only one connected to the grid) and select the one with the lowest overall cost
For X=2 I create extra node (Power plant node) beside my graph. I connect it with random node (eg. N0) by edge with cost of 0. I am now sure I have one power plant plug right (the random node will definitely be in one of the tree, so whole tree will be connected). Now the iterative part. I take other node (eg. N1) and again connected with PP with 0 cost edge. Now I calculate MST. Then repeat this process with replacing N1 with N2, N3 ...
So I will test every pair [N0, NX]. The lowest cost MST wins.
For X>2 is it really the same as for X=2, but I have to test connect to PP every (x-1)-tuple and calculate MST
with x^2 for MST I have complexity about (N over X-1) * x^2... Pretty complex, but I think it will give me THE OPTIMAL solution
what do you think?
edit by random node I mean random but FIXED node
attempt to visualize for x=2 (each description belongs to image above it)
Let this be our city, nodes A - F are houses, edges are candidates to future cables (each has some cost to build)
Just for image, this could be the solution
Let the green one be the power plant, this is how can look connection to one tree
But this different connection is really the same (connection to power plant(pp) cost the same, cables remains untouched). That is why we can set one of the nodes as fixed point of contact to the pp. We can be sure, that the node will be in one of the trees, and it does not matter where in the tree is.
So let this be our fixed situation with G as PP. Edge (B,G) with zero cost is added.
Now I am trying to connect second connection with PP (A,G, cost 0)
Now I calculate MST from the PP. Because red edges are the cheapest (the can actually have even negative cost), is it sure, that both of them will be in MST.
So when running MST I get something like this. Imagine detaching PP and two MINIMAL COST trees left. This is the best solution for A and B are the connections to PP. I store the cost and move on.
Now I do the same for B and C connections
I could get something like this, so compare cost to previous one and choose the better one.
This way I have to try all the connection pairs (B,A) (B,C) (B,D) (B,E) (B,F) and the cheapest one is the winner.
For X=3 I would just test other tuples with one fixed again. (A,B,C) (A,B,D) ... (A,C,D) ... (A,E,F)
I just came up with the easy solution as follows:
N - node count
C - direct connections to the grid
E - available edges
1, Sort all edges by cost
2, Repeat (N-C) times:
Take the cheapest edge available
Check if adding this edge will not caused circles in already added edge
If not, add this edge
3, That is all... You will end up with C disjoint sets of edges, connect every set to the grid
Sounds like the famous Traveling Salesman problem. The problem known to be NP-hard. Take a look at the Wikipedia as your starting point: http://en.wikipedia.org/wiki/Travelling_salesman_problem