Find equally-weighted complete graphs
Graph theory is used to study the relations between objects. A graph is composed of vertices and edges in a diagram such as this:
A-----B
| /
| /
| / E
| / /
|/ /
C-----D
In the above diagram, A
is linked to B
and C
; B
is linked to A
, C
, and E
; C
is linked to A
, B
, and D
; D
is linked to C
and E
; and E
is linked to B
and D
. As that description was rather wordy, a graph can be represented as a symmetric boolean matrix where a 1 represents a connection and a 0 represents the lack thereof. The above matrix is translated to this:
01100
10101
11010
00101
01010
For the purpose of this problem, the matrix definition can be extended to include the distances or weights of the paths between nodes. If individual ASCII characters in the diagram have weight 1, he matrix would be:
05500
50502
55050
00502
02020
A "complete graph" consists of a set of points such that each point is linked to every other point. The above graph is incomplete because it lacks connections from A
to D
and E
, B
to D
, and C
to E
. However, the subgraph between A
, B
, and C
is complete (and equally weighted). A 4-complete graph would look like this:
A---B
| /|
| X |
|/ |
C---D
and would be represented by the matrix:
01111
10111
11011
11101
11110
This problem is as follows: Given a symmetric matrix representing a graph and a positive integer n
, find the number of distinct equally-weighted complete subgraphs of size n
contained within.
You may assume that the input matrix is numeric and symmetric, and may choose input/output format. An entry in the matrix may be part of multiple equally-weighted subgraphs as long as they are distinct and of equal size. You may assume that n
is a positive integer greater than or equal to 3.
The winning criterion for this challenge is code golf. Standard rules apply.
code-golf matrix graph-theory
add a comment |
Graph theory is used to study the relations between objects. A graph is composed of vertices and edges in a diagram such as this:
A-----B
| /
| /
| / E
| / /
|/ /
C-----D
In the above diagram, A
is linked to B
and C
; B
is linked to A
, C
, and E
; C
is linked to A
, B
, and D
; D
is linked to C
and E
; and E
is linked to B
and D
. As that description was rather wordy, a graph can be represented as a symmetric boolean matrix where a 1 represents a connection and a 0 represents the lack thereof. The above matrix is translated to this:
01100
10101
11010
00101
01010
For the purpose of this problem, the matrix definition can be extended to include the distances or weights of the paths between nodes. If individual ASCII characters in the diagram have weight 1, he matrix would be:
05500
50502
55050
00502
02020
A "complete graph" consists of a set of points such that each point is linked to every other point. The above graph is incomplete because it lacks connections from A
to D
and E
, B
to D
, and C
to E
. However, the subgraph between A
, B
, and C
is complete (and equally weighted). A 4-complete graph would look like this:
A---B
| /|
| X |
|/ |
C---D
and would be represented by the matrix:
01111
10111
11011
11101
11110
This problem is as follows: Given a symmetric matrix representing a graph and a positive integer n
, find the number of distinct equally-weighted complete subgraphs of size n
contained within.
You may assume that the input matrix is numeric and symmetric, and may choose input/output format. An entry in the matrix may be part of multiple equally-weighted subgraphs as long as they are distinct and of equal size. You may assume that n
is a positive integer greater than or equal to 3.
The winning criterion for this challenge is code golf. Standard rules apply.
code-golf matrix graph-theory
6
Test cases wouldn't go amiss.
– Jonathan Allan
Dec 30 '18 at 16:32
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
Dec 30 '18 at 16:40
... and if so must they ben
too?
– Jonathan Allan
Dec 30 '18 at 16:45
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
Dec 30 '18 at 16:48
... and if so must they be n too?
– Jonathan Allan
Dec 30 '18 at 16:54
add a comment |
Graph theory is used to study the relations between objects. A graph is composed of vertices and edges in a diagram such as this:
A-----B
| /
| /
| / E
| / /
|/ /
C-----D
In the above diagram, A
is linked to B
and C
; B
is linked to A
, C
, and E
; C
is linked to A
, B
, and D
; D
is linked to C
and E
; and E
is linked to B
and D
. As that description was rather wordy, a graph can be represented as a symmetric boolean matrix where a 1 represents a connection and a 0 represents the lack thereof. The above matrix is translated to this:
01100
10101
11010
00101
01010
For the purpose of this problem, the matrix definition can be extended to include the distances or weights of the paths between nodes. If individual ASCII characters in the diagram have weight 1, he matrix would be:
05500
50502
55050
00502
02020
A "complete graph" consists of a set of points such that each point is linked to every other point. The above graph is incomplete because it lacks connections from A
to D
and E
, B
to D
, and C
to E
. However, the subgraph between A
, B
, and C
is complete (and equally weighted). A 4-complete graph would look like this:
A---B
| /|
| X |
|/ |
C---D
and would be represented by the matrix:
01111
10111
11011
11101
11110
This problem is as follows: Given a symmetric matrix representing a graph and a positive integer n
, find the number of distinct equally-weighted complete subgraphs of size n
contained within.
You may assume that the input matrix is numeric and symmetric, and may choose input/output format. An entry in the matrix may be part of multiple equally-weighted subgraphs as long as they are distinct and of equal size. You may assume that n
is a positive integer greater than or equal to 3.
The winning criterion for this challenge is code golf. Standard rules apply.
code-golf matrix graph-theory
Graph theory is used to study the relations between objects. A graph is composed of vertices and edges in a diagram such as this:
A-----B
| /
| /
| / E
| / /
|/ /
C-----D
In the above diagram, A
is linked to B
and C
; B
is linked to A
, C
, and E
; C
is linked to A
, B
, and D
; D
is linked to C
and E
; and E
is linked to B
and D
. As that description was rather wordy, a graph can be represented as a symmetric boolean matrix where a 1 represents a connection and a 0 represents the lack thereof. The above matrix is translated to this:
01100
10101
11010
00101
01010
For the purpose of this problem, the matrix definition can be extended to include the distances or weights of the paths between nodes. If individual ASCII characters in the diagram have weight 1, he matrix would be:
05500
50502
55050
00502
02020
A "complete graph" consists of a set of points such that each point is linked to every other point. The above graph is incomplete because it lacks connections from A
to D
and E
, B
to D
, and C
to E
. However, the subgraph between A
, B
, and C
is complete (and equally weighted). A 4-complete graph would look like this:
A---B
| /|
| X |
|/ |
C---D
and would be represented by the matrix:
01111
10111
11011
11101
11110
This problem is as follows: Given a symmetric matrix representing a graph and a positive integer n
, find the number of distinct equally-weighted complete subgraphs of size n
contained within.
You may assume that the input matrix is numeric and symmetric, and may choose input/output format. An entry in the matrix may be part of multiple equally-weighted subgraphs as long as they are distinct and of equal size. You may assume that n
is a positive integer greater than or equal to 3.
The winning criterion for this challenge is code golf. Standard rules apply.
code-golf matrix graph-theory
code-golf matrix graph-theory
asked Dec 30 '18 at 16:27
Arcturus
3,55011964
3,55011964
6
Test cases wouldn't go amiss.
– Jonathan Allan
Dec 30 '18 at 16:32
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
Dec 30 '18 at 16:40
... and if so must they ben
too?
– Jonathan Allan
Dec 30 '18 at 16:45
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
Dec 30 '18 at 16:48
... and if so must they be n too?
– Jonathan Allan
Dec 30 '18 at 16:54
add a comment |
6
Test cases wouldn't go amiss.
– Jonathan Allan
Dec 30 '18 at 16:32
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
Dec 30 '18 at 16:40
... and if so must they ben
too?
– Jonathan Allan
Dec 30 '18 at 16:45
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
Dec 30 '18 at 16:48
... and if so must they be n too?
– Jonathan Allan
Dec 30 '18 at 16:54
6
6
Test cases wouldn't go amiss.
– Jonathan Allan
Dec 30 '18 at 16:32
Test cases wouldn't go amiss.
– Jonathan Allan
Dec 30 '18 at 16:32
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
Dec 30 '18 at 16:40
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
Dec 30 '18 at 16:40
... and if so must they be
n
too?– Jonathan Allan
Dec 30 '18 at 16:45
... and if so must they be
n
too?– Jonathan Allan
Dec 30 '18 at 16:45
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
Dec 30 '18 at 16:48
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
Dec 30 '18 at 16:48
... and if so must they be n too?
– Jonathan Allan
Dec 30 '18 at 16:54
... and if so must they be n too?
– Jonathan Allan
Dec 30 '18 at 16:54
add a comment |
5 Answers
5
active
oldest
votes
Jelly, 16 bytes
ịⱮịŒDḊẎE
Jœcç€⁸S
Try it online!
add a comment |
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
add a comment |
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
Try it online!
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
1
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
1
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
@Mr.Xcoder - really likeand{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.
– ElPedro
Dec 31 '18 at 12:15
add a comment |
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
Try it online!
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.
add a comment |
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]&
Try it online!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178147%2ffind-equally-weighted-complete-graphs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Jelly, 16 bytes
ịⱮịŒDḊẎE
Jœcç€⁸S
Try it online!
add a comment |
Jelly, 16 bytes
ịⱮịŒDḊẎE
Jœcç€⁸S
Try it online!
add a comment |
Jelly, 16 bytes
ịⱮịŒDḊẎE
Jœcç€⁸S
Try it online!
Jelly, 16 bytes
ịⱮịŒDḊẎE
Jœcç€⁸S
Try it online!
edited Dec 30 '18 at 21:00
answered Dec 30 '18 at 20:28
Erik the Outgolfer
31.4k429103
31.4k429103
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Dec 30 '18 at 21:28
answered Dec 30 '18 at 17:47
Jonathan Allan
50.8k534165
50.8k534165
add a comment |
add a comment |
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
Try it online!
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
1
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
1
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
@Mr.Xcoder - really likeand{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.
– ElPedro
Dec 31 '18 at 12:15
add a comment |
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
Try it online!
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
1
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
1
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
@Mr.Xcoder - really likeand{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.
– ElPedro
Dec 31 '18 at 12:15
add a comment |
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
Try it online!
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
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
Try it online!
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
edited Dec 31 '18 at 12:23
answered Dec 31 '18 at 3:40
CCB60
1595
1595
1
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
1
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
@Mr.Xcoder - really likeand{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.
– ElPedro
Dec 31 '18 at 12:15
add a comment |
1
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
1
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
@Mr.Xcoder - really likeand{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.
– ElPedro
Dec 31 '18 at 12:15
1
1
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
178 bytes.
– Mr. Xcoder
Dec 31 '18 at 10:25
1
1
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
172 bytes. A program is cheaper than a function.
– ElPedro
Dec 31 '18 at 12:13
@Mr.Xcoder - really like
and{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.– ElPedro
Dec 31 '18 at 12:15
@Mr.Xcoder - really like
and{0}!=s
. Was trying to find a way to shorten that condition but didn't think of that. My suggestion goes to 168 if I use that.– ElPedro
Dec 31 '18 at 12:15
add a comment |
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
Try it online!
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.
add a comment |
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
Try it online!
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.
add a comment |
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
Try it online!
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.
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
Try it online!
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.
answered Dec 30 '18 at 22:26
Οurous
6,50211033
6,50211033
add a comment |
add a comment |
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]&
Try it online!
add a comment |
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]&
Try it online!
add a comment |
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]&
Try it online!
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]&
Try it online!
answered Dec 31 '18 at 22:03
Kelly Lowder
2,998416
2,998416
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178147%2ffind-equally-weighted-complete-graphs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Test cases wouldn't go amiss.
– Jonathan Allan
Dec 30 '18 at 16:32
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
Dec 30 '18 at 16:40
... and if so must they be
n
too?– Jonathan Allan
Dec 30 '18 at 16:45
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
Dec 30 '18 at 16:48
... and if so must they be n too?
– Jonathan Allan
Dec 30 '18 at 16:54