Example #1
0
def plot_remove(g):
    """
    #remove the node with largest cut,
     plot the size of connected components 
     with respect to number of nodes removed.
     Two methods: target & random remove
    #argument:
        g           -- graph
    #return:
        a list of dictionaries, keys are the 
        number of nodes removed, values are
        the largest CC of each graph
    """
    graph1 = comp182.copy_graph(g)
    graph2 = comp182.copy_graph(g)
    ############################
    # target remove
    result1 = {}
    nodeRemoved = 0
    # create an array of nodes from max cut to min cut
    nodeToBeRemoved = sorted(graph1, key = lambda node:len(graph1[node]), reverse = True)
    CC = connected_components.connected_components(graph1) 
    CC.sort(key=len,reverse=True) # sort CC from biggest to smallest
    result1[0] = len(CC[0]) # add biggest CC to the original graph
    while (nodeRemoved < len(graph1)):
        nodeRemoved += 1
        remove_node = nodeToBeRemoved[0]
        graph1.pop(remove_node,None) # remove node, update g
        for node in graph1:
            if graph1[node] & {remove_node} == {remove_node}:
                graph1[node].remove(remove_node)
        nodeToBeRemoved.remove(remove_node)
        CC = connected_components.connected_components(graph1) 
        CC.sort(key=len,reverse=True) # sort CC from biggest to smallest
        result1[nodeRemoved] = len(CC[0]) # add biggest CC to the original graph
    
    ##########################
    # random remove    
    result2 = {}
    nodeRemoved = 0
    CC = connected_components.connected_components(graph2) 
    CC.sort(key=len,reverse=True) # sort CC from biggest to smallest
    result1[0] = len(CC[0]) # add biggest CC to the original graph
    
    while (nodeRemoved < len(graph2)):
        nodeRemoved += 1
        graph2 = random_remove(graph2)
        CC = connected_components.connected_components(graph2) 
        CC.sort(key=len,reverse=True) # sort CC from biggest to smallest
        result2[nodeRemoved] = len(CC[0]) # add biggest CC to the original graph
        
    #############################
    # plot
    result = [result1, result2] # target, then random
    return result
Example #2
0
def analyze_graphs():
    """ Runs all six experiments and plots the pictorial graphs.

    Arguments:
    None

    Returns:
    None"""
    #Analyzes the network graph and creates similar perimeters for Erdos and UPA graph
    nodes = node_count_function(networktopology)
    print "Nodes in Network graph:", nodes
    string1, edgesavg = average_edge_per_node(networktopology)
    edges = edge_count(networktopology)
    print "Averge Number of Edges:", edgesavg
    totaldegree = provided.total_degree(networktopology)
    print "Total degree of Network graph:", totaldegree
    probabilityerdos = float(edges)/(nodes*(nodes-1)/2.0)
    print "Probability P for Erdos:", probabilityerdos
    upa = provided.upa(nodes,edgesavg)

    #Saves the grahes to be modified.
    savedrandomgraph = comp182.copy_graph(upa)
    
    erdos = provided.erdos_renyi(nodes,probabilityerdos)
    
    savederdosgraph = comp182.copy_graph(erdos)
    
    savednetworkgraph = comp182.copy_graph(networktopology)
    #runs the different attacks on the various graphs
    randomgraphattack = random_attack(comp182.copy_graph(upa))
    print "First plot finished."
    randomgraphtargeted = targeted_attack(comp182.copy_graph(upa))
    print "Second plot finished."
    erdosgraphattack = random_attack(comp182.copy_graph(erdos))
    print "Third plot finished."
    erdosgraphtargeted = targeted_attack(comp182.copy_graph(erdos))
    print "Fourth plot finished."
    networkgraphattack = random_attack(comp182.copy_graph(savednetworkgraph))
    print "Fifth plot finished."
    networkgraphtargeted = targeted_attack(savednetworkgraph)
    print "Sixth plot finished."

    #formats the pictorial graph
    labelerdos = "Erdos({}, {})".format(nodes,probabilityerdos)
    labelupa = "UPA({},{})".format(nodes,edgesavg)
    labels = ["Network Graph"+"Targeted", "Network Graph"+"Random", labelerdos+"Targeted", labelerdos+"Random",labelupa+"Targeted", labelupa+"Random"]
    comp182.plot_lines([networkgraphtargeted, networkgraphattack, erdosgraphtargeted, erdosgraphattack, randomgraphtargeted, randomgraphattack],"Resiliency of Graphs' Attacks", "Nodes Removded", "Largest Connected-Component", labels, filename="Resiliency of Graphs' Attacks")
Example #3
0
def gn_graph_partition(g):
    """
    Partition the graph g using the Girvan-Newman method.

    Requires connected_components, shortest_path_edge_betweenness, and
    compute_q to be defined.  This function assumes/requires these
    functions to return the values specified in the homework handout.

    Arguments:
    g -- undirected graph

    Returns:
    A list of tuples where each tuple contains a Q value and a list of
    connected components.
    """
    ### Start with initial graph
    c = connected_components(g)
    q = compute_q(g, c)
    partitions = [(q, c)]

    ### Copy graph so we can partition it without destroying original
    newg = comp182.copy_graph(g)

    ### Iterate until there are no remaining edges in the graph
    while True:
        ### Compute betweenness on the current graph
        btwn = shortest_path_edge_betweenness(newg)
        #print "something is left"
        #print "between value", btwn
        if not btwn:
            #print "between value:", btwn
            ### No information was computed, we're done
            break

        ### Find all the edges with maximum betweenness and remove them
        maxbtwn = max(btwn.values())
        maxedges = [edge for edge, b in btwn.iteritems() if b == maxbtwn]
        remove_edges(newg, maxedges)

        ### Compute the new list of connected components
        c = connected_components(newg)
        #print c
        if len(c) > len(partitions[-1][1]):
            ### This is a new partitioning, compute Q and add it to
            ### the list of partitions.
            q = compute_q(g, c)
            partitions.append((q, c))

    return partitions
def gn_graph_partition(g):
    """
    Partition the graph g using the Girvan-Newman method.

    Requires connected_components, shortest_path_edge_betweenness, and
    compute_q to be defined.  This function assumes/requires these
    functions to return the values specified in the homework handout.

    Arguments:
    g -- undirected graph

    Returns:
    A list of tuples where each tuple contains a Q value and a list of
    connected components.
    """
    ### Start with initial graph
    c = connected_components(g)
    q = compute_q(g, c)
    partitions = [(q, c)]

    ### Copy graph so we can partition it without destroying original
    newg = comp182.copy_graph(g)
    ### Iterate until there are no remaining edges in the graph
    while True:
        ### Compute betweenness on the current graph
        btwn = shortest_path_edge_betweenness(newg)
        #print 'shortest path edge betweenness function working'
        if not btwn:
            ### No information was computed, we're done
            break

        ### Find all the edges with maximum betweenness and remove them
        maxbtwn = max(btwn.values())
        maxedges = [edge for edge, b in btwn.iteritems() if b == maxbtwn]
        remove_edges(newg, maxedges)

        ### Compute the new list of connected components
        c = connected_components(newg)
        #print 'connected componets function working'
        if len(c) > len(partitions[-1][1]):
            ### This is a new partitioning, compute Q and add it to
            ### the list of partitions.
            q = compute_q(g, c)
            #print 'compute Q function working'
            partitions.append((q, c))

    return partitions