GRAPH HOMOMORPHISMS BETWEEN TREES

. In this paper we study several problems concerning the number of homomorphisms of trees. We give an algorithm for the number of homomorphisms from a tree to any graph by the Transfer-matrix method. By using this algorithm and some transformations on trees, we study various extremal problems about the number of homomorphisms of trees. These applications include a far reaching generalization of Bollobás and Tyomkyn’s result concerning the number of walks in trees. Some other highlights of the paper are the following.

We use standard notations and terminology of graph theory, see for instance [2,4]. The graphs considered here are finite and undirected without multiple edges and loops. Given a graph G, we write V (G) for the vertex set and E(G) for the edge set. A homomorphism from a graph H to a graph G is a mapping f : V (H) → V (G) such that the images of adjacent vertices are adjacent. Denote by Hom(H, G) the set of homomorphisms from H to G and by hom(H, G) the number of homomorphisms from H to G. Throughout this article, we write P n and S n for the path and the star on n vertices, respectively. The length of a path is the number of its edges. The union of graphs G and H is the graph G ∪ H with vertex set V (G) ∪ V (H) and edge set E(G) ∪ E(H). A tree T together with a root vertex v will be denoted by T (v).
The problem of computing hom(H, G) is difficult in general. However, there has been recent interest in counting homomorphisms between special graphs. In particular, formulas for computing the number of homomorphisms between two different paths were given in [1,14]. But even for these special trees, the formulas are bulky and inelegant. In Section 2, by using the Transfer-matrix method, we shall give an algorithm for computing the number of homomorphisms from trees to any graph. This algorithm will be called Tree-walk algorithm.
Recently, the first author proved a conjecture of Nikiforov concerning the number of closed walks on trees. He proved in [6] that, for a fixed integer m, the number of closed walks of length m on trees of order n attains its maximum at the star S n and its minimum at the path P n . In other words, (1.1) hom(C m , P n ) ≤ hom(C m , T n ) ≤ hom(C m , S n ), where T n is a tree on n vertices and C m is the cycle on m vertices. Bollobás and Tyomkyn [3] gave a variant of the first author's result by replacing the number of closed walks by the number of all walks, that is (1.2) hom(P m , P n ) ≤ hom(P m , T n ) ≤ hom(P m , S n ), where T n is a tree on n vertices. In both [3] and [6], the authors use a certain transformation of trees. In [6], it is called the generalized tree shift, whereas in [3], it is renamed to KC-transformation. To define this transformation, let x and y be two vertices of a tree T such that every interior vertex of the unique x-y path P in T has degree two, and write z for the neighbor of y on this path. Denote by N(v) the set of neighbors of a vertex v. The KC-transformation, KC(T, x, y), of the tree T with respect to the path P is obtained from T by deleting all edges between y and N(y) \ z and adding the edges between x and N(y) \ z instead (See Fig. 1). Note that KC(T, x, y) and KC(T, y, x) are isomorphic.
The following property of KC-transformation was proved in [6]. In [6] the first author proved that the KC-transformation increases the number of closed walks of fixed length in trees. By Proposition 1.1, this leads to the proof of inequality (1.1).
In the very same spirit, Bollobás and Tyomkyn [3] showed that the KCtransformation increases the number of walks of fixed length in trees. In the language of graph homomorphism, their result can be restated as follows.   of degree greater than two. Note that paths are starlike. We answer this question in the affirmative for starlike trees. holds when k is even and H is any tree, or k is odd and H is a starlike tree.
Moreover, we find a counterexample for inequality (1.4) when k is odd and H is not a starlike tree (see the end of Section 4). Another extremal problem concerning the number of homomorphisms between trees that worth considering is to find the extremal trees for hom(· , P n ) over all trees on m vertices. For this problem, we prove the following theorem, which can be considered as a dual of inequality (1.2). Theorem 1.4. Let T m be a tree on m vertices and let T ′ m be obtained from T m by a KC-transformation.
(i) If n is even, or n is odd and diam(T m ) ≤ n − 1, then (1.5) hom(T m , P n ) ≤ hom(T ′ m , P n ). (ii) For any m, n, hom(P m , P n ) ≤ hom(T m , P n ) ≤ hom(S m , P n ).
Note that inequality (1.5) is not true in general when n is odd and diam(T m ) is greater than n − 1; see Fig. 15 for a counterexample.
For the sake of keeping this paper self-contained, we will also give a new proof for the following theorem of Sidorenko [17] concerning the extremal property of the stars among trees. Note that Fiol and Garriga [8] proved the special case of this theorem when T m = P m , clearly, they were not aware of the work of Sidorenko. Theorem 1.5 (Sidorenko). Let G be an arbitrary graph and let T m be a tree on m vertices. Then hom(T m , G) ≤ hom(S m , G).
We have already seen a few examples to the phenomenon that in many extremal problems concerning trees it turns out that the maximal (minimal) value of the examined parameter is attained at the star and the minimal (maximal) value is attained at the path among trees on n vertices (cf. [7,15]). In what follows we will show that this phenomenon occurs quite frequently if one studies homomorphisms of trees.
Let Y a,b,c be the starlike tree on a + b + c + 1 vertices which has exactly 3 leaves and the vertex of degree 3 has distance a, b, c from the leaves, respectively.
Theorem 1.6. Let T n be a tree on n vertices. Assume that for a tree T m we have hom(T m , T n ) < hom(T m , P n ). Then T n = Y 1,1,n−3 and n is even.
In fact, we conjecture that we only have to exclude the case n = 4 and T 4 = S 4 . Conjecture 1.7. Let T n be a tree on n vertices, where n ≥ 5. Then for any tree T m we have hom(T m , P n ) ≤ hom(T m , T n ).
An endomorphism of a graph is a homomorphism from the graph to itself. For a graph G, denote by End(G) the set of endomorphisms of G. We remark that End(G) forms a monoid with respect to the composition of mappings. One of the main results of this paper is the following extremal property about the number of endomorphisms of trees. Theorem 1.8. For all trees T n on n vertices we have |End(P n )| ≤ |End(T n )| ≤ |End(S n )|.
This paper is organized as follows. In Section 2, we state the tree-walk algorithm. In Section 3, we prove some lower bounds involving Markov chains and an upper bound (Theorem 1.5) for the number of homomorphisms from trees to an arbitrary graph. Section 4 is devoted to the proof of Theorem 1.3. The proofs of Theorem 1.6 and Theorem 1.8 are given in Section 5, where some lower bounds concerning the homomorphisms of arbitrary trees are also proved. In Section 6 we prove Theorem 1.4, this part can be read separately, it only builds on the algorithm of Section 2.
In order to make our paper transparent, we offer the following two tables, Figure 3 and 4, which summarize our results. In both tables, the first row follows from Theorem 1.2 or its generalization Corollary 4.3. The last row is obvious since hom(S m , G) is the sum of degree powers of G and it also follows from Corollary 4.3. The first, second and third columns follow from Theorem 1.4, Theorem 1.5 and Corollary 4.5 respectively. The "X" means that there is no inequality between the two expressions in general and the "?" means that we don't know whether the statement is true or not. Figure 3. The number of homomorphisms between trees of the same size.

The Tree-walk algorithm
In this section we shall state an algorithm for the number of homomorphisms from a tree to any graph by the Transfer-matrix method. As a generalized concept of walks in graphs, we call a homomorphism from a tree to a graph a tree-walk on this graph.
Let a = (a 1 , a 2 , . . . , a n ) and b = (b 1 , b 2 , . . . , b n ) be two vectors. We usually denote by a = a 1 +a 2 +· · ·+a n the norm of a and by a * b = (a 1 b 1 , . . . , a n b n ) the Hadamard product of a and b. Denote by 1 n the n-dimensional row vector with all entries are equal to 1. Let G be a graph with n vertices. The adjacency matrix of G is the n×n matrix A G := (a uv ) u,v∈V (G) , where a uv = 1 when uv ∈ E(G), otherwise 0. We begin with a fundamental lemma about the number of walks in a graph. Lemma 2.1. Let G be a labeled graph and A = A G the adjacency matrix of G. Then the (i, j)-entry of the matrix A n counts the number of walks in G from vertex i to vertex j with length n.
Proof. By easy induction on n. See for example [18,Theorem 4.7.1].
Definition 2.2 (hom-vector). Let T be a tree and G be a graph with vertices labeled by 1, 2, . . . , n. Let v ∈ V (T ) be any vertex of T . The n-dimensional vector is called the hom-vector at v from T to G. Clearly, hom(T, G) = h(T, v, G) .
The following Tree-walk algorithm can be viewed as a generalization of Lemma 2.1 for computing the number of tree-walks in graphs.
The Tree-walk algorithm. Let A = A G be the adjacency matrix of the labeled graph G. Let v be a leaf of the tree T . We now give the algorithm to compute h(T, v, G).
Starting from a leaf of the tree T other than v (a tree usually has at least two leaves), v 0 say. Walking along the tree until you come to the first vertex with degree greater than two, v 1 say. Let T 1 denote the path from v 0 to v 1 with length d 1 . By Lemma 2.1, we have h(T 1 , v 1 , G) = 1 n A d 1 . If the degree of vertex v 1 is k, we denote its k − 1 branches other than T 1 by T 2 , T 3 , . . . , T k . There is one branch that contains the vertex v, T k say. We can compute the hom-vectors h(T 2 , v 1 , G), h(T 3 , v 1 , G), . . . , h(T k−1 , v 1 , G) by recursively using this algorithm. Clearly we have Now continue to walk on the tree T from v 1 along the branch T k until you go to the first vertex with degree bigger than two, v 2 say. Let d 2 be the length of the path, denoted by P , from v 1 to v 2 . By induction on d 2 , one easily shows that which is the most important operation of this algorithm. Do the same thing on v 2 as what we have done on v 1 , and the process stop until we walk to the last vertex of T , which must be v according to the algorithm. At the same time, the hom-vector at v from T to G is obtained and thus hom(T, G).
Note that we can modify this algorithm to obtain h(T, v, G) at any vertex v ∈ V (T ) by using operation (2.1). The following typical example would explain our algorithm better.    Fig. 5. We also choose G to be T . Denote by T [V ] the induced subtree on the vertex set V ⊆ V (T ). To compute h(T, 7, T ) by Tree-walk algorithm, we start at vertex 6 and first stop at 4 which is a vertex with degree greater than two. By Lemma (1,1,9,9,4,1,1).
Alternative way: recursions. There is an alternative way to think to the Tree-walk algorithm, namely we consider two type of recursion steps.
Recursion 1. If we have a tree T with a non-leaf root vertex v, then we can decompose T to T 1 ∪ T 2 such that V (T 1 ) ∩ V (T 2 ) = {v}, and T 1 and T 2 are strictly smaller than T . In this case Recursion 2. If we have a tree T with a root vertex v which is a leaf with the unique neighbor u then where A is the adjacency matrix of G.
Hence we use Recursion 1 or Recursion 2 according to the root vertex v is a non-leaf or a leaf. In most of the proofs we simply check whether some property of the vector h(T, v, G) remains valid after applying Recursion 1 and Recursion 2.
3. Graph homomorphisms from trees 3.1. Markov chains and homomorphisms. Theorem 3.1. Let G be a graph and let P = (p ij ) be a Markov chain on G: where p ij ≥ 0 and p ij = 0 if (i, j) / ∈ E(G). Let Q = (q i ) be the stationary distribution of P : Let us define the following entropies: where d i is the degree of the vertex i, and let Let T m be a tree with ℓ leaves on m vertices, where m ≥ 3. Then Proof. Let v be a root of T . Let a i be the number of homomorphisms of T m into G such that the root vertex v goes into the vertex i ∈ V (G). Let We will show by induction on m that where ℓ * is the number of leaves different from v, so it is ℓ if v is not a leaf and ℓ − 1 if v is a leaf. Note that If v is not a leaf of T m , then we can decompose T m to T 1 (v) and T 2 (v).
because of the Hadamard-products of the hom-vectors. From this the claim follows immediately by induction.
If v is a leaf of T m with the unique neighbor u, then let For positive numbers r 1 , . . . , r t and positive weights w 1 , . . . , w t with t i=1 w i = 1, the weighted AM-GM inequality says that In the last step we used that Q is a stationary distribution with respect to P . Hence

Now the claim follows by induction.
To finish the proof of the theorem, we only have to choose a nonleaf root and use that As Theorem 3.1 suggests, this is an inequality for entropies and indeed, it can be proved by this way. By P and Q, we defined a distribution on the set of homomorphisms: we choose a root according to Q, then we choose every nonleaf new vertex according to P and finally we choose the leaves uniformly. The entropy of this distribution is exactly H(Q) + ℓH(D|Q) + (m − 1 − ℓ)H(P |Q) since every nonleaf vertex has distribution Q. Note that this entropy is smaller than the entropy of the uniform distribution, that is, log hom(T m , G). For basic facts about entropy, see for example [5].
is the spectral entropy of the graph G.
Proof. We will use Theorem 3.1. Let p ij = y j λy i . Since y is a positive eigenvector, we have p ij > 0. For all i we have λy i = j∈N (i) y j , thus j∈N (i) p ij = 1. For q i = y 2 i we have This means that P = (p ij ) is a Markov chain with stationary distribution Q = (q i ). The conditional entropy Hence the result follows from Theorem 3.1.

Remark 3.4.
A Markov chain is called reversible if q i p ij = q j p ji for all i, j ∈ V (G). As we have seen, the Markov chain constructed in the previous proof is reversible. It is not hard to show that on trees every Markov chains are reversible.
Remark 3.5. Theorem 3.3 is the best possible in the sense that there cannot be a larger number than λ in such a statement since Indeed, Note that we can deduce that if (T m ) ∞ m=1 is a sequence of trees such that T m has m vertices then This result could have been deduced as well from a theorem of B. Rossman and E. Vee [19] claiming that where C m is the cycle on m vertices. In fact, this was proved for directed trees and cycles, but it implies the inequality for undirected tree and cycle. This result can also be deduced from Theorem 3.1 of [11].
Proof. Let us consider the following classical Markov chain: Hence the result follows from Theorem 3.1.
Definition 3.7. The homomorphism density t(H, G) is defined as follows: This is the probability that a random map is a homomorphism.
Sidorenko's conjecture says that for every bipartite graph H with e(H) edges. It is known that Sidorenko's conjecture [16] is true for trees. By now, there are many proofs for this particular case of Sidorenko's conjecture: see [9,13] and it can be deduced as well from Theorem 3.1 of [11]. Below we give a new proof for this fact. Proof. Let |V (G)| = n. The theorem will immediately follows form Theorem 3.6. By convexity of the function x log x we have Hence

3.2.
Sidorenko's theorem on extremality of stars. The objective of this section is to give a new proof for Theorem 1.5 in order to keep this paper self-contained. This was proved originally by Sidorenko [17]. Our proof is very similar to the original one, but it is slightly more elementary. Before we start the proof we will need two definitions and two lemmas.
Definition 3.9. Let M u and N v be two rooted graphs with root vertices u and v, respectively. Then M u • u=v N v denotes the graph obtained from M u ∪ N v by identifying the vertices u and v.
Lemma 3.10. Let R u,v be a graph with specified (not necessarily distinct) vertices u and v. Let J u ′ and K v ′ be two graphs with root vertices u ′ and v ′ . Finally, let the graphs A, B and C be obtained from R u,v , J u ′ , K v ′ as follows: (In other words, in B and C we attach two copies of the same graph at the specified vertex.) Then for any graph G we have Proof. Let i, j ∈ V (G) and let h(R u,v , i, j) denote the number of homomorphisms of R u,v to G where u goes to i and v goes to j. We similarly define h(J u ′ , i) and h(K v ′ , j). Then Similarly, and Hence hom(B, G) + hom(C, G) − 2 hom(A, G) We are done.
In our application R u,v will be a tree and J u ′ and K v ′ be the trees on 2 vertices. The following lemma about the Wiener-index is trivial. Lemma 3.12. Let R u,v be a tree with distinct vertices u and v. Let J u ′ and K v ′ be two copies of the two-node trees with root vertices u ′ and v ′ , respectively. Finally, let the graphs A, B and C be obtained from Proof of Theorem 1.5. Let T G be the set of those trees F on m vertices for which hom(F, G) is maximal. Let T ∈ T G be the tree for which W (T ) is minimal. We show that T = S m . Assume for contradiction that T = S n . Then T has two leaves, a and b such that d(a, b) ≥ 3. Let u and v be the unique neighbors of a and b, respectively. Then As in the lemmas, let Note that B and C are also trees on m vertices. By the Lemma we have , so one of them has strictly smaller Wiener-index than T , this contradicts the choice of T . Hence T must be S m .
Remark 3.13. After all, it is a natural question whether it is true or not that hom(P m , G) ≤ hom(T m , G) for any tree T m on m vertices. Surprisingly, the answer is no! It was already known to A. Leontovich [12]. It turns out that even if restrict G to be tree there is a counterexample. Let E 7 be the tree obtained from P 6 by putting a pendant edge to the third vertex of the path. Then there is a tree T for which hom(P 7 , T ) > hom(E 7 , T ). The following tree T is suitable: let T = T (k 1 , k 2 , k 3 ) be the tree where the root vertex v 0 have k 1 neighbors, all of its neighbors has k 2 + 1 neighbors and the vertices having distance 2 from v 0 have k 3 + 1 neighbors. If we choose

Tree-walks on trees
The main purpose of this section is to prove Theorem 1.3. We shall give an inductive proof of Theorem 1.2 which can be generalized to tree-walks by tree-walk algorithm.
We first need some notations. Let T be a tree and T ′ = KC(T, p 0 , p k ) its KC-transformation with respect to a path P of length k, a path with vertices labeled consecutively with p 0 , p 1 , . . . , p k . We denote by A and B the components of p 0 and p k in the subgraph of T by deleting all the edges of P . Let A ′ , B ′ and P ′ be the components of T ′ corresponding with components A, B and P under the KC-transformation, respectively. The vertices of the path P ′ will be labeled consecutively with Proof. Clearly, we only have to prove that Hence we are done.
We first treat the case of k being even.
Proof of first part of Theorem 1.3. In this proof k is even: , where we use the labels of vertices of T and T ′ to index the parameters of the hom-vectors to T and T ′ respectively. We hope that it will not cause any confusion. We shall prove by induction on the steps of tree-walk algorithm that It is easy to verify that all these inequalities are satisfied after applying any recursion step of the tree-walk algorithm. When v is a leaf of H then it is trivial that these inequalities are preserved. If v is not a leaf then we use Lemma 4.1 to see that the Hadamard-product preserves these inequalities.
Lemma 4.2. Let k be odd and assume that A and B have at least two vertices. Let a m (r), b n (r), p i (r), a ′ m (r), b ′ n (r), p ′ i (r) denote the number of homomorphism of P r into T and T ′ , respectively, such that the endvertex of P r goes to the vertices a m , b n , p i , a ′ m , b ′ n and p ′ i , respectively. Then the following inequalities hold for every r: Proof. We prove the claim by induction on r. For r = 1, 2, the claim is trivial. Note that we only have to prove that by simply exchanging the role of A and B. Also note that if we put i = j in the inequality (4.5) and (4.6) we obtain that Observe that for any vertex v we have: We will treat the cases k = 1 and k ≥ 3 separately.
Case 1: k = 1. In this case, we have to prove the inequalities: Observe that We used the induction hypothesis and that b m (r −1) ≥ b m (r −2). In general, u(r) ≥ u(r − 1) since any homomorphism of P r−1 starting at the vertex u can be extended to a homomorphism of P r starting at u. Clearly, we can get p ′ 0 (r) ≥ p 1 (r) similarly, or we just switch the role of A and B. Finally, Hence we are done in this case.
So we only have to consider the case i = 0. In this case we consider the cases j = 0, j = 1, 2 ≤ j ≤ k − 2, j = k − 1, j = k separately. Unfortunately, all of them behaves a bit differently.
Subcase j = 0: Subcase j = 1: Here we jump back from r to r −2, so we need a few notations. Let d A and d B denote the degree of p ′ 0 in A and B, respectively. Furthermore, let d(v, u) denote the distance of the vertices u and v. Then since the inequality follows from the following inequalities: Subcase j = k − 1: On the other hand, Subcase j = k:

Proof of the second part of Theorem 1.3. From Lemma 4.2 we only keep the inequalities
For a tree H and v ∈ H, let us write , where we use the labels of vertices of T and T ′ to index the parameters of the hom-vectors to T and T ′ , respectively. We say that As we have seen these inequalities hold for a path P r and its endvertex. Since these inequalities are preserved for Hadamard-product by Lemma The following generalization of inequality (1.2) follows immediately from Proposition 1.1 and Theorem 1.3. The reader may wonder that if inequality (1.4) holds when k is odd and H is not a starlike tree. This is not true in general. A counterexample will be constructed in the following, which also shows that is not true for any tree H.
If T = S m then the second inequality is strict.
Proof of Proposition 4.4. Since T and S n are bipartite graphs, a color class of T have to go into a color class of S n . If the color class A goes to the center of S n , then any vertex belonging to the color class of B can go to any leaf of the star, so it provides (n − 1) |B| homomorphisms. The other case provides (n − 1) |A| homomorphisms. This simple proposition also shows us how to construct a tree T n for which hom(T n , T n ) > hom(T n , S n ).
Let T n = S * 2k be the doublestar on 2k vertices with 2k − 2 leaves and two vertices of degree k. Then it is easy to see that . Note that S 2k can be obtained from S * 2k by a KC-transformation.

Homomorphisms of arbitrary trees
In this section we give the proof of Theorem 1.6 and Theorem 1.8. As we will see, Theorem 1.6 with some additional observations implies Theorem 1.8.
To prove Theorem 1.6 we will build on the fact that there are not many homomorphisms into a path. Indeed, by Theorem 1.5 we have So for a particular tree T n , it is enough to prove that for every tree T m we have This would immediately imply that We will prove that inequality 5.1 is indeed true for all tree T n with at least four leaves and for a large class of trees with three leaves. For the remaining trees with three leaves we use Theorem 1.3. Remark 5.1. To prove Theorem 1.6 and Theorem 1.8 we cannot rely entirely on the use of KC-transformation. That is why we had to find another strategy to prove these theorems. Indeed, KC-transformation does not always increase the number of endomorphisms of trees. The first counterexample is the two trees on 8 vertices in Fig. 7. The tree T ′ 8 is the KC-transformation of T 8 , but |End(T ′ 8 )| = 10430 < 17190 = |End(T 8 )|.
5.1. The extremality of star. Note that Theorem 1.5 and Theorem 1.3 together implies the following chain of inequalities: since S n is a starlike tree. In this section, we will also give a direct proof for it. If T n = S n then strict inequality holds.
Proof. For the sake of simplicity we prove the statement for n ≥ 17. The same proof applies to n < 17, we only need to compute a bit more carefully. In the end of the proof we will give the details of this more precise calculation.
Note that |End(S n )| = (n − 1) n−1 + (n − 1). Let T n be a tree on n vertices and let d = d 1 ≥ d 2 ≥ . . . d n be its degree sequence. Note that d 1 + d 2 ≤ n, since the tree has only n − 1 edges and the stars corresponding to the first two largest degrees can share at most one common edge.
First we prove that |End(T n )| ≤ nd n−1 . To see it, let u 1 , . . . u n be the vertices of the tree T n such that u 1 , . . . , u k induces a tree for every k. Then we can chose the image of u 1 by n ways, and if we have already chosen the image of u 1 , . . . , u k−1 , then we can chose the image of u k in at most d ways, since it must be the neighbor of some previous vertex. This means that |End(T n )| ≤ nd n−1 .
If d ≤ 2n/3 then So we can assume that d ≥ 2n 3 . Set d = n − k. We can assume that T n = S n , consequently k ≥ 2. Let v 1 be the vertex having the largest degree and v 2 , . . . , v d+1 its neighbors. Now we can decompose the set of endomorphisms according to the image of v 1 is v 1 or not. If it is v 1 then there can be at most d n−1 such endomorphisms. If the image of v 1 is not v 1 , then we can chose that image in at most (n − 1) ways and the image of It is enough to prove that (n − k) n−2 ≥ k n−k (n − k) k−1 . This is equivalent with (n − k) n−k−1 ≥ k n−k and it is true since it is equivalent with In the last step we have used that n/k ≥ 3. It is clear from the proof that we only have to check whether one of the inequalities hold for some d: An easy consequence of this theorem is the following. Proof. Indeed, where the second inequality follows from Theorem 1.5.
A consequence of this theorem is that path has the minimal number of endomorphisms. Proof. If T n has at least four leaves, then hom(T n , T n ) ≥ hom(T n , P n ) ≥ hom(P n , P n ), where the first inequality follows from Corollary 5.3, while the second inequality follows from Theorem 1.4. If the tree T n has exactly three leaves, then it is star-like. Hence we can use Theorem 1.3 to prove the first inequality: hom(T n , T n ) ≥ hom(T n , P n ) ≥ hom(P n , P n ).
The proof of Theorem 5.2 will be given next, which would complete the proof of Theorem 1.8.
First, we prove a reduction lemma which says that we only have to prove Theorem 5.2 for trees with exactly 4 leaves.
where k < n and T k has at least four leaves, or k = n and T k has exactly four leaves. Then for any tree T n on n vertices with at least 4 leaves we have In the proof of this lemma we will subsequently use the following very simple fact.
Fact. If G is a graph and G 1 , G 2 are induced subgraphs of G with possible intersection, then for any graph H we have Proof of the lemma. We can assume that m ≥ 2. Assume that T n is tree with at least 5 leaves. Otherwise we have nothing to prove.
Let us call a path maximal in T n if it connects leaves. If a maximal path contains k vertices of degree at least 3, then we say that the maximal path has k branches.
First we prove the statement if T n contains a maximal path with at least 3 branches. Let v 0 P v r be a maximal path with vertices u 1 , . . . , u k having degree at least 3. Let B 1 , . . . , B k be the branches which we get if we delete all vertices and edges of the path v 0 P v r except u 1 , . . . , u k . So B i is a rooted tree with root u i . Let u − 2 and u + 2 be the two neighbors of u 2 on the path v 0 P v r . Let T 2 be the tree induced by the vertices V (B 2 ) ∪ {u − 2 , u + 2 }. Let |V (T 2 )| = t. We distinguish two cases.
1. case. In this case we assume that hom(T m , T 2 ) < (t−2)2 m−1 +2. Consider the following trees G 1 and G 2 . G 1 is the tree spanned by the vertices v 0 P u + 2 and the branches B 1 , B 2 . G 2 is the tree spanned by the vertices u − 2 P v r and the branches B 2 , . . . , B k . Note that G 1 ∪ G 2 = T n , G 1 ∩ G 2 = T 2 and G 1 , G 2 contains at least 4 leaves, because k ≥ 3. By the hypothesis of the lemma we have In this case we are done.

case.
In this case we assume that hom(T m , T 2 ) ≥ (t−2)2 m−1 +2. Consider the following trees G 1 and G 2 . G 1 is the tree spanned by the vertices 2 , u 2 , u + 2 } = P 3 and G 1 contains at least 4 vertices. By the hypothesis of the lemma we have We also know that in this case In this case we are done too.
Hence if one of the maximal path of T n has at least 3 branches then we are done. We still have to consider the trees T n where all maximal paths have at most 2 branches. In the following we show that they have quite simple structure: they are starlike or double starlike trees, see Figure 9.
Let v 1 be a vertex of T n of degree at least 3. Let us decompose T n to the branches B 1 , B 2 , . . . B k at v 1 . So v 1 is a leaf in the trees B 1 , B 2 , . . . B k .
We show that all except at most one of B 1 , B 2 , . . . , B k are paths. Assume that, for instance, B 1 , B 2 are not paths. Then they contains at least two leaves of T n : B 1 contains u 1 , u 2 , B 2 contains u 3 , u 4 . Then the maximal path u 1 P u 3 has at least three branches: one-one inside the branches B 1 and B 2 and B 3 at the vertex v. If all branches are paths, then we are done: T n is starlike. If one of them is not path, say B 1 , then let us consider the vertex v 2 ∈ V (B 1 ) having degree at least 3 which is closest to v 1 . Repeating the previous argument to v 2 instead of v 1 , all except one branches at v 2 must be path and we also know that the branch containing v 1 is not path. Hence the tree is double starlike, where the middle path is v 1 P v 2 . We can consider a starlike tree as a double starlike tree, where v 1 = v 2 . Let a 1 , . . . , a s and b 1 , . . . , b t be the leaves of T n , where a 1 , . . . , a s are closer to v 1 than to v 2 , while b 1 , . . . , b t are closer to v 2 than to v 1 . If v 1 = v 2 we just decompose the set of leaves into two sets of (almost) equal size. Note that s, t ≥ 2. Since we can assume that there are at least 5 leaves, we assume that s + t ≥ 5.
If u 1 , . . . , u l are some vertices of a tree, then we say that the tree spanned by u 1 , . . . , u l is the smallest subtree which contains the vertices u 1 , . . . , u l . It is span(u 1 , . . . , u l ) = ∪ 1≤i,j≤l u i P u j . If s ≥ 3 and t ≥ 3 both hold, then let G 1 be the tree spanned by the vertices a 1 , a 2 , b 1 , b 2 , and let G 2 be the tree spanned by the vertices a 2 , . . . a s , b 2 , . . . , b t . Then G 1 ∪ G 2 = T n , G 1 ∩ G 2 = a 2 P b 2 and both G 1 , G 2 have at least 4 leaves. Since Hence we are done in this case.
If s ≥ 4 and t = 2 then let G 1 be the tree spanned by a 1 , a 2 , b 1 , b 2 and let G 2 be the tree spanned by a 2 , . . . , a k , b 1 . Then G 1 ∪G 2 = T n , G 1 ∩G 2 = a 2 P b 1 and both G 1 , G 2 have at least 4 leaves. In this case we are done as before. Clearly, the case s ≥ 2 and t ≥ 4 is completely similar.
The last case is s = 3, t = 2 (and s = 2, t = 3). Let G 1 = span(a 1 , a 2 , b 1 , b 2 ), Hence we are done in this case as well. Proof. We can assume that m ≥ 4, otherwise the statement is trivial. We prove the slightly stronger inequality If m ≥ 4, then this implies that hom(T m , T n ) > (n − 2)2 m−1 + 1 or equivalently, hom(T m , T n ) ≥ (n − 2)2 m−1 + 2. To prove this statement we use Theorem 3.1 with a suitable Markov chain. Let p ij = 1 2 for (i, j) ∈ E(T n ) if i has degree 2. Naturally, p ij = 1 if (i, j) ∈ E(G) and i is a leaf. Finally, if i ∈ {x, y}, j ∈ xP y then p ij = 1 2 and if i ∈ {x, y}, j / ∈ xP y then p ij = 1 4 . Let r be the number of vertices of xP y. Then n = r + t + 4. Let N = 4r + 2t + 4. Then the stationary distribution is the following: N if i / ∈ xP y, but has degree 2 and finally, On the other hand, Note that The above inequality is satisfied if This proves the statement of the theorem.
Lemma 5.6. Let T n be a tree obtained from a path on n − 8 vertices by gluing one-one P 5 at the middle vertices to both ends of the path P n−8 (see Fig. 10). Then for any tree T m on m vertices we have hom(T m , T n ) ≥ (n − 1)2 m−1 .
Proof. We will show by induction on m that hom(T m , T n ) ≥ (n − 1)2 m−1 .

(5.3)
Let v be any leaf of T m with unique neighbor u and let T m−1 = T m − v be a rooted tree with root u. It is easy to see by induction that z > 2x if T m−1 has at least two vertices. By tree-walk algorithm, we have hom(T m (v), T n ) = 4x + 8y + 6z + 2(a + · · · + b) ≥ 8x + 8y + 4z + 2(a + · · · + b) which shows (5.3).
Next we introduce a transformation which we will call LS-switch (Large-Small switch). Definition 5.7 (LS-switch). Let R(u, v) be a tree with specified vertices u and v such that the distance of u and v is even and R has an automorphism of order 2 which exchanges the vertices u and v. Let T 1 (x), T 2 (x), T 3 (y), T 4 (y) be rooted trees such that T 2 (x) is the rooted subtree of T 1 (x) and T 4 (y) is the rooted subtree of T 3 (y). Let the tree T be obtained from the trees R(u, v), T 1 (x), T 2 (x), T 3 (y), T 4 (y) by attaching a copy of T 1 (x), T 4 (y) to R(u, v) at vertex u and a copy of T 2 (x), T 3 (y) at vertex v. Assume that the tree T ′ is obtained from the trees R(u, v), T 1 (x), T 2 (x), T 3 (y), T 4 (y) by attaching a copy of T 1 (x), T 3 (y) to R(u, v) at vertex u and a copy of T 2 (x), T 4 (y) at vertex v. Then T ′ is the LS-switch of T . Observe that there is a natural bijection between the color classes of T ′ and T .
A particular case of the LS-switch is when R(u, v) is a path of even length with end vertices u and v, T 2 (x) and T 4 (y) are one-vertex rooted trees, then T ′ is obtained from T by an even-KC-transformation, i.e., KC-transformation according to a path of even length. Another useful special case is when R(u, v) is a tree where we attach an arbitrary tree to the middle vertex of the path on 3 vertices and u and v are the end vertices of the path (in this case the automorphism simply switches u and v), and T 2 (x), T 4 (y) are the rooted trees with 1 vertex, in this case we get back to a particular case of the original Kelmans-transformation [10].
Actually, the following theorem with respect to the LS-switch is also true and its proof is just a trivial extension of the even case of Theorem 1.3. Proof. (Sketch.) The unique shortest path connecting all T i 's in T (or T ′ ) will be denoted by P 2k , a path of even length with vertices labeled consecutively by 0, 1, . . . , 2k. Without loss of generality, we can assume that 0 ∈ V (T 1 ). For 1 ≤ j ≤ 2k − 1, let A j denote the component of T that contains the vertex j when we delete all the edges of P 2k . By the definition of LS-switch, the subtrees A j and A 2k−j are isomorphic, so we can identify We only need to check that the two operations in the tree-walk algorithm preserve all the above inequalities, which is routine and left to the reader.
where n is the number of vertices of T n .
Proof. If T n has a vertex of degree 4 then by Theorem 3.6 we have Hence hom(T m , T n ) ≥ (n − 1)2 m−1 ≥ (n − 2)2 m−1 + 2. Now assume that T n has two vertices, x and y, of degree 3. Among these trees (n vertices, 4 leaves, two vertices of degree 3) let us choose T n to be the one for which hom(T m , T n ) is minimal and among these trees the length of the path is maximal.
Let the four leaves of T n denoted by z 1 , z 2 , z 3 , z 4 such that z 1 , z 2 are closer to x than y, and z 3 , z 4 are closer to y than x. Let the number of edges of xP y, xP z 1 , xP z 2 , yP z 3 , yP z 4 be a, b, c, d, e, respectively. We show that max(b, c, d, e) ≤ 2. Indeed, if say b > 2 then T n can be obtained by an LS-switch from a graph T * n as follows. If b is even, then let u be the unique vertex such that d(z 1 , u) = 2. Then uP x = R(u, x) is a path of even length. Let T 2 = z 1 P u. Furthermore, let T 1 be the tree spanned by the vertices x, z 3 , z 4 . T 3 = xP z 2 and T 4 = {u}. Then T 2 is a rooted subtree of T 1 and T 4 is a rooted subtree of T 3 . Now making an inverse LS-swith we obtain T * n . We know that hom(T m , T n ) ≥ hom(T m , T * n ) and in T * n , the vertices of degree 3, y and u, has distance a + b − 2 > a contradicting the choice of T n .
If b is odd, then let u be the unique neighbor of z 1 and we repeat the previous argument. The distance of u and x is even again.
Hence we can assume that max(b, c, d, e) ≤ 2. If not all of them are 2, then we can use Lemma 5.5 to get that hom(T m , T n ) ≥ (n − 2)2 m−1 + 2.

Proof. (a)
We can think to Y a,b,c with min(a, b, c) ≥ 2 as follows: we consider Y 2,2,2 and we subdivide the edges between the vertex of degree 3 and its neighbors a few times. Let us write the weights 1, 4, 1, 4, 1, 4, 9 to the vertices  Since This is already greater than (n − 2)2 m−1 + 2 for m ≥ 3. The statement is trivial for m ≤ 2. We use completely the same argument as in part (a). We think to Y a,b,1 as a subdivision of Y 3,3,1 and use the Markov chain on the figure. Again we have H(P |Q) = log 2 and the sum of the weights is N = 48+12(n−8) = 12(n−4). Hence Since This is already greater than (n − 2)2 m−1 + 2 for m ≥ 2. The statement is trivial for m = 1.
Remark 5.11. Since every Markov chains are reversible on a tree, there is a natural way to define a new Markov chain on a subdivided edge. Assume that the probabilities of the stationary distribution were q i , q j and p ij , p ji were the transition probabilities at the vertices i, j. Then q i p ij = q j p ji (reversibility) and we can put a vertex r with weight 2q i p ij and p ri = p rj = 1/2 on the edge (i, j). Then the new stationary distribution will be proportional to the weights {q i |i ∈ V (T )} ∪ {2q i p ij }. Theorem 1.6. Let T n be a tree on n vertices. Assume that for a tree T m we have hom(T m , T n ) < hom(T m , P n ).
Then T n = Y 1,1,n−3 and n is even.
Proof. Note that if T n has at least 4 leaves then Theorem 5.9 implies that contradicting to the condition of the theorem. Hence T n = Y a,b,c for some a, b, c. Observe that if one of a, b, c is even then Y a,b,c can be obtained from P n by an even-KC-transformation and then Theorem 1.3 implies hom(T m , T n ) ≥ hom(T m , P n ) contradicting to the condition of the theorem. Note that if n is odd, then one of a, b, c is necessarily even and so we are done. From Lemma 5.10 we also know that min(a, b, c) = 1, say c = 1 and min(a, b) ≤ 2. But then min(a, b) = 1, because it must be even. Hence T n = Y 1,1,n−3 and n is even. Remark 5.12. There is a tree T m for which hom(T m , S 4 ) < hom(T m , P 4 ).
On the figure one can see a rooted tree and its homomorphism vectors to S 4 and P 4 . Now if we attach k copies of this rooted tree at the root then for the obtained tree T m we have hom(T m , P 4 ) = 2 · 4 k + 2 · 10 k > 4 · 9 k = hom(T m , S 4 ) for large enough k.
On the other hand, it seems that hom(T m , Y 1,1,n−3 ) ≥ hom(T m , P n ) if n ≥ 6 and even.

Homomorphisms into a path
In this section we study the extremal problem about the number of homomorphisms from trees to a fixed path. The main purpose of this section is to prove Theorem 1.4. As the theorem suggests we have to distinguish whether n is even or odd.
6.1. The KC-transformation: the case of even n. In this section we will prove the part (i) of Theorem 1.4.
First we need some new definitions and lemmas.
Definition 6.1. A vector a = (a 1 , a 2 , . . . , a n ) is symmetric if a i = a n−i+1 for 1 ≤ i ≤ n − 1, and unimodal if a 1 ≤ a 2 ≤ · · · ≤ a j ≥ a j+1 ≥ · · · ≥ a n for some j. Denote by R n the set of symmetric positive integer vectors of dimension n. For any a, b ∈ R n , define the dominance order on R n by It is clear that R n is a poset with respect to this order and a b implies a ≤ b . Let U n be the set of all unimodal vectors in R n . Lemma 6.2. Let c ∈ U n and a, b ∈ R n such that a b. Then a * c b * c.
Proof. It is clear that both a * c and b * c are in R n . For 1 ≤ k ≤ ⌈ n 2 ⌉, by the symmetry property of a, b and c, we have the identity where c 0 = 0. From c ∈ U n and a b, we see that c j − c j−1 ≥ 0 and Thus the right-hand side of the above identity is nonnegative, which is equivalent to a * c b * c.
It is clear that if a ∈ U 2n then aA k ∈ U 2n for any positive integer k, where A is the adjacency matrix of P 2n . But for the path with odd vertices, this is not true in general. Lemma 6.3. Let A be the adjacency matrix of P n and l a positive integer. If a ∈ U n , then aA l a * (1 n A l ). a = (a 1 , a 2 , . . . , a n ), aA l = (x 1 , x 2 , . . . , x n ) and a * (1 n A l ) = (y 1 , y 2 , . . . , y n ). For 1 ≤ j ≤ n, the sum of coefficients of all a i (1 ≤ i ≤ n) in x j is the sum of j-th column of A l and the coefficient of a j in y j also equals the sum of j-th column of A l . It follows that the sum of coefficients of all a i (1 ≤ i ≤ n) in n+1−k i=k x i equals the sum of coefficients of all a i (1 ≤ i ≤ n) in n+1−k i=k y i . But for every i, the coefficient of a i in aA l is the sum of i-th row of A l and the coefficient of a i in a * 1 n A l is the sum of i-th column of A l , which are equal. Thus n+1−k i=k x i ≤ n+1−k i=k y i follows from the unimodality of a, which shows that aA l a * (1 n A l ).

Proof. Let
Before we prove part (i) of Theorem 1.4, we show that we only have to prove the statement for the case of even n since the following lemma implies it for the case of odd n if diam(T ) ≤ n − 1. hom(T, P n ) = 1 2 (hom(T, P n−1 ) + hom(T, P n+1 )).
Proof. Let the vertices of P n be labeled consecutively by 1, 2, . . . , n. We can decompose the set Hom(T, P n ) to the following two sets. The first set consists of those homomorphisms which do not contain the vertex n in their image and the second set consists of those homomorphisms which contain the vertex n in their image. Clearly, the cardinality of the first set is hom(T, P n−1 ). The cardinality of the second set will be denoted by hom(T, P n , n ∈ f (T )). So (6.2) hom(T, P n ) = hom(T, P n−1 ) + hom(T, P n , n ∈ f (T )).

Proof of part (i) of Theorem 1.4. Let
A be the adjacency matrix of P n . Let T ′ m = KC(T, x, y) be the KC-transformation of the tree T m with respect to a x-y path P . Let B 1 and B 2 be the components of y and x in the subgraph of T m by deleting all the edges of P respectively.
The case n is even. It is easy to see that an element of U n multiplied by A is still in U n and the Hadamard product of two elements in U n is also in U n . By the tree-walk algorithm, any hom-vector from a tree to P n is in U n . Again by the tree-walk algorithm, we have h(T ′ m , x, P n ) = h(B 1 , y, P n ) * 1 n A k * h(B 2 , x, P n ), where k is the length of the path P . By lemma 6.3, we have It follows from lemma 6.2 that h(T, x, P n ) h(T ′ , x, P n ), which implies hom(T, P n ) ≤ hom(T ′ , P n ).
For the case of odd n we have already seen that Lemma 6.4 implies the statement.
The KC-transformation does not always increase the number of homomorphisms to the path P n when n is odd. For example, in Fig. 15, we have hom(T 6 , P 3 ) = 20 > 16 = hom(T ′ 6 , P 3 ). Figure 15. The trees T 6 (left) and T ′ 6 (right).
6.2. More tree transformations: the case of odd n. Clearly, if n is even or n is odd and diam(T m ) ≤ n − 1, then part (i) of Theorem 1.4 implies the part (ii). In this subsection, we will develop some more transformations on trees to prove part (ii) of Theorem 1.4 for odd n. From now on in this subsection, n is odd. For any u ∈ V (T ), denote by T (u) the rooted tree with a root at u. As usual, we will denote by h(T, u, G) the hom-vector of the rooted tree T (u) into G. One can easily check that the hom-vector of a rooted tree into P n , i.e., h(T, u, P n ) is not unimodal anymore. On the other hand, the situation is not as bad as one may think after the first sight. Definition 6.5. We say that (a 1 , a 2 , . . . , a n ) is symmetric bi-unimodal if the sequence itself is symmetric and the two subsequences (a 1 , a 3 , a 5 , . . . a n ) and (a 2 , a 4 , . . . a n−1 ) are unimodal. Proposition 6.6. Let T (u) be a rooted tree and (a 1 , . . . , a n ) be the homvector of T (u) into P n . Then (a 1 , . . . , a n ) is symmetric bi-unimodal.
The following one is a very surprising theorem. In fact, it is not true for even n. Lemma 6.7 (Correlation inequality). Let T 1 (u) be a rooted tree and T 2 (u) be its rooted subtree, i.e., T 2 is subtree of T 1 and their roots are the same. Let (a 1 , . . . , a n ) be the hom-vector of T 1 (u) into P n and let (b 1 , . . . , b n ) be the homvector of T 2 (u) into P n . Assume that i ≡ j (mod 2) and | n+1 2 −i| ≤ | n+1 2 −j|, so i is closer to the center than j. Then Proof. We need to prove that a i a j ≥ b i b j . (From this form it is clear that it is a natural associative ordering on rooted trees.) We prove this claim by induction on the number of vertices of T 1 (u). If |T 1 (u)| ≤ 2 then it is trivial to check the statement.
If u is not a leaf in T 1 then by induction this inequality holds for the branches at u and then it is true for the Hadamard products. If u is a leaf in T 1 then so in T 2 . Let v be the unique neighbor of u and let us consider the rooted trees ( Note that the numbers i−1, i+1, j−1, j+1 are still congruent modulo 2. Because of the symmetry we can assume that j < i ≤ n+1 2 . If in addition i < n+1 2 then we have j − 1 < j + 1 ≤ i − 1 < i + 1 ≤ n+1 2 and we can apply the induction hypothesis: in all four cases, thus 2 then the above four inequalities are still true, because {i − 1, i + 1} are still closer to n+1 2 than the numbers {j − 1, j + 1}. Hence we have proved the statement. Lemma 6.8 (Log-concavity of the hom-vector.). Let T 1 (u) be a rooted tree and let (a 1 , . . . , a n ) be its hom-vector. Assume that i < j and i = j ( mod 2). Then a i a j ≤ a i+1 a j−1 .
Proof. The proof is almost identical to the proof of the Correlation inequality and thus is omitted. Definition 6.9. Let T 1 , T 2 be trees. Let u be an arbitrary vertex of T 1 and let A and B be the color classes of T 2 considered as a bipartite graph. Let h A and h B be the number of homomorphisms from T 1 to T 2 where u goes to A and B, respectively. Then let Note that g(T 1 , T 2 ) is independent of the vertex u.
Let the vertices of P n be labeled consecutively by 1, 2, . . . , n. If T 2 = P n then hom 0 (T 1 (u), P n ) denotes the number of homomorphisms of T 1 into P n , where the image of u is a vertex of even index and hom 1 (T 1 (u), P n ) denotes the number of homomorphisms of T 1 into P n , where the image of u is a vertex of odd index. Thus in this case g(T 1 , P n ) = hom 0 (T 1 (u), P n ) · hom 1 (T 1 (u), P n ).
First we prove the following curious theorem on the function g(T, P n ). Theorem 6.10. Let T m be a tree on m vertices. Then g(T m , P n ) ≥ g(P m , P n ).
We will prove Theorem 6.10 by using two transformations: the LS-switch and the so-called short-path shift.
Recall that the LS-switch is a generalization of the even-KC-transformation. For the sake of convenience we repeat its definition below.
Let R(u, v) be a tree with specified vertices u and v such that the distance of u and v is even and R has an automorphism of order 2 which exchanges the vertices u and v. Let T 1 (x), T 2 (x), T 3 (y), T 4 (y) are rooted trees such that T 2 (x) is the rooted subtree of T 1 (x) and T 4 (y) is the rooted subtree of T 3 (y). Let the tree T be obtained from the trees R(u, v), T 1 (x), T 2 (x), T 3 (y), T 4 (y) by attaching a copy of T 1 (x), T 4 (y) to R(u, v) at vertex u and a copy of T 2 (x), T 3 (y) at vertex v. Assume that the tree T ′ is obtained from the trees R(u, v), T 1 (x), T 2 (x), T 3 (y), T 4 (y) by attaching a copy of T 1 (x), T 3 (y) to R(u, v) at vertex u and a copy of T 2 (x), T 4 (y) at vertex v. Then T ′ is the LS-switch of T . Observe that there is a natural bijection between the color classes of T ′ and T .
This transformation seems to be quite general, but still the following theorem is true. Theorem 6.11. Let T be a tree and T ′ be the LS-switch of T . Then for k = 0, 1. In particular, hom(T ′ , P n ) ≥ hom(T, P n ) and g(T ′ , P n ) ≥ g(T, P n ).
Proof. Let the vertices of P n be labeled consecutively by 1, 2, . . . , n. For a rooted tree T (r) let h(T, i) denote the number of homomorphisms of T into P n such that r goes to the vertex i. So (h(T, 1), h(T, 2), . . . , h(T, n)) is the hom-vector of T (r) into P n . Let a ij be the number of homomorphisms of R(u, v) into P n such that u goes to i and v goes to j. Note that a ij = a ji because of the automorphism of order 2 of R switching the vertices u and v.
Also note that a ij = 0 if i and j are incongruent modulo 2 since the distance of u and v is even. Observe that for k = 0, 1 we have and Using a ij = a ji we can rewrite these equations as follows: By the correlation inequalities, the signs of h(T 3 , i)h(T 4 , j) − h(T 3 , j)h(T 4 , i) only depend on the positions of i and j, so they are the same. Hence The main problem with the LS-switch is that it preserves the sizes of the color classes of the tree (considered as a bipartite graph). So we need another transformation which can help to compare trees with different color class sizes. For this reason we introduce the following transformation which we will call short path shift. Theorem 6.12. Assume that T ′ is obtained from the rooted tree T 1 (u) by attaching it to the middle vertex of a path on 3 vertices. Let us assume that T is obtained from T 1 (u) by attaching it to an end vertex of a path on 3 vertices. Then for any odd n ≥ 5, we have g(T ′ , P n ) ≥ g(T, P n ).
By the log-concavity of the hom-vector, all a 2 a i − a 1 a i+1 ≥ 0 for i ≡ 1 ( mod 2). Hence g(T ′ , P n ) ≥ g(T, P n ).
Observe that both transformations decreases the Wiener-index (sum of the distances of every pair of vertices in T ) strictly: W (T ′ ) < W (T ) if T ′ is obtained from T by an LS-switch or short-path shift and T ′ is not isomorphic to T .
Proof of Theorem 6.10. For n = 3 we have g(T, P 3 ) = 2 |T | , so there is nothing to prove. Hence we can assume that n ≥ 5.
Let us consider the tree T * m on m vertices for which hom(T * m , P n ) is minimal and among these trees it has the largest Wiener-index. We show that T * m is P m . Assume for contradiction that T * m is not P m . Let v be an end vertex of a longest path of T * m . Clearly, v is a leaf, let u be its unique neighbor. We distinguish two cases.
If deg(u) ≥ 3 then T * m can be decomposed into two branches, one of which is a star on at least 3 vertices. (Otherwise, v cannot be an end vertex of a longest path.) Hence u has another neighbor w which is a leaf. In this case, T * m is an image of a tree T by a short-path shift with respect to the path vuw. Hence g(T , P n ) ≤ g(T * m , P n ) and W (T ) > W (T * m ). This contradicts the choice of T * m . If deg(u) = 2 then let w be the closest vertex to v having degree at least 3. (Such a w must exist, because T * m is not P m .) Note that d(v, w) ≥ 2. Let us decompose the tree T * m into branches P (v, w), T 1 (w) and T 3 (w) at the vertex w, where T 1 (w) and T 3 (w) are non-trivial trees. If d(v, w) is even, then T * m is an image of a tree T by an LS-switch, where T 2 (v) = T 4 (v) are one-vertex trees. If d(v, w) is odd (consequently d(u, w) is even), then T * m is an image of a tree T by an LS-switch, where T 2 (u) is a one-vertex tree and T 4 (u) is the rooted tree on the vertex set {u, v}. In both cases g(T , P n ) ≤ g(T * m , P n ) and W (T ) ≥ W (T * m ). In the first case, the second inequality is strict contradicting the choice of T * m . In the second case, it may occur that T 3 (w) is also an edge implying that T * m = T . By changing the role of T 2 (v) and T 4 (v), we can ensure that T 1 (w) is also an edge (otherwise we get the same contradiction as before). Hence in this case T * m is a path with an edge attached on the second vertex. In this case we can realize that it is a short-path shift of a path at the vertex w. Hence we get a contradiction in this case too, which finishes the proof of the theorem.
To prove part (ii) of Theorem 1.4, we will distinguish two cases according to the parity of m.
6.2.1. Trees on even number of vertices. If m is even, then g(P m , P n ) = 1 4 hom(P m , P n ) 2 since h A = h B = 1 2 hom(P m , P n ). (However, the color classes of P n are not symmetric, but the color classes of P m are symmetric so it is equally likely which color class goes to which one.) Hence by Theorem 6.10 we have hom(T m , P n ) ≥ 2g(T m , P n ) 1/2 ≥ 2g(P m , P n ) 1/2 = hom(P m , P n ).
If m is odd then we still need to work a bit.

6.2.2.
Trees on odd number of vertices. From now on n and m are odd. Since m is odd, it makes sense to speak about large and small color class of the tree considered as a bipartite graph. If T m is a tree on m vertices, then S will denote the color class of size at most m−1 2 and L will denote the color class of size at least m+1 2 . S and L stands for small and large. The notation hom 0 (T (S), P n ) denotes hom 0 (T (u), P n ), where u ∈ S. Hence it means that the small color class goes to the even-indexed vertices of the path. We can similarly define hom 1 (T (S), P n ).
The following is a simple observation.
This lemma asserts that the small class of the path P m 'likes' to go the large class of the path P n .
Proof. Let u be a leaf of P m . Note that u ∈ L. Let v be its neighbor, and let (a 1 , a 2 , . . . , a n ) be the hom-vector of P m−1 (v). Then the hom-vector of (a 1 , 2a 2 , . . . , 2a n−1 , a n ). Note that hom 0 (P m (S), P n ) = hom 0 (P m (v), P n ) = 2 Note that a j since P m−1 has even number of vertices. Hence hom 0 (P m (S), P n ) ≥ hom 1 (P m (S), P n ).
The following theorem is the main result of this part of the proof. It will imply the minimality of the path.  Proof. From Theorem 6.10 we know that g(T m , P n ) ≥ g(P m , P n ).
Hence hom(T m , P n ) ≥ hom(P m , P n ).
Now we start to prove Theorem 6.14. We need a few lemmas. The first one is trivial, but crucial. Lemma 6.15. Let T 1 be a tree on m vertices, and let u ∈ L be a leaf. Let v ∈ S an arbitrary vertex of the small class. Let T 2 be a tree obtained from T 1 by deleting the vertex u from T 1 and attaching a leaf u ′ to v. (So we simply move a leaf of the large class to another place, but we take care not changing the sizes of the color classes.) Then hom 0 (T 1 (S), P n ) = hom 0 (T 2 (S), P n ).
We will use the following transformation too.
Definition 6.16 (Claw-deletion). Let T ′ be a tree which contains a claw: three leaves attached to the same vertex. Let T be obtained from T ′ by deleting the three leaves and attaching a path of length 3 to the common neighbor of the leaves. We call this transformation claw-deletion.
Note that the claw-deletion changes the sizes of the color classes. We have to be careful when we apply it, because it may occur that claw-deletion changes the small class into the large one.
We need the following property of the claw-deletion.
Lemma 6.17. Let T ′ be a tree on m vertices with color classes S ′ and L ′ . Assume that |L ′ | − |S ′ | ≥ 3 and T ′ contains a claw. Let T be obtained from T ′ by a claw-deletion, where we assume that the center v of the claw is in the class S ′ . Then hom 0 (T ′ (S ′ ), P n ) > hom 0 (T (S), P n ).
Our strategy will be the following. We transform a tree into a path by moving leaves and using the LS-switch and claw-deletion repeatedly. If we want to apply this last operation, we need to be sure that the condition |L ′ | − |S ′ | ≥ 3 holds. The following lemma will be useful. Proof. We prove the statement by induction on the number of vertices. If T has at most 3 vertices, the claim is trivial. Assume that T has n vertices and we have proved the statement for trees on at most n − 1 vertices. Let u 1 be a leaf of T , and let v be its unique neighbor. Let u 1 , . . . , u k be the leaves of T adjacent to v. Note that u 1 , . . . , u k ∈ A and v ∈ B. Erase u 1 , . . . , u k from T , and let T * be the obtained tree. If v is a leaf in the obtained tree then erase it as well and let us denote the resulted tree by T * * , otherwise T * * = T * . It may occur that T * * is the empty graph, but it is not problem. For T * * it is still true that one color class, A * * , contains all leaves. Thus by induction |A * * | ≥ |B * * |. Then |A| = |A * * | + k ≥ |B * * | + 1 ≥ |B|, as desired.
Remark 6.19. Let T ′ be a tree on m vertices with color classes L and S. Assume that T ′ contains a claw, and all leaves belong to L. Then |L| − |S| ≥ 3. Indeed, if we delete two leaves from the claw then it is still true for the resulting tree T * that all leaves belong to one color class, and it must be the larger one. Hence |L * | ≥ |S * |, and since m is odd, we have |L * | ≥ |S * | + 1. Thus for the original tree T ′ we have |L| − |S| ≥ 3.
Proof of Theorem 6.14. Let T m be the set of trees on m vertices which minimizes hom 0 (T (S), P n ). From T m let us choose the tree T m , which has the smallest number of leaves and among these trees it has the largest Wienerindex. We show that T m must be P m . Assume for contradiction that T m = P m .
First of all, T m cannot be an image of an LS-switch, because if T m can be obtained from a tree F by an LS-switch, then by Theorem 6.11 we have hom 0 (F (S), P n ) ≤ hom 0 (T m (S), P n ), and F has at most as many leaves as T m , and W (F ) > W (T m ). Hence it would contradict the choice of T m .
Let S and L be the small and large class of T m , respectively. Note that L must contain a leaf by Lemma 6.18. We will distinguish two cases according to S containing a leaf too or not. Case 1. S contains a leaf v. Let x be the unique neighbor of v. First, we show that x has degree at least 3. If deg(x) = 2 then let w be the closest vertex to v having degree at least 3. Such a w must exist, because T m is not P m . Note that d(v, w) ≥ 2. Let us decompose the tree T m into branches P (v, w), T 1 (w) and T 3 (w) at the vertex w, where T 1 (w) and T 3 (w) are non-trivial trees. If d(v, w) is even, then T m is an image of a tree F by an LS-switch, where T 2 (v) = T 4 (v) are one-vertex trees. If d(v, w) is odd (consequently d(x, w) is even), then T m is an image of a tree F by an LSswitch, where T 2 (x) is a one-vertex tree and T 4 (x) is the rooted tree on the vertex set {x, v}. In the second case, it may occur that T 3 (w) is also an edge implying that T m = F . By changing the role of T 2 (v) and T 4 (v), we can ensure that T 1 (w) is also an edge (otherwise we get the same contradiction as before). Hence in this case T m is a path with an edge attached on the second vertex. In this case we can realise that it is an LS-switch of a path since m is odd. Hence we get a contradiction in this case too. Now let u ∈ L be a leaf. Let us delete it, and attach u ′ to v. This way we get a tree T ′ . By Lemma 6.15 we know that hom 0 (T m (S), P n ) = hom 0 (T ′ (S), P n ).
Furthermore, T ′ is an LS-switch (in fact, an even-KC-transformation) of a tree F . Indeed, let us decompose the tree T ′ at x to T 1 (x), T 2 (x) and the path xvu ′ . Thus if we move T 2 (x) from x to u ′ we get a tree F for which hom 0 (F (S), P n ) ≤ hom 0 (T ′ (S), P n ).
Note that u ′ and v is not a leaf in F anymore. Maybe, the original neighbor of u became a leaf in F , but still the number of leaves of F is strictly less than the number of leaves of T m . This contradicts the choice of T m . Hence we are done in this case.
Case 2. S contains no leaf. Hence all leaves belong to the class L. Let u 1 P u 2 be a longest path of T m . As in the previous case, the unique neighbors x 1 and x 2 of u 1 and u 2 , respectively, have degree at least 3. We can assume that x 1 = x 2 , otherwise T m is a star and it contains a claw and we can do claw-deletion, which strictly decreases hom 0 (T (S), P n ). Since x 1 and x 2 have degree at least 3, and u 1 P u 2 were the longest path, the only possible way it can occur that x 1 and x 2 have other neighbors u 3 and u 4 , respectively, which are leaves. Now let us delete u 3 and add a new neighbor u ′ 3 to x 2 . Let T ′ be the obtained tree. Then by Lemma 6.15 hom 0 (T m (S), P n ) = hom 0 (T ′ (S), P n ).
On the other hand, it is still true that all leaves of T ′ belong to its large class. Moreover, it contains a claw: {x 2 , u 2 , u ′ 3 , u 4 }. By Remark 6.19 the conditions of Lemma 6.17 are satisfied, and we can do a claw-deletion. Then we get a tree F for which hom 0 (T m (S), P n ) = hom 0 (T ′ (S), P n ) > hom 0 (F (S), P n ).
This contradicts the choice of T m .
Hence we get contradictions in all cases.

Open problems
We collected a few open problems and conjectures in this section. We first recall a conjecture from the Introduction, namely that there is no exceptional case in Theorem 1.6 if n ≥ 5.
Conjecture 1.7 Let T n be a tree on n vertices, where n ≥ 5. Then for any tree T m we have hom(T m , P n ) ≤ hom(T m , T n ).
Note that to prove Conjecture 1.7, one only needs to prove that for any tree T m we have hom(T m , P n ) ≤ hom(T m , Y 1,1,n−3 ) for n ≥ 6, where n is even.
There is also an open problem in Figure 3. for every tree T n on n vertices?
We believe that the answer is affirmative for this question. This question naturally leads to the following problem. Note that if G is d-regular, then hom(P m , G) = hom(T m , G) = |V (G)|d m−1 . We have also seen that the inequality of Problem 7.2 is satisfied if G = P n or S n . Probably, it is hard to characterize these graphs. Maybe, it is easier to describe those graphs G for which the inequality of Problem 7.2 is satisfied for large enough m.
The dual of Problem 7.2 is also natural: for all graph G. Sidorenko [17] achieved nice results on this problem. Still the problem is far from being solved. In light of the tree-walk algorithm, it would be interesting to develop an algorithm for computing the number of homomorphisms from bipartite graphs to any graph.