About this blog

I feel this blog as a reflection of my thoughts to myself , and sometimes as a public diary, and the is my only friend to share my thoughts who says never a "oh no! ,you shouldn't....That is boring...."

Octave Matlab code Two friends wanted to meet for 1 hour. at least ,over a time-window of N hours

The method of solving is brute-force and the codes involved were optimized at my best.

All possible configurations of hours(n) spent over (N hours window) by each friend is given by either combR or comb.
Octave code for the subroutine : combR.m (depends on nested looping ann optimization)
  Octave code for the subroutine : comb.m (So many for loops and functions were used finally the result will same as combR)
Surprisingly, both the methods were taking nearly equal time for smaller N values.
   The final solution can be sought from friendsMeet(N) programme.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Codes start here------>>>>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$$$$$$$$$$$$$
combR.m code: 
$$$$$$$$$$$$$
function X=combR(N,n,Xpre)
if n>N
  return ;
endif
if (n==1)
    X=eye(N);
else
    if nargin==3
     X=Xpre;
    else
     X=combR(N,n-1);
    endif
    Y=[ ];
    for col=1:size(X,2)
      [rowidx]=find(X(:,col)==0);
      for g=1:length(rowidx)
        Y=[Y X(:,col)];
        Y(rowidx(g),end)=1;
      endfor
    endfor
    X=unique(Y',"rows")';
endif
endfunction

$$$$$$$$$$$$$
comb.m   code: 
$$$$$$$$$$$$$
function [X]=comb(N, n)
%% Generates all possible combinations for N  choose n
if n>N
  return ;
endif
X=eye(N);
for B=1:n
  for i=1:size(X,2)
    for j=1:N
      if(X(j,i)==1)
        continue
      endif
      X=[X, X(:,i)];
      X(j,end)=1;
    endfor
  endfor
  [idx]=find(sum(X,1)==B);
  X=X(:,idx);
  X=unique(X',"rows")';
endfor
endfunction

$$$$$$$$$$$$$
main function friendsMeet.m   code: 
$$$$$$$$$$$$$
function H=friendsMeet(N)
%% Find number of hours each, two friends should wait
%% at a location so that they can meet for at least
%% for one hour
%% Input: (N) Available time window for both.
%% Each friend spends time of integer
%% hours starting from an integer clock hour.

%% The technique is Brute force or Trail-n-Error
if nargin==0
  N=8;
endif
M=X=[];

for i=1:N
 if i==1
   Y=combR(N,i);
 else
   Y=combR(N,i,Y);
 endif
 X=[X, Y];
endfor

## j=0;
## for a=1:size(X,2)
##   for b=1:size(X,2)
##     M(++j)=X(:,a)'*X(:,b);
##     Hc(j)=min(sum(X(:,a)),sum(X(:,b)));
##   endfor
## endfor
 M=(X'*X)(:);
 Hc=full(sum(X));
 Hc=min(Hc,Hc')(:); %%Symmetric matrix output
 %hist(Hc(M>0))
 H=mean(Hc(M>0));
 V=std(Hc(M>0),1);
 printf("For %f%% chances Spend atleast %d hours\n", 99.7, H+3*V);
 printf("For %f%% chances Spend atleast %d hours\n", 95, H+2*V);
 printf("For %f%% chances Spend atleast %d hours\n", 68, H+V);
endfunction