Beispiel #1
0
def partition_girvan_newman(graph, max_depth):
    """
    Use your approximate_betweenness implementation to partition a graph.
    Unlike in class, here you will not implement this recursively. Instead,
    just remove edges until more than one component is created, then return
    those components.
    That is, compute the approximate betweenness of all edges, and remove
    them until multiple comonents are created.

    You only need to compute the betweenness once.
    If there are ties in edge betweenness, break by edge name (e.g.,
    (('A', 'B'), 1.0) comes before (('B', 'C'), 1.0)).

    Note: the original graph variable should not be modified. Instead,
    make a copy of the original graph prior to removing edges.
    See the Graph.copy method https://networkx.github.io/documentation/development/reference/generated/networkx.Graph.copy.html
    Params:
      graph.......A networkx Graph
      max_depth...An integer representing the maximum depth to search.

    Returns:
      A list of networkx Graph objects, one per partition.

    >>> components = partition_girvan_newman(example_graph(), 5)
    >>> components = sorted(components, key=lambda x: sorted(x.nodes())[0])
    >>> sorted(components[0].nodes())
    ['A', 'B', 'C']
    >>> sorted(components[1].nodes())
    ['D', 'E', 'F', 'G']
    """
    ###TODO

    copy = graph.copy()

    partition_edge = list(sorted(approximate_betweenness(copy, max_depth).items(), key=lambda x:(-x[1], x[0])))
    
    for i in range(0, len(partition_edge)):
        copy.remove_edge(*partition_edge[i][0])
        subgraphs = list(nx.connected_component_subgraphs(copy))
        if len(subgraphs) > 1:
            break

    return subgraphs
Beispiel #2
0
def volume(nodes, graph):
    """
    Compute the volume for a list of nodes, which
    is the number of edges in `graph` with at least one end in
    nodes.
    Params:
      nodes...a list of strings for the nodes to compute the volume of.
      graph...a networkx graph

    >>> volume(['A', 'B', 'C'], example_graph())
    4
    """
    #volume = nx.volume(graph, nodes, weight=None)
    copy = graph.copy()
    edgeV = 0
    for n in nodes:
        for e in copy.edges():
            if n == e[0] or n == e[1]:
                edgeV += 1
                copy.remove_edge(*e)
    return edgeV
Beispiel #3
0
def make_training_graph(graph, test_node, n):
    """
    To make a training graph, we need to remove n edges from the graph.
    As in lecture, we'll assume there is a test_node for which we will
    remove some edges. Remove the edges to the first n neighbors of
    test_node, where the neighbors are sorted alphabetically.
    E.g., if 'A' has neighbors 'B' and 'C', and n=1, then the edge
    ('A', 'B') will be removed.

    Be sure to *copy* the input graph prior to removing edges.

    Params:
      graph.......a networkx Graph
      test_node...a string representing one node in the graph whose
                  edges will be removed.
      n...........the number of edges to remove.

    Returns:
      A *new* networkx Graph with n edges removed.

    In this doctest, we remove edges for two friends of D:
    >>> g = example_graph()
    >>> sorted(g.neighbors('D'))
    ['B', 'E', 'F', 'G']
    >>> train_graph = make_training_graph(g, 'D', 2)
    >>> sorted(train_graph.neighbors('D'))
    ['F', 'G']
    """
    ###TODO
    
    # copy a new graph
    copy = graph.copy()

    neighbor_nodes = sorted(copy.neighbors(test_node))
    for i in range(n):
        copy.remove_edge(*(test_node, neighbor_nodes[i]))

    return copy
Beispiel #4
0
def partition_girvan_newman(graph, max_depth):
    """
    Use your approximate_betweenness implementation to partition a graph.
    Unlike in class, here you will not implement this recursively. Instead,
    just remove edges until more than one component is created, then return
    those components.
    That is, compute the approximate betweenness of all edges, and remove
    them until multiple comonents are created.
    Params:
      graph.......A networkx Graph
      max_depth...An integer representing the maximum depth to search.

    Returns:
      A list of networkx Graph objects, one per partition.
    """
    copy = graph.copy()
    betweenness = sorted(approximate_betweenness(copy, max_depth).items(), key= lambda x: (-x[1],x[0][0],x[0][1]))
    for edge,val in betweenness:
        copy.remove_edge(*edge)
        if nx.number_connected_components(copy) > 1:
            break
    partition_graph = sorted(list(nx.connected_component_subgraphs(copy)), key = lambda x: -len(x))
    return partition_graph
Beispiel #5
0
def volume(nodes, graph):
    """
    Compute the volume for a list of nodes, which
    is the number of edges in `graph` with at least one end in
    nodes.
    Params:
      nodes...a list of strings for the nodes to compute the volume of.
      graph...a networkx graph

    >>> volume(['A', 'B', 'C'], example_graph())
    4
    """
    ###TODO
    
    volumn = 0
    copy = graph.copy()

    for n in range(0, len(nodes)):
        for e in copy.edges():
            if nodes[n] == e[0] or nodes[n] == e[1]:
                copy.remove_edge(*e)
                volumn += 1

    return volumn