def random_upa_graphs(): """ Implementation on random graphs of varying sizes that are generated under the undirected preferential attachment model. Argument: None Returns: a tuple containing the result of the run and number of nodes used.""" first = provided.upa(2,2) second = provided.upa(4,2) third = provided.upa(6,2) fourth = provided.upa(8,2) fifth = provided.upa(10,2) sixth = provided.upa(12,2) print comp182.time_func(brute_force_distance, [first, 0]), "UPA(2,2)" print "Edges in graph:", edge_count(first) print comp182.time_func(brute_force_distance, [second, 0]),"UPA(4,2)" print "Edges in graph:", edge_count(second) print comp182.time_func(brute_force_distance, [third, 0]),"UPA(6,2)" print "Edges in graph:", edge_count(third) print comp182.time_func(brute_force_distance, [fourth, 0]),"UPA(8,2)" print "Edges in graph:", edge_count(fourth) print comp182.time_func(brute_force_distance, [fifth, 0]),"UPA(10,2)" print "Edges in graph:", edge_count(fifth) print comp182.time_func(brute_force_distance, [sixth, 0]),"UPA(12,2)" print "Edges in graph:", edge_count(sixth)
def bfs_random_upa_graphs(): """ Implementation on random graphs of varying sizes that are generated under the undirected preferential attachment model using the BFS algorithm. Argument: None Returns: a tuple containing the result of the run and number of nodes used.""" first = provided.upa(500,5) second = provided.upa(500,20) third = provided.upa(500,80) fourth = provided.upa(500,320) print comp182.time_func(bfs, [first, 0]), "UPA(500,5)" print "Edges in graph:", edge_count(first) print comp182.time_func(bfs, [second, 0]),"UPA(500,20)" print "Edges in graph:", edge_count(second) print comp182.time_func(bfs, [third, 0]),"UPA(500,80)" print "Edges in graph:", edge_count(third) print comp182.time_func(bfs, [fourth, 0]),"UPA(500,320)" print "Edges in graph:", edge_count(fourth)
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")
import comp182 import provided import connected_components import random network = comp182.read_graph('rf7.repr') random1 = provided.upa(1347,5) random2 = provided.erdos_renyi(1347, float(6224)/float(906531)) def random_remove(g): """ #remove one node from graph randomly #argument: g -- graph #return: a graph with a node removed """ random_node = random.sample(g,1)[0] # choose random node g.pop(random_node,None) # remove node for node in g: if g[node] & {random_node} == {random_node}: g[node].remove(random_node) return g # test for random_remove #test1 = random_remove(provided.upa(5,2)) def plot_remove(g): """ #remove the node with largest cut, plot the size of connected components with respect to number of nodes removed.
g3 = {0: {1,2,3}, 1: {0,4,5}, 2: {0,3,6}, 3: {0,2,7}, 4: {1,3,5,8}, 5: {1,4,9}, 6: {2,7,10}, 7: {3,6,8,10}, 8: {4,7,9,11}, 9: {5,8,11}, 10:{6,7,11}, 11:{8,9,10}, } result1 = bfs(g1,0) result2 = bfs(g2,0) result3 = bfs(g3,0) """ # test 2 # g4 = provided.upa(5, 5) g5 = provided.upa(500, 20) g6 = provided.upa(500, 80) g7 = provided.upa(500, 320) result4 = comp182.time_func(bfs, [g4, 0]) result5 = comp182.time_func(bfs, [g5, 0]) result6 = comp182.time_func(bfs, [g6, 0]) result7 = comp182.time_func(bfs, [g7, 0])