File Exchange

image thumbnail

K2 algorithm for learning DAG structure in Bayesian network

version 1.7 (9.84 KB) by

This is the code of Cooper's K2 algorithm proposed in 1992, quick and convenient for using.

3 Ratings



View License

K2 algorithm is the most famous score-based algorithm in Bayesian netowrk in the last two decades. Specifically, it recovers the underlying distribution in the form of DAG efficiently. For details, please refer to Cooper's published paper[1]

Please start from "ControlCentor.m", here is a simple example for understanding how to use our code.

If there is any question, please let me know, i will help you as soon as possible.

I use rewritten the K2 by mex programming, if you know how to compile it, please try K2.c because this code is able to handle the variables as large as 1000 variables, much efficient. It's tested under linux both 32-bit and 64-bit.

[1] G. Cooper and E. Herskovitz, A Bayesian method for the induction of probabilistic networks from data, Machine Learning
9 (1992), 330–347.

If you use this code, please cite our paper:

Bielza, C., Li, G. & Larrañaga, P. (2011). Multi-Dimensional Classification with Bayesian Networks. International Journal of Approximate Reasoning, 52, 705-727.

Comments and Ratings (8)

When simulating the alarm network this K2 algorithm does not reproduce the results in the original K2 paper. It seems to require that all nodes be connected to the first variable in the ordering. Am I doing something wrong?

Enrique Mote

I have a problem with the ConstructLGObj(OriginalSample). The matlab console throws an error like "Undefined function 'ConstructLGObj' for input arguments of type 'double'."


munjal (view profile)

I want to use K2 algorithm for prediction.
I got the bayesian network for my data.

Can you please tell me how can I use this bayesian network for the future prediction , because in your network the conditional probability tables are missing.

Thank You.

fares saab

i have downloaded your application, i tried to run it but it gave me an error while create the object LGObj = ConstructLGObj( Sample); % construct an object
can you please send me the sample data to test it and also if you please send me your email
thank you in advance
my email is


I'm studying about learning temporal node bayesian networks. From the paper i got before, i get that ordering is doing by partial set of ordering, for example we order {1}, {3 5 6} and {2 4}. In your code i get that ordering is given in one time (1 3 5 6 2 4). I've been trying to make a modification in your code but i failed. How can i modif it? Is there any suggestion? Thanks

Guangdi Li

Guangdi Li (view profile)

Dear Pedro:

I advise you to use software Graphviz, or draw in photoshop, or powerpoint.

Regarding the mapping of nodes in matlab, it takes extra effort to make it beautiful. You can refer to my paper: Bielza, C., Li, G. & Larrañaga, P. (2011). Multi-Dimensional Classification with Bayesian Networks. International Journal of Approximate Reasoning, 52, 705-727.

thanks for your interest!


Pedro (view profile)

Hi, is there a way to name the nodes? Not having them show like node 1, etc. Thanks

Guangdi Li

Guangdi Li (view profile)

There is one file named 'DimensionRangeValue.m' is missing, I am sorry for any inconvenience of you working. The updated version will be published in next week (March 20) after Matlab Center checks it. Enjoying you using my codes.



update description


improve the efficiency of code by mex programming.


Update the code to output the K2 score upon each node


I improve the efficiency of this algorithm again.


One file is missing, sorry, I add it again.

MATLAB Release
MATLAB 7.6 (R2008a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today