views:

172

answers:

2

Hi there,

a collegue of mine proposed to me an exercise from an online judge website, which is basically a graph solving problem of an evacuation plan on a small town.

i dont need the answer (nor do i want it) i just need an advice on which is the best approach to solving it since im kinda new to these kind of problems.

the problem consists of town buildings with workers and fallout shelters in case of a nuclear attack. i have to build an algorithm that will assign the workers of each building to one or more fallout shelters but in a way that some shelters wont became too overcrowded while others remain almost empty (else i would just make the workers go to the nearest one).

the problem is this: http://acm.timus.ru/problem.aspx?space=1&num=1237

in case its offline heres the google cached version of it: http://webcache.googleusercontent.com/search?q=cache:t2EPCzezs7AJ:acm.timus.ru/problem.aspx%3Fspace%3D1%26num%3D1237+vladimir+kotov+evacuation+problem&cd=1&hl=pt-PT&ct=clnk&gl=pt

what i've done so far is for each building get the nearest shelter and move the number of workers from that build equal to the shelter capacity. then move to the next building. but sometimes the number of workers is greater than the shelter capacity, in that case after i iterate through every building, ill just iterate then again apllying the same algorithm until every building has 0 workers in it, problem is this is hardly the best way to solve it.

any tip is welcome, please dont feel like im asking for the answer, i just want an advice in the right direction of solving it.

thanks in advance.

+2  A: 

This looks like a standard Min-cost max flow problem. A bipartite graph with ~200 vertices should run easily in time.

To create the vertex constraint (each node can only handle k people), you just need to create a second graph G_1 where you add an additional vertex u_i for each v_i - and having the flow v_i to u_i be whatever your constraint be, in this case, k+1, with the cost 0. So basically if there is an edge (a,b) in the original graph G, then in G_1, there will be an edge (u_a,v_b) for each of these edges. In effect, you're creating a second layer of vertices that constraints the flow to k for each vertex.

Larry
@Larry: I feel this will work too. Care to elaborate?
Moron
This and linear programming are equivalent. See [this wiki article](http://en.wikipedia.org/wiki/Ford-Fulkerson_algorithm) for a simple algorithm to solve these problems.
BlueRaja - Danny Pflughoeft
@BlueRaja: You mean every LP can be modelled as max-flow on some graph? (I am aware that there are LP solutions for max-flow.)
Moron
@Moron: I *thought* every LP problem could be modeled by a network flow problem (and I know every network flow can be modeled by a min-max flow problem, and every min-max flow problem can be modeled by LP), but I can't find any reference to back that thought up. I am probably mistaken.
BlueRaja - Danny Pflughoeft
@BlueRaja: I doubt that that will be the case (LP <= max-flow). If I remember correctly, polynomial time network flow problems solutions were around even before the polynomial time LP algorithms came out.
Moron
It's the other way around -- you can describe a max flow problem as an LP problem. I don't think, though it's been awhile, that the converse is true. As for elaborating @Moron, I think your figure is pretty much it, except for it's a max flow (with emphasis on the min-cost). The graph setup is exactly the same. Not sure why your particular link make no mention of it.
Larry
@Larry: It does not look exactly the same. How do you model the fact that the building vertices are capped too and that you need a specific outgoing flow amount (equal to number of people, I guess) at each of those? The image in my answer does not seem to carry as is. Seems like we need some kind of transformation before we can apply min-cost max flow.
Moron
I missed the standard transformation, you're right.
Larry
@Larry: You need to do that at the shelters too. Actually we just need to add a source and a sink. A source->building edge has capacity equal to building capacity. Similarly a shelter->sink edge has capacity equal to sink capacity. Assuming a solution to the problem exists, that solution will be a max-flow in the constructed one as the maximum that can flow is the sum of building capacities. The source to bldg caps enforce that the flow at each blding is exactly equal to the capacity.
Moron
+2  A: 

This looks exactly like the Transhipment Problem which can (apparently) be solved using Linear Programming. (I say apparently, because this looks to be an instance of Integer Linear programming).

From the site:

The standard scenario where a transportation problem arises is that of sending units of a product across a network of highways that connect a given set of cities. Each city is considered either as a "source," in that units are to be shipped out from there, or as a "sink," in that units are demanded there. Each source has a given supply, each sink has a given demand, and each highway that connects a source-sink pair has a given transportation cost per unit of shipment. This can be visualized in the form of a network, as depicted in Figure TP-1 below.

Transshipment Problem fig

Given such a network, the problem of interest is to determine an optimal transportation scheme that minimizes the total cost of shipments, subject to supply and demand constraints.

Hope that helps.

Moron
thanks a lot for the help, i turned out to use the transportation problem instead of the transhipment problem (which also takes into account midleman suppliers instead of just suppliers and consumers) but they are both related so solving one requires to know how to solve the other.as for the algorithms i used a least cost assignment algorithm to find a feasible solution and then the stepping stone method to find an optimal solution.again thanks for the advice, really really helped.
sap