You can use the built-in functions `FindGraphPartition`

to get `partition`

with *approximately* equal sized groups and `FindGraphCommunities`

to modify `partition`

to equalize the group sizes.

```
SeedRandom[123]
n = 72;
utm = UpperTriangularize[RandomChoice[{3/4, 1/4} -> {0, 1}, {n, n}], 1];
am = utm + Transpose[utm];
coords = RandomReal[100, {n, 2}];
dm = Rescale @ DistanceMatrix[coords];
wam = ((1 - dm) am) /. 0 | 0. -> ∞;
wag = WeightedAdjacencyGraph[Range[n], wam,
VertexSize -> Scaled[.03], VertexLabels -> Placed["Name", Center],
ImageSize -> Large]
```

```
CommunityGraphPlot[wag]
```

`FindGraphPartition`

:

FindGraphPartition[g, k] *gives a partition of vertices into* k **approximately** equal-size parts.

*For a weighted graph,* FindGraphPartition *finds a partition such that the sum of edge weights for edges having endpoints in different
parts is minimized.*

```
partition = FindGraphPartition[wag, ConstantArray[6, 12]]
```

{{5, 7, 34, 44, 45, 63, 68}, {6, 10, 12, 22, 36, 58},

{1, 18, 26, 38, 39, 62}, {28, 37, 48, 51, 56, 67},

{8, 16, 43, 47, 54, 70}, {13, 14, 29, 40, 57, 64},

{11, 19, 25, 42, 50, 53}, {2, 4, 17, 23, 24, 61},

{15, 35, 46, 52, 59, 66}, {9, 21, 31, 32, 33, 65},

{20, 27, 30, 49, 55, 60}, {3, 41, 69, 71, 72}}

```
Length /@ partition
```

{7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5}

```
CommunityGraphPlot[wag, partition]
```

Next, we use `FindGraphCommunities`

with option `Method -> "VertexMoving"`

using `partition`

as the value of the sub-option `"InitialPartitioning"`

and `(-(Length[#2] - 12)^2 - Total[(6 - Length@#)^2 & /@ #2] &)`

as the value of the sub-option `"ObjectiveFunction"`

to get equal sized groups:

```
partition2 = FindGraphCommunities[wag,
Method -> {"VertexMoving", "InitialPartitioning" -> partition,
"ObjectiveFunction" -> (-(Length[#2] - 12)^2 - Total[(6 - Length@#)^2 & /@ #2] &)}]
```

{{1, 18, 26, 38, 39, 62}, {2, 4, 17, 23, 24, 61},

{3, 5, 41, 69, 71, 72}, {6, 10, 12, 22, 36, 58},

{7, 34, 44, 45, 63, 68}, {8, 16, 43, 47, 54, 70},

{9, 21, 31, 32, 33, 65}, {11, 19, 25, 42, 50, 53},

{13, 14, 29, 40, 57, 64}, {15, 35, 46, 52, 59, 66},

{20, 27, 30, 49, 55, 60}, {28, 37, 48, 51, 56, 67}}

```
Length /@ partition2
```

{6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}

```
CommunityGraphPlot[wag, partition2]
```

```
HighlightGraph[Graph[Join @@ partition2, EdgeList @ wag],
Subgraph[wag, #] & /@ partition2, VertexSize -> 7/10,
GraphLayout -> "CircularEmbedding",
VertexLabels -> Placed["Name", Center]]
```

**... every node of a group must be adjacent to at least one of the remaining nodes in that cluster.**

```
Grid[Partition[Subgraph[wag, #, PlotLabel -> #,
GraphLayout -> "CircularEmbedding", ImageSize -> 200, VertexStyle -> White,
EdgeLabels -> {e_ :> Round[PropertyValue[{wag, e}, EdgeWeight], .1]},
VertexLabels -> Placed["Name", Center], VertexSize -> Medium] & /@
partition2, 4]]
```

1How about considering a smaller analogous problem, like 6 nodes that you want divided into 2 groups of 3. In this case you can list the matrices S and D explicitly, and maybe it will become more obvious how to solve it. With luck, that might generalize to the full problem. Even in the simpler case, it would seem that there are configurations of S which would make it impossible to achieve the goal -- for example, what if one of the nodes was isolated and not adjacent to any others... then it could not be in any group. – bill s – 2018-10-27T19:52:23.473

@bill s, however it is not the case. no node is isolated. – dipak narayanan – 2018-10-27T19:57:42.307

`FindGraphPartition[WeightedAdjacencyGraph[adjmat],6]`

? – kglr – 2018-10-27T20:00:42.483@kglr, the Mathematica documentation says that WeightedAdjacencyGraph works on weighted adjacency matrix. But in my case, the adjacency matrix is not weighted. What about your comment? – dipak narayanan – 2018-10-27T20:35:11.180

@kglr, Thanks a lot. But when run the code, I am getting this..WeightedAdjacencyGraph::matsq: Argument {{0,0,0,0,0,0,0,363.18,371.88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,431.19,0,0,0,0,0,0,0,0,632.24,366.3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},<<48>>,<<22>>} at position 1 is not a non-empty square matrix. – dipak narayanan – 2018-10-29T14:50:24.227

@kglr, I also get the following error...FindGraphPartition::graph: A graph object is expected at position 1 in FindGraphPartition[WeightedAdjacencyGraph[{{0,0,0,0,0,0,0,363.18,371.88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},<<49>>,<<22>>}],6]. – dipak narayanan – 2018-10-29T15:25:03.890

@kglr, Yes...it is indeed a square matrix. But, I got that error. – dipak narayanan – 2018-10-30T10:27:53.283

@dipaknarayanan If you edit your question and add the code you are currently using, that makes it easier for others to give you good advice. – Thies Heidecke – 2018-10-30T15:09:27.123

@kglr, Unfortunately, the code you suggested gave clusters but with non-adjacent cluster members. My target is that in each cluster, any node must be adjacent to at least one of the remaining nodes in that cluster. – dipak narayanan – 2018-10-30T21:17:42.863