Commit 1bce6e2d authored by Jirka Fink's avatar Jirka Fink
Browse files

Add 1st assignment

parent 2001086a
import gurobipy as gp
from gurobipy import GRB
def maximum_weighted_matching(vertices, edges):
Compute the maximal matching of a given multigraph
vertices - The number of vertices
edges - A list of edges; every edge is a triple of two vertices (from 0 to vertices-1) and weight
return - A subset of edges forming a maximal matching
env = gp.Env("")
env.setParam('LogToConsole', 0)
model = gp.Model(env=env)
# TODO: Find the maximal matching
return []
#!/usr/bin/env python3
import sys
import random
import copy
from time import process_time
from prettytable import PrettyTable
from matching import maximum_weighted_matching
# Test whether the given matching is already a matching and its weight is maximal
# Returns a pair of a boolean (whether all tests passed) and an error message string
def verify_matching(vertex_cnt, edges, matching, optimal_weight):
edges_set = set(edges)
for m in matching:
if not m in edges_set:
return (False, "Element {} is not an edge".format(m))
covered = [ False for _ in range(vertex_cnt) ]
for e in matching:
for u in [e[0], e[1]]:
if covered[u]:
return (False, "Vertex {} is covered by more than one edge".format(u))
covered[u] = True
weight = sum(e[2] for e in matching)
if weight < optimal_weight:
return (False, "The weight of your matching is not maximal")
if weight > optimal_weight:
return (False, "Solution is better than the optimal one which should be impossible")
return (True, "Correct solution")
# Run one test with a given instance parameters
def evaluate(vertex_cnt, edge_cnt, is_bipartite, seed, optimal_weight, max_weight):
rng = random.Random(seed)
if is_bipartite:
edges = [ (rng.randrange(0, vertex_cnt, 2), rng.randrange(1, vertex_cnt, 2), rng.randrange(1, max_weight+1)) for _ in range(edge_cnt) ]
edges = [ (rng.randrange(0, vertex_cnt), rng.randrange(0, vertex_cnt), rng.randrange(1, max_weight+1)) for _ in range(edge_cnt) ]
edges = [ (u,v,w) for (u,v,w) in edges if u != v ]
matching = maximum_weighted_matching(vertex_cnt, copy.deepcopy(edges))
return verify_matching(vertex_cnt, edges, matching, optimal_weight)
def main():
tests = {
"trivial": (10, 15, False, 2157, 2453, 1000, 0, 0.01),
"general_sparse_unweighted": (100, 200, False, 45876, 49, 1, 2, 4),
"bipartite_dense": (500, 1000000, True, 98754, 249987, 1000, 2, 10),
"general_dense": (500, 1000000, False, 2134, 249987, 1000, 2, 20),
"bipartite_sparse": (10000, 30000, True, 4578, 3700952, 1000, 2, 1.5),
"general_sparse": (10000, 30000, False, 7568, 3691312, 1000, 2, 1.5),
if len(sys.argv) == 1:
results = PrettyTable(["Instance name", "Points", "Vertices", "Edges", "Expected time [s]", "Your time [s]", "Evaluation"])
for name in tests:
print("Running test", name)
vertices, edges, bipartite, seed, optimal_weight, max_weight, points, expected_time = tests[name]
start_time = process_time()
status,msg = evaluate(vertices, edges, bipartite, seed, optimal_weight, max_weight)
running_time = process_time() - start_time
results.add_row([name, points, vertices, edges, expected_time, running_time, msg])
name = sys.argv[1]
if name in tests:
status,msg = evaluate(*tests[name])
print("Unknown test", name)
To run all tests, run the command
$ python3
To run a test NAME, run the command
$ python3 NAME
if __name__ == "__main__":
A matching of a (multi)graph is a subset of edges such that every vertex is covered by at most one edge. The weight of a matching is the sum of weights of edges of the matching.
The task is to implement the function *maximum_weighted_matching* in the file ** which is expected to find an maximum weighted matching in a given weighed graph. Note that your program has to find an optimal matching for any graph assuming sufficient amount of memory and time is provided.
You are expected to submit the file ** only. The reference computer will be Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz with 8 GB RAM.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment