Backpack corrections and updates for file validation
This commit is contained in:
+47
-1
@@ -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 = {}
|
||||
|
||||
Reference in New Issue
Block a user