function ypred = algoOVO(Xtrain, ytrain, Xtest, C, B)

if(~exist('C','var'))
    C = 1;
end

if(~exist('B','var'))
    B = 1;
end

s = 3;

Ztrain = bsxfun(@rdivide,Xtrain,max(Xtrain));
Ztest = bsxfun(@rdivide,Xtest,max(Xtrain));

classes = size(ytrain,2);

params = sprintf('-s %d -c %f -B %f -q',s,C,B);
model = cell(classes);
for i=1:classes
    for j=1:i-1
        inz = ytrain(:,i)>0.5&ytrain(:,j)<0.5;
        jnz = ytrain(:,j)>0.5&ytrain(:,i)<0.5;
        
        label_vector = [ones(sum(inz),1);zeros(sum(jnz),1)];
        instance_matrix = [Ztrain(inz,:);Ztrain(jnz,:)];
        
        model{i,j} = train(label_vector, instance_matrix, params);
    end
end

ypred = zeros(size(Ztest,1),classes);
for i=1:classes
    for j=1:i-1
        [~,~,values] = predict(zeros(size(Xtest,1),1),Xtest,model{i,j});
        ypred(values>1,i) = ypred(values>1,i) + 1;
        ypred(values<-1,j) = ypred(values<-1,j) + 1;
        ypred(abs(values)<=1,[i,j]) = ypred(abs(values)<=1,[i,j]) + 1;
    end
end

%fprintf('%.4f %.4f ',C,B);

end

