Exemplo n.º 1
0
def compute_resilience(ugraph, attack_order):
    """
    a function to test the resiliency of a graph by removing nodes/edges

    :rtype : list
    :param ugraph:
    :param attack_order:
    :return:
    """
    # copy the graph
    graph = copy_graph(ugraph)

    # get all connected components of ugraph and add to list
    lcc = [largest_cc_size(graph)]

    # loop through each attack
    for attack in attack_order:
        # remove the node
        edges = graph.pop(attack)

        # remove edges from other nodes
        for node in edges:
            graph[node].discard(attack)

        # calculate largest connected component
        lcc.append(largest_cc_size(graph))

    return lcc
Exemplo n.º 2
0
def fast_targeted_order(graph):
    """
    Compute a targeted attack order consisting of nodes of maximal degree

    :rtype : list
    :param ugraph: input graph
    """
    # copy graph
    graph = provided.copy_graph(graph)

    # initialize return variable
    attack_order = []

    # initialize array
    degree_sets = {}
    for itr in xrange(len(graph)):
        degree_sets[itr] = set()

    for idx in graph:
        degree_sets[len(graph[idx])].add(idx)

    rev_degrees = degree_sets.keys()
    rev_degrees.reverse()
    for idx in rev_degrees:
        while degree_sets[idx]:

            u = degree_sets[idx].pop()

            for neighbour in graph[u]:
                d = len(graph[neighbour])

                degree_sets[d].remove(neighbour)
                degree_sets[d - 1].add(neighbour)

            attack_order.append(u)

            provided.delete_node(graph, u)

    return attack_order