Skip to content
Snippets Groups Projects
Commit 216c99ac authored by Marehart, Tobias's avatar Marehart, Tobias
Browse files

Bugfix: uncolored vertices

parent 7e8d883b
Branches main
No related tags found
No related merge requests found
...@@ -4,6 +4,5 @@ ...@@ -4,6 +4,5 @@
## Running the code ## Running the code
Just start main.py with python Just start main.py with python<br/>
The script creates a random graph with degree 4-19, with 500-1000 Vertices and 1000-3000 Edges The script creates a randomized graph, where you can easily modify the range of degree, vertices and edges<br/>
Of course you can easily modify these values \ No newline at end of file
\ No newline at end of file
...@@ -5,6 +5,9 @@ class Graph: ...@@ -5,6 +5,9 @@ class Graph:
self.vertices = [] self.vertices = []
self.edges = [] self.edges = []
def __str__(self):
return f"{{ Vertices: {len(self.vertices)} Edges: {len(self.edges)} }}"
class Vertex: class Vertex:
def __init__(self): def __init__(self):
self.color = -1 self.color = -1
...@@ -65,9 +68,10 @@ def PerformAlgorithm(G, degree): ...@@ -65,9 +68,10 @@ def PerformAlgorithm(G, degree):
round = 0 round = 0
collisions = CountColorCollisions(G) collisions = CountColorCollisions(G)
uncolored = CountUncolored(G)
while(collisions > 0): while(collisions > 0 or uncolored > 0):
print(f"Starting round {round}. Collisions remaining: {collisions}") print(f"Starting round {round}. Collisions remaining: {collisions} Uncolored vertices: {uncolored}")
for i in range(len(G.vertices)): for i in range(len(G.vertices)):
G.vertices[i].performRound(colors) G.vertices[i].performRound(colors)
...@@ -75,8 +79,17 @@ def PerformAlgorithm(G, degree): ...@@ -75,8 +79,17 @@ def PerformAlgorithm(G, degree):
G.vertices[i].afterRound() G.vertices[i].afterRound()
collisions = CountColorCollisions(G) collisions = CountColorCollisions(G)
uncolored = CountUncolored(G)
round += 1 round += 1
def CountUncolored(G):
count = 0
for i in G.vertices:
if i.color == -1:
count += 1
return count
def CountColorCollisions(G): def CountColorCollisions(G):
collisions = 0 collisions = 0
for i in range(len(G.edges)): for i in range(len(G.edges)):
...@@ -85,12 +98,16 @@ def CountColorCollisions(G): ...@@ -85,12 +98,16 @@ def CountColorCollisions(G):
collisions += 1 collisions += 1
return collisions return collisions
degree = random.randrange(4, 20)
G = GetRandomGraph(degree, 500, 1000, 1000, 3000) degree = random.randrange(20, 50)
G = GetRandomGraph(degree, 1000, 3000, 2000, 5000)
print("Random Graph: ", str(G))
collisions = CountColorCollisions(G) collisions = CountColorCollisions(G)
PerformAlgorithm(G, degree) PerformAlgorithm(G, degree)
collisions = CountColorCollisions(G) collisions = CountColorCollisions(G)
print(f"After algorithm: Graph has {collisions} collisions") uncolored = CountUncolored(G)
\ No newline at end of file print(f"After algorithm: Graph has {collisions} collisions and {uncolored} uncolored vertices")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment