Backpack corrections and updates for file validation

This commit is contained in:
2025-06-14 12:15:20 +02:00
parent 12eb1d34a4
commit 878470fe73
3 changed files with 99 additions and 17 deletions
+47 -1
View File
@@ -1,6 +1,8 @@
import random
import time
import matplotlib.pyplot as plt
import sys
from typing import List
def generateGraph(n: int, saturation: int):
edges = (n * (n - 1)) // 2
@@ -49,7 +51,16 @@ def generateGraph(n: int, saturation: int):
return graph
def findEulerianCycle(graph_adj_sets):
def graphFromMatrix(matrix: List[List[int]]):
n = len(matrix)
graph = {i: [] for i in range(n)}
for i in range(n):
for j in range(n):
if matrix[i][j] == 1:
graph[i].append(j)
return graph
def findEulerianCycle(graph_adj_sets) -> List[int] | None:
current_graph = {v: set(neighbors) for v, neighbors in graph_adj_sets.items()}
if not current_graph:
@@ -113,6 +124,41 @@ def findHamiltonianCycle(graph):
return None
if __name__ == "__main__":
# Check if there was given a second argument, if yes run a test instead of the benchmark
if len(sys.argv) > 1:
file = sys.argv[1]
"""
Format we are expecting:
0 0 1 1 1 1
0 0 1 1 0 0
1 1 0 0 1 1
1 1 0 0 1 1
1 0 1 1 0 1
1 0 1 1 1 0
"""
with open(file, 'r') as f:
lines = f.readlines()
matrix = [list(map(int, line.strip().split())) for line in lines]
graph = graphFromMatrix(matrix)
euler = findEulerianCycle(graph)
hamilton = findHamiltonianCycle(graph)
if euler is None:
print("Eulerian cycle not found.")
exit(1)
if hamilton is None:
print("Hamiltonian cycle not found.")
exit(1)
# We were asked to print the cycles starting from 1. Add the first node to the end of the list to make it a cycle
# Expected: 1 6 5 4 6 3 5 1 4 2 3 1 | Actual: 1 3 2 4 1 5 3 6 4 5 6 1
print("Eulerian Cycle:", [v + 1 for v in euler ])
# 1 3 2 4 5 6 1 | Real: 1 3 2 4 5 6 1
print("Hamiltonian Cycle", [v + 1 for v in hamilton] + [hamilton[0] + 1])
exit(0)
n_values = range(10, 26) # Number of nodes to test
saturations = [30, 70] # Saturation levels to test
results = {}
+34
View File
@@ -1,6 +1,8 @@
import random
import time
import matplotlib.pyplot as plt
import sys
from typing import List
def generate_hamiltonian_graph(n, saturation_percent):
max_possible_edges = n * (n - 1) // 2
@@ -34,6 +36,16 @@ def generate_hamiltonian_graph(n, saturation_percent):
return {k: list(v) for k, v in graph.items()}
def graphFromMatrix(matrix: List[List[int]]):
n = len(matrix)
graph = {i: [] for i in range(n)}
for i in range(n):
for j in range(n):
if matrix[i][j] == 1:
graph[i].append(j)
return graph
def find_all_hamiltonian_cycles(graph):
n = len(graph)
cycles = set()
@@ -65,6 +77,28 @@ def find_all_hamiltonian_cycles(graph):
return list(cycles)
if __name__ == "__main__":
if len(sys.argv) > 1:
file = sys.argv[1]
"""
Format we are expecting:
0 0 1 1 1 1
0 0 1 1 0 0
1 1 0 0 1 1
1 1 0 0 1 1
1 0 1 1 0 1
1 0 1 1 1 0
"""
with open(file, 'r') as f:
lines = f.readlines()
matrix = [list(map(int, line.strip().split())) for line in lines]
graph = graphFromMatrix(matrix)
cycles = find_all_hamiltonian_cycles(graph)
for cycle in cycles:
print(cycle)
exit(0)
n_values = range(5, 15)
saturation = 50