Random Walks on Trees

A rooted tree is given, with a coin placed at one of its nodes. At each step, the coin moves to an adjacent node with equal probability. Find the expected distance traveled before the coin reaches an adjacent node.

Definitions

  • T=(V,E)T=(V,E): the tree under consideration
  • d(u)d(u): the degree of node uu
  • w(u,v)w(u,v): the weight of the edge between nodes uu and vv
  • pup_u: the parent of node uu
  • root\textit{root}: the root of the tree
  • sonu\textit{son}_u: the set of children of node uu
  • siblingu\textit{sibling}_u: the set of siblings of node uu

Expected Distance to the Parent

Let f(u)f(u) denote the expected distance for the walk starting at node uu to reach its parent pup_u. Then:

f(u)=w(u,pu)+∑v∈sonu(w(u,v)+f(v)+f(u))d(u)f(u) = \cfrac{w(u,p_u) + \sum\limits_{v \in \textit{son}_u}(w(u,v) + f(v) + f(u))}{d(u)}

The first part of the numerator corresponds to moving directly to the parent. The remaining part corresponds to first moving to a child, returning from that child, and then moving to the parent. The denominator d(u)d(u) appears because every adjacent node of uu is chosen with the same probability.

The expression simplifies to:

f(u)=w(u,pu)+∑v∈sonu(w(u,v)+f(v)+f(u))d(u)=w(u,pu)+∑v∈sonu(w(u,v)+f(v))+(d(u)−1)f(u)d(u)=w(u,pu)+∑v∈sonu(w(u,v)+f(v))=∑(u,t)∈Ew(u,t)+∑v∈sonuf(v)\begin{aligned} f(u) &= \cfrac{w(u,p_u) + \sum\limits_{v \in \textit{son}_u}(w(u,v) + f(v) + f(u))}{d(u)} \\ &= \cfrac{w(u,p_u) + \sum\limits_{v \in \textit{son}_u}(w(u,v) + f(v)) + (d(u)-1)f(u)}{d(u)} \\ &= w(u,p_u) + \sum\limits_{v \in \textit{son}_u}(w(u,v) + f(v)) \\ &= \sum\limits_{(u,t) \in E}w(u,t) + \sum\limits_{v \in \textit{son}_u}f(v) \end{aligned}

For a leaf ll, the initial value is f(l)=w(pl,l)f(l) = w(p_l, l).

When every edge of the tree has weight 11, this becomes:

f(u)=d(u)+∑v∈sonuf(v)f(u) = d(u) + \sum\limits_{v \in \textit{son}_u}f(v)

This is the sum of the degrees of all nodes in the subtree of uu, equivalently twice the size of the subtree rooted at uu −1-1 (each node has exactly one edge to its parent; except for the edge between uu and pup_u, whose contribution is 11, every edge contributes 22 degrees).

Expected Distance to a Child

Let g(u)g(u) denote the expected distance for the walk starting at the parent pup_u to reach its child uu. Then:

g(u)=w(pu,u)+(w(pu,ppu)+g(pu)+g(u))+∑s∈siblingu(w(pu,s)+f(s)+g(u))d(pu)g(u) = \cfrac{w(p_u,u) + \left(w(p_u,p_{p_u})+g(p_u)+g(u)\right) + \sum\limits_{s \in \textit{sibling}_u}(w(p_u,s)+f(s)+g(u))}{d(p_u)}

The first part of the numerator corresponds to moving directly to child uu. The second part corresponds to first moving to the parent, returning from it, and then moving to uu. The third part corresponds to first moving to a sibling of uu, returning from that sibling, and then moving to uu. The denominator d(pu)d(p_u) appears because all adjacent nodes of pup_u are equally likely.

The expression simplifies to:

g(u)=w(pu,u)+(w(pu,ppu)+g(pu)+g(u))+∑s∈siblingu(w(pu,s)+f(s)+g(u))d(pu)=w(pu,u)+w(pu,ppu)+g(pu)+∑s∈siblingu(w(pu,s)+f(s))+(d(pu)−1)g(u)d(pu)=w(pu,u)+w(pu,ppu)+g(pu)+∑s∈siblingu(w(pu,s)+f(s))=∑(pu,t)∈Ew(pu,t)+g(pu)+∑s∈siblinguf(s)=∑(pu,t)∈Ew(pu,t)+g(pu)+(f(pu)−∑(pu,t)∈Ew(pu,t)−f(u))=g(pu)+f(pu)−f(u)\begin{aligned} g(u) &= \cfrac{w(p_u,u) + \left(w(p_u,p_{p_u})+g(p_u)+g(u)\right) + \sum\limits_{s \in \textit{sibling}_u}(w(p_u,s)+f(s)+g(u))}{d(p_u)} \\ &= \cfrac{w(p_u,u) + w(p_u,p_{p_u}) + g(p_u) + \sum\limits_{s \in \textit{sibling}_u}\left(w(p_u,s)+f(s)\right)+(d(p_u)-1)g(u)}{d(p_u)} \\ &= w(p_u,u) + w(p_u,p_{p_u}) + g(p_u) + \sum\limits_{s \in \textit{sibling}_u}(w(p_u,s)+f(s)) \\ &= \sum\limits_{(p_u,t) \in E}w(p_u,t) + g(p_u) + \sum\limits_{s \in \textit{sibling}_u}f(s) \\ &= \sum\limits_{(p_u,t) \in E}w(p_u,t) + g(p_u) + \left(f(p_u)-\sum\limits_{(p_u,t) \in E}w(p_u,t)-f(u)\right) \\ &= g(p_u) + f(p_u) - f(u) \end{aligned}

The initial value is g(root)=0g(\text{root}) = 0.

Implementation (for an unweighted tree)

vector<int> G[MAXN];

void dfs1(int u, int p) {
  f[u] = G[u].size();
  for (auto v : G[u]) {
    if (v == p) continue;
    dfs1(v, u);
    f[u] += f[v];
  }
}

void dfs2(int u, int p) {
  if (u != root) g[u] = g[p] + f[p] - f[u];
  for (auto v : G[u]) {
    if (v == p) continue;
    dfs2(v, u);
  }
}

This content comes from OI Wiki: Random Walks on Trees. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.