def createRandomDAGIter(n): rng = 0 graph = DirectedGraph.DirectedGraph() for i in range(0,n): node = Graph.GraphNode(i) graph.verticies.append(node) rng = random.randint(0,len(graph.verticies)-1) if len(graph.verticies) > 1 and rng != node.data: graph.addDirectedEdge(node,graph.verticies[rng]) return graph
def __init__(self, criteria): """ initialize a lattice to the equilibrium state. """ self.__graph = DirectedGraph() self.__weights = dict() #coeff self.__descriptors = dict() #{'a' : 'Gout'} self.__importanceValue = dict() self.__visited = dict() for i in range(len(criteria)-1): self.__descriptors[DESCRIPTORS[i]] = criteria[i] self.__contribute() self.__equilibrate()
def createRandomDAGIter(n: int) -> DirectedGraph: g = DirectedGraph.DirectedGraph() taken = set() for i in range(n): g.addNode(random.randint(0, 10 * n)) for i in range(random.randint(1, n - 1)): first = random.sample(g.getAllNodes(), 1)[0] second = random.sample(g.getAllNodes(), 1)[0] if first != second and (first not in taken or second not in taken): g.addDirectedEdge( random.sample(g.getAllNodes(), 1)[0], random.sample(g.getAllNodes(), 1)[0]) taken.add(first) taken.add(second) return g
def remove_unit_productions(self): g = DirectedGraph() productions = self.productions.copy() for key in productions: # construimos el grafo for rule in productions[key]: if len(rule) == 1 and rule in self.nonterminals: g.add_edge(key, rule) followings = { v: g.vertices_forward(v) for v in g.vertices() } not_unit = dict() not_unit_fun = lambda s: len(s) != 1 or s in self.terminals for vertex in g.vertices(): not_unit[vertex] = set(filter(not_unit_fun, productions[vertex])) for e0, e1 in g.edges(): productions[e0].remove(e1) for node in followings[e0]: productions[e0] = productions[e0] | not_unit[node] return productions
def setUp(self): self.graph = DirectedGraph.DirectedGraph() self.graph.insert(1, 2) self.graph.insert(1, 3) self.graph.insert(2, 3) self.graph.insert(2, 4) self.graph.insert(3, 5) self.graph.insert(3, 6) self.graph.insert(5, 6) self.graph.insert(6, 7) self.graph.insert(7, 5) self.graph.insert(6, 8) self.graph.insert(4, 9) self.graph.insert(4, 10) self.graph.insert(8, 9) self.graph.insert(8, 11) self.graph.insert(9, 10) self.graph.insert(10, 11) self.graph.insert(11, 9)
random.sample(g.getAllNodes(), 1)[0]) taken.add(first) taken.add(second) return g if __name__ == "__main__": # just here for printout def printvals(lst: list) -> None: for i in range(len(lst) - 1): print(lst[i].val, end=" -> ") if len(lst) > 0: print(lst[-1].val) graph = DirectedGraph.DirectedGraph() graph.addNode('A') graph.addNode('B') graph.addNode('C') graph.addNode('D') graph.addNode('E') graph.addNode('F') graph.addNode('G') graph.addNode('H') graph.nodes[0].neighbors = set([graph.nodes[1], graph.nodes[3]]) graph.nodes[2].neighbors = set( [graph.nodes[3], graph.nodes[6], graph.nodes[7]]) graph.nodes[3].neighbors = set([graph.nodes[6]]) graph.nodes[7].neighbors = set([graph.nodes[4], graph.nodes[5]])
from DirectedGraph import * g = DirectedGraph() def test1(): g.add_edge('a', 'b') g.add_edge('b', 'f') g.add_edge('c', 'd') g.add_edge('d', 'h') g.add_edge('f', 'd') g.add_edge('f', 'i') g.add_edge('g') g.add_edge('i', 'e') g.add_edge('e', 'e') print('g.out_edges') for a in {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}: print(a) print(g.out_edges(a)) def test2(): g.add_edge('s', 'b') g.add_edge('b', 'a') g.add_edge('a', 'b') for a in {'s', 'a', 'b'}: print(a) print(g.out_edges(a))