function [teams1,nteams1,exitcode]=divtiebreaker(teams0,nteams0,a,conf,div,wltpct,cwltpct,dwltpct,sov,sos) % Copyright 2006 by William S. Krasker % Given a set of nteams0 teams from the same division, whose indices are in the array teams0, this % function runs through a sequence of criteria until one is found for which not all nteams0 teams % are equally good. The function then returns the number nteams1 of teams that are tied for the % best according to that criterion, the array teams1 containing their indices, and the exitcode % showing the criterion that achieved separation. % % The criteria are: % % 0. overall won-lost-tied percentage, % 1. won-lost-tied percentage in games among the teams, % 2. won-lost-tied percentage in the division, % 3. won-lost-tied percentage versus common opponents, % 4. won-lost-tied percentage in the conference, % 5. strength of victory, and % 6. strength of schedule. % % (Criteria 1 through 6 are called tiebreakers.) If none of these criteria differentiates among the % teams, then the function arbitrarily eliminates the last team. (Notice that if nteams0=2, then % the second criterion reduces to head-to-head.) teams1=zeros(4,1); for ii=1:1 %-------------------------------------------------- % find best win percentage v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,wltpct(teams0(i))); end % find teams with best win percentage k=0; for i=1:nteams0 if wltpct(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage nteams1=k; teams1(1:k)=v(1:k); exitcode=0; break end %-------------------------------------------------- % compute win percentages in games among the teams w=zeros(4,1); for j=1:256 for i=1:nteams0 for k=1:nteams0 if (a(j,1)==teams0(i)) & (a(j,2)==teams0(k)) if a(j,3)==1 w(i)=w(i)+1; elseif a(j,3)==0 w(k)=w(k)+1; else w(i)=w(i)+0.5; w(k)=w(k)+0.5; end end end end end w=w/( 2*sum(w(1:nteams0))/nteams0 ); % find best win percentage in games among the teams x=max(w(1:nteams0)); % find teams with best win percentage in games among the teams k=0; v=zeros(4,1); for i=1:nteams0 if w(i)==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage in games among the teams nteams1=k; teams1(1:k)=v(1:k); exitcode=1; break end %-------------------------------------------------- % find best win percentage in the division v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,dwltpct(teams0(i))); end % find teams with best win percentage in the division k=0; for i=1:nteams0 if dwltpct(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage in the division nteams1=k; teams1(1:k)=v(1:k); exitcode=2; break end %-------------------------------------------------- % find common opponents y=zeros(32,32); for j=1:256 y(a(j,1),a(j,2))=y(a(j,1),a(j,2))+1; y(a(j,2),a(j,1))=y(a(j,2),a(j,1))+1; end z=ones(32,1); p=zeros(32,1); for i=1:nteams0 z=z.*y(:,teams0(i)); % positive elements of z correspond to common opponents p(teams0(i))=1; % positive elements of p correspond to the teams under consideration end % find win percentage against common opponents w=zeros(32,1); num=zeros(32,1); for j=1:256 if (p(a(j,1))>0) & (z(a(j,2))>0) num(a(j,1))=num(a(j,1))+1; if a(j,3)==1 w(a(j,1))=w(a(j,1))+1; elseif a(j,3)==-1 w(a(j,1))=w(a(j,1))+0.5; end elseif (z(a(j,1))>0) & (p(a(j,2))>0) num(a(j,2))=num(a(j,2))+1; if a(j,3)==0 w(a(j,2))=w(a(j,2))+1; elseif a(j,3)==-1 w(a(j,2))=w(a(j,2))+0.5; end end end q=zeros(4,1); for i=1:nteams0 q(i)=w(teams0(i))/num(teams0(i)); end % find best win percentage against common opponents x=max(q(1:nteams0)); % find teams with best win percentage against common opponents k=0; v=zeros(4,1); for i=1:nteams0 if q(i)==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage against common opponents nteams1=k; teams1(1:k)=v(1:k); exitcode=3; break end %-------------------------------------------------- % find best win percentage in the conference v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,cwltpct(teams0(i))); end % find teams with best win percentage in the conference k=0; for i=1:nteams0 if cwltpct(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on win percentage in the conference nteams1=k; teams1(1:k)=v(1:k); exitcode=4; break end %-------------------------------------------------- % find best strength of victory v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,sov(teams0(i))); end % find teams with best strength of victory k=0; for i=1:nteams0 if sov(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on strength of victory nteams1=k; teams1(1:k)=v(1:k); exitcode=5; break end %-------------------------------------------------- % find best strength of schedule v=zeros(4,1); x=0; for i=1:nteams0 x=max(x,sos(teams0(i))); end % find teams with best strength of schedule k=0; for i=1:nteams0 if sos(teams0(i))==x k=k+1; v(k)=teams0(i); end end if k < nteams0 % tie broken based on strength of schedule nteams1=k; teams1(1:k)=v(1:k); exitcode=6; break end %-------------------------------------------------- % if all else fails, just drop the last team on the list nteams1=nteams0-1; teams1=teams0; exitcode=7; end