Created
October 14, 2012 21:50
-
-
Save melpomene/3889925 to your computer and use it in GitHub Desktop.
Parse rainbows from stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| """ | |
| Kallas med: | |
| cat bRPM_data/Yes/brpm_yes_11_1.in | python Main.py | |
| """ | |
| import sys | |
| import numpy as np | |
| from random import randint | |
| from itertools import combinations | |
| from GF import GFp | |
| def generate_biadj(edges, size): | |
| A = np.zeros((size , size)) | |
| for edge in edges: | |
| A[edge[0]][edge[1]] += 1 | |
| return A | |
| def algo1(biadj): | |
| p = 599 | |
| for i in xrange(len(biadj)): | |
| for j in xrange(len(biadj[0])): | |
| if biadj[i][j] == 1: | |
| biadj[i][j] = randint(0,p) | |
| gfp = GFp(p) | |
| det = gfp.det_gauss(biadj.tolist()) | |
| while det > p: | |
| det /= p | |
| #print "The determinant for the biadj is: " + str(det) | |
| return det != 0 | |
| def algo2(edges, n): | |
| total = 0 | |
| # run = 0 | |
| # for i in xrange(1, n+1): | |
| # for X in combinations(xrange(n), i): | |
| # run += 1 | |
| # print run | |
| for i in xrange(1, n+1): | |
| for X in combinations(xrange(n), i): | |
| G_X_edges = filter(lambda a: a[2] in X, edges) | |
| G_X = generate_biadj(G_X_edges, n) | |
| #print G_X | |
| total += (-1)**(n - len(X) ) * algo1(G_X) | |
| return total | |
| if __name__ == "__main__": | |
| # Parse input | |
| first_line = True | |
| edges = list() | |
| for line in sys.stdin: | |
| if first_line: | |
| n = int(line.split(" ")[0]) # number of colors | |
| m = int(line.split(" ")[1]) # number of lines | |
| first_line = False | |
| continue | |
| part = line.split(" ") | |
| if len(part) > 2: | |
| edges.append((int(part[0]), int(part[1]), int(part[2][0:-1]))) | |
| # Generate biadj | |
| biadj = generate_biadj(edges, n ) | |
| # Determinant | |
| p = 599 | |
| gfp = GFp(p) | |
| #print biadj | |
| #print "The determinant for the biadj is: " + str(gfp.det_gauss(biadj.tolist())) | |
| #print "Contains perfect match: " + str(algo1(biadj)) | |
| print "Running biparte Perfect Rainbow Matching check..." | |
| print "Total number of perfect rainbow matchings " + str(algo2(edges, n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment