在网络图中我们常常需要回家的一个问题:以航线图为例, 在我的图中,机场顶点之间的路线是如何分布的呢? 我们也可以用同样的查询找到一系列路线的静态模型。
An example of a common question we might want to answer with a network graph, of which air
routes are an example, is "how are the routes in my graph distributed between airport vertices?".
We can also use this same query to find the statistical mode (most common number) for a set of
routes.
看下面这个查询,它说明了哪何去通过图分析路线的分布。我们只对于机场顶点感兴趣,对这些顶点我们想要弘计每个机场有多少出边。我们把以有序键值对形式返回结果:其中键是出边的数量,值是有这么多出边的机场的数量。
Take a look at the next query that shows how we can do analysis on the distribution of routes
throughout the graph. We are only interested in vertices that are airports and for those vertices we
want to count how many outgoing routes each airport has. We want to return the results as a set of
ordered key:value pairs where the key is the number of outgoing routes and the value is the
number of airports that have that number of outgoing routes.
运行这个查询时,我们会得到的返回结果如下。结果是按值的降序排列的。我们看到了出边最常见的出边的数量只有一条出边,有786个机场就只有一条出边。我们看到654个机场有两条出边等等。我们也看到另一种极端情况:有一个机场有237条出边
When we run the query we get back the results below. As the results are sorted in descending order
by value, we can see that the mode (most common) number of outgoing routes is actually just one
route and that 786 airports have just one outgoing route. We can see that 654 airports have just two
routes and so on. We can also see at the other end of the scale that one airport has 237 outgoing
routes.
我们可以修改上边的查询语句用__.in()代替out() ,我们可以找出来入边的分布情况。记住在航线图中,出边也入边的数量并不是1:1这样平衡的,因为航空公司有它们自己规划的路线。另一种我们可以对我们的查询进行的修改是改变排序,对每个键值对使用key进行排序,这次是按升序排列的。
We could change our query above, replacing out() with __.in() and we could find out the distribution
of incoming routes. Remembering that in an air route network there is not always a one to one
equivalent number of outgoing to incoming routes due to the way airlines plan their routes.
Another change we could make to our query is to change the ordering to use the key field for each
key:value pair and this time sort in ascending order.
我们再次运行查询,得到了下面的结果。看到数据是有序的,这有利于发现一些新的事实。最有趣的是,我们可以立即指出来有16个机场它们根本就没有出边。
When we run our query again we get the results below. Looking at the data sorted this way helps
some new interesting facts stand out. The most interesting thing we can immediately spot is that
there are 16 airports that currently have no outgoing routes at all!
如果我们想统计图中路线的平均值,我们可以写一个下面这样的查询,告诉我们在图中有多少个机场,总共有多少个出边。
If we wanted to find the statistical mean number of routes in the graph we could easily write a
query like the one below to tell us how many airports and outgoing routes in total there are in the
graph.
我们可以用小精灵控制台来为我们做除法运算计算平均值。
We could then use the Gremlin Console do the division for us to calculate the mean.
然而,小精灵也提供了mean步骤,我们可以用它,如果我们想要找到一种方法,在这种情况下它可以为我们工作的。看下面的查询。关键是注意local的使用方式。这会将小精灵实际上做了我们刚刚上面手工做的处理。如果我们不包含local,答案将会是出边的总数,因为小精灵实际进行的计算是43400/1.通过使用local,我们让小精灵创建了一个数组,包含了每个机场的边数,把这些值加起来,然后除以数组的元素的个数(也就是机场的数量)。笔者希望这是有意义的。如果感到困惑,您可以自己在小精灵的控制台运行查询,有local的和没有local的, 尝试没有mena步骤的。您将会看到所有暂时的值(中间结果)。
However, Gremlin also has a mean step that we can take advantage of if we can figure out a way to
use it in this case that will do the work for us. Take a look at the next query. The key thing to note
here is the way local has been used. This will cause Gremlin to essentially do what we did a bit
more manually above. If we did not include local the answer would just be the total number of
outgoing routes as Gremlin would essentially calculate 43400/1. By using local we force Gremlin to
in essence create an array containing the number of routes for each airport, add those values up
and divide by the number of elements in the array (the number of airports). I hope that makes
sense. If it is confusing try the query yourself on the gremlin console with and without local and try
it without the mean step. You will see all of the interim values instead!
无论我们用哪种方式计算,看起来在图中每个机场只有12条出边,
So it seems there is an average of just over 12 outgoing routes per airport in the graph whichever
way we decide to calculate it!
现在我们有一个查询是找出来每下机场的出边的平均值,我们可以快速的调整它来为入边,及合起来的入边和出边进行计算。
Now that we have a query figured out for calculating the average number of outgoing routes per
airport, we can easily tweak it to do the same for incoming routes and combined, incoming and
outgoing, routes.
浙公网安备 33010602011771号