From 216c99ac7f0cfd0c7fc8efcf02a56dafca4f06b8 Mon Sep 17 00:00:00 2001 From: Tobias Marehart <marehart@student.tugraz.at> Date: Tue, 16 Jan 2024 03:19:11 +0100 Subject: [PATCH] Bugfix: uncolored vertices --- README.md | 5 ++--- main.py | 27 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2c7f0c5..7e42d19 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,5 @@ ## Running the code -Just start main.py with python -The script creates a random graph with degree 4-19, with 500-1000 Vertices and 1000-3000 Edges -Of course you can easily modify these values \ No newline at end of file +Just start main.py with python<br/> +The script creates a randomized graph, where you can easily modify the range of degree, vertices and edges<br/> \ No newline at end of file diff --git a/main.py b/main.py index 1b73657..9711e9f 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,9 @@ class Graph: self.vertices = [] self.edges = [] + def __str__(self): + return f"{{ Vertices: {len(self.vertices)} Edges: {len(self.edges)} }}" + class Vertex: def __init__(self): self.color = -1 @@ -65,9 +68,10 @@ def PerformAlgorithm(G, degree): round = 0 collisions = CountColorCollisions(G) + uncolored = CountUncolored(G) - while(collisions > 0): - print(f"Starting round {round}. Collisions remaining: {collisions}") + while(collisions > 0 or uncolored > 0): + print(f"Starting round {round}. Collisions remaining: {collisions} Uncolored vertices: {uncolored}") for i in range(len(G.vertices)): G.vertices[i].performRound(colors) @@ -75,8 +79,17 @@ def PerformAlgorithm(G, degree): G.vertices[i].afterRound() collisions = CountColorCollisions(G) + uncolored = CountUncolored(G) round += 1 +def CountUncolored(G): + count = 0 + for i in G.vertices: + if i.color == -1: + count += 1 + + return count + def CountColorCollisions(G): collisions = 0 for i in range(len(G.edges)): @@ -85,12 +98,16 @@ def CountColorCollisions(G): collisions += 1 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) PerformAlgorithm(G, degree) collisions = CountColorCollisions(G) -print(f"After algorithm: Graph has {collisions} collisions") \ No newline at end of file +uncolored = CountUncolored(G) +print(f"After algorithm: Graph has {collisions} collisions and {uncolored} uncolored vertices") \ No newline at end of file -- GitLab