Site Loader
Rock Street, San Francisco

# Implemented using Fleury’s algorithm from# http://www.geeksforgeeks.org/fleurys-algorithm-for-printing-eulerian-path/def find_vertices(graph_dict): in_dict = {} out_dict = {} #Check each vertex and assign in and out degrees for vertex in graph_dict: if vertex not in in_dict: in_dictvertex = 0 out_dictvertex = len(graph_dictvertex) for sink in graph_dictvertex: if sink in in_dict: in_dictsink += 1 else: in_dictsink = 1 if sink not in out_dict: out_dictsink = 0 startnode, endnode = None, None for node in in_dict: ins, outs = in_dictnode, out_dictnode if outs == ins + 1: if startnode == None: startnode = node elif ins == outs + 1: if endnode == None: endnode = node return startnode, endnodedef find_cycle_starting_at(graph, startnode): from random import sample cycle = startnode node = startnode complete = False while not complete: sinks = graphnode next = sample(sinks, 1)0 if len(sinks) > 1: graphnode = sinks – set(next) else: del(graphnode) cycle.append(next) node = next complete = (node == startnode) return cycle, graphdef combine_cycles(cycle, index, new_cycle): cycle = cycle:index + new_cycle + cycleindex+1: return cycleif __name__ == ‘__main__’: # Read the input data with open(‘C:/Users/Aditi/Desktop/Compbio/Assignment3/rosalind_epath.txt’, ‘r’) as infile: file_data = infile.readlines() edges = map(str.strip, file_data) graph_dict = {} for e in edges: a, b, directed_edges = e.split(‘ ‘) graph_dicta = set(directed_edges.split(‘,’)) #Begin actual processing startnode, endnode = find_vertices(graph_dict) if endnode in graph_dict: graph_dictendnode.add(startnode) else: graph_dictendnode = set(startnode) cycle, remaining_graph = find_cycle_starting_at(graph_dict, startnode) while remaining_graph: for index, new_start in enumerate(cycle): if new_start in remaining_graph: new_cycle, remaining_graph = find_cycle_starting_at(remaining_graph, new_start) cycle = combine_cycles(cycle, index, new_cycle) break answer_path = cycle:-1 import sys for i in answer_path: sys.stdout.write(str(i)+’->’)

Post Author: admin

x

Hi!
I'm Glenda!

Would you like to get a custom essay? How about receiving a customized one?

Check it out