Junction tree algorithm

Also known as: clique tree

On this page 7

The junction tree algorithm is an exact inference method for probabilistic graphical models that converts a Bayesian network or Markov random field into a tree of clusters of variables, called a junction tree or clique tree, and then computes all marginal distributions by passing messages along that tree. Among probabilistic models, it is the standard general-purpose way to obtain exact posterior marginals in graphs that contain loops.

The algorithm generalizes belief propagation, which is exact only on trees, to arbitrary graphs. Its price is complexity: the cost grows exponentially with the treewidth of the graph, so it is practical for sparse, moderately sized models and intractable for densely connected ones.

How the junction tree algorithm works

The junction tree algorithm proceeds in a fixed sequence of graph transformations followed by message passing. Each step either restructures the graph so that a tree of cliques exists, or uses that tree to propagate evidence and compute marginals.

StageWhat happensResult
MoralizationParents of each node are connected and directions removedUndirected moral graph
TriangulationChords added so that every cycle longer than three has a chordChordal graph
Clique identificationMaximal cliques of the chordal graph are foundSet of clusters
Tree constructionCliques linked by a maximum-weight spanning tree on separator sizeJunction tree with separators
Potential initializationEach factor assigned to one clique containing its variablesClique potentials
Message passingCollect and distribute passes between neighboring cliquesCalibrated clique marginals
Main stages of the junction tree algorithm

Moralization applies only to directed models such as Bayesian networks; a Markov random field already starts undirected. Triangulation is the decisive step, because the fill-in edges it adds determine the size of the largest clique and therefore the cost of every later operation.

The running intersection property

A tree of cliques is a valid junction tree only if it satisfies the running intersection property: whenever a variable appears in two cliques, it must also appear in every clique on the path between them. This property guarantees that local consistency implies global consistency, so cliques that agree with their neighbors on shared variables also agree with every other clique.

Each edge of the tree carries a separator, the set of variables shared by the two cliques it connects. Messages are functions over separator variables. After a full collect and distribute schedule, often implemented as the Hugin or Shafer-Shenoy architecture, every clique potential is proportional to the joint marginal of its variables given the evidence.

ArchitectureStored quantitiesMain trade-off
HuginClique and separator potentials, updated by divisionFaster updates, more memory
Shafer-ShenoyOriginal factors and messages, no divisionLess memory, repeated multiplications
Lauritzen-SpiegelhalterClique potentials updated by divisionHistorical form, close to Hugin
Two classic message-passing architectures

Complexity and the role of treewidth

The cost of the junction tree algorithm is dominated by the largest clique. For discrete variables with k states, a clique of size w requires tables with k to the power w entries, so memory and time grow exponentially in w. The smallest achievable maximum clique size minus one is the treewidth of the graph.

Finding an optimal triangulation is NP-hard, so implementations use heuristics such as min-fill or min-degree elimination orderings. A good ordering can reduce clique sizes dramatically, while a poor one can make a tractable problem appear impossible. The same elimination ordering also governs the cost of variable elimination, which is closely related.

Graph structureTypical treewidthJunction tree feasibility
Chain or tree1Trivial, equals belief propagation
Sparse network with few loopsLow, about 2-10Fast and exact
Grid of n by n binary nodesAbout nFeasible only for small n
Densely connected modelClose to number of nodesIntractable, use approximation
How graph structure affects tractability

Comparison with other inference methods

The junction tree algorithm is one of several ways to answer probabilistic queries, and its main strength is that it computes all single-clique marginals at once with guaranteed exactness. The table compares it with neighboring methods in the same family.

MethodExact or approximateBest suited for
Junction tree algorithmExactAll marginals in low-treewidth graphs
Variable eliminationExactA single query or marginal
Belief propagationExact on trees onlyTree-structured or factor-tree models
Loopy belief propagationApproximateLarge sparse graphs with cycles
Gibbs samplingApproximate, asymptotically exactHigh-treewidth models
Variational inferenceApproximateLarge models needing speed over exactness
Junction tree algorithm and related inference methods

Practical use and common pitfalls

The junction tree algorithm remains the backbone of exact inference engines for Bayesian networks in diagnostics, reliability analysis and decision support. Its key practical advantage is compilation: the tree is built once, and then many evidence scenarios can be propagated quickly, which suits interactive what-if analysis.

Beyond computing marginals, the same structure supports most probable explanation queries by replacing summation with maximization in the messages, a variant often called max-product propagation. It also provides the expected sufficient statistics needed by the expectation-maximization algorithm when parameters are learned from incomplete data. In such learning loops the tree is compiled once and reused across iterations, since only the numerical potentials change while the graph structure stays fixed.

Choosing between exact and approximate inference should start with a treewidth estimate. If the largest clique table fits comfortably in memory, the junction tree algorithm is usually the safest choice because it removes approximation error from the analysis. When it does not fit, practitioners either simplify the model, for example by removing weak dependencies, or switch to sampling and variational methods.

  • Ignoring treewidth leads to memory exhaustion; estimate the largest clique before compiling.
  • Continuous variables require special handling, such as conditional Gaussian models, and mixed models can be fragile.
  • Numerical underflow appears in large tables; work in log space or renormalize messages.
  • Dynamic Bayesian networks unrolled over many time slices can create very wide cliques.

In simple terms

A network of uncertain facts can be tangled, with many loops. Answering questions about it directly is hard. The junction tree algorithm first groups related facts into small clusters. It then arranges those clusters into a tree with no loops.

Each cluster sends a short summary to its neighbors. After two sweeps across the tree, every cluster knows the effect of all the evidence. The answers are exact, not estimates. The catch is that very tangled networks produce huge clusters, and then the method becomes too slow.

A practical example

A reliability team at a power utility models a substation with a Bayesian network of about 60 binary variables: component states, sensor readings, weather conditions and alarm signals. Engineers want the probability of each component failure whenever a new combination of alarms appears, and they need exact numbers because the outputs feed maintenance decisions.

The team compiles the network into a junction tree using a min-fill triangulation. The largest clique contains 9 variables, so its table has 512 entries, which is trivial to store. Each new alarm pattern is entered as evidence and propagated in milliseconds. Every component marginal is available after one propagation, letting operators rank likely faults immediately instead of running separate queries for each component.