diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-07-10 00:28:05 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2013-07-10 00:28:05 +0200 |
commit | b9181e4e7bdcb67091a1c6bb7bb76be1192ed2b0 (patch) | |
tree | 24953413d5ddf2352c9b2bbef226a5c891c46dab /02-graph-coloring/solver.py | |
parent | d80f03f5f99faa93861a3019657db406d7293fa7 (diff) | |
download | coursera-b9181e4e7bdcb67091a1c6bb7bb76be1192ed2b0.zip coursera-b9181e4e7bdcb67091a1c6bb7bb76be1192ed2b0.tar.gz |
Discrete : add 02-graph-coloring assignment
Diffstat (limited to '02-graph-coloring/solver.py')
-rw-r--r-- | 02-graph-coloring/solver.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/02-graph-coloring/solver.py b/02-graph-coloring/solver.py new file mode 100644 index 0000000..f929b1d --- /dev/null +++ b/02-graph-coloring/solver.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + + +def solveIt(inputData): + # Modify this code to run your optimization algorithm + + # parse the input + lines = inputData.split('\n') + + firstLine = lines[0].split() + nodeCount = int(firstLine[0]) + edgeCount = int(firstLine[1]) + + edges = [] + for i in range(1, edgeCount + 1): + line = lines[i] + parts = line.split() + edges.append((int(parts[0]), int(parts[1]))) + + # build a trivial solution + # every node has its own color + solution = range(0, nodeCount) + + # prepare the solution in the specified output format + outputData = str(nodeCount) + ' ' + str(0) + '\n' + outputData += ' '.join(map(str, solution)) + + return outputData + + +import sys + +if __name__ == '__main__': + if len(sys.argv) > 1: + fileLocation = sys.argv[1].strip() + inputDataFile = open(fileLocation, 'r') + inputData = ''.join(inputDataFile.readlines()) + inputDataFile.close() + print solveIt(inputData) + else: + print 'This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/gc_4_1)' + |