Create graph

N1 = 5;
N2 = 5;

p = 1;
q = 0.2;

creategraph[N1_,N2_,p_,q_] :=
Module[{links,i,j,Asparse},
links = {};
Do[
Do[
If[RandomReal[]≤p,
AppendTo[links,{i,j}->1];
AppendTo[links,{j,i}->1]
],
{j,i+1,N1}
],
{i,1,N1-1}
];
Do[
Do[
If[RandomReal[]≤p,
AppendTo[links,{i,j}->1];
AppendTo[links,{j,i}->1]
],
{j,i+1,N1+N2}
],
{i,N1+1,N1+N2-1}
];
Do[
Do[
If[RandomReal[]≤q,
AppendTo[links,{i,j}->1];
AppendTo[links,{j,i}->1]
],
{j,N1+1,N1+N2}
],
{i,1,N1}
];
Asparse =
SparseArray[links,{N1+N2,N1+N2}];
Normal[Asparse]
];

Amat = creategraph[N1,N2,p,q];
MatrixForm[Amat]

Assign features

d = 2;

assignfeatures[N1_,N2_,d_] :=
Module[{i,flist},
flist = {};
Do[
sex = Which[RandomReal[]<1/2,0,True,1];
x = RandomReal[1,d];
AppendTo[flist,{sex,x}],
{i,1,N1+N2}
];
flist
];

flist = assignfeatures[N1,N2,d]

Draw graph

plot1points =
Table[
Which[
flist[[i,1]] == 0,{Red,Text[Style["♂",Large],flist[[i,2]]]},
True,{Red,Text[Style["♀",Large],flist[[i,2]]]}
],
{i,1,N1}
];
plot2points =
Table[
Which[
flist[[i,1]] == 0,{Blue,Text[Style["♂",Large],flist[[i,2]]]},
True,{Blue,Text[Style["♀",Large],flist[[i,2]]]}
],
{i,N1+1,N1+N2}
];
plotgraph ={LightGray};
Do[
Do[
If[Amat[[i,j]] == 1,
AppendTo[plotgraph,Line[{flist[[i,2]],flist[[j,2]]}]]
],
{j,i+1,N1+N2}
],
{i,1,N1+N2-1}
];

Graphics[
{{White,Rectangle[{0,0},{1,1}]},
plotgraph,
plot1points,
plot2points
},
AspectRatio->Automatic
]

Add edges

ϵ = 0.3;

addedges[Amat_,flist_,ϵ_]:=
Module[{Bmat,i,j},
Bmat = Amat;
Do[
Do[
If[Norm[flist[[i,2]]-flist[[j,2]]]<ϵ,
Bmat = ReplacePart[Bmat,{i,j}->1];
Bmat = ReplacePart[Bmat,{j,i}->1]
],
{j,N1+1,N1+N2}
],
{i,1,N1}
];
Bmat
];

Bmat = addedges[Amat,flist,ϵ];
Bmat == Amat

Draw graph again

plot1points =
Table[
Which[
flist[[i,1]] == 0,{Red,Text[Style["♂",Large],flist[[i,2]]]},
True,{Red,Text[Style["♀",Large],flist[[i,2]]]}
],
{i,1,N1}
];
plot2points =
Table[
Which[
flist[[i,1]] == 0,{Blue,Text[Style["♂",Large],flist[[i,2]]]},
True,{Blue,Text[Style["♀",Large],flist[[i,2]]]}
],
{i,N1+1,N1+N2}
];
plotgraph ={LightGray};
Do[
Do[
If[Bmat[[i,j]] == 1,
AppendTo[plotgraph,Line[{flist[[i,2]],flist[[j,2]]}]]
],
{j,i+1,N1+N2}
],
{i,1,N1+N2-1}
];

Graphics[
{{White,Rectangle[{0,0},{1,1}]},
plotgraph,
plot1points,
plot2points
},
AspectRatio->Automatic
]

Match

match[Amat_,flist_] :=
Module[{matching,pairs,si,sj,xi,xj,i,j,dist,p,newpairs,pair},
pairs = {};
Do[
Do[
If[Amat[[i,j]] == 1,
{si,xi} = flist[[i]];
{sj,xj} = flist[[j]];
If[si≠sj,
dist = Norm[xi-xj];
AppendTo[pairs,{dist,i,j}]
]
],
{j,i+1,N1+N2}
],
{i,1,N1+N2-1}
];
pairs= Sort[pairs];
matching = {};
While[pairs≠{},
{dist,i,j} = First[pairs];
AppendTo[matching,{dist,i,j}];
newpairs = {};
Do[
pair = pairs[[p]];
If[!MemberQ[pair,i] && ! MemberQ[pair,j],
AppendTo[newpairs,pair]
],
{p,1,Length[pairs]}
];
pairs = newpairs
];
matching
];

matching = match[Amat,flist]

Draw matching

plotmatching = 
Table[
{Purple,Thick,
Line[
{flist[[matching[[i,2]],2]],
flist[[matching[[i,3]],2]]
}
]
},
{i,1,Length[matching]}
];

Graphics[
{{White,Rectangle[{0,0},{1,1}]},
plotgraph,
plotmatching,
plot1points,
plot2points
},
AspectRatio->Automatic
]

Compute statistics

r  =2;
N1 = 50;
N2 = 50;

Nruns = 100;

p = 1;
d = 2;

Clear[sizes];Clear[diversities];Clear[strengths];
sizes[i_,k_] := {};
diversities[i_,k_] := {};
strengths[i_,k_]:={};
Do[
q = i/10;
Do[
Amat = creategraph[N1,N2,p,q];
flist = assignfeatures[N1,N2,d];
matching = match[Amat,flist];
size0 = Length[matching];
interpairs = 
Select[matching,(#[[2]]≤N1 && #[[3]]> N1) &];
diversity0 = (Length[interpairs]/size0)*(r/(r-1));
strength0 = 
(Sqrt[2]-Mean[First[Transpose[matching]]])/Sqrt[2];
Do[
ϵ = Sqrt[2]*k/10;
Bmat = addedges[Amat,flist,ϵ];
matching = match[Bmat,flist];
size = Length[matching];
AppendTo[sizes[i,k],size-size0];
interpairs = 
Select[matching,(#[[2]]≤N1 && #[[3]]> N1) &];
diversity = ((Length[interpairs]/size)*(r/(r-1))) - diversity0;
AppendTo[diversities[i,k],diversity];
strength = 
((Sqrt[2]-Mean[First[Transpose[matching]]])/Sqrt[2])-strength0;
AppendTo[strengths[i,k],strength],
{k,1,10}
],
{run,1,Nruns}
],
{i,0,10}
];

Do[
Do[
msizes[i,k] = Mean[sizes[i,k]];
dsizes[i,k] = StandardDeviation[sizes[i,k]];
mdiversities[i,k] = Mean[diversities[i,k]];
ddiversities[i,k] = StandardDeviation[diversities[i,k]];
mstrengths[i,k] = Mean[strengths[i,k]];
dstrengths[i,k] = StandardDeviation[strengths[i,k]],
{k,1,10}
],
{i,0,10}
];

Plot statistics

sizegraphics = 
Graphics[
{Table[
{Hue[k/15],
Line[
{{i/10,msizes[i,k]-dsizes[i,k]},
{i/10,msizes[i,k]+dsizes[i,k]}
}
]
},
{i,0,10},{k,1,10}
]
}
];
sizeplot1 =
ListLinePlot[
Table[{i/10,msizes[i,1]},{i,0,10}],
PlotStyle->Hue[1/15]
];
sizeplot2 =
ListLinePlot[
Table[{i/10,msizes[i,2]},{i,0,10}],
PlotStyle->Hue[2/15]
];
sizeplot3 =
ListLinePlot[
Table[{i/10,msizes[i,3]},{i,0,10}],
PlotStyle->Hue[3/15]
];
sizeplot4 =
ListLinePlot[
Table[{i/10,msizes[i,4]},{i,0,10}],
PlotStyle->Hue[4/15]
];
sizeplot5 =
ListLinePlot[
Table[{i/10,msizes[i,5]},{i,0,10}],
PlotStyle->Hue[5/15]
];
sizeplot6 =
ListLinePlot[
Table[{i/10,msizes[i,6]},{i,0,10}],
PlotStyle->Hue[6/15]
];
sizeplot7 =
ListLinePlot[
Table[{i/10,msizes[i,7]},{i,0,10}],
PlotStyle->Hue[7/15]
];
sizeplot8 =
ListLinePlot[
Table[{i/10,msizes[i,8]},{i,0,10}],
PlotStyle->Hue[8/15]
];
sizeplot9 =
ListLinePlot[
Table[{i/10,msizes[i,9]},{i,0,10}],
PlotStyle->Hue[9/15]
];
sizeplot10 =
ListLinePlot[
Table[{i/10,msizes[i,10]},{i,0,10}],
PlotStyle->Hue[10/15]
];

Show[
sizeplot1,sizeplot2,sizeplot3,sizeplot4,sizeplot5,
sizeplot6,sizeplot7,sizeplot8,sizeplot9,sizeplot10,
sizegraphics,
AxesOrigin->{0,0},
AxesStyle->Directive[Thick],
AxesLabel->{"q","Δsize"},
LabelStyle->Directive[Large],
PlotRange->All,
ImageSize->720
]

divgraphics = 
Graphics[
{Table[
{Hue[k/15],
Line[
{{i/10,mdiversities[i,k]-ddiversities[i,k]},
{i/10,mdiversities[i,k]+ddiversities[i,k]}
}
]
},
{i,0,10},{k,1,10}
]
}
];
plot1 =
ListLinePlot[
Table[{i/10,mdiversities[i,1]},{i,0,10}],
PlotStyle->Hue[1/15]
];
plot2 =
ListLinePlot[
Table[{i/10,mdiversities[i,2]},{i,0,10}],
PlotStyle->Hue[2/15]
];
plot3 =
ListLinePlot[
Table[{i/10,mdiversities[i,3]},{i,0,10}],
PlotStyle->Hue[3/15]
];
plot4 =
ListLinePlot[
Table[{i/10,mdiversities[i,4]},{i,0,10}],
PlotStyle->Hue[4/15]
];
plot5 =
ListLinePlot[
Table[{i/10,mdiversities[i,5]},{i,0,10}],
PlotStyle->Hue[5/15]
];
plot6 =
ListLinePlot[
Table[{i/10,mdiversities[i,6]},{i,0,10}],
PlotStyle->Hue[6/15]
];
plot7 =
ListLinePlot[
Table[{i/10,mdiversities[i,7]},{i,0,10}],
PlotStyle->Hue[7/15]
];
plot8 =
ListLinePlot[
Table[{i/10,mdiversities[i,8]},{i,0,10}],
PlotStyle->Hue[8/15]
];
plot9 =
ListLinePlot[
Table[{i/10,mdiversities[i,9]},{i,0,10}],
PlotStyle->Hue[9/15]
];
plot10 =
ListLinePlot[
Table[{i/10,mdiversities[i,10]},{i,0,10}],
PlotStyle->Hue[10/15]
];

Show[
plot1,plot2,plot3,plot4,plot5,
plot6,plot7,plot8,plot9,plot10,
divgraphics,
AxesOrigin->{0,0},
AxesStyle->Directive[Thick],
AxesLabel->{"q","Δdiversity"},
LabelStyle->Directive[Large],
PlotRange->All,
ImageSize->720
]

strengthgraphics = 
Graphics[
{Table[
{Hue[k/15],
Line[
{{i/10,mstrengths[i,k]-dstrengths[i,k]},
{i/10,mstrengths[i,k]+dstrengths[i,k]}
}
]
},
{i,0,10},{k,1,10}
]
}
];
plot1 =
ListLinePlot[
Table[{i/10,mstrengths[i,1]},{i,0,10}],
PlotStyle->Hue[1/15]
];
plot2 =
ListLinePlot[
Table[{i/10,mstrengths[i,2]},{i,0,10}],
PlotStyle->Hue[2/15]
];
plot3 =
ListLinePlot[
Table[{i/10,mstrengths[i,3]},{i,0,10}],
PlotStyle->Hue[3/15]
];
plot4 =
ListLinePlot[
Table[{i/10,mstrengths[i,4]},{i,0,10}],
PlotStyle->Hue[4/15]
];
plot5 =
ListLinePlot[
Table[{i/10,mstrengths[i,5]},{i,0,10}],
PlotStyle->Hue[5/15]
];
plot6 =
ListLinePlot[
Table[{i/10,mstrengths[i,6]},{i,0,10}],
PlotStyle->Hue[6/15]
];
plot7 =
ListLinePlot[
Table[{i/10,mstrengths[i,7]},{i,0,10}],
PlotStyle->Hue[7/15]
];
plot8 =
ListLinePlot[
Table[{i/10,mstrengths[i,8]},{i,0,10}],
PlotStyle->Hue[8/15]
];
plot9 =
ListLinePlot[
Table[{i/10,mstrengths[i,9]},{i,0,10}],
PlotStyle->Hue[9/15]
];
plot10 =
ListLinePlot[
Table[{i/10,mstrengths[i,10]},{i,0,10}],
PlotStyle->Hue[10/15]
];

Show[
plot1,plot2,plot3,plot4,plot5,
plot6,plot7,plot8,plot9,plot10,
strengthgraphics,
AxesOrigin->{0,0},
AxesStyle->Directive[Thick],
AxesLabel->{"q","Δstrength"},
LabelStyle->Directive[Large],
PlotRange->All,
ImageSize->720
]