foundations of computational agents
The third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including full text).
The variable elimination (VE) algorithm, as used for finding solutions to CSPs and for optimization with soft constraints, can be adapted to find the posterior distribution for a variable in a belief network given conjunctive evidence. Many of the efficient exact methods are variants of this algorithm.
The algorithm is based on the notion that a belief network specifies a factorization of the joint probability distribution.
Before we provide the algorithm, we define factors and the operations that will be performed on them. Recall that $P(X\mid Y)$ is a function from variables (or sets of variables) $X$ and $Y$ into the real numbers that, given a value for $X$ and a value for $Y$, returns the conditional probability of the value for $X$, given the value for $Y$. A function of variables is called a factor. The VE algorithm for belief networks manipulates factors to compute posterior probabilities.
A conditional probability, $P(Y\mid {X}_{1},\mathrm{\dots},{X}_{k})$ is a function from the variables $Y,{X}_{1},\mathrm{\dots},{X}_{k}$ into nonnegative numbers that satisfies the constraints that for each assignment of values to all of ${X}_{1},\mathrm{\dots},{X}_{k}$ the values for $Y$ sum to 1. That is, given values to all of the variables, the function returns a number that satisfies the constraint:
$$\forall {x}_{1}\mathrm{\dots}\forall {x}_{k}\sum _{y\in domain(Y)}P(Y=y\mid {X}_{1}={x}_{1},\mathrm{\dots},{X}_{k}={x}_{k})=1$$  (8.1) 
With a finite set of variables with finite domains, conditional probabilities can be implemented as arrays. If there is an ordering of the variables (e.g., alphabetical) and the values in the domains are mapped into nonnegative integers, there is a unique representation of each factor as a onedimensional array that is indexed by natural numbers. This representation for a conditional probability is called a conditional probability table or CPT.
If the child variable is treated the same as the parent variables, the information is redundant; more numbers are specified than is required and a table could be inconsistent if it does not satisfy the above constraint. Using the redundant representation is common, but the following two methods are also used to specify and store probabilities:
Store unnormalized probabilities, which are nonnegative numbers that are proportional to the probability. The probability can be computed by normalizing: dividing each value by the sum of the values, summing over all values for the domain of $Y$.
Store the probability for allbutone of the values of $Y$. In this case, the probability of this other value can be computed to obey the constraint above. In particular, if $Y$ is binary, we only need to represent the probability for one value, say $Y=true$, and the probability for other other value, $Y=false$, can be computed from this.
$Fire$  $P(smoke\mid Fire)$ 

$true$  0.9 
$false$  0.01 
$Fire$  $Tampering$  $P(alarm\mid Fire,Tampering)$ 

$true$  $true$  0.5 
$true$  $false$  0.99 
$false$  $true$  0.85 
$false$  $false$  0.0001 
$X$  $Y$  $P(Z=t\mid X,Y)$ 

$t$  $t$  0.1 
$t$  $f$  0.2 
$f$  $t$  0.4 
$f$  $f$  0.3 
Figure 8.6 shows three conditional probabilities tables. On the top left is ${P}{\mathit{}}{\mathrm{(}}{S}{\mathit{}}{m}{\mathit{}}{o}{\mathit{}}{k}{\mathit{}}{e}{\mathrm{\mid}}{F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}{\mathrm{)}}$ and on the top right is ${P}{\mathit{}}{\mathrm{(}}{A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}{\mathrm{\mid}}{F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}{\mathrm{,}}{T}{\mathit{}}{a}{\mathit{}}{m}{\mathit{}}{p}{\mathit{}}{e}{\mathit{}}{r}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}{\mathrm{)}}$, from Example 8.15, which use Boolean variables.
These tables do not specify the probability for the child being false. This can be computed from the given probabilities, for example,
$${P}{(}{A}{l}{a}{r}{m}{=}{f}{a}{l}{s}{e}{\mid}{F}{i}{r}{e}{=}{f}{a}{l}{s}{e}{,}{T}{a}{m}{p}{e}{r}{i}{n}{g}{=}{t}{r}{u}{e}{)}{=}{1}{}{0.85}{=}{0.15}$$ 
On the bottom is a simple example, with domains ${\mathrm{\{}}{t}{\mathrm{,}}{f}{\mathrm{\}}}$, which will be used in the following examples.
Given a total ordering of the parents, such as ${F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}$ is before ${T}{\mathit{}}{a}{\mathit{}}{m}{\mathit{}}{p}{\mathit{}}{e}{\mathit{}}{r}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$ in the right table, and a total ordering of the values, such as ${t}{\mathit{}}{r}{\mathit{}}{u}{\mathit{}}{e}$ is before ${f}{\mathit{}}{a}{\mathit{}}{l}{\mathit{}}{s}{\mathit{}}{e}$, the table can be specified by giving the array of numbers in lexicographic order, such as ${\mathrm{[}}{\mathrm{0.5}}{\mathrm{,}}{\mathrm{0.99}}{\mathrm{,}}{\mathrm{0.85}}{\mathrm{,}}{\mathrm{0.0001}}{\mathrm{]}}$.
A factor is a function from a set of random variables into a number. A factor $f$ on variables ${X}_{1},\mathrm{\dots},{X}_{j}$ is written as $f({X}_{1},\mathrm{\dots},{X}_{j})$. The variables ${X}_{1},\mathrm{\dots},{X}_{j}$ are the variables of the factor $f$, and $f$ is a factor on ${X}_{1},\mathrm{\dots},{X}_{j}$.
Conditional probabilities are factors that also obey the constraint of Equation 8.1. This section describes some operations on factors, including conditioning, multiplying factors and summing out variables. The operations can be used for conditional probabilities, but do not necessarily result in conditional probabilities.
Suppose $f({X}_{1},\mathrm{\dots},{X}_{j})$ is a factor and each ${v}_{i}$ is an element of the domain of ${X}_{i}$. $f({X}_{1}={v}_{1},{X}_{2}={v}_{2},\mathrm{\dots},{X}_{j}={v}_{j})$ is a number that is the value of $f$ when each ${X}_{i}$ has value ${v}_{i}$. Some of the variables of a factor can be assigned to values to make a new factor on the other variables. This operation is called conditioning on the values of the variables that are assigned. For example, $f({X}_{1}={v}_{1},{X}_{2},\mathrm{\dots},{X}_{j})$, sometimes written as $f{({X}_{1},{X}_{2},\mathrm{\dots},{X}_{j})}_{{X}_{1}={v}_{1}}$, where ${v}_{1}$ is an element of the domain of variable ${X}_{1}$, is a factor on ${X}_{2},\mathrm{\dots},{X}_{j}$.
Figure 8.7 shows a factor ${r}{\mathit{}}{\mathrm{(}}{X}{\mathrm{,}}{Y}{\mathrm{,}}{Z}{\mathrm{)}}$ on variables ${X}$, ${Y}$ and ${Z}$ as a table. This assumes that each variable is binary with domain ${\mathrm{\{}}{t}{\mathrm{,}}{f}{\mathrm{\}}}$. This factor could be obtained from the last conditional probability table given in Figure 8.6. Figure 8.7 also gives a table for the factor ${r}{\mathrm{(}}{X}{\mathrm{=}}{t}{\mathrm{,}}{Y}{\mathrm{,}}{Z}{\mathrm{)}}$, which is a factor on ${Y}{\mathrm{,}}{Z}$. Similarly, ${r}{\mathrm{(}}{X}{\mathrm{=}}{t}{\mathrm{,}}{Y}{\mathrm{,}}{Z}{\mathrm{=}}{f}{\mathrm{)}}$ is a factor on ${Y}$, and ${r}{\mathrm{(}}{X}{\mathrm{=}}{t}{\mathrm{,}}{Y}{\mathrm{=}}{f}{\mathrm{,}}{Z}{\mathrm{=}}{f}{\mathrm{)}}$ is a number.
Factors can be multiplied together. Suppose ${f}_{1}$ and ${f}_{2}$ are factors, where ${f}_{1}$ is a factor that contains variables ${X}_{1},\mathrm{\dots},{X}_{i}$ and ${Y}_{1},\mathrm{\dots},{Y}_{j}$, and ${f}_{2}$ is a factor with variables ${Y}_{1},\mathrm{\dots},{Y}_{j}$ and ${Z}_{1},\mathrm{\dots},{Z}_{k}$, where ${Y}_{1},\mathrm{\dots},{Y}_{j}$ are the variables in common to ${f}_{1}$ and ${f}_{2}$. The product of ${f}_{1}$ and ${f}_{2}$, written ${f}_{1}*{f}_{2}$, is a factor on the union of the variables, namely ${X}_{1},\mathrm{\dots},{X}_{i},{Y}_{1},\mathrm{\dots},{Y}_{j},{Z}_{1},\mathrm{\dots},{Z}_{k}$, defined by:
$({f}_{1}*{f}_{2})$  $({X}_{1},\mathrm{\dots},{X}_{i},{Y}_{1},\mathrm{\dots},{Y}_{j},{Z}_{1},\mathrm{\dots},{Z}_{k})$  
$={f}_{1}({X}_{1},\mathrm{\dots},{X}_{i},{Y}_{1},\mathrm{\dots},{Y}_{j})*{f}_{2}({Y}_{1},\mathrm{\dots},{Y}_{j},{Z}_{1},\mathrm{\dots},{Z}_{k}).$ 
${f}_{1}=$


${f}_{2}=$

${f}_{1}*{f}_{2}=$ $A$ $B$ $C$ val $t$ $t$ $t$ 0.03 $t$ $t$ $f$ 0.07 $t$ $f$ $t$ 0.54 $t$ $f$ $f$ 0.36 $f$ $t$ $t$ 0.06 $f$ $t$ $f$ 0.14 $f$ $f$ $t$ 0.48 $f$ $f$ $f$ 0.32
Figure 8.8 shows the product of ${{f}}_{{\mathrm{1}}}{\mathit{}}{\mathrm{(}}{A}{\mathrm{,}}{B}{\mathrm{)}}$ and ${{f}}_{{\mathrm{2}}}{\mathit{}}{\mathrm{(}}{B}{\mathrm{,}}{C}{\mathrm{)}}$, which is a factor on ${A}{\mathrm{,}}{B}{\mathrm{,}}{C}$. Note that ${\mathrm{(}}{{f}}_{{\mathrm{1}}}{\mathrm{*}}{{f}}_{{\mathrm{2}}}{\mathrm{)}}{\mathrm{(}}{A}{\mathrm{=}}{t}{\mathrm{,}}{B}{\mathrm{=}}{f}{\mathrm{,}}{C}{\mathrm{=}}{f}{\mathrm{)}}{\mathrm{=}}{{f}}_{{\mathrm{1}}}{\mathrm{(}}{A}{\mathrm{=}}{t}{\mathrm{,}}{B}{\mathrm{=}}{f}{\mathrm{)}}{\mathrm{*}}{{f}}_{{\mathrm{2}}}{\mathrm{(}}{B}{\mathrm{=}}{f}{\mathrm{,}}{C}{\mathrm{=}}{f}{\mathrm{)}}{\mathrm{=}}{\mathrm{0.9}}{\mathrm{*}}{\mathrm{0.4}}{\mathrm{=}}{\mathrm{0.36}}$.
The remaining operation is to sum out a variable in a factor. Given factor $f({X}_{1},\mathrm{\dots},{X}_{j})$, summing out a variable, say ${X}_{1}$, results in a factor on the other variables, ${X}_{2},\mathrm{\dots},{X}_{j}$, defined by
$$(\sum _{{X}_{1}}f)({X}_{2},\mathrm{\dots},{X}_{j})=f({X}_{1}={v}_{1},{X}_{2},\mathrm{\dots},{X}_{j})+\mathrm{\cdots}+f({X}_{1}={v}_{k},{X}_{2}\mathrm{\dots},{X}_{j}),$$ 
where $\{{v}_{1},\mathrm{\dots},{v}_{k}\}$ is the set of possible values of variable ${X}_{1}$.
Figure 8.9 gives an example of summing out variable ${B}$ from a factor ${{f}}_{{\mathrm{3}}}{\mathit{}}{\mathrm{(}}{A}{\mathrm{,}}{B}{\mathrm{,}}{C}{\mathrm{)}}$, which is a factor on ${A}{\mathrm{,}}{C}$. Notice how
${(}{\displaystyle {\sum}_{{B}}}{{f}}_{{3}}{)}{(}{A}{=}{t}{,}{C}{=}{f}{)}{=}$  ${{f}}_{{3}}{(}{A}{=}{t}{,}{B}{=}{t}{,}{C}{=}{f}{)}{+}{{f}}_{{3}}{(}{A}{=}{t}{,}{B}{=}{f}{,}{C}{=}{f}{)}$  
${=}$  ${0.07}{+}{0.36}$  
${=}$  ${0.43}$ 
Given evidence ${Y}_{1}={v}_{1}$, …, ${Y}_{j}={v}_{j}$, and query variable or variables $Q$, the problem of computing the posterior distribution on $Q$ can be reduced to the problem of computing the probability of conjunctions:
$P(Q\mid {Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})$  $={\displaystyle \frac{P(Q,{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})}{P({Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})}}$  
$={\displaystyle \frac{P(Q,{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})}{{\displaystyle \sum _{Q}}P(Q,{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})}}.$ 
The algorithm computes the factor $P(Q,{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})$ and normalizes. Note that this is a factor only of $Q$; given a value for $Q$, it returns a number that is the probability of the conjunction of the evidence and the value for $Q$.
Suppose the variables of the belief network are ${X}_{1},\mathrm{\dots},{X}_{n}$. To compute the factor $P(Q,{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})$, sum out the other variables from the joint distribution. Suppose ${Z}_{1},\mathrm{\dots},{Z}_{k}$ is an enumeration of the other variables in the belief network, that is,
$$\{{Z}_{1},\mathrm{\dots},{Z}_{k}\}=\{{X}_{1},\mathrm{\dots},{X}_{n}\}\setminus \{Q,{Y}_{1},\mathrm{\dots},{Y}_{j}\}$$ 
and the variables ${Z}_{i}$ are ordered according to an elimination ordering.
The probability of $Q$ conjoined with the evidence is
$$p(Q,{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j})=\sum _{{Z}_{k}}\mathrm{\cdots}\sum _{{Z}_{1}}P{({X}_{1},\mathrm{\dots},{X}_{n})}_{{Y}_{1}={v}_{1},\mathrm{\dots},{Y}_{j}={v}_{j}}.$$ 
By the chain rule and the definition of a belief network,
$P({X}_{1},\mathrm{\dots},{X}_{n})={\displaystyle \prod _{i=1}^{n}}P({X}_{i}\mid parents({X}_{i}))$ 
where $parents({X}_{i})$ is the set of parents of variable ${X}_{i}$.
The belief network inference problem is thus reduced to a problem of summing out a set of variables from a product of factors. The distribution law specifies that a sum of products such as $xy+xz$, can be simplified by distributing out the common factors (here $x$), which results in $x(y+z)$. The resulting form is more efficient to compute. Distributing out common factors is the essence of the VE algorithm. The elements multiplied together are called “factors” because of the use of the term in algebra. Initially, the factors represent the conditional probability distributions, but the intermediate factors are just functions on variables created by adding and multiplying factors.
To compute the posterior distribution of a query variable given observations
Construct a factor for each conditional probability distribution.
Eliminate each of the nonquery variables:
if the variable is observed, its value is set to the observed value in each of the factors in which the variable appears,
otherwise the variable is summed out.
Multiply the remaining factors and normalize.
To sum out a variable $Z$ from a product ${f}_{1},\mathrm{\dots},{f}_{k}$ of factors, first partition the factors into those not containing $Z$, say ${f}_{1},\mathrm{\dots},{f}_{i}$, and those containing $Z$, ${f}_{i+1},\mathrm{\dots},{f}_{k}$; then distribute the common factors out of the sum:
$$\sum _{Z}{f}_{1}*\mathrm{\cdots}*{f}_{k}={f}_{1}*\mathrm{\cdots}*{f}_{i}*\left(\sum _{Z}{f}_{i+1}*\mathrm{\cdots}*{f}_{k}\right).$$ 
VE explicitly constructs a representation (in terms of a multidimensional array, a tree, or a set of rules) of the rightmost factor.
Figure 8.10 gives pseudocode for the VE algorithm. The elimination ordering could be given a priori or computed on the fly. It is worthwhile to select observed variables first in the elimination ordering, because eliminating these simplifies the problem.
This algorithm assumes that the query variable is not observed. If it is observed to have a particular value, its posterior probability is just 1 for the observed value and 0 for the other values.
Consider Example 8.15 with the query
$${P}{(}{T}{a}{m}{p}{e}{r}{i}{n}{g}{\mid}{S}{m}{o}{k}{e}{=}{t}{r}{u}{e}{\wedge}{R}{e}{p}{o}{r}{t}{=}{t}{r}{u}{e}{)}{.}$$ 
Suppose it first eliminates the observed variables, ${S}{\mathit{}}{m}{\mathit{}}{o}{\mathit{}}{k}{\mathit{}}{e}$ and ${R}{\mathit{}}{e}{\mathit{}}{p}{\mathit{}}{o}{\mathit{}}{r}{\mathit{}}{t}$. After these are eliminated, the following factors remain:
$$\begin{array}{cc}{C}{}{o}{}{n}{}{d}{}{i}{}{t}{}{i}{}{o}{}{n}{}{a}{}{l}{}{P}{}{r}{}{o}{}{b}{}{a}{}{b}{}{i}{}{l}{}{i}{}{t}{}{y}\hfill & {F}{}{a}{}{c}{}{t}{}{o}{}{r}\hfill \\ {P}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}\hfill & {{f}}_{{0}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}\hfill \\ {P}{}{(}{F}{}{i}{}{r}{}{e}{)}\hfill & {{f}}_{{1}}{}{(}{F}{}{i}{}{r}{}{e}{)}\hfill \\ {P}{}{(}{A}{}{l}{}{a}{}{r}{}{m}{\mid}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{F}{}{i}{}{r}{}{e}{)}\hfill & {{f}}_{{2}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{F}{}{i}{}{r}{}{e}{,}{A}{}{l}{}{a}{}{r}{}{m}{)}\hfill \\ {P}{(}{S}{m}{o}{k}{e}{=}{y}{e}{s}{\mid}{F}{i}{r}{e}{)}\hfill & {{f}}_{{3}}{}{(}{F}{}{i}{}{r}{}{e}{)}\hfill \\ {P}{}{(}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{\mid}{A}{}{l}{}{a}{}{r}{}{m}{)}\hfill & {{f}}_{{4}}{}{(}{A}{}{l}{}{a}{}{r}{}{m}{,}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}\hfill \\ {P}{(}{R}{e}{p}{o}{r}{t}{=}{y}{e}{s}{\mid}{L}{e}{a}{v}{i}{n}{g}{)}\hfill & {{f}}_{{5}}{}{(}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}\hfill \end{array}$$ 
Suppose ${F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}$ is next in the elimination ordering. To eliminate ${F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}$, collect all of the factors containing ${F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}$, namely ${{f}}_{{\mathrm{1}}}{\mathit{}}{\mathrm{(}}{F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}{\mathrm{)}}$, ${{f}}_{{\mathrm{2}}}{\mathit{}}{\mathrm{(}}{T}{\mathit{}}{a}{\mathit{}}{m}{\mathit{}}{p}{\mathit{}}{e}{\mathit{}}{r}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}{\mathrm{,}}{F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}{\mathrm{,}}{A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}{\mathrm{)}}$, and ${{f}}_{{\mathrm{3}}}{\mathit{}}{\mathrm{(}}{F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}{\mathrm{)}}$, multiply them together, and sum out ${F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}$ from the resulting factor. Call this factor ${{F}}_{{\mathrm{6}}}{\mathit{}}{\mathrm{(}}{T}{\mathit{}}{a}{\mathit{}}{m}{\mathit{}}{p}{\mathit{}}{e}{\mathit{}}{r}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}{\mathrm{,}}{A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}{\mathrm{)}}$. At this stage, ${F}{\mathit{}}{s}$ contains the factors:
$${{f}}_{{0}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}{,}{{f}}_{{4}}{}{(}{A}{}{l}{}{a}{}{r}{}{m}{,}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{,}{{f}}_{{5}}{}{(}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{,}{{f}}_{{6}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{A}{}{l}{}{a}{}{r}{}{m}{)}{.}$$ 
Suppose ${A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}$ is eliminated next. VE multiplies the factors containing ${A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}$ and sums out ${A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}$ from the product, giving a factor, call it ${{f}}_{{\mathrm{7}}}$:
$${{f}}_{{7}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{=}{\sum}_{{A}{}{l}{}{a}{}{r}{}{m}}{{f}}_{{4}}{}{(}{A}{}{l}{}{a}{}{r}{}{m}{,}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{*}{{f}}_{{6}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{A}{}{l}{}{a}{}{r}{}{m}{)}$$ 
${F}{}{s}$ then contains the factors:
$$\begin{array}{c}{{f}}_{{0}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}{,}{{f}}_{{5}}{}{(}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{,}{{f}}_{{7}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{.}\hfill \end{array}$$ 
Eliminating ${L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$ results in the factor
$${{f}}_{{8}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}{=}{\sum}_{{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}}{{f}}_{{5}}{}{(}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{*}{{f}}_{{7}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{,}{L}{}{e}{}{a}{}{v}{}{i}{}{n}{}{g}{)}{.}$$ 
To determine the distribution over ${T}{\mathit{}}{a}{\mathit{}}{m}{\mathit{}}{p}{\mathit{}}{e}{\mathit{}}{r}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$, multiply the remaining factors, giving
$${{f}}_{{9}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}{=}{{f}}_{{0}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}{*}{{f}}_{{8}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}{.}$$ 
The posterior distribution over tampering is given by
$$\frac{{{f}}_{{9}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}}{{{\sum}}_{{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}}{{f}}_{{9}}{}{(}{T}{}{a}{}{m}{}{p}{}{e}{}{r}{}{i}{}{n}{}{g}{)}}{.}$$ 
Note that the denominator is the prior probability of the evidence, namely ${P}{\mathrm{(}}{S}{m}{o}{k}{e}{\mathrm{=}}{t}{r}{u}{e}{\mathrm{\wedge}}{R}{e}{p}{o}{r}{t}{\mathrm{=}}{t}{r}{u}{e}{\mathrm{)}}$
Consider the same network as in the previous example but with the following query:
$${P}{(}{A}{l}{a}{r}{m}{\mid}{F}{i}{r}{e}{=}{t}{r}{u}{e}{)}{.}$$ 
When ${F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}$ is eliminated, the factor ${P}{\mathit{}}{\mathrm{(}}{F}{\mathit{}}{i}{\mathit{}}{r}{\mathit{}}{e}{\mathrm{)}}$ becomes a factor of no variables; it is just a number, ${P}{\mathrm{(}}{F}{i}{r}{e}{\mathrm{=}}{t}{r}{u}{e}{\mathrm{)}}$.
Suppose ${R}{\mathit{}}{e}{\mathit{}}{p}{\mathit{}}{o}{\mathit{}}{r}{\mathit{}}{t}$ is eliminated next. It is in one factor, which represents ${P}{\mathit{}}{\mathrm{(}}{R}{\mathit{}}{e}{\mathit{}}{p}{\mathit{}}{o}{\mathit{}}{r}{\mathit{}}{t}{\mathrm{\mid}}{L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}{\mathrm{)}}$. Summing over all of the values of ${R}{\mathit{}}{e}{\mathit{}}{p}{\mathit{}}{o}{\mathit{}}{r}{\mathit{}}{t}$ gives a factor on ${L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$, all of whose values are 1. This is because ${P}{\mathrm{(}}{R}{e}{p}{o}{r}{t}{\mathrm{=}}{t}{r}{u}{e}{\mathrm{\mid}}{L}{e}{a}{v}{i}{n}{g}{\mathrm{=}}{v}{\mathrm{)}}{\mathrm{+}}{P}{\mathrm{(}}{R}{e}{p}{o}{r}{t}{\mathrm{=}}{f}{a}{l}{s}{e}{\mathrm{\mid}}{L}{e}{a}{v}{i}{n}{g}{\mathrm{=}}{v}{\mathrm{)}}{\mathrm{=}}{\mathrm{1}}$ for any value ${v}$ of ${L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$.
If ${L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$ is eliminated next, a factor that is all 1 is multiplied by a factor representing ${P}{\mathit{}}{\mathrm{(}}{L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}{\mathrm{\mid}}{A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}{\mathrm{)}}$ and ${L}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{v}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$ is summed out. This, again, results in a factor all of whose values are 1.
Similarly, eliminating ${S}{\mathit{}}{m}{\mathit{}}{o}{\mathit{}}{k}{\mathit{}}{e}$ results in a factor of no variables, whose value is ${\mathrm{1}}$. Note that even if smoke had also been observed, eliminating ${S}{\mathit{}}{m}{\mathit{}}{o}{\mathit{}}{k}{\mathit{}}{e}$ would result in a factor of no variables, which would not affect the posterior distribution on ${A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}$.
Eventually, there is only the factor on ${A}{\mathit{}}{l}{\mathit{}}{a}{\mathit{}}{r}{\mathit{}}{m}$ that represents its prior probability and a constant factor that will cancel in the normalization.
To speed up the inference, variables that are irrelevant to answer a query given the observations can be pruned. In particular, any node that has no observed or queried descendants and is itself not observed or queried may be pruned. This may result in a smaller network with fewer factors and variables. For example, to compute $P(Alarm\mid Fire=true)$, the variables $Report$, $Leaving$ and $Smoke$ may be pruned.
The complexity of the algorithm depends on a measure of complexity of the network. The size of a tabular representation of a factor is exponential in the number of variables in the factor. The treewidth of a network, given an elimination ordering, is the maximum number of variables in a factor created by summing out a variable when using the elimination ordering. The treewidth of a belief network is the minimum treewidth over all elimination orderings. The treewidth depends only on the graph structure and is a measure of the sparseness of the graph. The complexity of VE is exponential in the treewidth and linear in the number of variables. Finding the elimination ordering with minimum treewidth is NPhard, but there are some good elimination ordering heuristics, as discussed for CSP VE.
Consider the belief network of Figure 8.4. To compute the probability of ${S}{\mathit{}}{n}{\mathit{}}{e}{\mathit{}}{e}{\mathit{}}{z}{\mathit{}}{i}{\mathit{}}{n}{\mathit{}}{g}$, the variables ${F}{\mathit{}}{e}{\mathit{}}{v}{\mathit{}}{e}{\mathit{}}{r}$ and ${\mathrm{"}}{\mathit{}}{A}{\mathit{}}{c}{\mathit{}}{h}{\mathit{}}{o}{\mathit{}}{o}{\mathit{}}{\mathrm{"}}{\mathit{}}{s}{\mathit{}}{o}{\mathit{}}{u}{\mathit{}}{n}{\mathit{}}{d}$ may be pruned, as they have no children and are not observed or queried. Summing out ${S}{\mathit{}}{e}{\mathit{}}{a}{\mathit{}}{s}{\mathit{}}{o}{\mathit{}}{n}$ involves multiplying the factors
$${P}{}{(}{S}{}{e}{}{a}{}{s}{}{o}{}{n}{)}{,}{P}{}{(}{P}{}{o}{}{l}{}{l}{}{e}{}{n}{\mid}{S}{}{e}{}{a}{}{s}{}{o}{}{n}{)}{,}{P}{}{(}{I}{}{n}{}{f}{}{l}{}{u}{}{e}{}{n}{}{z}{}{a}{\mid}{S}{}{e}{}{a}{}{s}{}{o}{}{n}{)}$$ 
and results in a factor on ${I}{\mathit{}}{n}{\mathit{}}{f}{\mathit{}}{l}{\mathit{}}{u}{\mathit{}}{e}{\mathit{}}{n}{\mathit{}}{z}{\mathit{}}{a}$ and ${P}{\mathit{}}{o}{\mathit{}}{l}{\mathit{}}{l}{\mathit{}}{e}{\mathit{}}{n}$. The treewidth of this belief network is two; there is an ordering of the variables that only constructs factors of size one or two, and there is no ordering of the variables that has a smaller treewidth.
The moral graph of a belief network is the undirected graph where there is an arc between any two nodes that appear in the same initial factor. This is obtained by “marrying” the parents of a node, and removing the directions. If we prune as outlined in the previous paragraph, moralize the graph, and remove all observed variables, only those variables connected to the query in this graph are relevant to answering the query. The other variables can be pruned.
Many modern exact algorithms use what is essentially variable elimination, and they speed it up by preprocessing as much as possible into a secondary structure before any evidence arrives. This is appropriate when, for example, the same belief network may be used for many different queries, and where observations are added incrementally. The algorithms save intermediate results so that evidence is incrementally added. Unfortunately, extensive preprocessing, allowing arbitrary sequences of observations and deriving the posterior on each variable, precludes pruning the network. So for each application you need to choose whether you will save more by pruning irrelevant variables for each query and observation or by preprocessing before you have any observations.