Minimizing the maximum distance in a graph - algorithm

Let's say we have a weighted undirected graph. Assume there are N nodes(cities) in the graph and we want to build M (M<=N) hospitals in the city. Now we need to choose the most optimal solution, such that the maximum distance from a city to a city that has a hospital will be minimized.
Let's say we have a 3 cities and we need to build 1 hospital. Let there be edges 1-3 and 2-3, with weights 83 and 71 respectively. Obviously the optimal solution is to build a hospital in city 3, since then the maximum distance would be 83.
My idea was to use the Floyd-Warshall Algorithm and then build a hospital in a city that has a minimal max value in the distance array. Then update another array b such that b1 shows the minimum distance from city 1 to a city that has hospital and define bi simularly. After that I want to update the distance value like this:
dist_i_j = min (dist_i_j, b_j)
And repeat this until we have build all M hospitals.
But there are some cases for which this algorithm runs into a problem. Let's say we're given this graph and we need to build 3 hospitals:
edge 1-2 with distance 1
edge 1-3 with distance 2
edge 2-4 with distance 7
edge 2-6 with distance 3
edge 3-4 with distance 5
edge 4-5 with distance 2
edge 5-6 with distance 4
After the Floyd-Warshall algorithm the distance table will look like:
0 1 2 7 8 4
1 0 3 7 7 3
2 3 0 5 7 6
7 7 5 0 2 6
8 7 7 2 0 4
4 3 6 6 4 0
Obviously now it's best to build a hospital in city 6, since the max value would be 6. Now update the values:
0 1 2 6 4 0
1 0 3 6 4 0
2 3 0 5 4 0
4 3 5 0 2 0
4 3 6 2 0 0
4 3 6 6 4 0
But know we don't know whether to build a hospital in city 3 or in city 4. If we build a hospital in city 4, then updating the table we would get that we need to build hospital in city 1 and the maximum distance will be 2.
But if we build a hospital in city 3 and update the values we would get that it's best to build a hospital in city 4 or in city 5. But in both cases the max value would be 3. So how do I overcome this problem?

This is the k-center problem and is known to be NP-hard. If the graph satisfies triangle inequality, then there is a 2-approximation algorithm. See http://algo2.iti.kit.edu/vanstee/courses/kcenter.pdf

Related

Algorithm - Finding the most rewarding path in a given graph

Question: You are given the following inputs:
3
0 0 1
3 1 1
6 0 9
The first line is the number of points on the graph.
The rest of the lines contain the points on the graph, and their reward. For example:
0 0 1 would mean at point (0,0) [which is the starting point] you are given a reward of 1.
3 1 1 would mean at point (3,1) you are given a reward of 1.
6 0 9 would mean at point (6, 0) you are given a reward of 9.
Going from point a, to point b costs 1.
Therefore if you go from (0,0) -> (3,1) -> (6,0) your reward is 11-2 (cost of traversing 2 nodes) * sqrt(10).
Goal: Determine the maximum amount of rewards you can make (the total amount of reward you collect - the cost) based on the provided inputs.
How would I go about solving this? It seems like dynamic programming is the way to go, but I am not sure where to start.

Generate a Random Flow Network

I am trying to create a flow network for a given graph so that it can be used to test an algorithm. To provide clarity, I want the flow into each vertex to equal the flow out. All flow comes from the source and goes to the sink. Each edge has a maximum capacity and a direction. I would like to generate a flow through this network that equals the maximum flow (found by the min cut) that never exceeds the capacity for each edge.
Below is a graphical example of what I am given and what I am trying to obtain. Of course the "Desired Flow Graph" is not a unique example. I want this generated randomly.
Given Weighted Graph
Desired Flow Graph
I have this graph represented in MatLab with three arrays. The first array s gives the "from" vertex, the second array t gives the "to" vertex, and the third array w1 gives the maximum capacity from s to t. I would like to generate a random array such as w2 that represents the flow. (Note the letters in the pictures are equivalent to their corresponding numbers in the code where "A" = 1.
s = [1 1 1 2 3 3 4 6 5 6];
t = [2 3 4 5 5 6 6 5 7 7];
w1 = [10 15 10 8 5 7 6 5 18 15];
w2 = [8 12 6 8 5 7 6 0 13 13];
Any help with some sort of algorithm that can perform this task would be greatly appreciated. I would love a link to an algorithm, pseudocode, direct code, or even just a description of how such an algorithm may be implemented. Thanks in advance for the help.
Check out the maxflow function in MATLAB:
http://www.mathworks.com/help/matlab/ref/graph.maxflow.html
For the graph you posted, you can construct a directed graph with these commands:
s = [1 1 1 2 3 3 4 6 5 6];
t = [2 3 4 5 5 6 6 5 7 7];
w1 = [10 15 10 8 5 7 6 5 18 15];
g = digraph(s,t,w1);
Then you can use maxflow to calculate the flow values between node 1 and node 7 and return them in a new directed graph gf:
[mf,gf] = maxflow(g,1,7);
The w2 vector you refer to now just includes the edge weights of the gf graph, so you can extract it like this:
w2 = gf.Edges.Weight

What heuristic cost is correct? Why my is incorrect? Finding optimal path on graph

I have simple graph and I need to find heuristic costs for this graph.
Graph is (matrix representation):
0 1 2 0 0 0 0
1 0 0 3 3 2 0
3 0 0 2 0 0 0
0 3 1 0 1 0 0
0 3 0 1 0 6 0
0 2 0 0 6 0 2
0 0 0 0 0 2 0
Image:
Values in brackets means heuristic costs of the vertex for current goal vertex.
Green vertex is start and red vertex is goal.
I created this heristic costs matrix:
0 2 6 3 1 9 5
9 0 2 4 6 4 1
1 3 0 5 2 9 4
3 1 5 0 1 7 8
0 6 2 1 0 10 14
2 1 6 3 7 0 5
1 4 3 2 1 3 0
I have to explain this. This matrix represents this: for example goal vertex is 7; we find 7th row in matrix; value in 1st col means heuristic cost from 1 vertex to 7 vertex (7 is goal); value in 5nd col means heurisitc cost from 5 vertex to 7 vertex (7 is goal); if 5 is goal, we will work with 5 row, etc...
This heusristic costs based on nothing. I don't know how to find good heuristic costs. That is the question.
To summarize:
Firstly, my algorithm found wrong path (because of wrong heuristics probably). It found 1-3-4-5 (length 5), but best is 1-2-5 (length 4).
Also, teacher said, that my heuristic costs prevents the algorithm to find good path, but not helps him. I have problems with translating what he said into english, but he said somethink like: "your heuristic mustn't overestimate best path". What does it mean?
So the question: how to find good heuristic costs in my case?
I am going to wrap my comments as an answer.
First, note that "overestimate best path" means that your shortest path from some node v to the goal is of length k, but h(v)=k' such that k'>k. In this case, the heuristic is overestimating the length of the path. A heuristic that does it for 1 or more nodes is called "inadmissible", and A* is not guaranteed to find the shortest path with such a heuristic.
An admissible heuristic function (never overestimating) is guaranteed to provide the optimal path for A*.
The simplest admissible heuristic is h(v) = 0 for all v. Note that in this case, A* will actually behave like Dijsktra's Algorithm (which is basically an uniformed A*).
You can find more informative heuristics, one example is to first pre-process the graph and find the shortest unweighted path from each node to the goal. This can be done efficiently by BFS. Denote this unweighted distance from some v to the goal as uwd(v).
Now, you can create a heuristic which is uwd(v) * MIN_WEIGHT, where MIN_WEIGHT is the smallest edge weight in the graph.

Traveling Salesman: Matrix and Tours

Consider the following matrix/array that contains the distances between 4 cities:
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
Each row/column pair (i,j) represents the distance between city i and city j.
For example the distance between city 1 and city 4 is 3.
I just wanted to check if my understanding here is correct. Like an array, the first city starts off at 0. So in the matrix, city 1 is 0 and city 2 is 1.
The path between city 3 and city 3 would be 0? First we look at row 2 and then column 2.
Let's imagine we had the following tour: T = {1,3,2,4}. To work this out we do...
City 1 to city 3 is 2. City 3 to city 2 is 4. City 2 to 4 is 5.
So the length of the tour should be 2 + 4 + 5 = 11? In the traveling salesman problem however, we always travel back to the starting position, so from city 4 we must go back to 1 which will cost a extra 3, so our final tour is 14 (11 + 3).
Yes, correct. For more TSP info see the TSP webpage.

Construct a graph from given node degrees

I have to find which of the following values can be the degrees of an undirected graph with 6 vertices:
a) 3 2 2 2 3 3
b) 4 2 2 2 3 2
c) 5 2 2 2 0 3
d) 5 2 2 2 1 2
I only method I found is to try to draw the graph on a sheet of paper and then check if it is possible.
I just need a hint to start this problem, if possible, in other way than drawing each graph.
The following algorithm decides if a simple graph can be constructed with given node degrees:
sort the degrees in descending order
if the first degree is 0 (i.e.all degrees are 0) then obviously such a graph can be formed (no edges) and you are done.
if the first degree has value d (> 0) then the following d degrees must be greater 0. If not you are done: no such graph can be formed.
take away the first degree (value d) and reduce the following d degrees by one (i.e. draw the requested number of edges from the node with highest degree to the nodes with highest degrees among the remaining ones - see proof below for correctness of this assumption), then continue with step 1 (with now one node less)
example a) (can be rejected because of the odd sum of weights, but also the above algorithms works)
3 2 2 2 3 3
3 3 3 2 2 2
2 2 1 2 2
2 2 2 2 1
1 1 2 1
2 1 1 1
0 0 1
1 0 0
-1 not possible
example c)
5 2 2 2 0 3
5 3 2 2 2 0
2 1 1 1 -1 not possible
example d)
5 2 2 2 1 2
5 2 2 2 2 1
1 1 1 1 0
0 1 1 0
1 1 0 0
0 0 0 ok
What is missing is a proof that if a graph can be drawn with given node degrees, then one of the matching graphs has this property of step 4, i.e. that the node with highest degree is connected with the nodes with next highest degrees.
Let us therefore assume that A is the node with highest degree and that it is connected with a node B whose degree is less then the degree of node C not being connected to A. Since degree(C) > degree(B), there is node D connected to C and not connected to B. Thus, there are edges AB and CD, and there are no edges AC nor BD. So we can replace AB and CD by the edges AC and BD without changing the nodes' degrees.
By repeating this procedure enough times we can make all nodes with the next highest degrees being connected to node with the highest degree.
The handshaking lemma or degree sum formula is necessary and sufficient condition in this case, since we only care that it forms an undirected graph (orientation of the edge doesn't matter, but nothing is said about loop or parallel edges). Therefore, option c and option d are valid 6-vertex undirected graph.
If the question asks for simple undirected graph (loop and parallel edges disallowed), then we need to bring in the algorithm by Havel/Hakimi, which is as described by #coproc.

Resources