% 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 4.0: 75 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  = 15
 dim. of free   var  =  5
 12 linear variables from unrestricted variable.
 *** 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.6e+01|5.3e+03| 1.600000e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|0.992|0.987|3.9e-01|4.3e-01|6.4e+01| 2.312263e+01 -9.647124e+00| 0:0:00| chol  1  1 
 2|1.000|1.000|1.1e-06|1.0e-02|4.7e+00| 2.706490e+00 -1.936915e+00| 0:0:00| chol  1  1 
 3|0.995|0.724|3.8e-08|3.5e-03|7.1e-01|-1.054827e-01 -7.979247e-01| 0:0:00| chol  1  1 
 4|0.745|0.960|7.1e-09|2.4e-04|2.4e-01|-6.247315e-01 -8.670599e-01| 0:0:00| chol  1  1 
 5|0.982|0.975|2.2e-10|1.6e-05|5.4e-03|-7.468838e-01 -7.522799e-01| 0:0:00| chol  1  1 
 6|0.989|0.989|3.5e-11|1.2e-06|6.1e-05|-7.499647e-01 -7.500254e-01| 0:0:00| chol  1  1 
 7|0.989|0.989|1.1e-11|1.5e-06|7.3e-07|-7.499996e-01 -7.500003e-01| 0:0:00| chol  2  2 
 8|0.997|0.989|5.7e-13|1.8e-08|9.1e-09|-7.500000e-01 -7.500000e-01| 0:0:00| chol  2  2 
 9|0.998|0.989|1.7e-13|2.3e-10|1.1e-10|-7.500000e-01 -7.500000e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value = -7.50000000e-01
 dual   objective value = -7.50000000e-01
 gap := trace(XZ)       = 1.07e-10
 relative gap           = 4.27e-11
 actual relative gap    = 4.22e-11
 rel. primal infeas (scaled problem)   = 1.71e-13
 rel. dual     "        "       "      = 2.28e-10
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 1.1e+00, 8.3e-01, 2.8e+00
 norm(A), norm(b), norm(C) = 9.1e+00, 2.0e+00, 2.3e+00
 Total CPU time (secs)  = 0.25  
 CPU time per iteration = 0.03  
 termination code       =  0
 DIMACS: 1.7e-13  0.0e+00  3.8e-10  0.0e+00  4.2e-11  4.3e-11
-------------------------------------------------------------------
 
------------------------------------------------------------
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