| Bytes | Lang | Time | Link |
|---|---|---|---|
| 118 | Python3 | 250819T181150Z | Ajax1234 |
| 090 | Wolfram Language Mathematica | 181231T220330Z | Kelly Lo |
| 168 | Python 2 | 181231T034047Z | CCB60 |
| 152 | Clean | 181230T222632Z | Οurous |
| 015 | Jelly | 181230T174730Z | Jonathan |
| 016 | Jelly | 181230T202815Z | Erik the |
Python3, 118 bytes
lambda g,n:sum(all(g[x][y]for x in i for y in i if x!=y)for i in combinations(range(len(g)),n))
from itertools import*
Wolfram Language (Mathematica), 90 bytes
There is probably some room to further golf this, after some test cases are posted.
We construct a graph from the unitized adjacency matrix, and then use the FindClique function with All as the third argument to list out the vertices with complete subgraphs. We then select the submatrices of the original input corresponding to the cliques (which will have zeros along the diagonal) and count the number of distinct elements which should be 2 if the weights are equal.
In this implementation, self connections can have a weight other than zero and the graph will still be considered complete and equal weighted, so long as the weights of all self connections in the subgraph are identical.
Count[Length@*Union@@@(b[[#,#]]&/@FindClique[AdjacencyGraph[Unitize/@(b=#)],{#2},All]),2]&
Python 2, 195 bytes 178 bytes 168 bytes
I'm sure there's a lot more golfing possible here, but for a change I want to get an entry in before there are dozens of other ones. I've provided a couple of test cases on TIO, but would love more examples.
First round of suggested golf steps: Remove some spaces, be smarter about the import. Shorter way of testing if the single link value in a set is zero (Thanks to M. XCoder).
Second round: ElPedro points out that in this case a program is shorter than the function. I was hoping to find a way to package this as a lambda, so was thinking "function" from the start. Thanks.
from itertools import*
r=set()
M,n=input()
for g in set(combinations(range(len(M)),n)):
s={M[i][j]for i in g for j in g if i>j}
if len(s)==1and{0}!=s:r.add(g)
print r
A slightly ungolfed version, to clarify logic:
from itertools import combinations
def f3(M,n):
r = set()
groups = set(combinations(range(len(M)), n)) # all possible combinations of indexes
for group in groups:
# Generate a set of link values associated with the current combination
# This selects only non-diagonal indexes, assuming matrix symmetry.
s = {M[i][j]for i in group for j in group if i>j}
# If the set has only one member, and that's not zero, you have
# a connected subgraph.
if len(s) == 1 and 0 not in s:
r.add(group)
return r
Clean, 152 bytes
import StdEnv,Data.List
$m n=sum[1\\i<-subsequences[0..size m-1]|length i==n,j<-permutations i|case[m.[x,y]\\x<-i&y<-j]of[u:v]=all((==)u)v&&u>0;_=1<0]/2
TIO driver takes n as a command-line argument and the matrix through STDIN (weights up to 9).
The actual function $ :: {#{#Int}} Int -> Int works with any size weights.
Jelly, 18 16 15 bytes
Assumes self-connections may be any weight (i.e. they are not necessarily only either \$0\$ or the equal weight).
Jœcṗ2EÐḟœị³EƲ€S
Try it online!
the two subsets of size 3 being ACE and BCD
With n=2 all 10 subsets of size 2 work as its symmetric.
How?
Jœcṗ2EÐḟœị³EƲ€S - Link: list of lists of integers, M; integer, n
J - range of length of M = [1,2,3,...,length(M)]
œc - Combinations of length n e.g. [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
€ - for each:
Ʋ - last four links as a monad:
ṗ2 - Cartesian power with 2
Ðḟ - discard if:
E - all-equal (i.e. diagonal co-ordinates like [3,3])
œị - multi-dimensional index into:
³ - program's 3rd argument (1st input), M
E - all equal?
S - sum