The Shortest Augmenting Paths for Online Matchings on Trees

This paper is devoted to understanding the shortest augmenting path approach for computing a maximum matching. Despite its apparent potential for designing efficient matching and flow algorithms, it has been poorly understood. Chaudhuri et. al. [K. Chaudhuri, C. Daskalakis, R. D. Kleinberg, and H. Lin. Online bipartite perfect matching with augmentations. In INFOCOM 2009.] study this classical approach in the following model. A bipartite graph $G=W \uplus B$ is revealed online and in each round a vertex of $b$ is presented together with the adjacent edges. It is then matched by applying the shortest among the augmenting paths. Chaudhuri et. al. conjecture that the total length of the augmenting paths is $O(n \log n)$, where $n$ is the number of vertices in the final graph. Recently a bound of $O(n \log^2 n)$ has been proven given that the underlying graph is a tree [B. Bosek, D. Leniowski, P. Sankowski, and A. Zych. Shortest augmenting paths for online matchings on trees. In WAOA 2015]. We further improve this bound to $O(n \log n)$. To achieve that, we introduce brand new techniques that we believe are applicable to bipartite graphs as well.


Introduction
One of the most fundamental techniques used by matching and flow algorithms is to apply augmenting paths until a matching/flow is found. Intuitively, the work needed for that should be minimized if the shortest path is chosen each time. Using this approach Edmonds and Karp in 1972 have shown the first strongly polynomial time algorithm for the maximum flow problem [EK72]. Various complicated algorithms for flows and matchings have been invented since then, forgetting this classical technique without properly understanding it. This may be due to the fact that shortest augmenting paths do not seem to admit a nice structure useful for analysis, contrary to other choices of augmenting paths [BLSZ14,CDKL09].
To be able to analyze the approaches for computing matchings using augmenting paths, we adopt the following model. A bipartite graph G = W B is revealed online and in each round a vertex of b is presented together with the adjacent edges. It is then matched by applying the shortest augmenting path. In fact, Bosek et. al. [BLSZ14] introduce a different way to choose an augmenting path, which allows to prove that the total length of all augmenting paths applied to find a final matching is O(n √ n). This gives raise to a dynamic algorithm that computes the maximum matching online in a total time O(m √ n), where m is the number of edges in the final graph. This matches the best offline algorithm for bipartite matching proposed by Hopcroft and Karp [HK73]. Chaudhuri et. al. [CDKL09] conjecture, that for shortest paths the total length should be O(n log n). We feel that proving this conjecture may have significant impact on algorithms computing bipartite matching. In [BLSZ15] the authors prove a bound of O(n log 2 n) given that the underlying graph is a tree. In this paper we further improve this bound to O(n log n). To achieve that, we introduce brand new techniques that we believe are applicable to bipartite graphs as well.

Preliminaries
In this section we introduce in detail the model that we study throughout the paper and the notation we describe it with. We start with describing a more general model than studied here, where the presented graph is an arbitrary bipartite graph. We then provide some relevant properties that hold in this more general model. The restriction that the graph being presented is a tree becomes relevant from the next section on.
Let W and B be two sets of vertices over which the bipartite graph will be formed. The set W (called white vertices) is given up front to the algorithm, whereas the vertices in B (black vertices) arrive online. We denote by G t = W B t , E t the bipartite graph after the t'th black vertex has arrived where X Y is a disjoint sum of X and Y . The graph G t is constructed online in the following manner. We start with G 0 = W B 0 , E 0 = W ∅, ∅ . In turn t ∈ {1, . . . , n} a new vertex b t ∈ B together with all its incident edges E(b t ) is revealed and G t is defined as: The final graph is G n = (W B, E).
The goal of the online algorithm is to compute for each G t the maximum size matching M t . It can use M t−1 to obtain M t . In this paper we study one particular such algorithm, referred to as the Shortest Augmenting Path algorithm (Sap). When b t arrives, the Sap algorithm always chooses the shortest among all available augmenting paths. In this paper, without loss of generality of our results, we assume for simplicity that all the presented vertices can be matched, so the algorithm always applies some path. If there were unmatchable vertices, we could completely ignore them and regard them as invisible, or in other words nonexistant. This assumption implies that Hall's condition holds through all turns: |N(X)| |X| for all t = 0, 1, . . . , n and all X ⊆ B t , where by |Y | we denote the number of elements of a set Y .
For every t ∈ {1, . . . , n}, matching M t induces an orientation of the edges of G t : the matched edges are oriented towards black vertices, while the unmatched edges are oriented towards white vertices. When a new vertex b t arrives, we get an intermediate orientation where the edges of b t are oriented towards its neighbors, and the rest of the edges is oriented according to M t−1 . Any simple directed path from b t to some unmatched white vertex is an augmenting path. In turn t the edges are reoriented along augmenting path π t chosen by the Sap algorithm, and the resulting orientation is G t . An important property of the orientation induced by matching is that black vertices have at most one incoming edge (one if they are matched) and the white vertices have at most one outgoing edge (one if they are matched). The unmatched white vertices are called free and have no outgoing edges.
One of the crucial concepts used further is a concept of dead vertices. Dead vertices are the presented vertices that cannot reach any free white vertex via a directed path. The black vertices not presented yet are assumed not dead. Clearly, an augmenting path cannot pass through a dead vertex. A more interesting (and less clear) observation is that a dead vertex stays dead through all the upcoming turns. Hence, once a vertex becomes dead, no augmenting path ever contains it in the upcoming turns. Interestingly, whether a vertex is dead depends purely on the structure of the graph and not on the matching. We adopt the following alternative definition of a dead vertex.
and |N(X)| = |X|, • a vertex w ∈ W is dead in turn t iff it has a dead neighbour.
We denote the set of dead vertices in turn t as D t . The set of alive and presented vertices is denoted as A t = W ∪ B t \ D t . Moreover the set of not presented vertices is denoted as P t = B n \ B t . Consider, for a dead vertex b ∈ D t , a set X from Definition 1 that certifies that b is dead (see Figure 1). All vertices of N(X) are matched to X and there are no directed edges leaving the set of vertices X ∪ N(X). This means in particular that b cannot reach any free white vertex. Also, it implies that no alternating path can ever enter X ∪ N(X) and reorient the edges, so an edge leaving this set never appears. If a vertex b does not admit a directed path to a free white vertex, the black vertices reachable from b constitute a set X certifying that b is dead according to the Definition 1.
In the remainder of the paper study the restriction where the final graph G n is a tree and denote it as T = G n . The consequence of this restriction is also that for each t = 0, 1, . . . , n the graph G t is a forest and denote it as F t = G t .

The mini-max game
Our goal in this paper is to prove that the total length of all augmenting paths applied by the shortest augmenting path algorithm on a tree is O(n log n). More formally, we want to prove the following, where by ||π|| we denote the number of edges on a path π.
Theorem 1. Let π t be the path applied by Sap in turn t. Then n t=0 ||π t || ∈ O(n log n) In [?] a worse bound of O(n log 2 n) is given. There, the authors bound the length of each path π t by the length of some other augmenting path in F t . Any other path is by definition longer than π t . The authors bound the total length of these other paths. This is because, although the paths applied by the Sap algorithm are very simple, they do not admit any nice structure useful for analysis.
In this paper we also do not study directly the paths applied by Sap, but a collection of other paths that are possibly longer. The main reason we do so is that the paths we introduce are easier to analyze and allow an elegant proof. The second, but not any less important reason is that thanks to them we obtain a slightly stronger result than stated in Theorem 1.
The main idea explored here is to assume that with each turn the Sap algorithm gets the worst possible matching on F t (i.e., the one maximizing the shortest augmenting path). The shortest augmenting paths consecutively chosen in such scenario by Sap are the subject of our analysis. We will prove that their total length is O(n log n). Luckily for us, these paths admit a very nice mini-max structure. We now move on to describing it and introducing some notation and definitions needed for that.
Let us consider what is the worst possible tree orientation. Think of a game, where the algorithm chooses the shortest augmenting path, and the adversary tries to produce a matching (given by the corresponding orientation) where such path is the longest. As mentioned before, the orientation given by matching is characterized by the fact, that each black vertex has at most one incoming edge, and each white vertex has at most one outgoing edge. If there is several outgoing edges, the algorithm may choose which one to follow. If, however, there is only one, the algorithm has no choice but to follow it. The freedom of the adversary is to maximize the augmenting path length by routing the algorithm towards long branches via white vertices.
These considerations lead us to defining the determined mini-max distance as a function taking a vertex as an argument. It represents the worst case directed distance from a vertex to the closest free white vertex. This distance, however, depends on the edge used by the augmenting path to enter the vertex. Let us imagine the algorithm enters a black vertex b via a determined edge {w, b}. The adversary cannot spoil anything here -all the remaining edges incident to b are outgoing. The algorithm picks an edge that leads to the shortest among all available paths. The definition follows. There, det-dist(, ) stands for determined distance. Also, det-dir(, ) stands for determined direction and denotes the white vertex that gives the shortest available path.
• The determined min-max distance is defined as • Vertex w ∈ N(b) \ {w} which determines the minimum is denoted as det-dir t (w, b). If there are more such vertices we choose the first one in some a priory order. If N(b) \ {w} is empty than det-dir t (w, b) is not defined.
Note, that if b is a leaf, then det-dist t (w, b) = 2n + 1, the value that plays a role of infinity. This makes sense since we seek a white leaf which we cannot reach if we follow {w, b}. Now imagine the algorithm enters a white vertex w via an edge {b, w}. The edge {b, w} is not a matching edge. The adversary has thus the freedom to pick the matching edge and route the path along this edge. Clearly, the adversary picks the longest available path and routes the algorithm there. The definition follows.
• The determined min-max distance is defined as . If there are more such vertices we choose the first one in some a priory order. If N(w) ∩ B t \ {b} is empty than det-dir t (b, w) is not defined.
Note that if w is a leaf, then det-dist t (b, w) = 0. The next step is to get rid of the context, i.e., the dependence of the minimax distance on the edge entering the vertex. Upon a black vertex Sap generally wants to follow the "minimum" direction, so it is reasonable to define for a black vertex b the minimum determined distance over all outgoing edges. If, however, the algorithm is unlucky, the augmenting path comes in via the edge that determines the minimum. In this case the minimum is not available for the algorithm. To be prepared for such a coincidence, we define the second minimum as well. The definitions regarding black vertices follow, where dist() stands for distance and sec-dist() stands for second distance.
• The min-max distance is defined as • Vertex w ∈ N(b) which determines the minimum is denoted as dir t (b). If there are more such vertices we choose the first one in some a priory order.
• Vertex which determines sec-dist t (b) is denoted as sec-dir t (b). If there are more such vertices we choose the first one in some a priory order.
We introduce analogous definitions for white vertices.
Definition 5. For w ∈ W : • The min-max distance is defined as • Vertex b ∈ N(w)∩B t which determines the maximum is denoted as dir t (w).
If there are more such vertices we choose the first one in some a priory order. If N(w) ∩ B t is empty than dir t (w) is not defined. • The second min-max distance is defined as are more such vertices we choose the first one in some a priory order. If The usefulness of the sec-dist() function is stated formally in the following observation. It states, that the only possibility for sec-dist t (v) to determine the mini-max distance is to enter v via the edge determining dist t (v).
The function det-dist t (v, u), given the entry edge (v, u) to vertex u, returns one plus the distance of some neighbor det-dir t (v, u) of u other than v. This defines a determined mini-max path in a natural recursive way. An undetermined mini-max path can be defined using the notion of a determined mini-max path.
Definition 6. For any t = 0, . . . , n we define and The above definition brings us to the merits of this section. First of all, it should be clear now why the lengths of the mini-max paths bound from above the lengths of the augmenting paths used by Sap in a non-adversary scenario. Nevertheless we give a formal proof below.
Lemma 3. For each black vertex b t0 ∈ B and each turn t = t 0 , . . . , n it holds that where π is the shortest augmenting path from b t0 to a white unmatched vertex according to a given arbitrary matching M in W ∪ B t .
Proof. Let's 1 t 0 t n and indicate vertex b t0 as a root in of the b t0 's connected We begin to deal with the problem of the dead vertices by the introduction the tree T which is extension of T by joining to each black leaf b i of T the new path P i by adding 2n + 1 vertices: and 2n + 1 edges: Moreover we extend the matching M to M by adding matches {w i,1 , b i,1 }, {w i,2 , b i,2 }, . . . , {w i,n , b i,n } for each new path P i . First of all, we note that the path π is also the shortest augmenting path from b t to unmatched white vertex in the tree T according to the matching M . This is true because extension from T , M to T , M causes extension the family of possible augmenting paths only by paths π which contains whole some new path P i and in consequence with the length ||π || in like manner as versions without primes (see Definitions 2, 3, 4, 5, and 6) but with the difference that we substitute T to T . One can easily check that by induction on height of subtree . This allows us to reformulate the statement of Lemma 3 to the equivalent following version. For 1 t 0 t n it holds that where π is one of the shortest augmenting path in T according to a given matching To prove the equation (1) we introduce ρ (v) which is the shortest alternating path from v to some unmatched white vertex according to the matching M in subtree T (v) rooted in v ∈ T . Alternating path means that the edges are alternately matched. If alternating path leads from unmatched black vertex to unmatched white vertex then it is augmenting path. In particular, ||ρ (b t0 )|| = ||π ||. Now, we again apply induction on the height of subtree T (v) rooted in v ∈ T to proof the following induction hypothesis: (1) If a vertex v = b is black and, none of children of b is matched with b, then ρ (b) exists and (2) If a vertex v = w is white then ρ (w) exists and Because in T there is no black leaves, if the height of subtree equals 0 then just check case when v = w is white, i.e. case (2). The induction hypothesis is true because both ρ (w) and det-path t (b, w) have length 0. Now let assume that the subtree T (v) rooted in v has height equals h > 0 and v = b is black which is not matched to any of his child. Let w 0 , . . . , w k be the children of b. Because by the induction hypothesis there are the shortest alternating paths ρ (w 0 ), . . ., ρ (w k ) we construct alternating path by starting from b and then following any of them. So, there must exists the shortest one = ρ (b). Without lost of generality we can assume that ρ is the shortest alternating path and any of ρ It remained the case when the subtree T (v) rooted in v has height equals h > 0 and v = w is white. If w is unmatched with any of his child then ρ (w) = (w) and ||ρ (w)|| = 0 det-dist t (b, w). So we can assume that w is matched with some child = b 0 . Then b 0 cannot be matched with any of his child and then we can use induction hypothesis for b . Because ρ (b ) exists we also obtain existing of an alternating path by concatenation of w and ρ (b ). Because this only one possible construction of an alternating path from w to some unmatched white vertices in T we obtain that this construction gives ρ t (w). In consequence by induction hypothesis for b 0 we obtain that Taking all together we have that Thus, it suffices to bound from above the total length of mini-max paths. The statement that we prove in the remainder of the paper is as follows.
Theorem 4 clearly implies Theorem 1 based on Lemma 3 stated above. Moreover, it states a stronger result: even if an adversary messes up the matching with each turn, the total length of the shortest augmenting paths is still O(n log n). Also, thanks to mini-max paths we got rid of the matching and the corresponding orientation of the graph.
We conclude with a few lemmas and observations related to the mini-max distance functions defined in this section. One can observe, that each distance function is associated with a path: . It is clear from the definitions that the minimax distance is finite (i.e., less than 2n + 1) if and only if the corresponding path ends in a white leaf. If the distance is finite, it is equal to the length of the corresponding path. It turns out that we can provide yet another definition of dead vertices relying purely on whether or not some mini-max distance is finite. The details are given in the following corollaries of Lemma 3 and following Lemma 5.
Lemma 5. For any black vertex b t0 ∈ B and each turn t = t 0 , . . . , n there is the matching M in W ∪ B t for which the following statements are satisfied. ( exists and is the second shortest augmenting path. We apply induction on the height of subtree T (v) to proof the following induction hypothesis for v ∈ W ∪ B \ {b t0 }: (1) If a vertex v = b is black and is alive in 2n + 1 and any alternating path in T (b) from b finish on a matched black vertex.
(3) If a vertex v = w is white and alive in T (w) then there is a matching M (T (w)) in T (w) for which all black vertices from T (w) are matched. Moreover, det-path t (parent T (w), w) exists and is the shortest alternating path from w to unmatched white leaf in T (w). (4) If a vertex v = w is white and dead in T (w) then there is a matching M (T (w)) in T (w) for which all black vertices from T (w) are matched. Moreover, det-dist t (parent T (w), w) 2n + 1, the vertex w is matched in M (T (w)) and any alternating path in T (w) from w with the first edge from M (T (w)) finish on a matched black vertex.
If v is a leaf in T then induction hypothesis is trivially satisfied in all cases. For the induction step, suppose the subtree T (v) rooted in v has height equals h > 0.
We start from the case where v = b is black and alive in T (b)∪{parent T (b)}. Let w 1 , . . . , w k be the children of b. By the induction hypothesis for each i = 1, . . . , k there is a matching M i of T (w i ) which satisfied condition (3) or (4) depending on aliveness of w i . We define matching for T (b) as M = M 1 ∪ . . . ∪ M k . By the induction hypothesis (the cases (3) and (4)) we have that all black vertices beyond b are matched in M . Now we prove that there is a child w i which is alive We will use repeatedly Observation 6 for X 1 , . . . , X k .
In consequence we obtain that . By the induction hypothesis for w j (the case (4)) the vertex w j cannot be dead in T (w j ), so it is alive in T (w j ). By the induction hypothesis for w j (the case (3)) det-path t (b, w j ) exists and is the shortest alternating path from w j to unmatched white leaf in T (w j ). Because by the induction hypothesis for each alive w l (in T (w l )) we have that det-path t (parent T (w l ), w l ) is the shortest alternating path from w l to unmatched white leaf in T (w l ) and for each dead w l (in T (w l )) there is no alternating path from w l to unmatched white leaf in 2n + 1 and any alternating path in T (b) from b finish on a matched black vertex. The vertex b is dead in T (b) ∪ {parent t (b)}. It means that there is a set X ⊆ B (b) for which b ∈ X and |X| = |N(X)|. Let define X i = X ∩ T (w i ). By a priori assumption we know that |X i | |N(X i )| for each i = 1, . . . , k. Note that |X i | = |{w i } ∪ N(X i )| for each i = 1, . . . , k, because otherwise Because for each i = 1, . . . , k we have that The last one is possible only if w i has a child b i ∈ X i . The set X i is a witness of deadness of b i . In consequence we obtain that also each w i is dead. By the induction hypothesis for w i (case (4)) we have that det-dist t (b, w i ) 2n + 1 and there is no alternating path according to M i from w i to unmatched white leaf in T (w i ). This immediately implies that det-dist t (parent t (b), b) 2n + 1 and there is no alternating path according to In the next case v = w is white and alive in T (w). Let b 1 , . . . , b k be the children of w. By the induction hypothesis for each i = 1, . . . , k there is a matching Because only w can be a common vertex of N(X 1 ), . . . , N(X k ) we have that Because choices of b j and X ⊆ B (w) were arbitrary we proved that each b j is alive in B (w) which contradict with conditions of the case under consideration. Now we prove that there is only one child b j of w which is dead in T (b j ) ∪ {w}. Suppose for the contradiction that b 1 and b 2 are dead in T (b 1 ) ∪ {w} and T (b 2 ) ∪ {w} respectively. It means that there is X 1 ⊆ B (b 1 ) such that b 1 ∈ X 1 and |X 1 | = |N(X 1 )| and there is X 2 ⊆ B (b 2 ) such that b 2 ∈ X 2 and |X 2 | = |N(X 2 )|. Because b 1 ∈ X 1 and b 2 ∈ X 2 we have that w ∈ N(X 1 ) ∩ N(X 2 ) an in consequence This contradict with a priori assumption that Hall's Condition is satisfied for all subset of black vertices. Without lost of generality we can assume that b 1 is dead in T (b 1 ) ∪ {w} and for each j = 2, . . . , k the vertex b j is alive in T where ⊕ is the symmetric difference. By above operation we obtain matching M i witch matches all black vertices in T (b i ). Now we can construct matching It is easy to see that all black vertices from T (w) are matched in M and det-path t (parent T (w), w) exists and is the shortest alternating path from w to unmatched white leaf in T (w).
We already proved induction hypothesis in all cases. Because b t0 is alive in T + , by the induction hypothesis applied for b t0 (see cases (1)), we have the statements (i) and (ii) of Lemma 5. Let's w 1 = det-dir t (ŵ, b t0 ) and w 2 , . . . , w k be the rest of children of b t0 . Then in tree T we have that sec-dist t (b t0 ) = min i∈{2,...,k} det-dist t (b t0 , w i ) + 1 if k 2, 2n + 1 otherwise.
Because sec-dist t (b t0 ) < 2n + 1 we have that k 2 and there is j ∈ {2, . . . , k} such that w j = det-dir t (ŵ, b t0 ), det-dist t (b t0 , w j ) < 2n + 1, i.e. det-path t (b t0 , w j ) exists. Moreover by (3) and (4) we have that for i ∈ {2, . . . , k} there is the shortest alternating path from w i to unmatched white leaf in T (w i ) if and only if det-dist t (b t0 , w i ) < 2n + 1. The length of the shortest alternating path from w i to unmatched white leaf in T (w i ) equals det-dist t (b t0 , w i ) for all w i for which det-dist t (b t0 , w i ) < 2n + 1. Putting it all together gives that det-path t (b t0 , w j ) is the shortest alternating path from b t0 to unmatched white vertex in T \ T (w 1 ). The last sentence is reformulation of point (iii). We finish the proof of (iii) and whole Lemma 5.
Proof. By the statement (ii) of Lemma 5 we have that there is a matching for which path t (b t0 ) is one of the shortest augmenting path. In particular it shows that path t (b t0 ) is well defined for b t0 and dist t (b t0 ) = ||path t (b t0 )|| < |W ∪ B t | < 2n + 1.
Corollary 8. For each vertex b t0 ∈ B and each turn t ∈ {t 0 , . . . , n} we have that sec-dist t (b t0 ) < 2n + 1 iff b t0 is alive in turn t.
Proof. We start from the proof that for each alive black vertex b t0 ∈ B in turn t ∈ {t 0 , . . . , n} we have that sec-dist t (b t0 ) < 2n+1. Let's indicate vertex b t0 as a root in of the b t0 's connected component . By Corollary 7 we have that path t (b t0 ) exists. In particular dir t (b t0 ) exists. Now let's define T = W ∪ B , E as T without subtree T (dir t (b t0 )) rooted in dir t (b t0 ). We will check whether T satisfies Hall's Condition, i.e. for any and b t0 is alive in T . Again by Corollary 7 we obtain that dist t (b t0 ) < 2n + 1 where dist t (v) is defined in like manner as dist t (v) (see Definition 4) but with the difference that we substitute T to T . Because T is T without T (dir t (b t0 )) the second distance sec-dist t (b t0 ) was defined as dist t (b t0 ) (see Definition 4). In consequence we obtain that sec-dist t (b t0 ) = dist t (b t0 ) < 2n + 1. Now we will show that for any turns t 0 and t, such that 1 t 0 t n, the black vertex b t0 , for which sec-dist t (b t0 ) < 2n + 1, is alive in turn t, i.e. for each subset X ⊆ B t , which contains b t0 , we have that |X| < |N(X)|. By Lemma 5 let's M be the matching of W ∪ B t \ {b t0 } which satisfies conditions (i)-(iii). Let's π and ρ be the shortest augmenting path and the second shortest path in M . Moreover let's w and w be the last vertices of the paths π and ρ in connected component of b t0 in X ∪ N(X). Because π and ρ are augmenting paths, the both vertices w and w are not matched with any vertex from X. Because all vertices in X \ {b t0 } are matched with vertices from N(X) \ {w, w } we obtain that |X \ {b t0 }| |N(X) \ {w, w }| and |X| = |X \ {b t0 }| + 1 < |N(X) \ {w, w }| + 2 = |N(X)|.
Corollary 9. For each vertex w ∈ W and each t ∈ {0, . . . , n} we have that w is alive in turn t iff dist t (w) < 2n + 1.
Proof. If w has no neighbours the statement is obvious. So we can assume that w has k > 0 neighbours b 1 , . . . , b k . Aliveness of the vertex w implies that all neighbours b 1 , . . . , b k are also alive. So, for all i ∈ {1, . . . , k} we have det-dist t (w, b i ) sec-dist t (b i ) < 2n + 1. It is not impossible that det-dist t (w, b i ) = 2n because otherwise det-path t (w, b i ) would have 2n vertices and contains in W \ {w} ∪ B t which has size smaller than 2n. In consequence dist t (w) = max i∈{1,...,k} det-dist t (w, b i ) + 1 < 2n + 1.
Now we will prove that if dist t (w) < 2n + 1 then w is alive in turn t. Because max i∈{1,...,k} Inequality (2) also implies that for any j ∈ {1, . . . , k} there is w j such that det-dist t (b j , w j ) < 2n. In consequence we obtain that for any j ∈ {1, . . . , k} we have that sec-dir t (b j ) < 2n + 1 and b j is alive. By definition we obtain that w is also alive.
Definition 1 and corollaries 8 and 9 implies the next two helpful corollaries.
Corollary 10. The black vertex is alive iff it has at most two alive white neighbours.
Corollary 11. The white vertex is alive iff all his neighbours are alive.
Corollaries 8 and 9 which implies, that no matter how we approach an alive vertex (from which direction), the mini-max distance is always finite (i.e., less than 2n + 1), and the mini-max path is well defined and ends at a white leaf. We state this important property of alive vertices as a corollary.

Corollary 12. For any turn t and any vertex
The next corollary is implied by Lemma 5 less directly, so we include the proof. It states that in our setting there is always a finite length (less then 2n + 1) minimax path from b t to a white leaf of the tree. This path certifies that dist t (b t ) is finite.
Corollary 13. Let t ∈ {0, . . . , n} and b ∈ B t . Under our assumption that Hall's Condition holds for the vertices of F t at all times (every new vertex b t can be matched) it holds that We conclude this section by observing the monotonicity of the mini-max distance functions we defined.
Proof. We start with proof Observation 14.
(1). For any t = t 0 , . . . , n let's indicate vertex b t0 as a root in of the b t0 's connected component If we define T as T without the subtree rooted in vertex w we obtain the rooted tree in b t0 which corresponds to order of calculates of values det det-dist t (v, u) = min x is a child of u det-dist t (u, x) + 1 if u has any children, 2n + 1 otherwise; if u is black; see Definition 2; and det-dist t (v, u) = max x is a child of u det-dist t (u, x) + 1 if u has any children, 0 otherwise; if u is white; see Definition 3. Let's fix some t = t 0 +1, . . . , n and we apply structural induction on F t −1 (going from leafs to root) to show that det-dist t −1 (v, u) det-dist t (v, u).
Let's assume that u is black. In this case note that the neighbourhood of a black vertices is unchanged after it's presentation. If u is a leaf then det-dist t −1 (v, u) = 2n + 1 = det-dist t (v, u). If u has children then by induction hypothesis for children we have Now, let's consider the case when u is white. The neighbourhood of u can be change after it's presentation but if some black vertex x come to the neighborhood of u then x will be a neighbor of u forever. If u is a leaf in turn t − 1 then by fact If u has children in turn t − 1 then by induction hypothesis For proof of Observation 14.
(2) we indicate vertex w as a root in of the w's connected component If we define T * as T * without the subtree rooted in vertex b t0 we obtain the rooted tree in w which responds to order of calculates of values det-dist t (v, u) where v is a parent of u. The rest of the proof is exactly the same as the proof of Observation 14.(1).
(3) follows from the fact that the collection of the values det-dist t (w, ·) didn't decrease (see Observation 14.(1)) and dist t (w) is the biggest value from this collection if there is at least one such element or equals 0 and sec-dist t (w) is the second biggest value if there is at least two such elements or equals 0 (see Definition 5).
Observation 14.(4) follows from the fact that the collection of the values det-dist t (b t0 , ·) didn't decrease (see Observation 14.(2)) and dist t (b t0 ) is the smallest value from this collection if there is at least one such element or equals 2n + 1 and sec-dist t (b t0 ) is the second smallest value if there is at least two such elements or equals 2n + 1 (see Definition 4) and the fact that the neighbourhood of black vertex stays unchanged.

The proof
In the remainder of the paper we present the proof of Theorem 4. The proof of such simple statement is unfortunately rather complex. Before we move on to it, we give some intuitions on where the actual problem hides. It is enlightening to discover, that with the additional assumption that the black vertices are of degree two or more, the statement of Theorem 4 has a very simple proof.
Lemma 15. If each black vertex b t has degree at least 2, then n t=1 ||path t (b t )|| n log 2 n + 1.
Proof. We start with proving that all vertices in each round are alive. Before the first black vertex is added (t = 0) there are only white vertices which are obviously alive. For t > 0, because the presented graph is a forest, all the neighbours of the newly added black vertex lie in different connected components. Because the new vertex b t has at least 2 neighbours, b t connects at least 2 components. It is easy to check that Hall's condition is still satisfied with surplus at least 1 i.e. for each subset B ⊆ B t we have |B | + 1 |N(B )|. Now since b t has two alive neigbours w 1 , w 2 ∈ N(b t ), based on Corollary 12, det-path t (b t , w 1 ) and det-path t (b t , w 2 ) are two separate finite paths, contained in different components connected in turn t by b t . Path path t (b t ) is at most as long as the shorter one of the two determined paths, so it is at most as long as the size of the smaller component. We pay for path t (b t ) by charging 1 token to each vertex in the smallest component being connected by b t .
Let us count how many times an individual vertex v was charged. A vertex v is charged when v's component size increases twice, because v is charged only in cases when v is in the smallest component of those which are connected. This gives that v cannot be charged more than log 2 n + 1 times. As a consequence, all vertices are charged no more than n log 2 n + 1 times.
The essence of this proof is that every time a black vertex is added, two or more trees are connected into one tree. Also, each time a black vertex is added, there are at least two alternative paths starting from the added vertex, each in a separate tree. This allows charging the cost of the augmenting paths to vertices of the smallest tree and guarantees, that a vertex is not charged too much. One might think of it that we pay for a path by merging the trees. If we allow black vertices of degree 1, the situation becomes more complicated, because: (1) there is no alternative, i.e., the path may only follow the only edge adjacent to the newly added black vertex, and (2) we do not merge any trees. Nevertheless the proof of Theorem 1 is a generalization of the proof of Lemma 15.
Our way around issue (1) is to introduce the concept of a dispatching vertex. Even though b t does not necessarily fork into two alternative mini-max paths, there is another vertex which does. We refer to it as dispatching vertex in turn t.
Definition 7. A black vertex b ∈ B t is called dispatching at time t ∈ {1, . . . , n} if |N (b) ∩ A t | 2 and b is the first from b t such vertex on path t (b t ). In such case we write b =b t .
The role of a dispatching vertex is described by the following lemmas.
Lemma 16. There is at least one neighbour of b t which alive in turn t − 1.
Proof. By Corollary 13 we have that So, the new vertex b t has a white neighbour w such that dist t−1 (w ) < 2n + 1. By Corollary 9 we have that w is alive in turn t − 1.
Lemma 17. If b t has at least two neighbours which are alive in turn t − 1 then aliveness of all vertices are not changed.
Proof. Let's w 1 , w 2 , . . . , w k be neighbours of b t and let's fixed any vertex v of connected component of b t in turn t as a root. Then we proceed the structural induction on thus constructed rooted tree T to prove that for i 2 det-dist t (parent t (u), u) < 2n + 1 if det-dist t−1 (parent t (u), u) < 2n + 1 oth.
Lemma 18. If b t has exactly one neighbour which is alive in turn t − 1 and there is a path (v = v 1 , . . . , v k = b t ) such that for each i = 1, . . . , k − 1 (i) the vertex v i is alive in turn t − 1, (ii) if v i is black then v i has at most two neighbours which are alive in turn t − 1, then v becomes dead in turn t. The state of aliveness of the rest of the vertices are not changed.
Proof. Monotonicity of all types of mini-max distances (see Observation 14) and rules of aliveness (see Corollaries 10 and 11) imply that all dead vertices in turn t − 1 and all vertices which are connected with b t by path satisfied (i) and (ii), are dead in turn t. Therefore, we are left to prove that all other vertices are still alive. Let's fixed any vertex v for which is alive in turn t − 1 and is not connected with b t by path which satisfies (i) and (ii). Let's indicate vertex v as a root in of the b t 's Next let b be the last black vertex on the path from v to b t which has at least 3 alive neighbours in turn t − 1 and let w be the successor of b on this path. Notice that b is a parent of w in tree T rooted in v.

Now we prove that for any
The proof will be induction on the length the longest path to the leaf. Only one non-routine moment of this proof is that det-dist t (parent t (b ), b ) < 2n + 1. The vertex b has at least 3 alive neighbours in turn t − 1 and in consequence b has a child w = w which is also alive in turn t − 1. By Corollary 9 we have that det-dist t−1 (b , w ) dist t−1 (w ) < 2n + 1. By the induction hypothesis for w we have that also det-dist t (b , w ) < 2n + 1. In consequence One of the conclusions of the Lemma 18 is that in Definition 7 if b t is of degree 1 Addressing (2) is not so straightforward. By the end of the paper, though, it should be clear that the charging scheme we propose is based on merging trees.
We now introduce the crucial notion in our proof: the notion of level. The vertices are assigned levels. The meaning of the level of a vertex is the following. Consider path t (b t ), which is the worst case shortest augmenting path for a black vertex b t added in turn t. For a vertex v, if path t (b t ) crosses v, level in F t returns the value representing the length of the suffix of path t (b t ) starting in v. In particular, for a black vertex, its level is its mini-max distance to a leaf. Formally, the level function is defined in the following way. otherwise.
It may at first seem confusing that the level of a white vertex is the second maximum distance to a leaf. It is defined this way because, surprisingly, in every turn t the mini-max path path t (b t ) enters its white vertices through the edge determining the maximum distance from the white vertex to a leaf. The second intuition gives the following Lemma 19 and Corollary 20. Roughly speaking, they say that level on some vertex v approximates the distance from v to the white unmatched vertex on mini-max path.
Lemma 19. Let t ∈ {0, 1, . . . , n}, b ∈ B t , w ∈ N(b) \ D t and let us number the consecutive vertices of (b, det-path t (b, w)) with consecutive natural numbers from k down to 0 It then holds that for i = 0, 1, . . . , k Now we will proof the first Inequality, i.e.
for i = 0, 1, . . . , k. First we note, that for i = k the Inequality (3) implies useful later inequality i − (k − level t (b)) i (5) for i = 0, 1, . . . , k. We prove Inequality (4) by induction on i = k − i. For i = 0 it follows immediately as v (k) = b so level t (v (k) ) = level t (b). Let us assume that i > 0. In that case 0 i < k. We distinguish two cases depending on the parity of i.
First we assume that i is even, i.e., v (i) = w (i) ∈ W . By the inductive assumption Because det-dist t (b (i+1) , w (i) ) = i and this is the maximum direction among all presented neighbours of w (i) except b (i+1) , we have that Based on Inequality (5), this implies that Now we assume that i is odd, i.e., v (i) = b (i) ∈ B t . By the inductive assumption regarding w (k−(i −1)) = w (i+1) ∈ W we have that Because det-dist t (w (i+1) , b (i) ) = i and this is the minimum direction among all neighbours of b (i) except w (i+1) , we have that min(i + 2 − (k − level t (b)), i) dist t (b (i) ) = level t (b (i) ). Based on Inequality (5), this implies (1) , w (0) )|| = k we have the following Corollary.
Corollary 20. Let t ∈ {0, 1, . . . , n}, b ∈ B t and let us number the consecutive vertices of path t (b) with consecutive natural numbers from k down to 0 It then holds that for i = 0, 1, . . . , k Given the level function, we want to consider the vertices of T whose level in turn t is above a certain value l. To be more precise, we need to consider the forest induced by such vertices. This forest changes dynamically as the turns are passing by. We describe it more formally below. By F [X] = X, E(X) we denote a subgraph of T induced by X ⊆ W ∪ B, where E(X) = {e ∈ E : e ⊆ X}.
Definition 9. For t, l ∈ N we define The family of all connected components is denoted as We illustrate the introduced definitions in Figure 2. We present there an example run of an online scenario together with changing levels of vertices. We mark the dispatching vertices in each turn. For a fixed l we observe how F l changes from turn t − 1 to t. Since the level function satisfies level t−1 (v) level t (v) (see Observation 14), the following hold: Next we move on to the proof of Theorem 4. There are two cases that we need to consider: (1) the level of a dispatching vertex in turn t grows by at most a factor of β (2) the level of a dispatching vertex in turn t grows by more than a factor of β where β is some constant value greater than 1 which we reveal later on. This case distinction gives two classes of paths. The total length of paths in each class is bounded separately. The paths defined by case (1) are bounded by Lemma 23 while the paths defined by case (2) are bounded by Lemma 24.
Lemma 23. The total length of path t (b t ) for cases when level t (b t ) < β level t−1 (b t ) is bounded by βn log 2 n + 1 + (1 + β)n.
Proof. Let t be such that level t (b t ) < β level t−1 (b t ). We analyze the connected components of the subtree of T induced on the set P t ∪ A t of alive vertices: In other words, in each turn we consider tree T with all the dead vertices of D t removed. At the beginning P 0 ∪ A 0 = W ∪ B n , so F [P 0 ∪ A 0 ] = T . As the vertices die, F [P t ∪ A t ] falls apart. By Corollary 13, path path t (b t ) is of finite length and we may assume it is of the following form.
We let path p t = (b t , w , . . . , w ) and path s t = (b t , w , . . . ,w ) be the prefix and the suffix of path t (b t ). First we notice that b t =b t , otherwise level t−1 (b t ) = 0 < level t (b t )/β. Due to Lemma 18, the vertices from the non-empty prefix path p t of path t (b t ) die in turn t. As a consequence we obtain that the connected component Figure 3). In order to pay for the length of path t (b t ), we charge the vertices of C in the following way: • if a vertex dies then it is charged 1 + β tokens, whereas • if a vertex is still alive and its connected component in F [P t ∪ A t ] decreased at least twice compared to its component in F [P t−1 ∪ A t−1 ], then it is charged β tokens.
The length of the prefix path p t is paid by 1 token charged to each vertex of this prefix. The remaining β tokens charged to each vertex of path p t are left to pay for the suffix path s t . Let C 1 be the largest component among C 1 . . . C k . If path path s t ⊆ C i for i = 1, then path s t is contained in a component that deacreased at least twice. The length of path s t is paid by its vertices who are charged β (recall that 1 < β). In the remaining case path s t ⊆ C 1 , as shown in Figure 3. By the assumption of Lemma 23 and definition of level() (see Definition 8) and definition of dist() (see Definition 4 and 5) we have that On the one hand, w is dead in turn t and hence det-dist t (b t , w ) 2n + 1 based on Corollary 12. Therefore we have ||path s t || = minw ∈N(bt) det-dist t (b t ,w). On the other hand, vertex w is one of neighbours ofb t , so Together with equation (6) we obtain Since w is alive in turn t − 1, based on Corollary 12 the length of det-path t (b t , w ) is det-dist t−1 (b t , w ) (see Figure 3). Also due to Lemma 18 det-path t (b t , w ) contains only vertices alive in turn t − 1, so det-path t (b t , w ) ⊆ C. Moreover, due to Lemma 18, the only vertex path det-path t (b t , w ) has in common with C 1 isb t . Each vertex of det-path t (b t , w ) butb t pays now β tokens to cover the length of ||path s t ||.
w C 1 Figure 3. Easy case Let us now count the total charge during all turns. When a vertex dies it encounters one time charge of 1 + β. This sums up to n(1 + β). When a vertex is in a component of size at most half of the initial component, it is charged β tokens, but this happens at most log 2 n + 1 times. This sums up to βn log 2 n + 1.
Proof. We fix a turn t. By Corollary 13, path t (b t ) is of finite length and we may assume it is of the following form.
We let path p t = (b t , w , . . . , w ,b t ) and path s t = (b t , w , . . . ,w ) be the prefix and the suffix of path t (b t ). Due to Lemma 18 the vertices of path p t excludingb t die in turn t. Since a vertex dies at most once, it holds that t∈T ||path p t || n. The remainder of the proof is devoted to show that t∈T ||path s t || ∈ O(n log n). The idea is the following. We let l = level t−1 (b t ) and l = level t (b t ). For the purpose of the proof we need a function that describes some intermediate level between level t−1 () and level t (). We thus extend the level function to rational indices: We illustrate these definitions by example in Figure 4. Observe that level t (v) function is still monotonic in t after the extension. In Figure 4 we observe that on levels l from l to l − 1 the components of F l t−1/2 merge in F l t . It is this merging that Let us fix a level l. In every turn t = 0, 1 2 , 1, 1 1 2 , . . . , n we assign δ tokens to every connected component C in F l t such that |C| ρl, where δ and ρ are constants that we compute later. Smaller components are not assigned any tokens. Note that if l is large, only large components are assigned tokens. We plan to use these tokens to pay for the minimax paths in each turn. First, however, we describe how we can afford to maintain such an assignment on level l.
First we consider moving from turn t − 1 to turn t − 1/2. The forest F l t−1/2 is produced in this way that the set of vertices ∆ = V (F l t−1/2 ) \ V (F l t−1 ) = {v ∈ B t ∪ W : level t−1 (v) < l level t−1/2 (v)} is added to F l t−1 (see Observations 21 and 22). We want to add some structure to this process. We divide transformation from F l t−1 to F l t−1/2 into two sub-phases. In the first sub-phase, the vertices of ∆ form new singleton components: C = v∈∆ { {v}, ∅ }, where {v}, ∅ is a graph with only one vertex v and without edges. Together with the set of connected components of F l t−1 (referred to as C(F l t−1 )) they form a family I = C(F l t−1 ) ∪ C . In the second sub-phase, components in I merge whenever there is an edge of T connecting them, finally becoming the connected components of F l t−1/2 . Every component C ∈ C(F l t−1/2 ) can be assigned a set of components I C = {C 1 . . . C k } ⊆ I that merged into C. There are two possible options: (a) there is the component C i ∈ C with size |C i | ρl, so C i is already assigned δ tokens (b) every C i ∈ C satisfies |C i | < ρl, so none of them is assigned any tokens. In case (a), δ assigned to C i , which ceases to exist, is now transferred to C. In case (b), if |C| ρl, every vertex v ∈ C chips in with a payment of δ ρl , so the vertices of C pay in total at least δ. We count summary amount paid on the end of the proof.
We now consider the transition from turn t − 1/2 to turn t. There is only one vertex, mainlyb t , which changes its level. Its level increases from l to l. Level l is only affected by this transition if l < l l. So, the forest F l t is formed from F l t−1/2 by addingb t . It causes that the only difference between C(F l t−1/2 ) and C(F l t ) is that the some family of components of C(F l t−1/2 ) connect byb t and form the new component equals comp(b t , F l t ). The set of components that merge into comp(b t , F l t ) is precisely C = C(comp(b t , F l t ) \b t )∪{ b t , ∅ }. The way of assigning δ to comp(b t , F l t ) if |comp(b t , F l t )| ρl is the same as in the transition from t − 1 to t − 1/2. The difference is that now we want to utilize some of the assigned tokens to pay for the mini-max path in turn t. Thus, we distinguish three cases now: (i) exactly one of the components C ∈ C satisfies |C | ρl, so C is already assigned δ tokens,