Figure 4 shows an efficient version that exploits the packed-forest structure of HG(x) in computing the expected counts.
it first runs the inside-outside procedure, which annotates each node (say v) with both an inside weight β(v) and an outside weight α(v).