function [t]=timeOfLink(deg,labels,node1,node2 )
%TIMEOFLINK The first time two nodes establish a connecting path in a time-evolving graph.
%
% T=TIMEOFLINK(DEGREE,LABELINDEX,NODE1,NODE2)
%
% Given cell arrays DEGREE and LABELINDEX of length D such that DEGREE{d}
% is the degree separation matrix describing the network after the dth unit of time
% and LABELINDEX{d} is a vector holding the index-label information
% corresponding to DEGREE{d}, TIMEOFLINK ouputs the first time T, 1<=T<=D,
% at which nodes NODE1 and NODE2 establish a connecting path. If NODE1
% equals NODE2, or NODE1 never establishes a connection with NODE2, T=0.
%
% Works for directed and undirected, weighted and unweighted graphs.
% In the directed graph case, NODE1 is the from node.
%
% This function was designed to be used with adjacencyMatrix.m and degreeSeparation.m
% both written by the same author. degreeSeparation.m should not be used
% for a weighted graph but the matrix generated by this function can
% still be the input to TIMEOFLINK for accurate results.
t=0;
if node1~=node2
D=length(deg);
found=0;
d=1;
node_labels=[node1;node2];
while found==0 && d0
if d==1
t=d;
found=1;
else
[tf,ind]=ismember(node_labels,labels{d-1});
if length(find(tf==1))~=2
t=d;
found=1;
else
test_connected_yesterday=deg{d-1}(ind(1),ind(2));
if test_connected_yesterday==0
t=d;
found=1;
end
end
end
end
end
d=d+1;
end
end