Example #1
0
 def add_node(self, u):
     Graph.add_node(self, u)
     self.vertexRecords.extend(
         [None for x in range(len(vertexRecords), u + 1)])
     self.vertexRecords[u] = VertexInfo(parent=None,
                                        children=[],
                                        depth=None)
def trans_frater_augmentation(orig, g, trans, frat, col,
                              nodes, step, td, ldoFunc):
    fratGraph = Graph()
    newTrans = {}

    for v in g:
        for x, y, _, in g.trans_trips(v):
            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(v):
            fratGraph.add_edge(x, y)
            assert (not g.adjacent(x, y)), \
                "{0} {1} fraternal but adjacent".format(x, y)

    for (s, t) in newTrans.keys():
        g.add_arc(s, t, 1)
        fratGraph.remove_edge(s, t)

    # TODO: support dict to see current in-degree...
    fratDigraph = ldoFunc(fratGraph)

    # calculate result
    trans.update(newTrans)

    for s, t, _ in fratDigraph.arcs():
        frat[(s, t)] = step
        g.add_arc(s, t, 1)

    return (g, trans, frat)
Example #3
0
 def add_nodes_from(self, u):
     """Adds an iterable of nodes at once to save time"""
     Graph.add_nodes_from(self, u)
     # Make a local reference to self.vertexRecords for use in the loop
     vertexRecords = self.vertexRecords
     for n in u:
         self.vertexRecords.extend([None for x in range(len(vertexRecords), n + 1)])
         vertexRecords[n] = VertexInfo(parent=None, children=[], depth=None)
Example #4
0
 def add_nodes_from(self, u):
     """Adds an iterable of nodes at once to save time"""
     Graph.add_nodes_from(self, u)
     # Make a local reference to self.vertexRecords for use in the loop
     vertexRecords = self.vertexRecords
     for n in u:
         self.vertexRecords.extend(
             [None for x in range(len(vertexRecords), n + 1)])
         vertexRecords[n] = VertexInfo(parent=None, children=[], depth=None)
Example #5
0
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
Example #6
0
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
Example #7
0
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
Example #8
0
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
Example #9
0
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
Example #10
0
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 truncated_tf_augmentation(orig, g, trans, frat, col, nodes,
                              step, td, ldoFunc):
    fratGraph = Graph()
    newTrans = {}
    print "Ready to go"

    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))
    print "Finished adding to newTrans"

    for (s, t) in newTrans:
        g.add_arc(s, t, step)
        fratGraph.remove_edge(s, t)
    print "Finished removing from fratGraph and adding to g"

    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) )
    print "Finished initializing indegs"

    fratDigraph = ldoFunc(fratGraph, indegs)
    print "Finished ldo"

    # calculate result
    trans.update(newTrans)
    print "Finished updating"

    for s, t, _ in fratDigraph.arcs():
        frat[(s, t)] = step
        g.add_arc(s, t, step)
    print "Finished adding to g"

    print sys.getsizeof(trans), sys.getsizeof(frat)
    print sys.getsizeof(g)
    g.get_size()

    return (g, trans, frat)
Example #12
0
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
Example #13
0
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
Example #14
0
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
Example #15
0
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
Example #16
0
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 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)
Example #18
0
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
Example #19
0
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
Example #20
0
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 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)
Example #22
0
 def add_node(self, u):
     Graph.add_node(self, u)
     self.vertexRecords.extend([None for x in range(len(vertexRecords), u + 1)])
     self.vertexRecords[u] = VertexInfo(parent=None, children=[],
                                        depth=None)