def read_graphml(filename): from BeautifulSoup import BeautifulSoup as Soup soup = Soup(open(filename).read()) graph = Graph() for edge in soup.findAll("edge"): source = int(edge['source']) target = int(edge['target']) graph.add_edge(source, target) return graph
def read_gml(filename): graph = Graph() data = read_gml_data(filename) for n in data['graph']['node']: graph.add_node(int(n['id'])) for e in data['graph']['edge']: graph.add_edge(int(e['source']), int(e['target'])) return graph
def read_edgelist(filename): graph = Graph() for line in open(filename).readlines(): line = line.strip() if line[0] == '#': continue source, target = line.split() s = int(source) t = int(target) graph.add_edge(s, t) return graph
def path(num_vertices): """ Creates a path of size num_vertices and returns a Graph object representing it :param num_vertices: The number of vertices in the path :return: A Graph object representing path """ # Instantiate a Graph pattern = Graph() # Populate it for u in range(num_vertices - 1): pattern.add_edge(u, u + 1) # Return the path return pattern
def cycle(num_vertices): """ Creates a cycle of size num_vertices and returns a Graph object representing it :param num_vertices: The number of vertices in the cycle :return: A Graph object representing cycle """ # Instantiate a Graph pattern = Graph() # Populate it for u in range(num_vertices): pattern.add_edge(u, (u + 1) % num_vertices) # Return the cycle return pattern
def clique(num_vertices): """ Creates a clique of size num_vertices and returns a Graph object representing it :param num_vertices: The number of vertices in the clique :return: A Graph object representing clique """ # Instantiate a Graph pattern = Graph() # Populate it for u in range(num_vertices): for v in range(u + 1, num_vertices): pattern.add_edge(u, v) # Return the clique return pattern
def star(num_vertices): """ Creates a star of size num_vertices and returns a Graph object representing it :param num_vertices: The number of vertices in the star :return: A Graph object representing star """ # Instantiate a Graph pattern = Graph() # Populate it for v in range(num_vertices): if v != 0: pattern.add_edge(0, v) # Return the star return pattern
def biclique(m, n): """ Creates a biclique of size m + n and returns a Graph object representing it :param m: The number of vertices in the first set :param n: The number of vertices in the second set :return: A Graph object representing the biclique """ # Instantiate a Graph pattern = Graph() for u in range(m): for v in range(m, m + n): pattern.add_edge(u, v) # Return the biclique return pattern
def truncated_tf_augmentation(orig, g, trans, frat, col, nodes, step, td, ldoFunc): fratGraph = Graph() newTrans = {} for v in g: for x, y, _ in g.trans_trips_weight(v, step): newTrans[(x, y)] = step # assert (not g.adjacent(x, y)), "{0} {1} transitive but # adjacent".format(x, y) for x, y, _ in g.frat_trips_weight(v, step): fratGraph.add_edge(x, y) # assert (not g.adjacent(x, y)), "{0} {1} fraternal but # adjacent".format(x, y, list(g.arcs)) for (s, t) in newTrans: g.add_arc(s, t, step) fratGraph.remove_edge(s, t) indegs = [] for v in g: #Flexible indegs[v] = g.in_degree(v) if v < len(indegs): indegs[v] = g.in_degree(v) else: indegs.extend([0 for x in range(len(indegs), v)]) indegs.insert(v, g.in_degree(v)) fratDigraph = ldoFunc(fratGraph, indegs) # calculate result trans.update(newTrans) for s, t, _ in fratDigraph.arcs(): frat[(s, t)] = step g.add_arc(s, t, step) return (g, trans, frat)
def read_leda(filename): graph = Graph() numVertices = 10**10 lines = open(filename) # Skip preable skip_lines(lines, 4) numVertices = int(next(lines)) # We do not need vertex labels skip_lines(lines, numVertices) numEdges = int(next(lines)) for line in lines: line = line.strip() if line == '' or line[0] == '#': continue s, t, r, l = line.split(' ') graph.add_edge(int(s) - 1, int(t) - 1) # LEDA is 1-based. return graph