Skip to main content
1 of 5
kglr
  • 403.4k
  • 18
  • 501
  • 959
ClearAll[grph] grph[mat_, t_, v_, opts : OptionsPattern[Graph]] := Module[{vertices = CharacterRange["A", "Z"][[;; Length@mat]], assoc, edges, g}, assoc = AssociationThread[vertices, UnitStep[Normalize[#, Total] - t] & /@ Transpose[mat]]; edges = Join @@ KeyValueMap[Thread[DirectedEdge[#, vertices[[Flatten@#2]]]] &][ Position[#, 1] & /@ assoc]; g = Graph[edges]; Subgraph[g, VertexOutComponent[g, v], VertexLabels -> "Name", opts]]; 

Examples:

Using mat and Transpose @ mat as the first argument:

Row[Panel /@ MapThread[grph[#, .25, "A", ImageSize -> 300, EdgeStyle -> #2, PlotLabel -> MatrixForm[#]] &, {{mat, Transpose@mat}, {Blue, Red}}]] 

enter image description here

To show the two graphs for mat and Transpose@mat together:

edgeadd = Complement[EdgeList@grph[Transpose@mat, .25, "A", EdgeStyle -> Red], EdgeList@grph[mat, .25, "A"]]; SetProperty[EdgeAdd[grph[mat, .25, "A"], edgeadd], EdgeStyle -> {_ :> Blue, Alternatives @@ edgeadd -> Red}] 

enter image description here

Several combinations of thresholds and starting nodes:

Grid[Outer[ grph[mat, #, #2, ImageSize -> 200, PlotLabel -> Grid[{{"threshold :", #}, {"starting node : ", #2}}]] &, {.1, .25, .3}, {"A", "B", "C"}], Dividers -> All] 

enter image description here

kglr
  • 403.4k
  • 18
  • 501
  • 959