def convert_graphs_to_text(graphs): # For each graph convert it into a paragraph paragraphs = {} for key, graph in graphs.items(): # Implement graph to text summary = "" if not is_directed_acyclic_graph(graph): print("Graph is not a DAG for ", key) #print(graph.edges) #for e in graph.edges: # print(e) #print(len(list(nx.simple_cycles(graph))), " cycles for ", key) max_len = 10000000 smallest_cycle = [] for c in nx.simple_cycles(graph): if len(c) < max_len: max_len = len(c) smallest_cycle = c for v in smallest_cycle: print(v) #continue topo_sorted_vertices = nx.topological_sort(graph) i = 0 for vertex in topo_sorted_vertices: i += 1 tokens = vertex.split('#') summary += tokens[0] if i != graph.number_of_nodes(): # Prevent an extra '.' from appearing at the end summary += '.' paragraphs[key] = summary return paragraphs
def __init__(self, g, X, causes, effects, variable_types=None, expectation=False, density=True): if not is_directed_acyclic_graph(g): raise Exception("Directed, Acyclic graph required.") self.g = g self.causes = causes self.effects = effects self.find_predecessors() self.admissable_set = self.predecessors if self.assumptions_satisfied(): self.effect = CausalEffect(X, causes, effects, admissable_set=list(self.predecessors), variable_types=variable_types, expectation=expectation, density=density) else: raise Exception("Adjustment assumptions not satisfied")
def assumptions_satisfied(self, g, causes, effects, predecessors): if not is_directed_acyclic_graph(g): return False if not len(set(effects).intersection( set(causes).union(predecessors))) == 0: return False return True
def prog_16(fname): graphs = {} f = open(fname) n = map(int, f.readline().strip().split()) n = n[0] f.readline() for i in xrange(n): graph = nx.DiGraph() line = f.readline() while line: edges = map(int, line.strip().split()) graph.add_edge(edges[0],edges[1]) line = f.readline() # print line line = line.strip() # print 'xxxxx ',line graphs[i]=graph f.close() print graphs for k,g in graphs.iteritems(): if is_directed_acyclic_graph(g): print 1, else: print -1,
def deterministic_topological_ordering(nodes, links, start_node): """ Topological sort that is deterministic because it sorts (alphabetically) candidates to check """ graph = DiGraph() graph.add_nodes_from(nodes) for link in links: graph.add_edge(*link) if not is_directed_acyclic_graph(graph): raise NetworkXUnfeasible task_names = sorted(graph.successors(start_node)) task_set = set(task_names) graph.remove_node(start_node) result = [start_node] while task_names: for name in task_names: if graph.in_degree(name) == 0: result.append(name) # it is OK to modify task_names because we break out # of loop below task_names.remove(name) new_successors = [ t for t in graph.successors(name) if t not in task_set ] task_names.extend(new_successors) task_names.sort() task_set.update(set(new_successors)) graph.remove_node(name) break return result
def deterministic_topological_ordering(nodes, links, start_node): """ Topological sort that is deterministic because it sorts (alphabetically) candidates to check """ graph = DiGraph() graph.add_nodes_from(nodes) for link in links: graph.add_edge(*link) if not is_directed_acyclic_graph(graph): raise NetworkXUnfeasible task_names = sorted(graph.successors(start_node)) task_set = set(task_names) graph.remove_node(start_node) result = [start_node] while task_names: for name in task_names: if graph.in_degree(name) == 0: result.append(name) # it is OK to modify task_names because we break out # of loop below task_names.remove(name) new_successors = [t for t in graph.successors(name) if t not in task_set] task_names.extend(new_successors) task_names.sort() task_set.update(set(new_successors)) graph.remove_node(name) break return result
def assumptions_satisfied(self, g, causes, effects, predecessors): if not is_directed_acyclic_graph(g): return False if not len(set(effects).intersection(set(causes).union(predecessors))) == 0: return False return True