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
4
Test cases wouldn't go amiss.
– Jonathan Allan
7 hours ago
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
7 hours ago
... and if so must they ben
too?
– Jonathan Allan
7 hours ago
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
7 hours ago
... and if so must they be n too?
– Jonathan Allan
7 hours ago
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 7 hours ago
Arcturus
3,54011964
3,54011964
4
Test cases wouldn't go amiss.
– Jonathan Allan
7 hours ago
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
7 hours ago
... and if so must they ben
too?
– Jonathan Allan
7 hours ago
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
7 hours ago
... and if so must they be n too?
– Jonathan Allan
7 hours ago
add a comment |
4
Test cases wouldn't go amiss.
– Jonathan Allan
7 hours ago
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
7 hours ago
... and if so must they ben
too?
– Jonathan Allan
7 hours ago
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
7 hours ago
... and if so must they be n too?
– Jonathan Allan
7 hours ago
4
4
Test cases wouldn't go amiss.
– Jonathan Allan
7 hours ago
Test cases wouldn't go amiss.
– Jonathan Allan
7 hours ago
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
7 hours ago
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
7 hours ago
... and if so must they be
n
too?– Jonathan Allan
7 hours ago
... and if so must they be
n
too?– Jonathan Allan
7 hours ago
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
7 hours ago
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
7 hours ago
... and if so must they be n too?
– Jonathan Allan
7 hours ago
... and if so must they be n too?
– Jonathan Allan
7 hours ago
add a comment |
3 Answers
3
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 |
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 |
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
3 Answers
3
active
oldest
votes
3 Answers
3
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 2 hours ago
answered 3 hours ago
Erik the Outgolfer
31.3k429103
31.3k429103
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 2 hours ago
answered 6 hours ago
Jonathan Allan
50.7k534165
50.7k534165
add a comment |
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 1 hour ago
Οurous
6,44311033
6,44311033
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
4
Test cases wouldn't go amiss.
– Jonathan Allan
7 hours ago
Can complete graphs contain redundant self-connections (e.g. A to A)?
– Jonathan Allan
7 hours ago
... and if so must they be
n
too?– Jonathan Allan
7 hours ago
@JonathanAllan Complete graphs can but do not necessarily contain self-connections.
– Arcturus
7 hours ago
... and if so must they be n too?
– Jonathan Allan
7 hours ago