"git@git.math.uzh.ch:stenli.karanxha/exercises.git" did not exist on "8249eb609962ae8f517db56067c2aeb74aab0149"
Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import networkx as nx\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"G = nx.read_graphml('./datasets/openflights/openflights_usa.graphml.gz')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EXERCISE 1\n",
"\n",
"Is there a direct flight between Indianapolis and Fairbanks, Alaska (FAI)? A direct flight is one with no intermediate stops."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Answer 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'FAI' in list(G.neighbors('IND'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"'FAI' does not belong to the neighbours of 'IND', so there is no direct flight between the two airports."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EXERCISE 2\n",
"\n",
"If I wanted to fly from Indianapolis to Fairbanks, Alaska what would be an itinerary with the fewest number of flights?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Answer 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nx.has_path(G, 'IND', 'FAI')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we know that there is a path between the two airports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"shortest_path_IND_FAI = nx.shortest_path(G, 'IND', 'FAI')\n",
"for i in shortest_path_IND_FAI:\n",
" print(G.nodes[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So the itinerary between Indianapolis and Fairbanks with the fewest number of flights passes through Boston and Seattle."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EXERCISE 3\n",
"\n",
"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",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"components = list(nx.connected_components(G))\n",
"len(components)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.3 ('venv')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "3699540f6baed5bd29a193b0c2d028af3f2c80498e3cac18f2b44cdd848387e2"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}