
图算法实战
|
149
7.2.5
通过航空公司互连的机场
现在假设我们已经旅行过很多次,并且想用飞行常客积分尽可能高效地游览更多目的地,
因为这些积分很快就要到期了。如果从美国某个机场出发,需要经过多少个机场,才可以
乘坐同一家航空公司的航班返回出发机场呢?
先找出所有航空公司,计算出每家航空公司的航班数量:
airlines = (g.edges
.groupBy("airline")
.agg(F.count("airline").alias("flights"))
.sort("flights", ascending=False))
full_name_airlines = (airlines_reference
.join(airlines, airlines.airline
== airlines_reference.code)
.select("code", "name", "flights"))
创建一张条形图来展示航空公司的情况:
ax = (full_name_airlines.toPandas()
.plot(kind='bar', x='name', y='flights', legend=None))
ax.xaxis.set_label_text("")
plt.tight_layout()
plt.show()
执行该查询,输出结果如图
7-13
所示。
图 7-13:航空公司的航班数量