Reputation: 11
I am trying to determine an efficient, maximum flow algorithm using a directed graph where, given a list of n flights (where each entry has the starting city, ending city, departure time, arrival time, and capacity of the flight), will route as many people as possible starting from city A and ending at city B. I also want to be able to return the set of flights that can be taken such that the maximum possible amount of people will get to city B from city A. I think that it can just be an implementation of the Ford-Fulkerson algorithm, or something similar, but I am having trouble being able to transform this schedule into a max-flow instance in an efficient way, and specifically what the pseudocode of said algorithm would look like after having done so.
Upvotes: 1
Views: 809
Reputation: 2346
The algorithm you're thinking about would to the trick, but the graph is it set to work on must be constructed properly.
Your issue here is timing. Let's say You want to people from A
to C
by 14:00 and we have a total of 4 flights:
Flight 1: A -> B, 10:00 -> 11:00, Cap. 100
Flight 2: A -> B, 11:00 -> 12:00, Cap. 100
Flight 3: B -> C, 11:30 -> 12:30, Cap. 100
Flight 4: B -> C, 12:30 -> 13:30, Cap. 100
You can see here that you could fill all flights and get 200 from A
to C
in time, but constructing the graph needs to take the time into account.
What I suggest is that you don't have one node to represent B
, but rather have a few of them:
(B, 11:00)
- B
at 11:00.
(B, 12:00)
- B
at 12:00.
(B, 12:30)
- when flight #3 departs.
(B, 13:30)
- when flight #4 departs.
Any flight that can leave from B
will be added to the graph once, starting from the relevant B
node.
B
nodes are connected to other B
nodes in an edge with infinite capacity in the order of the time moving forwards. This allows passengers to "wait" between different times in B
.
This example would end up with the following list of edges:
// Flight edges
[(A, 10:00)
, (B, 11:00)
], Cap. 100
[(A, 11:00)
, (B, 12:00)
], Cap. 100
[(B, 11:30)
, (C, 12:00)
], Cap. 100
[(B, 12:30)
, (C, 13:00)
], Cap. 100
// Waiting edges
[(B, 11:00)
, (B, 11:30)
], Cap. infinite
[(B, 11:30)
, (B, 12:00)
], Cap. infinite
[(B, 12:00)
, (B, 12:30)
], Cap. infinite
Upvotes: 1