Skip to content

Instantly share code, notes, and snippets.

View DominikPeters's full-sized avatar

Dominik Peters DominikPeters

View GitHub Profile
@DominikPeters
DominikPeters / cayley_distance.py
Created April 29, 2020 01:19
Compute Cayley distance between two rankings
# for two permutations of [0,1,...,n], compute how many swaps (not necessarily adjacent)
# are needed to transform one into the other
# code uses: distance of a permutation from the identity permutation
# equals n - #cycles in the cycle notation of the permutation
def cayley_distance(x,y):
A = range(len(x))
inv_y = tuple(y.index(a) for a in A)
comp = tuple(x[inv_y[a]] for a in A)
cycles = 0
@DominikPeters
DominikPeters / probabilistic_serial.py
Created April 29, 2020 01:08
Simple implementation of the Probabilistic Serial fair division mechanism
from fractions import Fraction
def probabilistic_serial(profile):
"input is a list of preference lists"
N = range(len(profile)) # agents
O = range(len(profile[0])) # items
supply = {o : Fraction(1,1) for o in O}
allocation = {(i,o) : Fraction(0,1) for i in N for o in O}
while any(supply.values()):
# in each iteration, at least one remaining item is fully depleted