Structure of growing networks with no preferential attachment

(*author: Pablo Moriano (mpabloandres@hotmail.com)*)
(*This notebook simulates the dynamics of the model presented at the ACC2013*)

ClearAll["Global`*"] 
(*Function definition*)
(*calculate the local clustering coefficient*)

localClustering[directedInLinks_,directedOutLinks_]:=(Module[
{totalDegree = Table[0,{Length[directedOutLinks]}], (*Incoming plus outcoming links*)
totalTriplets = Table[0,{Length[directedOutLinks]}], (*Possible number of creative triplets*)
totalClosed = Table[0,{Length[directedOutLinks]}], (*Number of closed triplets*)
totalCluster =Table[0,{Length[directedOutLinks]}],
first={},second={},selected={},i,j,k}, (*Clustering coefficient of every node*)

For[i=1,i≤Length[directedOutLinks],i++, (*every node*)
totalDegree[[i]]=Length[directedInLinks[[i]]]+Length[directedOutLinks[[i]]];
totalTriplets[[i]]=(totalDegree[[i]](totalDegree[[i]]-1))*0.5;

If [totalTriplets[[i]]!=0,

(*configuration 1: there exist two outgoing neighbors (either direction*)
For[j=1,j≤Length[directedOutLinks[[i]]],j++,(*every outgoing neighbord*)
first = directedOutLinks[[i,j]];
selected = Cases[directedOutLinks[[i]],Except[first]];
For[k=1,k≤Length[selected],k++,(*every outgoing neighbord minus itself*)
If[MemberQ[directedInLinks[[selected[[k]]]],first],totalClosed[[i]]++];
] 
] ;

(*configuration 2*)
For[j=1,j≤Length[directedInLinks[[i]]],j++,
first = directedInLinks[[i,j]];
selected = Cases[directedInLinks[[i]],Except[first]];
For[k=1,k≤Length[directedOutLinks[[i]]],k++,
second=directedOutLinks[[i,k]];
If[MemberQ[directedInLinks[[second]],first],totalClosed[[i]]++];
] 
For[k=1,k≤Length[selected],k++,(*every incoming neighbord minus itself*)
If[MemberQ[directedInLinks[[selected[[k]]]],first],totalClosed[[i]]++];
]
] ;
totalCluster[[i]]=((totalClosed[[i]])/(totalTriplets[[i]])),totalCluster[[i]]=0]
];
Return[totalCluster];
])

(* Verify whether it is possible to complete a triad *)
makePossibleTriangle[t_,nodes_,directedInLinks_]:=Module[{acum=0},
For[i=1,i≤Length[nodes],i++,
If[MemberQ[directedInLinks[[nodes[[i]]]],t],acum=acum+1]
] ;(*end For i*)
If[acum==Length[nodes],Return[False],Return[True]]
]

analyticalClustering[inDegree_,m_,p_]:=2(( inDegree + m p (1+(1-(1/(p^2)))Log[((inDegree + m(((1+p)/(p ))))/(m(((1+p )/p))))]))/((inDegree+m(1+p))(inDegree+m(1+p)-1)));

analyticalDegree[inDegree_,m_,p_]:=((((((m(1+(1/( p))))/(inDegree+ m(1+(1/( p)))))))^(1+(1/( p)))));

networkSimulation[n0_,p_,m_]:=(
output={};
Module[{
directedInLinks=Table[{},{n0+T}],
directedOutLinks=Table[{},{n0+T}],
inDegree=Table[0,{n0+T}],
outDegree=Table[0,{n0+T}],
graph=RandomGraph[BernoulliGraphDistribution[n0,.25],DirectedEdges->True,GraphLayout->"CircularEmbedding",ImageSize->1.4{200,200}],
parents={},
nodesToSearch={},
emdist={},
clustering={},
(*output=Table[{},{T}];*)
},


inDegree[[1;;n0]]= VertexInDegree[graph];
outDegree[[1;;n0]]= VertexOutDegree[graph];
directedOutLinks[[1;;n0]] = Cases[IncidenceList[graph,#],#\[DirectedEdge]x_->x] &/@ VertexList[graph];
directedInLinks[[1;;n0]] =  Cases[IncidenceList[graph,#],x_\[DirectedEdge]#->x] &/@ VertexList[graph];

ticks=Prepend[Partition[Range[T],timeStep][[All,-1]],1];

For[t=n0+1,t≤(n0+T),t++,

parents=RandomSample[Range[t-1],m];

For[k=1,k≤m,k++,
AppendTo[directedOutLinks[[t]],parents[[k]]];
AppendTo[directedInLinks[[parents[[k]]]],t];
inDegree[[parents[[k]]]]++;
outDegree[[t]]++;

nodesToSearch=Complement[directedOutLinks[[parents[[k]]]],parents];

If[makePossibleTriangle[t,nodesToSearch,directedInLinks]&&Length[nodesToSearch]≠0,

FriendofFriend=RandomSample[nodesToSearch,1];

While[MemberQ[directedOutLinks[[t]],FriendofFriend[[1]]]&&!MemberQ[parents,FriendofFriend[[1]]],FriendofFriend=RandomSample[nodesToSearch,1]];

If[RandomReal[]≤p,

AppendTo[directedOutLinks[[t]],FriendofFriend[[1]]];
AppendTo[directedInLinks[[FriendofFriend[[1]]]],t];
inDegree[[FriendofFriend[[1]]]]++;
outDegree[[t]]++;
]
]
];
If[MemberQ[ticks+n0,t],
(*graph=GraphPlot[RandomGraph[BernoulliGraphDistribution[1000,.3]]];*)
(*g=Graph[Union[Sort/@Flatten[edges]]]; - do not allow reciprocal links*)
graph=GraphPlot[Graph[Flatten[Table[DirectedEdge[i,#] &/@ directedInLinks[[i]],{i,1,Length[directedInLinks]}]]], AspectRatio->1,ImageSize->1.4{200,200}];
(*graph=SetProperty[graph,VertexLabels-> {Placed["Name",Tooltip]}];*)
emdist=EmpiricalDistribution[inDegree];
clustering=localClustering[directedInLinks,directedOutLinks];
AppendTo[output,{graph,emdist,clustering,inDegree}];
(*output[[t-n0]]={graph,emdist,clustering,inDegree};*)
]
]
];
output
)

T=5000;(*total number of network generations*)
timeStep=500;
n0=10; p=.8;m=1;
output=networkSimulation[n0,p,m];
graph=output[[All,1]];
emdist=output[[All,2]];
clustering=output[[All,3]];
inDegree=output[[All,4]];

SlideView[Table[Row[{Column[{Style["network with n="<>ToString[#[[1]]]<>", p="<>ToString[#[[2]]]<>", m="<>ToString[#[[3]]],14]&@ {(i-1) timeStep + n0, p, m},graph[[i]]},Alignment->Center], Column[{"   ",Style["degree distribution",14],
Show[
ListLogLogPlot[{Table[{x,1-CDF[emdist[[i]],x]},{x,DistributionDomain[emdist[[i]]]}]},PlotRange->{{0.1,Max[inDegree]},{0.0001,1}},PlotMarkers->{Graphics[{Red,Circle[{0,0},1]}],0.03},AspectRatio->1,ImageSize->1.4{200,200}],
ListLogLogPlot[Table[{j,analyticalDegree[j,m,p]},{j,Union[inDegree[[i]]]}],Joined->True,PlotStyle-> Black]],
Style["degree k",14]},Alignment->{Center}], Column[{"   ",Style["local clustering ",14],
Show[
ListLogLogPlot[Table[{x,Mean[Pick[clustering[[i]],inDegree[[i]],x]]},{x,Union[inDegree[[i]]]}],PlotRange->{{0.1,Max[inDegree]},{0.01,1}},PlotMarkers->{Graphics[{Blue,Circle[{0,0},1]}],0.03},AspectRatio->1,ImageSize->1.4{200,200}],
ListLogLogPlot[Table[{j,analyticalClustering[j,m,p]},{j,Union[inDegree[[i]]]}],Joined->True,PlotStyle-> Black]],
Style["degree k",14]},Alignment->{Center}]
}],{i,2,T/timeStep}]]