Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
K
KTLA Study
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kirill Smelkov
KTLA Study
Commits
0b525e3e
Commit
0b525e3e
authored
Feb 15, 2022
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
2c427c0f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
0 deletions
+60
-0
allgraphs.py
allgraphs.py
+60
-0
No files found.
allgraphs.py
0 → 100755
View file @
0b525e3e
#!/usr/bin/env python
"""generate all connected graphs up to N nodes"""
from
__future__
import
print_statement
class
Graph
:
# .nodes {nodes}
# .edges {edges}
def
__init__
(
nodes
,
edges
):
self
.
nodes
=
set
(
nodes
)
self
.
edges
=
set
(
edges
)
def
__str__
(
self
):
return
"Graph nodes=%s edges=%s"
%
(
self
.
nodes
,
self
.
edges
)
# generate all connected graphs with N(nodes) =< N
def
genGraphs
(
N
):
assert
N
>=
1
nodes
=
[]
# of all nodes
Sprevedges
=
set
()
# set of edges for all graphs with (i-1) nodes
for
i
in
range
(
N
):
n
=
(
'r'
if
i
==
0
else
'n%d'
%
i
)
nodes
=
nodes
+
[
n
]
Sedges
=
set
()
# the graph remains connected if previous (N-1) graph is connected, and n
# is connected to at least one node in the previous graph.
# for all combinations to which nodes n is connected in the previous graph.
for
j
in
range
(
1
,
2
**
(
i
-
1
)):
Snewconn
=
set
()
for
k
in
range
(
i
-
1
):
if
j
&
(
1
<<
k
):
Snewconn
.
add
((
nodes
[
k
],
n
))
for
edges
in
Sprevedges
:
for
newconn
in
Snewconn
:
newedges
=
edges
.
union
(
newconn
)
Sedges
.
add
(
newedges
)
yield
Graph
(
node
,
newedges
)
Sprevedges
=
Sedges
def
main
():
N
=
int
(
sys
.
argv
[
1
])
for
g
in
genGraphs
(
N
):
print
(
g
)
if
__name__
==
'__main__'
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment