From 4c32c46982de2f08ce7f818735d88f458100a111 Mon Sep 17 00:00:00 2001 From: AlexVanin Date: Thu, 17 Mar 2016 16:47:11 +0300 Subject: [PATCH] PsyFinder --- findpsy.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ input_example | 8 ++++++++ 2 files changed, 61 insertions(+) create mode 100755 findpsy.py create mode 100755 input_example diff --git a/findpsy.py b/findpsy.py new file mode 100755 index 0000000..794c176 --- /dev/null +++ b/findpsy.py @@ -0,0 +1,53 @@ +#!/usr/bin/python + +""" +input file - connection matrix for a graph +numbers with ; as separator + +ex. + +0;1;2 +1;0;3 +2;3;0 +""" + +import sys + +#Initial node for search \ start element in result chain. +start = 0; + +all_paths = [] + +def deep (path, result): + for i in range(path[-1], len(matrix[0])): + if matrix[path[-1]][i] == 0: + new_path = list(path) + new_result = list(result) + + new_path.append(i) + + for j in range(0, len(new_result)): + new_result[j] = new_result[j] | matrix[i][j] + + if all(v > 0 for v in new_result): + print "PATH: {0}".format(new_path) + all_paths.append(new_path) + return 1 + else: + deep (new_path, new_result) + + return 0 + + +f = open (sys.argv[1]) +matrix = [ map(int,line.split(';')) for line in f ] + + +while True: + my_path = [ start ] + my_result = list(matrix[start]) + deep( my_path, my_result ) + if len(matrix[0]) > start + 1: + start += 1 + else: + break \ No newline at end of file diff --git a/input_example b/input_example new file mode 100755 index 0000000..43dc868 --- /dev/null +++ b/input_example @@ -0,0 +1,8 @@ +1;1;1;0;0;0;0;0 +1;1;0;1;0;0;1;1 +1;0;1;1;1;1;0;0 +0;1;1;1;0;0;0;0 +0;0;1;0;1;0;1;1 +0;0;1;0;0;1;1;0 +0;1;0;0;1;1;1;0 +0;1;0;0;1;0;0;1