Advertisement
haggerhermy

Graph Applications in Python - Abhishek Ahuja

Dec 20th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. """
  2. Implements functions:
  3.    bfs_visited(ugraph, startnode)
  4.    cc_visited(ugraph)
  5.    largest_cc_size(ugraph)
  6.    compute_resilience(ugraph, attack_order)
  7. """
  8.  
  9. def bfs_visited(ugraph, startnode):
  10.     """
  11.    lists the nodes connected to 'startnode' in the
  12.    graph 'ugraph'
  13.    
  14.    input: undirected graph, in the form of
  15.            adjacency matrix
  16.    output: list of visited nodes
  17.    """
  18.     node_queue = []
  19.     visited = set([startnode])
  20.     node_queue.append(startnode)
  21.     #adding neighbours to queue
  22.     while node_queue != []:
  23.         #removing nodes from queue
  24.         connec = node_queue.pop(0)
  25.         for ne_bour in ugraph[connec]:
  26.             if ne_bour not in visited:
  27. #adds node to 'visited' list if not already present
  28.                 visited.add(ne_bour)
  29.                 node_queue.append(ne_bour)
  30.     return visited
  31.  
  32. def cc_visited(ugraph):
  33.     """
  34.    lists all the connected components in an
  35.    undirected graph
  36.    
  37.    input: undirected graph in the form of
  38.            an adjacency matrix
  39.    output: set of connected components, each in
  40.            the form of a list
  41.    """
  42.     rem_nodes = []
  43.     for dummy_p in ugraph.keys():
  44.         rem_nodes.append(dummy_p)
  45.     connec_comp = []
  46.     while rem_nodes != []:
  47.         rand_node = rem_nodes[0]
  48.         new_comp = bfs_visited(ugraph, rand_node)
  49.         connec_comp.append(new_comp)
  50.         for dummy_node in new_comp:
  51.             rem_nodes.remove(dummy_node)
  52.     return connec_comp
  53.  
  54. def largest_cc_size(ugraph):
  55.     """
  56.    returns the size of the largest connected
  57.    component in a graph
  58.    
  59.    input: undirected graph
  60.    output: number
  61.    
  62.    """
  63.     compos = cc_visited(ugraph)
  64.     compo_size = []
  65.     for dummy_comp in compos:
  66.         compo_size.append(len(dummy_comp))
  67.     compo_size.sort()
  68.     compo_size.reverse()
  69.     #print compo_size
  70.     #print "inside largest comp"
  71.     #print compo_size
  72.     #print len(compo_size)
  73.     #print "quit largest comp"
  74.     if compo_size == []:
  75.         return 0
  76.     else:
  77.         return compo_size[0]
  78.  
  79. def attack(ugraph, node):
  80.     """
  81.    attacks a particular node in a graph by removing
  82.    the node and all its edges
  83.    
  84.    input:
  85.    undirected graph, in the form of an adjecency matrix
  86.    
  87.    output:
  88.    undirected graph, in the form of an adjecency matrix
  89.    with the 'node' and it's edges removed
  90.    """
  91.     del(ugraph[node])
  92.     for dummy_x in ugraph:
  93.         if node in ugraph[dummy_x]:
  94.             ugraph[dummy_x].remove(node)
  95.     return ugraph
  96.  
  97. def compute_resilience(ugraph, attack_order):
  98.     """
  99.    returns resilience of a graph to a
  100.    sequential attack on its nodes
  101.    
  102.    input: undirected graph, list of k nodes in
  103.             the order of attack
  104.    output: list of size (k+1) where index 0 contains
  105.            value of largest connected component before
  106.            attack and index k contains size of largest
  107.            connected component after attacking all
  108.            k nodes
  109.    """
  110.     resil = []
  111.     resil.append(largest_cc_size(ugraph))
  112.     for node_now in attack_order:
  113.         #print ugraph
  114.         ugraph = attack(ugraph, node_now)
  115.         resil.append(largest_cc_size(ugraph))
  116.     return resil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement