% Boyd & Vandenberghe "Convex Optimization"
% Joëlle Skaf - 09/26/05
%
% The 'fastest mixing Markov chain problem' is to find a transition
% probability matrix P on a graph E that minimizes the mixing rate r, where
% r = max{ lambda_2, -lambda_n } with lambda_1>=...>=lambda_n being the
% eigenvalues of P.

% Generate input data
n = 5;
E = [0 1 0 1 1; ...
     1 0 1 0 1; ...
     0 1 0 1 1; ...
     1 0 1 0 1; ...
     1 1 1 1 0];

% Create and solve model
cvx_begin
    variable P(n,n) symmetric
    minimize(norm(P - (1/n)*ones(n)))
    P*ones(n,1) == ones(n,1);
    P >= 0;
    P(E==0) == 0;
cvx_end
e = flipud(eig(P));
r = max(e(2), -e(n));

% Display results
disp('------------------------------------------------------------------------');
disp('The transition probability matrix of the optimal Markov chain is: ');
disp(P);
disp('The optimal mixing rate is: ');
disp(r);
 
Calling SDPT3: 68 variables, 9 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  9
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
 dim. of linear var  =  8
 dim. of free   var  =  5 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|5.1e+01|2.1e+01|3.8e+03| 1.600000e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|0.929|0.955|3.7e+00|1.0e+00|1.7e+02| 2.267598e+01 -9.470840e+00| 0:0:00| chol  1  1 
 2|1.000|0.951|1.8e-06|6.0e-02|1.9e+01| 9.706394e+00 -7.808212e+00| 0:0:00| chol  1  1 
 3|0.994|0.277|2.7e-06|4.4e-02|7.5e+00| 6.612292e-01 -6.215577e+00| 0:0:00| chol  1  1 
 4|1.000|0.873|5.0e-06|5.6e-03|9.5e-01| 3.400135e-02 -8.928562e-01| 0:0:00| chol  1  1 
 5|0.677|0.489|1.6e-06|2.9e-03|4.5e-01|-5.555305e-01 -9.948414e-01| 0:0:00| chol  1  1 
 6|0.986|0.940|8.9e-08|1.7e-04|2.0e-02|-7.414098e-01 -7.608264e-01| 0:0:00| chol  1  1 
 7|0.988|0.988|4.8e-09|2.2e-06|2.4e-04|-7.498951e-01 -7.501312e-01| 0:0:00| chol  1  1 
 8|0.989|0.989|3.1e-10|5.3e-06|8.3e-06|-7.499988e-01 -7.500014e-01| 0:0:00| chol  1  2 
 9|1.000|0.989|1.1e-12|1.8e-07|2.6e-07|-7.499999e-01 -7.500000e-01| 0:0:00| chol  2  2 
10|1.000|0.989|9.6e-13|5.8e-09|7.9e-09|-7.500000e-01 -7.500000e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -7.49999998e-01
 dual   objective value = -7.50000000e-01
 gap := trace(XZ)       = 7.90e-09
 relative gap           = 3.16e-09
 actual relative gap    = 8.22e-10
 rel. primal infeas     = 9.59e-13
 rel. dual   infeas     = 5.78e-09
 norm(X), norm(y), norm(Z) = 1.1e+00, 8.3e-01, 2.8e+00
 norm(A), norm(b), norm(C) = 1.0e+01, 2.0e+00, 2.5e+00
 Total CPU time (secs)  = 0.21  
 CPU time per iteration = 0.02  
 termination code       =  0
 DIMACS: 9.6e-13  0.0e+00  1.1e-08  0.0e+00  8.2e-10  3.2e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.75
 
------------------------------------------------------------------------
The transition probability matrix of the optimal Markov chain is: 
         0    0.3750         0    0.3750    0.2500
    0.3750         0    0.3750         0    0.2500
         0    0.3750         0    0.3750    0.2500
    0.3750         0    0.3750         0    0.2500
    0.2500    0.2500    0.2500    0.2500         0

The optimal mixing rate is: 
    0.7500