I need to find the best (shortest path) flow distribution for a net, where each node has export and import that I want to dispatch.
I believe the Floyd-Warshall algorithm is the best fit for this problem. But the problem is that each connection has a maximum capacity, meaning that the flow from u to v cannot exceed a certain number. How can I implement this restriction?
Also, how could I implement in the algorithm that if for a certain connection, there are two paths with the same minimum distance, then the dispatch is half in each way?
Related
I have programmed the minimum Hungarian algorithm for a bipartite graph, with Dijkstra's algorithm to find the minimum cost of a maximum matching. However, I want to use such an algorithm to implement the maximum Hungarian algorithm and don't know if it's correct to just negate the edges, because I don't know if the algorithm will handle it.
My implementation is based on the explanation on the following site: https://www.ics.uci.edu/~eppstein/163/lecture6b.pdf
Given G=(AUB, E), the idea is to label the vertices via an artificial start vertex s which has edges with unsaturated nodes in A, and run Dijkstra's algorithm from s in order to label each vertex, then after labeling each, the edges will be reweighted by their original weight minus the labels of the edge's endpoints.
I have read a lot of articles, and the only I could see is that a minimum Hungarian algorithm can be handled well with maximum cost by negating each edge, however, I am afraid that due to the fact that Dijkstra's algorithm doesn't handle negative edges well, it won't work.
First find the maximum weight in your graph. Then negate all of the weights and add the maximum weight to them. Adding the original maximum to all of the negated values makes them all positive.
You can also use INT_MAX (or whatever is equivalent to it in the programming language you're using) instead of the maximum weight. This skips the step of finding the maximum weight, but could make the first iteration of the Hungarian Algorithm take longer, or cause you to need an extra iteration of the algorithm to get the result. It probably doesn't make much of a difference either way and the performance difference will vary based on the particular weights in your graph.
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 was thinking of implementing a simple load-balancing scheme by modifying Dijkstra in that way:
Calculate shortest path
Increase cost of those links which belong to that shortest path by a number CostInc.
The idea is to make the same route less attractive for next call of Dijsktra's algorithm.
But I have questions:
Does it really work, because I can't find an info about such modification
2.What is an optimal CostInc number. It should be related with present link cost or number of nodes N, or with both of them?
Appreciate any advices.
Given an undirected graph G = G(V, E), how can I find the size of the largest clique in it in polynomial time? Knowing the number of edges, I could put an upper limit on the maximal clique size with
https://cs.stackexchange.com/questions/11360/size-of-maximum-clique-given-a-fixed-amount-of-edges
, and then I could iterate downwards from that upper limit to 1. Since this upper cap is O(sqrt(|E|)), I think I can check for the maximal clique size in O(sqrt(|E|) * sqrt(|E|) * sqrt(|E|)) time.
Is there a more efficient way to solve this NP-complete problem?
Finding the largest clique in a graph is the clique number of the graph and is also known as the maximum clique problem (MCP). This is one of the most deeply studied problems in the graph domain and is known to be NP-Hard so no polynomial time algorithm is expected to be found to solve it in the general case (there are particular graph configurations which do have polynomial time algorithms). Maximum clique is even hard to approximate (i.e. find a number close to the clique number).
If you are interested in exact MCP algorithms there have been a number of important improvements in the past decade, which have increased performance in around two orders of magnitude. The current leading family of algorithms are branch and bound and use approximate coloring to compute bounds. I name the most important ones and the improvement:
Branching on color (MCQ)
Static initial ordering in every subproblem (MCS and BBMC)
Recoloring: MCS
Use of bit strings to encode the graph and the main operations (BBMC)
Reduction to maximum satisfiability to improve bounds (MaxSAT)
Selective coloring (BBMCL)
and others.
It is actually a very active line of research in the scientific community.
The top algorithms are currently BBMC, MCS and I would say MaxSAT. Of these probably BBMC and its variants (which use a bit string encoding) are the current leading general purpose solvers. The library of bitstrings used for BBMC is publicly available.
Well I was thinking a bit about some dynamic programming approach and maybe I figured something out.
First : find nodes with very low degree (can be done in O(n)). Test them, if they are part of any clique and then remove them. With a little "luck" you can crush graph into few separate components and then solve each one independently (which is much much faster).
(To identify component, O(n) time is required).
Second : For each component, you can find if it makes sense to try to find any clique of given size. How? Lets say, you want to find clique of size 19. Then there has to exist at least 19 nodes with at least 19 degree. Otherwise, such clique cannot exist and you dont have to test it.
I'm searching for an algorithm to find a path between two nodes with minimum cost and maximum length given a maximum cost in an undirected weighted complete graph. Weights are non negative.
As I stand now I'm using DFS, and it's pretty slow (high number of nodes and maximum length too). I already discard all the impossible nodes in every iteration of the DFS.
Could someone point me to a known algorithm for better handling of this problem?
To clarify: ideally the algorithm should search for the path of minimum cost, but is allowed to add cost if this means visiting more nodes. It should end when it concludes that it's impossible to reach more than n nodes without crossing the cost limit and it's impossible to reach n nodes with less cost.
Update
Example of a graph. We have to go from A to B. Cost limit is set to 5:
This path (in red) is ok, but the algorithm should continue searching for better solutions
This is better because although the cost is increased to 4, it contains 1 more node
Here the path contains 3 nodes so it's a lot better than before and the cost is an acceptable 5
Finally this solution is even better because the path also contains 3 nodes but with cost 4, with is less than before.
Hope images explain better than text
Idea 1:
In my opinion your problem is a variation of the pareto optimal shortest path search problem. Because you refer to 2 different optimality metrics:
Longest Path by edge count
Shortest Path by edge weight
Of course some side constraints just make the problem more easy to calculate.
You have to implement a multi criteria dijkstra for pareto optimal results. I found two promising paper in english for this problem:
A multicriteria Pareto-optimal path algorithm
On a multicriteria shortest path problem
Unfortunately I wasn't able to find the pdf files for those papers and the papers I read before where in german :(
Nevertheless this should be your entry point and will lead you to an algorithm to solve your problem nice and smoothly.
Idea 2:
Another way to solve this problem could lie in the calculation of hamilton path, because the longest path in a complete graph is indeed the hamilton path. After calculation of all such path you still have to find the one with the smallest total edge weight cost. This scenario is useful if the length of the path is in every case more relevant than the cost.
Idea 3:
If the cost of the edges is the more important fact you should calculate all paths between those two nodes of a given maximum length and search for the one with the most used edges.
Conclusion:
I think the best results will be obtained by using idea 1. But I didn't know your scenario to well, therefore the other ideas might be an option two.
This problem can formulated as Multi-objective Constraint Satisfaction Problem with priority:
First, solution must satisfy the constraint about maximum cost.
Next, solution must has maximum number of nodes (1st objective).
Finally, solution must has minimum cost (2st objective).
This problem is NP-hard. So, there isn't exact polynomial time algorithm for this problem. But a simple local search algorithm may help you:
First, use Dijkstra algorithm to find minimum cost path, called P. If the cost is bigger than maximum cost, there isn't solution satisfy constraint.
Next, try add more nodes to P by using 2 move operators:
Insert: select a node outside P and insert in best position in P.
Replace: select a node outside P and replace a node inside P (when can't use insert operator).
Finally, try reduce cost by using replace operator.