5
$\begingroup$

Working from the code below, how can the table it generates produce these results for the hierarchical community (where there are 6 communities given by common node color) instead of the 5 communities produced?

edges = {NS <-> N1, NS <-> N11, NS <-> N18, NS <-> N24, NS <-> N27, 
   NS <-> N35, NS <-> N37, N1 <-> N4, N1 <-> N5, N1 <-> N6, N1 <-> N7,
    N1 <-> N8, N1 <-> N9, N2 <-> N5, N3 <-> N5, N3 <-> N15, N4 <-> N2,
    N4 <-> N3, N4 <-> N5, N4 <-> N12, N4 <-> N13, N4 <-> N14, 
   N5 <-> N15, N5 <-> N16, N5 <-> N19, N6 <-> N15, N7 <-> N10, 
   N8 <-> N16, N9 <-> N10, N10 <-> N5, N10 <-> N6, N10 <-> N8, 
   N11 <-> N6, N11 <-> N8, N11 <-> N21, N12 <-> N17, N12 <-> N40, 
   N13 <-> N6, N13 <-> N21, N14 <-> NG, N15 <-> N30, N16 <-> N6, 
   N17 <-> NG, N18 <-> N15, N19 <-> N20, N20 <-> N21, N20 <-> N30, 
   N21 <-> N22, N22 <-> N23, N22 <-> N43, N23 <-> NG, N24 <-> NG, 
   N25 <-> NG, N26 <-> N29, N26 <-> N40, N27 <-> N28, N27 <-> N40, 
   N28 <-> NG, N29 <-> NG, N30 <-> N31, N30 <-> N32, N30 <-> N33, 
   N30 <-> N34, N31 <-> N36, N32 <-> N36, N33 <-> N36, N34 <-> N31, 
   N35 <-> N34, N36 <-> N12, N36 <-> N39, N37 <-> N36, N38 <-> N40, 
   N38 <-> N43, N39 <-> N40, N40 <-> N41, N41 <-> N42, N42 <-> NG, 
   N43 <-> N25, N43 <-> N26, N36 <-> N38};

g = Graph[edges, VertexLabels -> "Name", VertexLabels -> Automatic, 
  VertexSize -> 0.9, VertexLabelStyle -> 14, 
  VertexStyle -> {NS | N1 | N11 | N18 | N24 | N27 | N35 -> 
     Darker[Red], N37 | N4 | N5 | N6 | N7 | N8 | N9 -> Red, 
    N2 | N3 | N15 | N12 | N13 -> Lighter[Lighter[Orange]], 
    N14 | N16 | N19 | N10 | N21 | N17 | N40 -> Yellow, 
    NG | N30 | N20 | N22 | N23 | N43 | N25 | N26 | N29 | N28 | N31 -> 
     Darker[Green], 
    N32 | N33 | N34 | N36 | N39 | N38 | N41 | N42 -> Lighter[Green]}]

cg = CommunityGraphPlot[g, 
  VertexStyle -> {v_ :> PropertyValue[{g, v}, VertexStyle]}]
communities = 
  GatherBy[VertexList[g], PropertyValue[{g, #}, VertexStyle] &];
CommunityGraphPlot[g, communities, 
  VertexStyle -> PropertyValue[g, VertexStyle]];
CommunityGraphPlot[g, communities, Method -> "Hierarchical", 
 VertexStyle -> PropertyValue[g, VertexStyle]]

ClearAll[connectingEdgesF, tabulateF]
connectingEdgesF = 
  Module[{g = #}, 
    Complement[EdgeList[#], 
     Flatten[EdgeList[Subgraph[g, #]] & /@ 
       FindGraphCommunities[g]]]] &;
tabulateF = 
  Module[{rule = 
      Join @@ MapIndexed[Thread[# -> #2[[1]]] &, 
        FindGraphCommunities[#], 1], edges = connectingEdgesF[#]}, 
    TableForm[(List @@@ edges) /. {a_, b_} :> 
       Join[{a, a /. rule}, {b, b /. rule}], 
     TableHeadings -> {None, {"From Vertex", "in Community", 
        "To Vertex", "in Community"}}, TableAlignments -> Center]] &;
connectingEdgesF[g];
tabulateF[g]
$\endgroup$

1 Answer 1

6
$\begingroup$

Use communities instead of FindGraphCommunities[g] when you define connectingEdgesF:

ClearAll[connectingEdgesF, tabulateF]
connectingEdgesF = Module[{g = #}, 
    Complement[EdgeList[g], Flatten[EdgeList[Subgraph[g, #]] & /@ communities]]] &;

tabulateF = Module[{rule = Join @@ MapIndexed[Thread[# -> #2[[1]]] &, communities, 1], 
     edges = connectingEdgesF[#]}, 
    TableForm[(List @@@ edges) /. {a_, b_} :> 
       Join[{a, a /. rule}, {b, b /. rule}], 
     TableHeadings -> {None, {"From Vertex", "in Community", 
        "To Vertex", "in Community"}}, TableAlignments -> Center]] &;

tabulateF[g] // TeXForm

$\small\begin{array}{cccc} \text{From Vertex} & \text{in Community} & \text{To Vertex} & \text{in Community} \\ \text{N1} & 1 & \text{N4} & 2 \\ \text{N1} & 1 & \text{N5} & 2 \\ \text{N1} & 1 & \text{N6} & 2 \\ \text{N1} & 1 & \text{N7} & 2 \\ \text{N1} & 1 & \text{N8} & 2 \\ \text{N1} & 1 & \text{N9} & 2 \\ \text{N10} & 4 & \text{N5} & 2 \\ \text{N10} & 4 & \text{N6} & 2 \\ \text{N10} & 4 & \text{N8} & 2 \\ \text{N11} & 1 & \text{N21} & 4 \\ \text{N11} & 1 & \text{N6} & 2 \\ \text{N11} & 1 & \text{N8} & 2 \\ \text{N12} & 3 & \text{N17} & 4 \\ \text{N12} & 3 & \text{N40} & 4 \\ \text{N13} & 3 & \text{N21} & 4 \\ \text{N13} & 3 & \text{N6} & 2 \\ \text{N14} & 4 & \text{NG} & 5 \\ \text{N15} & 3 & \text{N30} & 5 \\ \text{N16} & 4 & \text{N6} & 2 \\ \text{N17} & 4 & \text{NG} & 5 \\ \text{N18} & 1 & \text{N15} & 3 \\ \text{N19} & 4 & \text{N20} & 5 \\ \text{N2} & 3 & \text{N5} & 2 \\ \text{N20} & 5 & \text{N21} & 4 \\ \text{N21} & 4 & \text{N22} & 5 \\ \text{N24} & 1 & \text{NG} & 5 \\ \text{N26} & 5 & \text{N40} & 4 \\ \text{N27} & 1 & \text{N28} & 5 \\ \text{N27} & 1 & \text{N40} & 4 \\ \text{N3} & 3 & \text{N5} & 2 \\ \text{N30} & 5 & \text{N32} & 6 \\ \text{N30} & 5 & \text{N33} & 6 \\ \text{N30} & 5 & \text{N34} & 6 \\ \text{N31} & 5 & \text{N36} & 6 \\ \text{N34} & 6 & \text{N31} & 5 \\ \text{N35} & 1 & \text{N34} & 6 \\ \text{N36} & 6 & \text{N12} & 3 \\ \text{N37} & 2 & \text{N36} & 6 \\ \text{N38} & 6 & \text{N40} & 4 \\ \text{N38} & 6 & \text{N43} & 5 \\ \text{N39} & 6 & \text{N40} & 4 \\ \text{N4} & 2 & \text{N12} & 3 \\ \text{N4} & 2 & \text{N13} & 3 \\ \text{N4} & 2 & \text{N14} & 4 \\ \text{N4} & 2 & \text{N2} & 3 \\ \text{N4} & 2 & \text{N3} & 3 \\ \text{N40} & 4 & \text{N41} & 6 \\ \text{N42} & 6 & \text{NG} & 5 \\ \text{N5} & 2 & \text{N15} & 3 \\ \text{N5} & 2 & \text{N16} & 4 \\ \text{N5} & 2 & \text{N19} & 4 \\ \text{N6} & 2 & \text{N15} & 3 \\ \text{N7} & 2 & \text{N10} & 4 \\ \text{N8} & 2 & \text{N16} & 4 \\ \text{N9} & 2 & \text{N10} & 4 \\ \text{NS} & 1 & \text{N37} & 2 \\ \end{array}$

$\endgroup$
2
  • $\begingroup$ I was close! ... again, many thanks kglr ... I've learned much from your help ... prg $\endgroup$
    – user42700
    Commented Dec 27, 2019 at 0:06
  • $\begingroup$ @PRG, you're welcome. Thank you for accepting. $\endgroup$
    – kglr
    Commented Dec 27, 2019 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.