
function [connrules,list_of_high_corr_spectra, idxV] ...
             =LibraryPartitioning_FindForbiddenPairs(libsall, threshold)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% General description:
%
%
% This program performs two major tasks:
% 1. Extract components that cause single correlation values that are greater
%    than 'threshold'.
% 2. Find forbidden pairs for the library. 
%
% Input:
%      libsall - The whole library to be partitioned
%      threshold - The threshold for single correlations and canonical 
%                   correlations.
% Output:
%      1. connrules(m,m) - Forbidden pairs.  If connrules(i,j)=1, 
%                          then (i,j) is a forbidden pair.
%      2. idxV -- Indices of reserved components after extracting high
%                 correlated components.
%      3. list_of_high_corr_spectra - list of pairs with correlation 
%         greater than threshold. The firt column is indices of components 
%         that have been removed from the library. The second column is the
%         indices of components that are kept. 
%         In detection, when a component whose index is in the second column
%         appears, a second stage classification is performed to identify
%         the target between this component and its counterpart.
%
% Written by: Wei Wang
% 8/21/2007
%
% Copyright(c) 2007 by
% Machine Learning for Signal Processing Laboratory
% University of Maryland, Baltimore County
%
% Permission to use, copy, modify, and distribute this software for any
% purpose without fee is hereby granted, provided that this entire notice
% is included in all copies of any software which is or includes a copy
% or modification of this software and in all copies of the supporting
% documentation for such software.
% THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
% WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR DOES NOT MAKE ANY
% REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
% OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[n,m]=size(libsall);
for i=1:m
    libsall(:,i)=libsall(:,i)/norm(libsall(:,i));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Merge those correlated spectra above threshold    %%%%%
%%%% data saved at list_of_high_corr_spectra.mat       %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
list_of_high_corr_spectra=0;
count_of_high_corr_spectra=0;
libs=libsall;
tlist=[1:m];
while 1            
    cors=corr(libs);
    cors=cors-eye(size(libs,2));
    cors=abs(cors);
    
    corrmax=max(cors);
    maxcorr=max(corrmax);
    
    if maxcorr<threshold
        break;
    end
    
    poses=find(corrmax==max(corrmax));
    pos=poses(1);
    
    curcorr=cors(pos,:);
    id=find(curcorr==max(curcorr));
    count_of_high_corr_spectra=count_of_high_corr_spectra+1;
    list_of_high_corr_spectra(count_of_high_corr_spectra,1)=tlist(id);  % index of spectrum which is to be removed
    list_of_high_corr_spectra(count_of_high_corr_spectra,2)=tlist(pos); % index of spectrum highly correlated 
                                                                 % with the removed spectrum                                                                    
    libs(:,id)=[];
    tlist(id)=[];
    
end
%save list_of_high_corr_spectra_cc95.mat list_of_high_corr_spectra libsall
% load list_of_high_corr_spectra_cc90.mat;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove the first elemenet in each high correlated pair from the library %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idxV=[1:m]; 
idxV(list_of_high_corr_spectra(:,1))=[];
libs=libsall(:,idxV);
m=size(libs,2);


% high_corr_list=LibraryPartitioning_FindHighCorrPairs(libsall, threshold);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Find all pairs that cause canonical correlation above   %%%%%
%%%%     the given threshold                                 %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
connrules=zeros(m);
 
tpos=round(m/10*[1:10]);
nt=1;
for i=1:m
    restlibids=[1:i-1 i+1:m];
    connrules=coloring_NCC_findrules(connrules, restlibids, i, libs, threshold);
    
    if nt<=10 & i==tpos(nt)-nt % a ratio for displaying progress since the later the faster
        sp=sprintf('\t%s %d%s','finished',nt*10,'%');
        disp(sp);
        nt=nt+1;
    end
end



