Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Eaa Bonus
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kirillova, Tatiana
Eaa Bonus
Commits
aa550795
Commit
aa550795
authored
1 year ago
by
Kirillova, Tatiana
Browse files
Options
Downloads
Patches
Plain Diff
added tests; added readme
parent
09b5a528
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
bonus.py
+155
-11
155 additions, 11 deletions
bonus.py
with
155 additions
and
11 deletions
bonus.py
+
155
−
11
View file @
aa550795
...
...
@@ -2,6 +2,8 @@ import random
# python 3.10.12
random
.
seed
(
None
)
class
Node
:
def
__init__
(
self
):
self
.
color
=
0
...
...
@@ -21,7 +23,8 @@ class Edge:
class
Graph
:
def
__init__
(
self
,
delta
,
nodes
,
edges
):
def
__init__
(
self
,
name
,
delta
,
nodes
,
edges
):
self
.
name
=
name
self
.
delta
=
delta
self
.
nodes
=
nodes
self
.
edges
=
edges
...
...
@@ -43,6 +46,7 @@ class Graph:
available_colors
=
self
.
colors
.
copy
()
for
neighbor
in
node
.
neighbors
:
if
neighbor
.
perm
:
if
neighbor
.
color
in
available_colors
:
available_colors
.
remove
(
neighbor
.
color
)
node
.
changeColor
(
random
.
choice
(
available_colors
))
for
neighbor
in
node
.
neighbors
:
...
...
@@ -53,20 +57,160 @@ class Graph:
self
.
uncolored
-=
1
def
main
():
random
.
seed
(
None
)
def
isDegreeCorrect
(
graph
):
'''
Returns True if none of the nodes exceeds
the maximum degree.
'''
delta
=
1
nodes
=
[
Node
(),
Node
(),
Node
()]
edges
=
[
Edge
(
nodes
[
0
],
nodes
[
1
]),
Edge
(
nodes
[
1
],
nodes
[
2
])]
for
node
in
graph
.
nodes
:
if
len
(
node
.
neighbors
)
>
graph
.
delta
:
print
(
f
"
{
graph
.
name
}
'
s degree is INCORRECT! Delta is
{
graph
.
delta
}
but a node has
{
len
(
node
.
neighbors
)
}
neighbors
"
)
return
False
print
(
f
"
{
graph
.
name
}
'
s degree is correct!
"
)
return
True
graph
=
Graph
(
delta
,
nodes
,
edges
)
def
compute
(
graph
):
'''
compute the coloring
'''
while
graph
.
uncolored
:
graph
.
color
()
for
i
,
n
in
enumerate
(
graph
.
nodes
):
print
(
f
'
{
i
}
:
{
n
.
color
}
'
)
# # uncomment to print the nodes and their assigned color:
# for e in graph.edges:
# print(f'{e.v.color} - {e.u.color}')
def
graphTiny
():
'''
3 nodes, connected in a line
'''
delta
=
2
nodes
=
[
Node
(),
Node
(),
Node
()]
edges
=
[
Edge
(
nodes
[
0
],
nodes
[
1
]),
Edge
(
nodes
[
1
],
nodes
[
2
])]
return
Graph
(
"
Tiny
"
,
delta
,
nodes
,
edges
)
def
graphSimple
():
'''
5 nodes, connected in a line
'''
delta
=
2
nodes
=
[]
for
_
in
range
(
5
):
nodes
.
append
(
Node
())
edges
=
[]
for
i
in
range
(
4
):
edges
.
append
(
Edge
(
nodes
[
i
],
nodes
[
i
+
1
]))
return
Graph
(
"
Simply
"
,
delta
,
nodes
,
edges
)
def
graphBob
():
'''
300 nodes, each node is connected to the next one
and the one after that
'''
delta
=
4
nodes
=
[]
for
_
in
range
(
300
):
nodes
.
append
(
Node
())
edges
=
[]
for
i
in
range
(
298
):
edges
.
append
(
Edge
(
nodes
[
i
],
nodes
[
i
+
1
]))
edges
.
append
(
Edge
(
nodes
[
i
],
nodes
[
i
+
2
]))
return
Graph
(
"
BobThreeHundred
"
,
delta
,
nodes
,
edges
)
def
graphRandom
(
min_d
,
max_d
,
min_n
,
max_n
):
delta
=
random
.
randint
(
min_d
,
max_d
)
nodes
=
{}
# node - connections_left
for
_
in
range
(
random
.
randint
(
min_n
,
max_n
)):
nodes
[
Node
()]
=
delta
edges
=
[]
for
node
in
nodes
.
keys
():
for
another_node
in
nodes
:
if
node
is
not
another_node
:
if
nodes
[
node
]
and
nodes
[
another_node
]:
edges
.
append
(
Edge
(
node
,
another_node
))
nodes
[
node
]
-=
1
nodes
[
another_node
]
-=
1
return
Graph
(
f
"
Randy
{
delta
}
-
{
len
(
nodes
)
}
"
,
delta
,
nodes
,
edges
)
def
graphComplete
(
delta
):
'''
Returns a complete graph of maximum degree delta
'''
nodes
=
[]
available_nodes
=
[]
for
_
in
range
(
delta
+
1
):
node
=
Node
()
nodes
.
append
(
node
)
available_nodes
.
append
(
node
)
edges
=
[]
for
node
in
nodes
:
for
another_node
in
available_nodes
:
if
node
is
not
another_node
:
edges
.
append
(
Edge
(
node
,
another_node
))
available_nodes
.
remove
(
node
)
return
Graph
(
f
"
Complete
{
delta
}
"
,
delta
,
nodes
,
edges
)
def
isCorrect
(
colored_graph
):
'''
Returns True if the graph is correctly colored
Returns False otherwise
'''
for
edge
in
colored_graph
.
edges
:
if
edge
.
v
.
color
is
edge
.
u
.
color
:
return
False
return
True
def
check
(
graph
):
'''
Check if the coloring is correct
'''
if
isCorrect
(
graph
):
# print(f"Colors: {len(graph.colors)}, delta: {graph.delta}.")
print
(
f
"
{
graph
.
name
}
is colored correctly!
\n
"
)
else
:
print
(
f
"
Something went wrong with
{
graph
.
name
}
...
\n
"
)
def
randomTest
(
i
):
'''
Many random tests
'''
while
i
:
graph
=
graphRandom
(
min_d
=
10
,
max_d
=
50
,
min_n
=
100
,
max_n
=
500
)
print
(
"
-------------------------------------------------------
\n
"
)
compute
(
graph
)
check
(
graph
)
i
-=
1
def
test
():
'''
Each of the functions returns a graph
To test a graph create one: Graph(name, delta, nodes, edges)...
or use the graphComplete(delta) function to create a complete graph with any max degree you like...
...and add it to the list
'''
graphs
=
[
graphTiny
(),
graphSimple
(),
graphBob
(),
graphComplete
(
4
),
graphComplete
(
200
),
graphRandom
(
min_d
=
10
,
max_d
=
50
,
min_n
=
100
,
max_n
=
500
)
]
for
graph
in
graphs
:
print
(
"
-------------------------------------------------------
\n
"
)
# # uncomment to check degree:
isDegreeCorrect
(
graph
)
compute
(
graph
)
check
(
graph
)
if
__name__
==
"
__main__
"
:
main
()
\ No newline at end of file
test
()
# randomTest(20)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment