Please create your FREE account or login here

**CURRENT COURSE PROGRESS:**

### Progress:

Please login to view your progress.

**LEARNING PATHS**

### Related Content

**Ordering Traversals**

**DSE Version: **6.0

Video

Ordering traversals are very simple. They arranges objects in a traversal stream into an ordered sequence. They will take objects in, apply some known criteria we give it, and emit the objects one by one in their new order. In this unit, you will learn about ordering traversals.

Hey everyone, I am denise gosnell and these are ordering traversals.

Conceptually, an ordering traversal is very simple. It arranges objects in a traversal stream into an ordered sequence. It is going to take objects in, apply some known criteria we give it, and emit the objects one by one in their new order. It is a very important thing to do, and also a pretty simple thing to set up.

We only have one new step, and that is order. The really important thing in the order step is over course its modulator, by. We have to specify what we want to sort on, and whether we are going to sort on increasing order (the default) or decreasing order. And, if we have map, if we want to sort on the keys or the values. We also have the option of shuffling; basically applying a random ordering to the objects in the traversal stream.

And, order itself has an optional scope. The deafult is global, where the ordering computation is applicable to the whole traversal stream. There is also local scope, which applies the computation to each collection type object in the traversal stream. So, we will use local scope with a collection in an upcoming example.

The distinction between global and local can be a little tricky. So hopefully the examples help to make it more clear.

As always, here is our killr video movie schema.

In all of these examples, we will be studying the movies by nicolas cage. In the first line, we will find the nicholas cage person vertex. And, then, we are going to traverse the incoming actor edge, which will bring us to all movies in which nicolas cage has acted. Then, we will order the movies by year decreasing. So, we could get an ordered collection of nicholas cage movies with the newest movie first and the oldest movie last. But, for convenient display, let’s convert them to a value map with just the year and title properties in the map.

So, we see the most recent film in our dataset is national treasure book of secrets from 2007. And then ghost rider also from 2007. And so on down the list.

Now, in that previous example, we hadn’t applied any specific example to the films that were in the same year. In some examples, it might be useful to apply an ordering for those films that are from the same year. As some actors can release more than one film in the same year.

Now, we will call the by modulator again. We will first order by year decreasing, and then by title increasing. Now being specific about the order, we see that the movies from 2007 are properly ordered by title.

Next, how about ordering the movies by rating. We want to now order these movies by their average rating in decreasing order.

To look at the year and title of these movies in the decreasing order of their rating. As we see here, the highest rated nicholas cage film is leaving las vegas.

Here, again, we start with nicholas cage and traversing out to all of the movies he acted in. Next, we want to order them by, and we have to put a traversal in this by modulator. We follow the incoming rated edges to get to the rating. We are at a movie, and we need to go backwards against the edge with the rated label, get the rating value from those edges, calculate the mean of this collection of values, and then specify that we want this in decreasing order. Then, we just want.

Now, let’s do something a little more complex. Let’s say we want to limit the universe of ratings to those that are positive. We will define that as being 5 or above. Then, we want to order the movies by the average age of the people rating them. So, broadly speaking, these are the people who had a favorable impression of the movie and wanting to know -- how old were they?

We start with nicholas cage, go through the actor edges to get to the movies, and we order them by -- and again we need to have a traversal for the by modulator.

We are at the movies so we go back across the rated edge that has a rating greater or equal to 5. So, that traversal gives us only positive ratings. Now, we are on the edges, so we have to go to the outbound side of this edge to the user vertex. Let’s get the age value, calculate the mean, and order in increasing order.

We have specified that we only want highly rated movies, and we want to order them by the average age by the people who did the rating. And, we get results that are not too surprising. We see that the 2 national treasure movies did well among young people, since we are ordering by the age of the people doing the rating.

Now, random ordering, is a little hard to see on a slide, but let’s look at the code.

Again, we start with the nicholas cage person vertex, and we get all of his related movies. Then, we order by shuffle and output the value map. Now, if you actually run this code, it will give you a different ordering each time. It is completely random, which might be useful for your application.

Finally, let’s take a look at cage’s productivity in each year that he has released films in this sample graph. To do this, we want to get a count of the number of movies he has in each year.

We start by finding nicholas cage, walking over to his movies, then we want to group count these movies by year. This will find all of the distinct years and count the number of distinct vertices in each year. If you would like to print it out in order, you will need to order it by key decreasing.

Now, let’s look at the use of the local scope in this last line, which is really important. Now, the object that we have at the end of group count by year is a map. The keys in that map are years. The values in that map are counts, the number of movies made in each year. That map is just a map, it is not ordered. We would like to order it and we would like that ordering to apply to the map. That is, to a collection type. So, the ordering has to be local. Specifying local means do the ordering inside the collection and not among all of the objects in the stream. And, that local ordering of that map should be by key. Remember, the keys in the map are years. So, we want by key, decreasing. Most recent movies first and least recent movies last.

And, we see in 2010 we have one movie and in 1984 we also have 1. Looks like 1997 and 2007 were his most productive years with 2 movies each.

And that is your introduction to ordering traversals. It let’s you do some pretty powerful things, has you have seen in these examples.