Skip to content
Snippets Groups Projects
Commit cf009ab6 authored by Samuel Koovely's avatar Samuel Koovely
Browse files

Solutions to tutorial3 ES1

parent 5b1c693b
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import networkx as nx
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
G = nx.read_graphml('./datasets/openflights/openflights_usa.graphml.gz')
```
%% Cell type:markdown id: tags:
# EXERCISE 1
Is there a direct flight between Indianapolis and Fairbanks, Alaska (FAI)? A direct flight is one with no intermediate stops.
%% Cell type:markdown id: tags:
# Answer 1
%% Cell type:code id: tags:
``` python
'FAI' in list(G.neighbors('IND'))
```
%% Cell type:markdown id: tags:
'FAI' does not belong to the neighbours of 'IND', so there is no direct flight between the two airports.
%% Cell type:markdown id: tags:
# EXERCISE 2
If I wanted to fly from Indianapolis to Fairbanks, Alaska what would be an itinerary with the fewest number of flights?
%% Cell type:markdown id: tags:
# Answer 2
%% Cell type:code id: tags:
``` python
nx.has_path(G, 'IND', 'FAI')
```
%% Cell type:markdown id: tags:
So we know that there is a path between the two airports
%% Cell type:code id: tags:
``` python
shortest_path_IND_FAI = nx.shortest_path(G, 'IND', 'FAI')
for i in shortest_path_IND_FAI:
print(G.nodes[i])
```
%% Cell type:markdown id: tags:
So the itinerary between Indianapolis and Fairbanks with the fewest number of flights passes through Boston and Seattle.
%% Cell type:markdown id: tags:
# EXERCISE 3
Is it possible to travel from any airport in the US to any other airport in the US, possibly using connecting flights? In other words, does there exist a path in the network between every possible pair of airports?
%% Cell type:code id: tags:
``` python
components = list(nx.connected_components(G))
len(components)
```
%% Cell type:markdown id: tags:
The graph contains multiple connected components, so it is not possible to fly from any airport in the US to any other airport in the US (possibly using connecting flights).
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment