I request an efficient Code
for the following computations. The computations do exactly what I intend to achieve (I assume?), however, the code looks poor and inefficient, and I cannot go on with higher order layers because of my poor programming skills. Therefore, for illustrative purposes, I could compute two layers only.
Here is a brief description of how to identify cascades of layers of one-edge neighbors
of a given sector i in a directed graph.
- Layer(i,1) denotes layer 1 of sector i, which is defined by one-edge neighbors of i (denoted by N(i)={sectors(i), binary links(i)});
- Suppose that sectors j, k, l are elements of the set
sectors(i)
inN(i)
, for each sector j, k, l, find their one-edge neighbors: N(j), N(k), N(l); - Subtract binary links(i) from the
Union
of binary links(j), binary links(k), binary links(l). The remaining list of binary links and the associated sectors define Layer(i, 2); - To obtain Layer(i,3), repeat the same procedure using N(j), N(k), N(l);
- Identification of layers should stop when all the sectors in the given
start
network are exhausted.
Here is the code implementing the above procedure for two layers only:
el = {"EGW" \[DirectedEdge] "MA2", "EGW" \[DirectedEdge] "HLT", "EGW" \[DirectedEdge] "AGF", "EGW" \[DirectedEdge] "WHS", "EGW" \[DirectedEdge] "TSC", "EGW" \[DirectedEdge] "CST", "HLT" \[DirectedEdge] "MA2", "HLT" \[DirectedEdge] "EGW", "HLT" \[DirectedEdge] "TSC", "HLT" \[DirectedEdge] "CST", "AGF" \[DirectedEdge] "EGW", "AGF" \[DirectedEdge] "HLT", "AGF" \[DirectedEdge] "WHS", "AGF" \[DirectedEdge] "TSC", "CO12" \[DirectedEdge] "HLT", "CO12" \[DirectedEdge] "AGF", "CO12" \[DirectedEdge] "WHS", "FIN" \[DirectedEdge] "AGF", "WHS" \[DirectedEdge] "CO12", "TSC" \[DirectedEdge] "CO12", "TSC" \[DirectedEdge] "FIN", "CST" \[DirectedEdge] "FIN"};
vl = {"MA2", "EGW", "HLT", "AGF", "CO12", "FIN", "WHS", "TSC", "CST"};
ewl = {0.021, 0.019, 0.017, 0.026, 0.023, 0.026, 0.015, 0.011, 0.015,
0.013, 0.017, 0.015, 0.026, 0.016, 0.025, 0.018, 0.018, 0.017,
0.016, 0.012, 0.021, 0.014};
wGraph = Thread[{el, ewl}];
gr = Graph[el, VertexLabels -> "Name", PlotLabel -> "Given a graph",
ImageSize -> 250];
HighlightGraph[gr, Subgraph[gr,layer1 = NeighborhoodGraph[gr, "MA2", PlotLabel -> "Layer 1"]], ImageSize -> 250];
el1 = EdgeList[layer1];
vl1 = VertexList[layer1];
HighlightGraph[gr,Subgraph[gr, layer21 = NeighborhoodGraph[gr, "EGW"]]];
HighlightGraph[gr,Subgraph[gr, layer22 = NeighborhoodGraph[gr, "HLT"]]];
el21 = EdgeList[layer21];
vl21 = VertexList[layer21];
el22 = EdgeList[layer22];
vl22 = VertexList[layer22];
el21Uel22 = Union[el21, el22];
layer2 = Graph[Complement[el21Uel22, el1], VertexLabels -> "Name",
PlotLabel -> "Layer 2", ImageSize -> 250];
Row[{gr, Spacer[20], layer1, Spacer[20], layer2}]
Here is what it produces:
I would be grateful if the efficient Code
derives layers
- as a sequence of
Subgraphs
like I did above to allow for the reconstruction of thestarting
graph by sequentially clipping individual layers (Subgraphs
) to each other. - show the final layered graph as a
CommunityGraph
.