Number of Acyclic Alkane Isomers - Python Version
In the previous post I have outlined the method of calculating the number of open-chain alkane isomers in R. Since the calculation times for higher alkanes were impractically long, I have loosely translated, with minor changes, my program into python. The code follows below. As for the results - no substantial improvement could be observed. My next attempt would be in Java.
import networkx as nx
import matplotlib.pyplot as plt
import copy
from datetime import datetime
def addc(graph):
n = graph.number_of_nodes()
list = []
for i in range(n):
if graph.degree[i] < 4:
graph_c = copy.deepcopy(graph)
graph_c.add_node(n)
graph_c.add_edge(i, n)
res = [nx.is_isomorphic(gr, graph_c) for gr in list]
if not any(res):
list.append(graph_c)
return list
def comblist(list1, list2):
list_out = copy.deepcopy(list1)
for gr2 in list2:
res = [nx.is_isomorphic(gr2,gr1) for gr1 in list1]
if not any(res):
list_out.append(gr2)
return list_out
def generate_isomers(depth):
propane = nx.Graph()
propane.add_nodes_from([0,1,2])
propane.add_edge(0,1)
propane.add_edge(1,2)
iter_list = []
iter_list.append(propane)
for a in range(4, depth+1):
total = []
for grf in iter_list:
new_list = addc(grf)
if not total:
total = total + new_list
else:
total = comblist(total, new_list)
nisomers = len(total)
print(datetime.now())
print(nisomers)
iter_list = copy.deepcopy(total)
return nisomers
To run it simply pass the desired number of carbons:
generate_isomers(10)
Comments
Post a Comment