2
$\begingroup$

The circular route gas station problem asks: If given a car with an unlimited gas tank, find the gas station on a circular road that if started from would allow you to travel station to station refilling with enough gas to make it back to the start. Each station i has gas[i] to be collected and each road between stations costs cost[i] gas to travel.

I have a solution and I'm taking a stab at writing a proof for its central assumption: If the total gas available across all stations is greater than or equal to the total cost required to travel between them, then a valid starting station exists. I like this proof, but I'm going for something more visual.

I'd appreciate guidance on whether the following is close to a proof. If it is, then any suggestions for massaging it into correctness are welcome.

  1. Assume the sum of all gas at the stations is greater than or equal to the sum of all road costs.
  2. There must be a station s where gas[s] >= cost[s]. If this were not true then, every gas[i] would be less than cost[i] and it would be impossible for total gas to be equal to total cost-- a contradiction.
  3. Given this, we can move from station s to the next station in the loop.
  4. After this move, the sum of our gas in the tank and gas remaining at stations must still be greater than the sum of all costs remaining. This is because the station we just moved from had at least as much gas as cost to travel from, so total gas was decreased by, at most, the amount cost was decreased. If total gas >= total cost before this, then it must also be true after, assuming any left-over gas from the move still exists in the new loop (correctly dealing with any left-over gas from the move is something I would like help with-- intuitively I feel like it can still be included).
  5. If we remove the station we just left, we can imagine closing the loop of the remaining stations. 2. follows from 4.-- there is a station we can move from.
  6. We can repeat this until we have a "loop" with no more stations.
  7. Now we are in a situation where we have shown the car can leave each station for the next (albeit with the redistribution of any extra gas). This means the whole loop can be completed, i.e. there is a valid start.

We can "leave out" the old stations because we have already completed those "links" in the route. When we close the circle without them they are still there in theory, but no longer of interest.

$\endgroup$

1 Answer 1

0
$\begingroup$

That's a pretty good proof. The core idea is that you can reduce an instance of the problem (a sequence of gas and cost values) to a smaller instance: if there is enough gas in city i to go from there to (i+1), then you can "merge" city i into city (i+1) by adding their gas and subtracting the cost of going from i to (i+1) from their total gas.

To refine this into a more detailed proof, we can make more formal (1) the notions of inputs and outputs (= solutions) of the problem, and (2) the structure of a proof by induction.

It is quite long winded. Once you are confident that you can spell out proofs to this level of detail, it becomes normal to give high-level outlines instead, which is basically what you did.


Definitions

An input is a triple $(n, \mathrm{gas}, \mathrm{cost})$ where $n \in \mathbb{N}^+$ is the number of cities ($n \ge 1$), and for all $i$ such that $1 \le i \le n$, $\mathrm{gas}_i \in \mathbb{R}^+$, $\mathrm{cost}_i \in \mathbb{R}^+$. Furthermore, valid inputs are those that satisfy: $$\sum_{i=1}^n \mathrm{gas}_i \ge \sum_{i=1}^n \mathrm{cost}_i$$

A solution to a valid input $(n,\mathrm{gas},\mathrm{cost})$ is an index $j_0$ such that, for all $k$ such that $0 \le k \le n-1$,

$$\sum_{i=j_0}^{j_0+k} \mathrm{gas}_i \ge \sum_{i=j_0}^{j_0+k} \mathrm{cost}_i$$

Where, for brevity, the indexing of cities wraps around, if $i > n$, city $i$ = city $i - n$.

We want to prove:

Theorem. Every valid input has a solution.

Proof

For that, we will prove a key lemma:

Lemma. For every valid input $(n,\mathrm{gas},\mathrm{cost})$ where $n > 1$, there exists a valid input $(n-1,\mathrm{gas}', \mathrm{cost}')$ such that if it has a solution, then $(n,\mathrm{gas},\mathrm{cost})$ has a solution.

Postponing the proof of that lemma to the end of this answer, let us first show how the rest of the proof goes using that lemma.

By induction on $n$, we prove that any valid input with $n$ cities has a solution.

  • Base case: a valid input with 1 city has a solution. Proof: because the input is valid, $\mathrm{gas}_1 \ge \mathrm{cost}_1$, so we have enough gas to take the trip from city $1$ to city $1$, which costs $\mathrm{cost}_1$.

  • Induction step: We assume that any valid input with $n-1$ cities has a solution (induction hypothesis). We prove that any valid input with $n$ cities has a solution.

    • Let $(n,\mathrm{gas},\mathrm{cost})$ be a valid input.
    • By the earlier Lemma, there exists a valid input $(n-1,\mathrm{gas}',\mathrm{cost}')$ such that: if $(n-1,\mathrm{gas}',\mathrm{cost}')$ has a solution, then $(n,\mathrm{gas},\mathrm{cost})$ has a valid solution (call this the implication).
    • By induction hypothesis, $(n-1,\mathrm{gas}',\mathrm{cost}')$ has a valid solution.
    • By the implication which we just got from our application of the Lemma, $(n,\mathrm{gas},\mathrm{cost})$ has a valid solution.

That concludes the proof by induction. To sum up, any valid input has a solution. QED.


Proof of the Lemma

Let $(n,\mathrm{gas},\mathrm{cost})$ be a valid input with $n > 1$. The goal is to (1) construct an input with $n-1$ cities, and (2) map a solution of the smaller input to a solution of the original input.

By definition:

$$\sum_{i=1}^n \mathrm{gas}_i \ge \sum_{i=1}^n \mathrm{cost}_i$$

Construction of a smaller input

There must exist $i_0$ such that $\mathrm{gas}_{i_0} \ge \mathrm{cost}_{i_0}$. (Proof by contradiction: if for all $i$, $\mathrm{gas}_i < \mathrm{cost}_i$, then $\sum_{i=1}^n \mathrm{gas}_i < \sum_{i=1}^n \mathrm{cost}_i$, which contradicts the inequality above.)

We can now define an input with $n-1$ cities, by defining its $\mathrm{gas}'$ and $\mathrm{cost}'$. Intuitively, we merge cities $i_0$ and $i_0+1$, the merged city will be at index $i_0$, and the cities at greater indices will be shifted down accordingly. There is a technicality if $i_0 = n$ which we circumvent by symmetry: we can rotate the indexing of cities beforehand so we can assume $i_0 \not= n$ without loss of generality.

$$\mathrm{gas}'_i = \left\{\begin{array}{ll}\mathrm{gas}_i & \text{if }i < i_0 \\ \mathrm{gas}_{i_0} + \mathrm{gas}_{i_0+1} - \mathrm{cost}_{i_0} & \text{if } i = i_0 \\ \mathrm{gas}_{i+1} & \text{if } i_0 < i \le n - 1 \end{array}\right.$$

$$\mathrm{cost}'_i = \left\{\begin{array}{ll}\mathrm{cost}_i & \text{if }i < i_0 \\ \mathrm{cost}_{i+1} & \text{if } i \ge i_0 \end{array}\right.$$

As a sanity check against off-by-one mistakes, note that for every $i$ such that $1 \le i \le n$, the values $\mathrm{gas}_i$ and $\mathrm{cost}_i$ are each used exactly once in the right-hand sides of the above formulas. In some sense, there is a "conservation law" going on between the bigger input and the smaller one we just constructed and which enables the rest of this proof.

Transforming a small solution to a bigger solution

Next we show that if $(n-1, \mathrm{gas}', \mathrm{cost}')$ has a solution $j_0$, then $(n,\mathrm{gas},\mathrm{cost})$ has a solution. In fact, it will be the same solution $j_0$.

Let $j_0$ be a solution of $(n-1, \mathrm{gas}', \mathrm{cost}')$. For all $k$ such that $0 \le k \le n - 2$,

$$\sum_{i=j_0}^{j_0+k} \mathrm{gas}'_i \ge \sum_{i=j_0}^{j_0+k} \mathrm{cost}'_i$$

The intuition is to split the sum at index $i=i_0$, where the split happens in the definition of $\mathrm{gas}'$. But there are technicalities to be mindful of, the actual index for the split could be $i=n+i_0$, that is the case if $i_0 < j_0$, and there is another "discontinuity" around $i = n-1$, where the indexing of $\mathrm{gas}$ and $\mathrm{gas}'$ wraps around.

By symmetry, we rotate the cities around so that we can assume $j_0 = 0$ without loss of generality. That way there is no "wrap around" to worry about. The above inequality becomes, for all $k$ such that $0 \le k \le n - 2$:

$$\sum_{i=0}^{k} \mathrm{gas}'_i \ge \sum_{i=0}^{k} \mathrm{cost}'_i$$

We want to show that, for all $k$ such that $0 \le k \le n - 1$,

$$\sum_{i=0}^{k} \mathrm{gas}_i \ge \sum_{i=0}^{k} \mathrm{cost}_i$$

There are three cases:

  • If $k < i_0$, then those inequalities are identical.
  • If $k = i_0$, we apply the former inequality at $k=i_0-1$, and add $\mathrm{gas}_{i_0} \ge \mathrm{cost}_{i_0}$ to it.
  • If $i_0 < k \le n - 1$, the following inequalities are equivalent (with the main technicality being the shifting of indices in the first step, when the sums of $\mathrm{gas}'_i$ and $\mathrm{cost}'_i$ become sums of $\mathrm{gas}_i$ and $\mathrm{cost}_i$):

$$\begin{aligned} \sum_{i=0}^{k-1} \mathrm{gas}'_i & \ge \sum_{i=0}^{k-1} \mathrm{cost}'_i \\ \sum_{i=0}^{i_0-1} \mathrm{gas}_i + \mathrm{gas}'_{i_0} + \sum_{i=i_0+2}^{k} \mathrm{gas}_i &\ge \sum_{i=0}^{i_0-1} \mathrm{cost}_i + \sum_{i=i_0+1}^{k} \mathrm{cost}_i \\ \sum_{i=0}^{i_0-1} \mathrm{gas}_i + \mathrm{gas}_{i_0} + \mathrm{gas}_{i_0+1} -\mathrm{cost}_{i_0} + \sum_{i=i_0+2}^{k} \mathrm{gas}_i &\ge \sum_{i=0}^{i_0-1} \mathrm{cost}_i + \sum_{i=i_0+1}^{k} \mathrm{cost}_i \\ \sum_{i=0}^{i_0-1} \mathrm{gas}_i + \mathrm{gas}_{i_0} + \mathrm{gas}_{i_0+1} + \sum_{i=i_0+2}^{k} \mathrm{gas}_i &\ge \sum_{i=0}^{i_0-1} \mathrm{cost}_i + \mathrm{cost}_{i_0} + \sum_{i=i_0+1}^{k} \mathrm{cost}_i \\ \sum_{i=0}^{k} \mathrm{gas}_i & \ge \sum_{i=0}^{k} \mathrm{cost}_i \end{aligned}$$

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.