# Generalized distributive law

The generalized distributive law (GDL) is a generalization of the distributive property which gives rise to a general message passing algorithm. It is a synthesis of the work of many authors in the information theory, digital communications, signal processing, statistics, and artificial intelligence communities. The law and algorithm were introduced in a semi-tutorial by Srinivas M. Aji and Robert J. McEliece with the same title.

## Introduction

"The distributive law in mathematics is the law relating the operations of multiplication and addition, stated symbolically, $a*(b+c)=a*b+a*c$; that is, the monomial factor $a$ is distributed, or separately applied, to each term of the binomial factor $b+c$, resulting in the product $a*b+a*c$" - Britannica.
As it can be observed from the definition, application of distributive law to an arithmetic expression reduces the number of operations in it. In the previous example the total number of operations reduced from three (two multiplications and an addition in $a*b+a*c$) to two (one multiplication and one addition in $a*(b+c)$). Generalization of distributive law leads to a large family of fast algorithms. This includes the FFT and Viterbi algorithm.
This is explained in a more formal way in the example below:
$\alpha (a,\,b){\stackrel {\mathrm {def} }{=}}\displaystyle \sum \limits _{c,d,e\in A}f(a,\,c,\,b)\,g(a,\,d,\,e)$ where $f(\cdot )$ and $g(\cdot )$ are real-valued functions, $a,b,c,d,e\in A$ and $|A|=q$ (say)
Here we are "marginalizing out" the independent variables ($c$, $d$, and $e$) to obtain the result. When we are calculating the computational complexity, we can see that for each $q^{2}$ pairs of $(a,b)$, there are $q^{3}$ terms due to the triplet $(c,d,e)$ which needs to take part in the evaluation of $\alpha (a,\,b)$ with each step having one addition and one multiplication. Therefore, the total number of computations needed is $2\cdot q^{2}\cdot q^{3}=2q^{5}$. Hence the asymptotic complexity of the above function is $O(n^{5})$.
If we apply the distributive law to the RHS of the equation, we get the following:
    $\alpha (a,\,b){\stackrel {\mathrm {def} }{=}}\displaystyle \sum \limits _{c\in A}f(a,\,c,\,b)\cdot \sum _{d,\,e\in A}g(a,\,d,\,e)$
This implies that $\alpha (a,\,b)$ can be described as a product $\alpha _{1}(a,\,b)\cdot \alpha _{2}(a)$ where $\alpha _{1}(a,b){\stackrel {\mathrm {def} }{=}}\displaystyle \sum \limits _{c\in A}f(a,\,c,\,b)$ and $\alpha _{2}(a){\stackrel {\mathrm {def} }{=}}\displaystyle \sum \limits _{d,\,e\in A}g(a,\,d,\,e)$
Now, when we are calculating the computational complexity, we can see that there are $q^{3}$ additions in $\alpha _{1}(a,\,b)$ and $\alpha _{2}(a)$ each and there are $q^{2}$ multiplications when we are using the product $\alpha _{1}(a,\,b)\cdot \alpha _{2}(a)$ to evaluate $\alpha (a,\,b)$. Therefore, the total number of computations needed is $q^{3}+q^{3}+q^{2}=2q^{3}+q^{2}$. Hence the asymptotic complexity of calculating $\alpha (a,b)$ reduces to $O(n^{3})$ from $O(n^{5})$. This shows by an example that applying distributive law reduces the computational complexity which is one of the good features of a "fast algorithm".

## History

Some of the problems that used distributive law to solve can be grouped as follows:
* Decoding algorithms: A GDL like algorithm was used by Gallager's for decoding low density parity-check codes. Based on Gallager's work Tanner introduced the Tanner graph and expressed Gallagers work in message passing form. The tanners graph also helped explain the Viterbi algorithm. It is observed by Forney that Viterbi's maximum likelihood decoding of convolutional codes also used algorithms of GDL-like generality.
* Forward-backward algorithm: The forward backward algorithm helped as an algorithm for tracking the states in the Markov chain. And this also was used the algorithm of GDL like generality
* Artificial intelligence: The notion of junction trees has been used to solve many problems in AI. Also the concept of bucket elimination used many of the concepts.

## The MPF problem

MPF or marginalize a product function is a general computational problem which as special case includes many classical problems such as computation of discrete Hadamard transform, maximum likelihood decoding of a linear code over a memory-less channel, and matrix chain multiplication. The power of the GDL lies in the fact that it applies to situations in which additions and multiplications are generalized.
A commutative semiring is a good framework for explaining this behavior. It is defined over a set $K$ with operators "$+$" and "$.$" where $(K,\,+)$ and $(K,\,.)$ are a commutative monoids and the distributive law holds.
Let $p_{1},\ldots ,p_{n}$ be variables such that $p_{1}\in A_{1},\ldots ,p_{n}\in A_{n}$ where $A$ is a finite set and $|A_{i}|=q_{i}$. Here $i=1,\ldots ,n$. If $S=\{i_{1},\ldots ,i_{r}\}$ and $S\,\subset \{1,\ldots ,n\}$, let $A_{S}=A_{i_{1}}\times \cdots \times A_{i_{r}}$, $p_{S}=(p_{i_{1}},\ldots ,p_{i_{r}})$,
$q_{S}=|A_{S}|$, $\mathbf {A} =A_{1}\times \cdots \times A_{n}$, and $\mathbf {p} =\{p_{1},\ldots ,p_{n}\}$
Let $S=\{S_{j}\}_{j=1}^{M}$ where $S_{j}\subset \{1,...\,,n\}$. Suppose a function is defined as $\alpha _{i}:A_{S_{i}}\rightarrow R$, where $R$ is a commutative semiring. Also, $p_{S_{i}}$ are named the local domains and $\alpha _{i}$ as the local kernels.
Now the global kernel $\beta :\mathbf {A} \rightarrow R$ is defined as: $\beta (p_{1},...\,,p_{n})=\prod _{i=1}^{M}\alpha (p_{S_{i}})$
Definition of MPF problem: For one or more indices $i=1,...\,,M$, compute a table of the values of $S_{i}$-marginalization of the global kernel $\beta$, which is the function $\beta _{i}:A_{S_{i}}\rightarrow R$ defined as $\beta _{i}(p_{S_{i}})\,=\displaystyle \sum \limits _{p_{S_{i}^{c}}\in A_{S_{i}^{c}}}\beta (p)$
Here $S_{i}^{c}$ is the complement of $S_{i}$ with respect to $\mathbf {\ 1,...\,,n\}$ and the $\beta _{i}(p_{S_{i}})$ is called the $i^{th}$ objective function, or the objective function at $S_{i}$. It can observed that the computation of the $i^{th}$ objective function in the obvious way needs $Mq_{1}q_{2}q_{3}\cdots q_{n}$ operations. This is because there are $q_{1}q_{2}\cdots q_{n}$ additions and $(M-1)q_{1}q_{2}...q_{n}$ multiplications needed in the computation of the $i^{\text{th}}$ objective function. The GDL algorithm which is explained in the next section can reduce this computational complexity.
The following is an example of the MPF problem.
Let $p_{1},\,p_{2},\,p_{3},\,p_{4},$ and $p_{5}$ be variables such that $p_{1}\in A_{1},p_{2}\in A_{2},p_{3}\in A_{3},p_{4}\in A_{4},$ and $p_{5}\in A_{5}$. Here $M=4$ and $S=\{\{1,2,5\},\{2,4\},\{1,4\},\{2\}\}$. The given functions using these variables are $f(p_{1},p_{2},p_{5})$ and $g(p_{3},p_{4})$ and we need to calculate $\alpha (p_{1},\,p_{4})$ and $\beta (p_{2})$ defined as:
    $\alpha (p_{1},\,p_{4})=\displaystyle \sum \limits _{p_{2}\in A_{2},\,p_{3}\in A_{3},\,p_{5}\in A_{5}}f(p_{1},\,p_{2},\,p_{5})\cdot g(p_{2},\,p_{4})$
    $\beta (p_{2})=\sum \limits _{p_{1}\in A_{1},\,p_{3}\in A_{3},\,p_{4}\in A_{4},\,p_{5}\in A_{5}}f(p_{1},\,p_{2},\,p_{5})\cdot g(p_{2},\,p_{4})$
Here local domains and local kernels are defined as follows:
| local domains | local kernels |
| $\{p_{1},p_{2},p_{5}\}$ | $(f(p_{1},p_{2},p_{5})$ |
| $\{p_{2},p_{4}\}$ | $g(p_{2},p_{4})$ |
| $\{p_{1},p_{4}\}$ | $1$ |
| $\{p_{2}\}$ | $1$ |
where $\alpha (p_{1},p_{4})$ is the $3^{rd}$ objective function and $\beta (p_{2})$ is the $4^{th}$ objective function.
Consider another example where $p_{1},p_{2},p_{3},p_{4},r_{1},r_{2},r_{3},r_{4}\in \{0,1\}$ and $f(r_{1},r_{2},r_{3},r_{4})$ is a real valued function. Now, we shall consider the MPF problem where the commutative semiring is defined as the set of real numbers with ordinary addition and multiplication and the local domains and local kernels are defined as follows:
| local domains | local kernels |
| $\{r_{1},r_{2},r_{3},r_{4}\}$ | $f(r_{1},r_{2},r_{3},r_{4})$ |
| $\{p_{1},r_{1}\}$ | $(-1)^{p_{1}r_{1}}$ |
| $\{p_{2},r_{2}\}$ | $(-1)^{p_{2}r_{2}}$ |
| $\{p_{3},r_{3}\}$ | $(-1)^{p_{3}r_{3}}$ |
| $\{p_{4},r_{4}\}$ | $(-1)^{p_{4}r_{4}}$ |
| $\{p_{1},p_{2},p_{3},p_{4}\}$ | $1$ |
Now since the global kernel is defined as the product of the local kernels, it is
    $F(p_{1},p_{2},p_{3},p_{4},r_{1},r_{2},r_{3},r_{4})=f(p_{1},p_{2},p_{3},p_{4})\cdot (-1)^{p_{1}r_{1}+p_{2}r_{2}+p_{3}r_{3}+p_{4}r_{4}}$
and the objective function at the local domain $p_{1},p_{2},p_{3},p_{4}$ is
    $F(p_{1},p_{2},p_{3},p_{4})=\displaystyle \sum \limits _{r_{1},r_{2},r_{3},r_{4}}f(r_{1},r_{2},r_{3},r_{4})\cdot (-1)^{p_{1}r_{1}+p_{2}r_{2}+p_{3}r_{3}+p_{4}r_{4}}.$
This is the Hadamard transform of the function $f(\cdot )$. Hence we can see that the computation of Hadamard transform is a special case of the MPF problem. More examples can be demonstrated to prove that the MPF problem forms special cases of many classical problem as explained above whose details can be found at

## GDL: an algorithm for solving the MPF problem

If one can find a relationship among the elements of a given set $S$, then one can solve the MPF problem basing on the notion of belief propagation which is a special use of "message passing" technique. The required relationship is that the given set of local domains can be organised into a junction tree. In other words, we create a graph theoretic tree with the elements of $S$ as the vertices of the tree $T$, such that for any two arbitrary vertices say $v_{i}$ and $v_{j}$ where $i\neq j$ and there exists an edge between these two vertices, then the intersection of corresponding labels, viz $S_{i}\cap S_{j}$, is a subset of the label on each vertex on the unique path from $v_{i}$ to $v_{j}$.
For example,
Example 1: Consider the following nine local domains:
* $\{p_{2}\}$
* $\{p_{3},p_{2}\}$
* $\{p_{2},p_{1}\}$
* $\{p_{3},p_{4}\}$
* $\{p_{3}\}$
* $\{p_{1},p_{4}\}$
* $\{p_{1}\}$
* $\{p_{4}\}$
* $\{p_{2},p_{4}\}$
For the above given set of local domains, one can organize them into a junction tree as shown below:
Similarly If another set like the following is given
Example 2: Consider the following four local domains:
* $\{p_{1},p_{2}\}$
* $\{p_{2},p_{3}\}$
* $\{p_{3},p_{4}\}$
* $\{p_{1},p_{4}\}$
Then constructing the tree only with these local domains is not possible since this set of values has no common domains which can be placed between any two values of the above set. But however, if add the two dummy domains as shown below then organizing the updated set into a junction tree would be possible and easy too.
5.$\{p_{1},p_{2}$,$p_{4}\$ 6.$\{p_{2},p_{3}$,$p_{4}\$
Similarly for these set of domains, the junction tree looks like shown below:

### Generalized distributive law (GDL) algorithm

Input: A set of local domains. Output: For the given set of domains, possible minimum number of operations that is required to solve the problem is computed.
So, if $v_{i}$ and $v_{j}$ are connected by an edge in the junction tree, then a message from $v_{i}$ to $v_{j}$ is a set/table of values given by a function: $\mu _{i,j}$:$A_{S_{i}\cap S_{j}}\rightarrow R$. To begin with all the functions i.e. for all combinations of $i$ and $j$ in the given tree, $\mu _{i,j}$ is defined to be identically $1$ and when a particular message is update, it follows the equation given below.
    $\mu _{i,j}(p_{S_{i}\cap S_{j}})$ = $\sum _{p_{S_{i}\setminus S_{j}}\in A_{S_{i}\setminus S_{j}}}\alpha _{i}(p_{S_{i}})\prod _{{v_{k}\operatorname {adj} v_{i}},{k\neq j}}\mu _{k,j}(p_{S_{k}\cap S_{i}})(1)$
where $v_{k}\operatorname {adj} v_{i}$ means that $v_{k}$ is an adjacent vertex to $v_{i}$ in tree.
Similarly each vertex has a state which is defined as a table containing the values from the function $\sigma _{i}:A_{S_{i}}\rightarrow R$, Just like how messages initialize to 1 identically, state of $v_{i}$ is defined to be local kernel $\alpha (p_{S_{i}})$, but whenever $\sigma _{i}$ gets updated, it follows the following equation:
    $\sigma (p_{S_{i}})=\alpha _{i}(p_{S_{i}})\prod _{v_{k}\operatorname {adj} v_{i}}\mu _{k,j}(p_{S_{k}\cap S_{i}})(2).$

### Basic working of the algorithm

For the given set of local domains as input, we find out if we can create a junction tree, either by using the set directly or by adding dummy domains to the set first and then creating the junction tree, if construction junction is not possible then algorithm output that there is no way to reduce the number of steps to compute the given equation problem, but once we have junction tree, algorithm will have to schedule messages and compute states, by doing these we can know where steps can be reduced, hence will be discusses this below.

## Scheduling of the message passing and the state computation

There are two special cases we are going to talk about here namely Single Vertex Problem in which the objective function is computed at only one vertex $v_{0}$ and the second one is All Vertices Problem where the goal is to compute the objective function at all vertices.
Lets begin with the single-vertex problem, GDL will start by directing each edge towards the targeted vertex $v_{0}$. Here messages are sent only in the direction towards the targeted vertex. Note that all the directed messages are sent only once. The messages are started from the leaf nodes(where the degree is 1) go up towards the target vertex $v_{0}$. The message travels from the leaves to its parents and then from there to their parents and so on until it reaches the target vertex $v_{0}$. The target vertex $v_{0}$ will compute its state only when it receives all messages from all its neighbors. Once we have the state, We have got the answer and hence the algorithm terminates.
For Example, let us consider a junction tree constructed from the set of local domains given above i.e. the set from example 1, Now the Scheduling table for these domains is (where the target vertex is $p_{2}$).
${\text{Round Message or State Computation}}$ $1.\mu _{8,4}(p_{4})=\alpha _{8}(p_{4})$ $2.\mu _{8,4}(p_{4})=\Sigma _{p_{2}}\alpha _{9}(p_{2},p_{4})$ $3.\mu _{5,2}(p_{3})=\alpha _{5}(p_{3})$ $4.\mu _{6,3}(p_{1})=\Sigma _{p_{4}}\alpha _{6}(p_{1},p_{4})$ $5.\mu _{7,3}(p_{1})=\alpha _{7}(p_{1})$ $6.\mu _{4,2}(p_{3})=\Sigma _{p_{4}}\alpha _{4}(p_{3},p_{4}).\mu _{8,4}(p_{4}).\mu _{9,4}(p_{4})$ $7.\mu _{3,1}(p_{2})=\Sigma _{p_{1}}\alpha _{3}(p_{2},p_{1}).\mu _{6,3}(p_{1}).\mu _{7,3}(p_{1})$ $8.\mu _{2,1}(p_{2})=\Sigma _{p_{3}}\alpha _{2}(p_{3},p_{2}).\mu _{4,2}(p_{3}).\mu _{5,2}(p_{3})$ $9.\sigma _{1}(p_{2})=\alpha _{1}(p_{2}).\mu _{2,1}(p_{2}).\mu _{3,1}(p_{2})$
Thus the complexity for Single Vertex GDL can be shown as
$\Sigma _{v}d(v)|A_{S_{(v)}}|$ arithmetic operations Where (Note: The explanation for the above equation is explained later in the article ) $S(v)$ is the label of $v$. $d(v)$ is the degree of $v$ (i.e. number of vertices adjacent to v).
To solve the All-Vertices problem, we can schedule GDL in several ways, some of them are parallel implementation where in each round, every state is updated and every message is computed and transmitted at the same time. In this type of implementation the states and messages will stabilizes after number of rounds that is at most equal to the diameter of the tree. At this point all the all states of the vertices will be equal to the desired objective function.
Another way to schedule GDL for this problem is serial implementation where its similar to the Single vertex problem except that we don't stop the algorithm until all the vertices of a required set have not got all the messages from all their neighbors and have compute their state.
Thus the number of arithmetic this implementation requires is at most $\Sigma _{v\in V}d(v)|A_{S_{(v)}}|$ arithmetic operations.

## Constructing a junction tree

The key to constructing a junction tree lies in the local domain graph $G_{LD}$, which is a weighted complete graph with $M$ vertices $v_{1},v_{2},v_{3},\ldots ,v_{M}$ i.e. one for each local domain, having the weight of the edge $e_{i,j}:v_{i}\leftrightarrow v_{j}$ defined by $\omega _{i,j}=|S_{i}\cap S_{j}|$. if $x_{k}\in S_{i}\cap S_{j}$, then we say $x_{k}$ is contained in$e_{i,j}$. Denoted by $\omega _{max}$ (the weight of a maximal-weight spanning tree of $G_{LD}$), which is defined by
    $\omega ^{*}=\Sigma _{i=1}^{M}|S_{i}|-n$
where n is the number of elements in that set. For more clarity and details, please refer to these.

## Scheduling theorem

Let $'T'$ be a junction tree with vertex set $'V'$ and edge set $'E'$. In this algorithm, the messages are sent in both the direction on any edge, so we can say/regard the edge set E as set of ordered pairs of vertices. For example, from Figure 1 $'E'$ can be defined as follows
    $E=\{(1,2),(2,1),(1,3),(3,1),(4,2),(2,4),(5,2),(2,5),(6,3),(3,6),(7,3),(3,7),(8,4),(4,8),(9,4),(4,9)\}$
NOTE:$E$ above gives you all the possible directions that a message can travel in the tree.
The schedule for the GDL is defined as a finite sequence of subsets of$E$. Which is generally represented by ${\mathcal {E}}=${$E_{1},E_{2},E_{3},\ldots ,E_{N}$}, Where $E_{N}$ is the set of messages updated during the $N^{th}$ round of running the algorithm.
Having defined/seen some notations, we will see want the theorem says, When we are given a schedule ${\mathcal {E}}=\{E_{1},E_{2},E_{3},\ldots ,E_{N}\}$, the corresponding message trellis as a finite directed graph with Vertex set of $V\times \{0,1,2,3,\ldots ,N\}$, in which a typical element is denoted by $v_{i}(t)$ for $t\in \{0,1,2,3,\ldots ,N\}$, Then after completion of the message passing, state at vertex $v_{j}$ will be the $j^{\text{th}}$ objective defined in
    $\sigma (p_{S_{i}})=\alpha _{i}(p_{S_{i}})\prod _{v_{k}\operatorname {adj} v_{i}}\mu _{k,j}(p_{S_{k}\cap S_{i}})$
and iff there is a path from $v_{i}(0)$ to $v_{j}(N)$

## Computational complexity

Here we try to explain the complexity of solving the MPF problem in terms of the number of mathematical operations required for the calculation. i.e. We compare the number of operations required when calculated using the normal method (Here by normal method we mean by methods that do not use message passing or junction trees in short methods that do not use the concepts of GDL) and the number of operations using the generalized distributive law.
Example: Consider the simplest case where we need to compute the following expression $ab+ac$.
To evaluate this expression naively requires two multiplications and one addition. The expression when expressed using the distributive law can be written as $a(b+c)$ a simple optimization that reduces the number of operations to one addition and one multiplication.
Similar to the above explained example we will be expressing the equations in different forms to perform as few operation as possible by applying the GDL.
As explained in the previous sections we solve the problem by using the concept of the junction trees. The optimization obtained by the use of these trees is comparable to the optimization obtained by solving a semi group problem on trees. For example, to find the minimum of a group of numbers we can observe that if we have a tree and the elements are all at the bottom of the tree, then we can compare the minimum of two items in parallel and the resultant minimum will be written to the parent. When this process is propagated up the tree the minimum of the group of elements will be found at the root.
The following is the complexity for solving the junction tree using message passing
We rewrite the formula used earlier to the following form. This is the eqn for a message to be sent from vertex v to w
    $\mu _{v,w}(p_{v\cap w})=\sum _{p_{v\setminus w}\in A_{S(v)\setminus S(w)}}\alpha _{v}(p_{v})\prod _{uadjv_{u\neq v}}\mu _{u,v}(p_{u\cap v})$ ----message equation
Similarly we rewrite the equation for calculating the state of vertex v as follows
    $\sigma _{v}(p_{v})=\alpha _{v}(p_{v})\prod _{u\operatorname {adj} v}\mu _{v,w}(p_{v\cap w})$
We first will analyze for the single-vertex problem and assume the target vertex is $v_{0}$ and hence we have one edge from $v$ to $v_{0}$. Suppose we have an edge $(v,w)$ we calculate the message using the message equation. To calculate $p_{u\cap v}$ requires
    $q_{v\setminus w}-1$
additions and
    $q_{v\setminus w}(d(v)-1)$
multiplications.
(We represent the $|A_{S(v)\ S(w)}|$ as $q_{v\setminus w}$.)
But there will be many possibilities for $x_{v\cap w}$ hence
$q_{v\cap w}{\stackrel {\mathrm {def} }{=}}|A_{S(v)\cap S(w)}|$ possibilities for $p_{v\cap w}$. Thus the entire message will need
    $(q_{v\cap w})(q_{v\setminus w}-1)=q_{v}-q_{v\cap w}$
additions and
    $(q_{v\cap w})q_{v\setminus w}.(d(v)-1)=(d(v)-1)q_{v}$
multiplications
The total number of arithmetic operations required to send a message towards $v_{0}$along the edges of tree will be
    $\sum _{v\neq v0}(q_{v}-q_{v\cap w})$
additions and
    $\sum _{v\neq v0}(d(v)-1)q_{v}$
multiplications.
Once all the messages have been transmitted the algorithm terminates with the computation of state at $v_{0}$. The state computation requires $d(v_{0})q_{0}$ more multiplications.
Thus number of calculations required to calculate the state is given as below
    $\sum _{v\neq v_{0}}(q_{v}-q_{v\cap w})$
additions and
    $\sum _{v\neq v_{0}}(d(v)-1)q_{v}+d(v_{0})q_{v_{0}}$
multiplications
Thus the grand total of the number of calculations is
    $\chi (T)=\sum _{v\in V}d(v)q_{v}-\sum _{e\in E}q_{e}$ ----$(1)$
where $e=(v,w)$ is an edge and its size is defined by $q_{v\cap w}$
The formula above gives us the upper bound.
If we define the complexity of the edge $e=(v,w)$ as
    $\chi (e)=q_{v}+q_{w}-q_{v\cap w}$
Therefore, $(1)$ can be written as
    $\chi (T)=\sum _{e\in E}\chi (e)$
We now calculate the edge complexity for the problem defined in Figure 1 as follows
    $\chi (1,2)=q_{2}+q_{2}q_{3}-q_{2}$
    $\chi (2,4)=q_{3}q_{4}+q_{2}q_{3}-q_{3}$
    $\chi (2,5)=q_{3}+q_{2}q_{3}-q_{3}$
    $\chi (4,8)=q_{4}+q_{3}q_{4}-q_{4}$
    $\chi (4,9)=q_{2}q_{4}+q_{3}q_{4}-q_{4}$
    $\chi (1,3)=q_{2}+q_{2}q_{1}-q_{2}$
    $\chi (3,7)=q_{1}+q_{1}q_{2}-q_{1}$
    $\chi (3,6)=q_{1}q_{4}+q_{1}q_{2}-q_{1}$
The total complexity will be $3q_{2}q_{3}+3q_{3}q_{4}+3q_{1}q_{2}+q_{2}q_{4}+q_{1}q_{4}-q_{1}-q_{3}-q_{4}$ which is considerably low compared to the direct method. (Here by direct method we mean by methods that do not use message passing. The time taken using the direct method will be the equivalent to calculating message at each node and time to calculate the state of each of the nodes.)
Now we consider the all-vertex problem where the message will have to be sent in both the directions and state must be computed at both the vertexes. This would take $O(\sum _{v}d(v)d(v)q_{v})$ but by precomputing we can reduce the number of multiplications to $3(d-2)$. Here $d$ is the degree of the vertex. Ex: If there is a set $(a_{1},\ldots ,a_{d})$ with $d$ numbers. It is possible to compute all the d products of $d-1$ of the $a_{i}$ with at most $3(d-2)$ multiplications rather than the obvious $d(d-2)$. We do this by precomputing the quantities$b_{1}=a_{1},b_{2}=b_{1}\cdot a_{2}=a_{1}\cdot a_{2},b_{d-1}=b_{d-2}\cdot a_{d-1}=a_{1}a_{2}\cdots a_{d-1}$ and $c_{d}=a_{d},c_{d-1}=a_{d-1}c_{d}=a_{d-1}\cdot a_{d},\ldots ,c_{2}=a_{2}\cdot c_{3}=a_{2}a_{3}\cdots a_{d}$ this takes $2(d-2)$ multiplications. Then if $m_{j}$ denotes the product of all $a_{i}$ except for $a_{j}$ we have $m_{1}=c_{2},m_{2}=b_{1}\cdot c_{3}$ and so on will need another $d-2$ multiplications making the total $3(d-2)$.
There is not much we can do when it comes to the construction of the junction tree except that we may have many maximal weight spanning tree and we should choose the spanning tree with the least $\chi (T)$ and sometimes this might mean adding a local domain to lower the junction tree complexity.
It may seem that GDL is correct only when the local domains can be expressed as a junction tree. But even in cases where there are cycles and a number of iterations the messages will approximately be equal to the objective function. The experiments on Gallager–Tanner–Wiberg algorithm for low density parity-check codes were supportive of this claim.