# What is the best way to draw cluster for every station?

I have a binary matrix "X" of U rows and N columns. Each row of X has 1 to maximum 3 ones. Each row corresponds to a subscriber and every column corresponds to a service point. So, the ones in a given row corresponds to the service points that the subscriber can be served by.

How can I graphically show this association?

Lets say, we have 5 service points, randomly/systematically arranged over an area.

x-coordinates of the service points are {200, 600, 200, 600, 400}

y-coordinates of the service points are {200, 200, 600, 600, 400}

And we have 10 subscribers randomly distributed over the area with boundary points (0, 0), (0, 800), (800, 0) and (800, 800).

And the X matrix is given by

X = {{0, 0, 1, 0, 1}, {0, 1, 1, 0, 0}, {1, 0, 0, 1, 0}, {0, 0, 0, 1, 0},
{1, 1, 0, 1, 0}, {0, 0, 1, 1, 0}, {0, 0, 1, 0, 1}, {0, 1, 0, 0, 1},
{1, 1, 0, 0, 1}, {1, 1, 0, 0, 1}};


How to graphically show this connections?

Lets say, we call the set of service points associated with given users a clusters. So, different subscribers will have different clusters. I just want to see the relationship of different clusters for different users, for example, if two nearby subscribers have the same cluster or they have different clusters?

• Start by at least representing your matrices in Mathematica code, i.e. as nested lists. Then, are you looking to generate a graph output? Or some kind of plot? Nov 16, 2020 at 0:03
• @MarcoB, I prefer graph output. Plots are also welcome. Nov 16, 2020 at 0:04
• I am not sure that this is a Mathematica question yet. You should tell us how you want the relationship represented, and then somebody can help you construct the graph you need. Nov 16, 2020 at 0:16

## 1 Answer

If you have IGraphM you can use the function IGBipartiteIncidenceGraph using your matrix X input:

<< IGraphM

subscribers = "Subscriber" <> ToString[#] & /@ Range[First@Dimensions[X]];

servicepoints = "ServicePoint" <> ToString[#] & /@ Range[Last@Dimensions[X]];

IGBipartiteIncidenceGraph[{subscribers, servicepoints}, X,
GraphLayout -> {"MultipartiteEmbedding", "VertexPartition" -> Dimensions[X]},
VertexLabels -> {v : Alternatives @@ subscribers :> Placed["Name", Before],
v : Alternatives @@ servicepoints :> Placed["Name", After]},
PerformanceGoal -> "Quality",
ImagePadding -> {{70, 70}, {10, 10}},
ImageSize -> Large]


If you don't have IGraphM you can construct an AdjacencyMatrix from your X and use AdjacencyGraph:

vl = Join[subscribers, servicepoints];

am = ArrayFlatten[{{0, X}, {Transpose[X], 0}}];

AdjacencyGraph[vl, am,
GraphLayout -> {"MultipartiteEmbedding", "VertexPartition" -> Dimensions[X]},
VertexLabels -> {v : Alternatives @@ subscribers :>
Placed["Name", Before],
v : Alternatives @@ servicepoints :> Placed["Name", After]},
PerformanceGoal -> "Quality", ImagePadding -> {{70, 70}, {10, 10}},
ImageSize -> Large]


Remove the option GraphLayout and use coordinates of your choice as VertexCoordinates:

xsp = {200, 600, 200, 600, 400};
ysp = {200, 200, 600, 600, 400};

SeedRandom[1]
locations = Join[Thread[servicepoints -> Transpose[{xsp, ysp}]],
Thread[subscribers -> RandomPoint[Rectangle[{0, 0}, {800, 800}], 10]]];

ag = AdjacencyGraph[vl, am, VertexCoordinates -> locations,
VertexShapeFunction -> {v : Alternatives @@ servicepoints -> "Star"},
VertexSize -> Large,
VertexLabels -> {v : Alternatives @@ subscribers :>
Placed["Name", Before],
v : Alternatives @@ servicepoints :> Placed["Name", After]},
PerformanceGoal -> "Quality", ImagePadding -> {{70, 70}, {10, 10}},
ImageSize -> Large]


We can also use CommunityGraphPlot:

1. Using the default community structure:
CommunityGraphPlot[ag,
CommunityRegionStyle -> (Opacity[.3, #] & /@ RandomColor[10])]


Or, using the option Method -> "Hierarchical":

CommunityGraphPlot[ag,
CommunityRegionStyle -> (Opacity[.3, #] & /@ RandomColor[10]),
Method -> "Hierarchical"]


The automatic community structure used above is:

FindGraphCommunities[ag] // Column


1. Defining a custom community structure such that each subscriber i together with service points connected to i form a community (cluster):
communitystructure1 = Prepend[AdjacencyList[ag, #], #] & /@ subscribers;
Column @ communitystructure1


CommunityGraphPlot[ag, communitystructure1,
CommunityRegionStyle -> (Opacity[.3, #] & /@ RandomColor[10])]


1. Defining a custom community structure such that each service point j together with subscribers connected to j form a community (cluster):
communitystructure2 = Prepend[AdjacencyList[ag, #], #] & /@ servicepoints;
Column @ communitystructure2


CommunityGraphPlot[ag, communitystructure2,
CommunityRegionStyle -> (Opacity[.3, #] & /@ RandomColor[10])]
`

• would you please explain a bit the last two graphs. Also, why there are three different sets of subscribers (as we have three colors to distinguish the subscribers). In the last graph, we have 5 regions corresponding to 5 service points. Is it possible to have same color for the service points markers as their region Nov 16, 2020 at 10:46
• @dipaknarayanan, I added some explanation re how the example community structures are formed.
– kglr
Nov 16, 2020 at 10:57