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
+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