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
4fa0b3f6
Commit
4fa0b3f6
authored
Feb 15, 2022
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
1859fc9f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
10 deletions
+36
-10
allgraphs.py
allgraphs.py
+36
-10
No files found.
allgraphs.py
View file @
4fa0b3f6
...
...
@@ -14,7 +14,15 @@ class Graph:
g
.
edges
=
set
(
edges
)
def
__str__
(
g
):
return
"Graph nodes=%s edges=%s"
%
(
g
.
nodes
,
g
.
edges
)
nv
=
[
'%s'
%
_
for
_
in
g
.
nodes
]
ev
=
[
setstr
(
_
)
for
_
in
g
.
edges
]
return
"Graph nodes={%s} edges={%s}"
%
(
','
.
join
(
nv
),
', '
.
join
(
ev
))
def
setstr
(
s
):
assert
isinstance
(
s
,
(
set
,
frozenset
)),
s
v
=
[
'%s'
%
(
_
,)
for
_
in
s
]
return
'{%s}'
%
','
.
join
(
v
)
# generate all connected graphs with N(nodes) =< N
...
...
@@ -25,25 +33,43 @@ def genGraphs(N):
Sprevedges
=
set
()
# set of edges for all graphs with (i-1) nodes
for
i
in
range
(
1
,
N
+
1
):
print
()
n
=
(
'r'
if
i
==
1
else
'n%d'
%
(
i
-
1
))
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.
# only 1 node
if
i
==
1
:
yield
Graph
(
nodes
,
set
())
continue
# only 2 nodes
if
i
==
2
:
yield
Graph
(
nodes
,
set
([
frozenset
(
nodes
),]))
Sprevedges
.
add
(
frozenset
([
frozenset
(
nodes
)]))
continue
# induction: the graph remains connected if previous (N-1) graph is
# connected, and n is connected to at least one node in the previous graph.
Sedges
=
set
()
# for all combinations to which nodes n is connected in the previous graph.
for
j
in
range
(
1
,
1
<<
(
i
-
1
)):
S
newconn
=
set
()
newconn
=
set
()
for
k
in
range
(
i
-
1
):
if
j
&
(
1
<<
k
):
Snewconn
.
add
((
nodes
[
k
],
n
))
newconn
.
add
(
frozenset
([
nodes
[
k
],
n
]))
#print("j: %d\tnewconn: %s" % (j, newconn))
#print("Sprevedges: %s" % Sprevedges)
for
edges
in
Sprevedges
:
for
newconn
in
Snewconn
:
newedges
=
edges
.
union
(
newconn
)
Sedges
.
add
(
newedges
)
yield
Graph
(
nodes
,
newedges
)
newedges
=
edges
.
union
(
newconn
)
#print("edges: %s" % edges)
#print("newconn: %s" % newconn)
#print("newedges: %s" % newedges)
Sedges
.
add
(
newedges
)
yield
Graph
(
nodes
,
newedges
)
Sprevedges
=
Sedges
...
...
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