function [cost_tran, cost_state, state, tran, trans_list]=fsm_sim(connectionMat, states, k)
% INPUT: fsm_connect - connection matric; Ecost, k - max itteration
% OUTPUT: 

if ~exist('connectionMat','var') || ~exist('states','var'),
    %print('Using Default Data');
    connectionMat = [1 1 0 0; 1 0 1 0; 0 1 0 1; 0 0 1 1];
    states = [bin2dec('00') bin2dec('01') bin2dec('10') bin2dec('11')];
end;

if ~exist('k','var')||isempty(k),
    k=500;
end;

StateEncCost = arrayfun(@(x)eff_ham(x), states);

%Compute the Connection Matrix Cost
connectCost = zeros(size(connectionMat));
for i = 1:size(connectionMat,1)
    for j = (i+1):size(connectionMat,2)
        connectCost(i,j) = eff_ham(bitxor(states(i), states(j)));
    end
end
connectCost = (connectCost + connectCost').*connectionMat;

%Perfom the Simulation

curr_state = 1; %randi(length(connectionMat),1,1);
cost_tran = zeros(1,k);
cost_state = cost_tran;

trans_list = cost_tran;
state = cost_tran;

for i = 1:k
    inx= find(connectionMat(curr_state,:));
    %TODO: Computing the next state probability based on a weight factor
    next_state = inx(randi(length(inx),1,1));
    if (next_state == curr_state)
       trans_list(i) = (curr_state > 1);       
    else
       trans_list(i) = (next_state > curr_state);    
    end

    cost_tran(i) = connectCost(curr_state, next_state);
    tran(i) = pow2(curr_state)+pow2(next_state);
    cost_state(i) = StateEncCost(next_state);
    curr_state = next_state;
    state(i) = curr_state;
end

