In [3]:
import networkx as nx
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)
import numpy as np

In [4]:
AG=nx.read_gpickle("../data/airport/smaller_airportnet.pickle")

E1.1 Explore the graph AG using the methods:

  • AG.nodes()
  • AG.edges()
  • AG.number_of_nodes()

etc.

Draw the graph using nx.draw


In [5]:
AG.nodes()


Out[5]:
['SAN',
 'PWM',
 'VCE',
 'LAX',
 'ADD',
 'BEY',
 'RIC',
 'CMH',
 'CDG',
 'MLA',
 'LGW',
 'YYZ',
 'PTY',
 'TIJ',
 'FRA',
 'SNN',
 'MGA',
 'CJS']

In [6]:
AG.edges()


Out[6]:
[('SAN', 'LGW'),
 ('SAN', 'YYZ'),
 ('SAN', 'LAX'),
 ('SAN', 'RIC'),
 ('SAN', 'FRA'),
 ('SAN', 'CMH'),
 ('PWM', 'RIC'),
 ('PWM', 'CMH'),
 ('VCE', 'CDG'),
 ('VCE', 'MLA'),
 ('VCE', 'LGW'),
 ('VCE', 'FRA'),
 ('VCE', 'LAX'),
 ('LAX', 'MGA'),
 ('LAX', 'CDG'),
 ('LAX', 'CMH'),
 ('LAX', 'RIC'),
 ('LAX', 'LGW'),
 ('LAX', 'YYZ'),
 ('LAX', 'PTY'),
 ('LAX', 'TIJ'),
 ('LAX', 'FRA'),
 ('LAX', 'SNN'),
 ('ADD', 'CDG'),
 ('ADD', 'BEY'),
 ('ADD', 'FRA'),
 ('BEY', 'CDG'),
 ('BEY', 'FRA'),
 ('RIC', 'CMH'),
 ('RIC', 'YYZ'),
 ('CMH', 'YYZ'),
 ('CDG', 'PTY'),
 ('CDG', 'LGW'),
 ('CDG', 'TIJ'),
 ('CDG', 'YYZ'),
 ('CDG', 'FRA'),
 ('CDG', 'SNN'),
 ('MLA', 'FRA'),
 ('MLA', 'LGW'),
 ('LGW', 'YYZ'),
 ('LGW', 'FRA'),
 ('LGW', 'SNN'),
 ('YYZ', 'SNN'),
 ('YYZ', 'FRA'),
 ('PTY', 'MGA'),
 ('TIJ', 'CJS')]

In [7]:
nx.draw(AG)


/home/dmanik/venvs/teaching/lib/python3.4/site-packages/matplotlib/collections.py:650: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors_original != str('face'):
/home/dmanik/venvs/teaching/lib/python3.4/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):

E1.2 Find out the average distance between all pairs of airports in AG


In [8]:
np.average([nx.shortest_path_length(AG, source=u, target=v) for u in AG.nodes() for v in AG.nodes() if u!=v])


Out[8]:
1.9673202614379084

E1.3 Find out which airport in AG has the highest number of neighbours.


In [9]:
maxnei=0
node_w_max_nei=None

for n in AG.nodes():
    numnei=len(AG[n])
    if numnei>maxnei:
        maxnei=numnei
        node_w_max_nei=n

print(node_w_max_nei)


LAX

E1.4 Now remove that node from AG, and redo E1.2.


In [10]:
AG.remove_node('LAX')

In [11]:
np.average([nx.shortest_path_length(AG, source=u, target=v) for u in AG.nodes() for v in AG.nodes() if u!=v])


Out[11]:
2.2794117647058822

In [ ]: