Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 9, 2023 10:11 pm GMT

Become a Traffic Wizard: A Beginner's Guide to Routing Traffic with Apache AGE

Introduction

Traffic routing refers to the process of directing users from one destination to another to form a path. You may be familiar with it from route directory in maps and also from networking, which is the backbone of how the internet works by requesting to the appropriate server or resource in a distributed computing environment.

Apache AGE is a powerful open-source graph database software that can be used also used to route traffic. However, for beginners, it can be overwhelming to know where to start with Apache AGE.

That is why I've put together this comprehensive guide to help you become a traffic routing wizard using Apache AGE. In this blog post, I will cover the basics of traffic routing for optimizing a routing configuration. Whether you're just starting out or you're looking to improve your existing skills, this guide has something for everyone. So, let's dive in and learn how to route traffic with Apache AGE!

Background

For this guide, I'll be using the SkyTrain lines in Vancouver as our example dataset.

The SkyTrain is a rapid transit system that serves the Metro Vancouver area in British Columbia, Canada. The system consists of three lines and 53 stations, where 20 stations are served by the Expo Line, 14 by the Millennium Line, 15 by the Canada Line, 1 by both the Expo and Canada Lines, and 3 by both the Expo and Millennium Lines [1]. The SkyTrain is a popular mode of transportation for commuters and tourists alike, and it's important to find out the most efficient route in order to cut costs and time.

Skytrain and Seabus Map
Skytrain and Seabus Map (Image source)

To set up the data for the SkyTrain system, we'll need to create a dataset that contains information about each station and the connections between them. This dataset will include nodes for each station, and relationships between the nodes to represent the connections between stations.

By using the SkyTrain system as the example dataset, I will demonstrate how to set up data for a real-world traffic routing scenario. This will allow you to apply the concepts and techniques you learn to your own projects, whether you're working on a transportation system, a web application, or any other distributed computing environment.

Setting Up the Data

Let's first create a new graph to work with. We can do this by the following command:

SELECT create_graph('SkyTrain');

A graph called 'SkyTrain' has been newly created. Now we would like to create nodes and relationships to match the connections from the transit map. The CREATE clause will be used to do this.

SELECT *FROM cypher('SkyTrain', $$    CREATE (tss:Station {name: '22nd Street', line: ['Expo']}),        (tna:Station {name: '29th Avenue', line: ['Expo']}),        (abd:Station {name: 'Aberdeen', line: ['Canada']}),        (bra:Station {name: 'Braid', line: ['Expo']}),        (btw:Station {name: 'Brentwood Town Centre', line: ['Millennium']}),        (bgp:Station {name: 'Bridgeport', line: ['Canada']}),        (bch:Station {name: 'Broadway-City Hall', line: ['Canada']}),        (bql:Station {name: 'Burquitlam', line: ['Millennium']}),        (bur:Station {name: 'Burrard', line: ['Expo']}),        (col:Station {name: 'Columbia', line: ['Expo']}),        (cmb:Station {name: 'Commercial-Broadway', line: ['Expo', 'Millennium']}),        (cqc:Station {name: 'Coquitlam Central', line: ['Millennium']}),        (edm:Station {name: 'Edmonds', line: ['Expo']}),        (gtw:Station {name: 'Gateway', line: ['Expo']}),        (gil:Station {name: 'Gilmore', line: ['Millennium']}),        (gvl:Station {name: 'Granville', line: ['Expo']}),        (hdm:Station {name: 'Holdom', line: ['Millennium']}),        (ilc:Station {name: 'Inlet Centre', line: ['Millennium']}),        (jcw:Station {name: 'Joyce-Collingwood', line: ['Expo']}),        (ked:Station {name: 'King Edward', line: ['Canada']}),        (kgg:Station {name: 'King George', line: ['Expo']}),        (lld:Station {name: 'Lafarge Lake-Douglas', line: ['Millennium']}),        (lcw:Station {name: 'Lake City Way', line: ['Millennium']}),        (lfa:Station {name: 'Langara-49th Avenue', line: ['Canada']}),        (ldn:Station {name: 'Lansdowne', line: ['Canada']}),        (lcn:Station {name: 'Lincoln', line: ['Millennium']}),        (ltc:Station {name: 'Lougheed Town Centre', line: ['Expo', 'Millennium']}),        (mss:Station {name: 'Main Street-Science World', line: ['Expo']}),        (mdv:Station {name: 'Marine Drive', line: ['Canada']}),        (mtn:Station {name: 'Metrotown', line: ['Expo']}),        (mct:Station {name: 'Moody Centre', line: ['Millennium']}),        (nnm:Station {name: 'Nanaimo', line: ['Expo']}),        (nwm:Station {name: 'New Westminster', line: ['Expo']}),        (ofa:Station {name: 'Oakridge-41st Avenue', line: ['Canada']}),        (ovg:Station {name: 'Olympic Village', line: ['Canada']}),        (pat:Station {name: 'Patterson', line: ['Expo']}),        (pwu:Station {name: 'Production Way-University', line: ['Expo', 'Millennium']}),        (ren:Station {name: 'Renfrew', line: ['Millennium']}),        (rbh:Station {name: 'Richmond-Brighouse', line: ['Canada']}),        (ryo:Station {name: 'Royal Oak', line: ['Expo']}),        (rpt:Station {name: 'Rupert', line: ['Millennium']}),        (sap:Station {name: 'Sapperton', line: ['Expo']}),        (srd:Station {name: 'Scott Road', line: ['Expo']}),        (sic:Station {name: 'Sea Island Centre', line: ['Canada']}),        (sbl:Station {name: 'Sperling-Burnaby Lake', line: ['Millennium']}),        (sct:Station {name: 'Stadium-Chinatown', line: ['Expo']}),        (src:Station {name: 'Surrey Central', line: ['Expo']}),        (tem:Station {name: 'Templeton', line: ['Canada']}),        (vcc:Station {name: 'Vancouver City Centre', line: ['Canada']}),        (vck:Station {name: 'VCC-Clark', line: ['Millennium']}),        (wtf:Station {name: 'Waterfront', line: ['Canada', 'Expo']}),        (yrh:Station {name: 'Yaletown-Roundhouse', line: ['Canada']}),        (yvr:Station {name: 'YVR-Airport', line: ['Canada']}),        (rbh)-[:Route {time: 2}]->(ldn)-[:Route {time: 2}]->(abd)-[:Route {time: 3}]->(bgp)-[:Route {time: 3}]->(mdv)-[:Route {time: 3}]->(lfa)-[:Route {time: 3}]->(ofa)-[:Route {time: 5}]->(ked)-[:Route {time: 2}]->(bch)-[:Route {time: 4}]->(ovg)-[:Route {time: 2}]->(yrh)-[:Route {time: 2}]->(vcc)-[:Route {time: 1}]->(wtf),        (yvr)-[:Route {time: 2}]->(sic)-[:Route {time: 2}]->(tem)-[:Route {time: 3}]->(bgp),        (wtf)-[:Route {time: 4}]->(bur)-[:Route {time: 1}]->(gvl)-[:Route {time: 2}]->(sct)-[:Route {time: 2}]->(mss)-[:Route {time: 3}]->(cmb)-[:Route {time: 3}]->(nnm)-[:Route {time: 2}]->(tna)-[:Route {time: 3}]->(jcw)-[:Route {time: 2}]->(pat)-[:Route {time: 1}]->(mtn)-[:Route {time: 2}]->(ryo)-[:Route {time: 3}]->(edm)-[:Route {time: 2}]->(tss)-[:Route {time: 3}]->(nwm)-[:Route {time: 1}]->(col)-[:Route {time: 4}]->(sap)-[:Route {time: 2}]->(bra)-[:Route {time: 3}]->(ltc)-[:Route {time: 2}]->(pwu),        (col)-[:Route {time: 3}]->(srd)-[:Route {time: 3}]->(gtw)-[:Route {time: 2}]->(src)-[:Route {time: 2}]->(kgg),        (vck)-[:Route {time: 2}]->(cmb)-[:Route {time: 2}]->(ren)-[:Route {time: 2}]->(rpt)-[:Route {time: 2}]->(gil)-[:Route {time: 1}]->(btw)-[:Route {time: 2}]->(hdm)-[:Route {time: 3}]->(sbl)-[:Route {time: 2}]->(lcw)-[:Route {time: 2}]->(pwu),        (ltc)-[:Route {time: 3}]->(bql)-[:Route {time: 4}]->(mct)-[:Route {time: 2}]->(ilc)-[:Route {time: 3}]->(cqc)-[:Route {time: 2}]->(lcn)-[:Route {time: 1}]->(lld)$$) AS (x agtype);

Each node created has a label called Station and two properties that are the name and line. The line property takes an array variable as the stations may belong to more than one type of line. Aliases are given to each node which are used to set the relationships between the stations with Route labels consisting a time property.

Exploring the Data

Now that we have successfully created and set up our graph, let's explore the information we can retrieve from the data.

Using the following query will display all the names of the existing stations:

SELECT *FROM cypher('SkyTrain', $$    MATCH (n:Station)    RETURN n.name$$) AS (station agtype);

All 53 station names should be outputted. If not, check that you have created the nodes and relationships correctly. Don't worry if you are encountering issues; run the query SELECT * FROM cypher('SkyTrain', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype); to remove all nodes and relationships and then try setting up the data again by copying and pasting the query from the previous section.

To view station names from specified lines, run the following query:

SELECT *FROM cypher('SkyTrain', $$    MATCH (n:Station)    WHERE 'Canada' IN n.line    RETURN n.name$$) AS (station agtype);

You will be able to view the station names that belong to the 'Canada' line. Add the AND clause to further filter the results, for example, replace the line with the WHERE clause to WHERE 'Canada' IN n.line AND 'Expo' IN n.line and you will see that only the 'Waterfront' station gets outputted.

To check which station connects to another, run the following query:

SELECT *FROM cypher('SkyTrain', $$    MATCH (a:Station)-[r:Route]-(b:Station)    RETURN a.name, r.time, b.name$$) AS (station_from agtype, time agtype, station_to agtype);

Three columns will be displayed, showing the station names from and to one another with the time it takes to travel between. You might notice that there are duplicates going to and from stations, as the query ignores the direction of the relationships, which is the favoured option in this case as a train is able to go back and forth between stations in practice.

Finding the Optimal Path

The optimal path in this case will be defined as the most time efficient path. The path with the least total travel time would be the desired selection. Running the query below will show all the paths available from 'Richmond-Brighouse' station to 'Lafarge Lake-Douglas' station.

SELECT *FROM cypher('SkyTrain', $$    MATCH paths = (a:Station {name: 'Richmond-Brighouse'})-[:Route*]-(b:Station {name: 'Lafarge Lake-Douglas'})    WITH paths, relationships(paths) AS rels    UNWIND rels AS rel    WITH nodes(paths) AS path,        collect(rel.time) AS times,        sum(rel.time) AS total_time    RETURN total_time, times, path$$) AS (total_time agtype, times agtype, path agtype)ORDER BY total_time;

The query searches for connections from a starting point to an end point, in this case from 'Richmond-Brighouse' station to 'Lafarge Lake-Douglas' station, and saves the information into a variable called paths. The relationships contained in the variable is stored into another vairable rels. The UNWIND clause removes the inner arrays from rels and splits them into different rows of the output, which can be referenced as rel. The nodes in the path is saved into path, the time of each route is saved into times, and the total travel time is saved into total_time, which then are returned to be displayed in the final table. Here is what the output should look like:

 total_time |                                                    times                                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           path------------+--------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 77         | [2, 2, 3, 3, 3, 3, 5, 2, 4, 2, 2, 1, 4, 1, 2, 2, 3, 2, 2, 2, 1, 2, 3, 2, 2, 2, 3, 4, 2, 3, 2, 1]             | [{"id": 1125899906842769, "label": "Station", "properties": {"line": ["Canada"], "name": "Richmond-Brighouse"}}::vertex, {"id": 1125899906842755, "label": "Station", "properties": {"line": ["Canada"], "name": "Lansdowne"}}::vertex, {"id": 1125899906842733, "label": "Station", "properties": {"line": ["Canada"], "name": "Aberdeen"}}::vertex, {"id": 1125899906842736, "label": "Station", "properties": {"line": ["Canada"], "name": "Bridgeport"}}::vertex, {"id": 1125899906842759, "label": "Station", "properties": {"line": ["Canada"], "name": "Marine Drive"}}::vertex, {"id": 1125899906842754, "label": "Station", "properties": {"line": ["Canada"], "name": "Langara-49th Avenue"}}::vertex, {"id": 1125899906842764, "label": "Station", "properties": {"line": ["Canada"], "name": "Oakridge-41st Avenue"}}::vertex, {"id": 1125899906842750, "label": "Station", "properties": {"line": ["Canada"], "name": "King Edward"}}::vertex, {"id": 1125899906842737, "label": "Station", "properties": {"line": ["Canada"], "name": "Broadway-City Hall"}}::vertex, {"id": 1125899906842765, "label": "Station", "properties": {"line": ["Canada"], "name": "Olympic Village"}}::vertex, {"id": 1125899906842782, "label": "Station", "properties": {"line": ["Canada"], "name": "Yaletown-Roundhouse"}}::vertex, {"id": 1125899906842779, "label": "Station", "properties": {"line": ["Canada"], "name": "Vancouver City Centre"}}::vertex, {"id": 1125899906842781, "label": "Station", "properties": {"line": ["Canada", "Expo"], "name": "Waterfront"}}::vertex, {"id": 1125899906842739, "label": "Station", "properties": {"line": ["Expo"], "name": "Burrard"}}::vertex, {"id": 1125899906842746, "label": "Station", "properties": {"line": ["Expo"], "name": "Granville"}}::vertex, {"id": 1125899906842776, "label": "Station", "properties": {"line": ["Expo"], "name": "Stadium-Chinatown"}}::vertex, {"id": 1125899906842758, "label": "Station", "properties": {"line": ["Expo"], "name": "Main Street-Science World"}}::vertex, {"id": 1125899906842741, "label": "Station", "properties": {"line": ["Expo", "Millennium"], "name": "Commercial-Broadway"}}::vertex, {"id": 1125899906842768, "label": "Station", "properties": {"line": ["Millennium"], "name": "Renfrew"}}::vertex, {"id": 1125899906842771, "label": "Station", "properties": {"line": ["Millennium"], "name": "Rupert"}}::vertex, {"id": 1125899906842745, "label": "Station", "properties": {"line": ["Millennium"], "name": "Gilmore"}}::vertex, {"id": 1125899906842735, "label": "Station", "properties": {"line": ["Millennium"], "name": "Brentwood Town Centre"}}::vertex, {"id": 1125899906842747, "label": "Station", "properties": {"line": ["Millennium"], "name": "Holdom"}}::vertex, {"id": 1125899906842775, "label": "Station", "properties": {"line": ["Millennium"], "name": "Sperling-Burnaby Lake"}}::vertex, {"id": 1125899906842753, "label": "Station", "properties": {"line": ["Millennium"], "name": "Lake City Way"}}::vertex, {"id": 1125899906842767, "label": "Station", "properties": {"line": ["Expo", "Millennium"], "name": "Production Way-University"}}::vertex, {"id": 1125899906842757, "label": "Station", "properties": {"line": ["Expo", "Millennium"], "name": "Lougheed Town Centre"}}::vertex, {"id": 1125899906842738, "label": "Station", "properties": {"line": ["Millennium"], "name": "Burquitlam"}}::vertex, {"id": 1125899906842761, "label": "Station", "properties": {"line": ["Millennium"], "name": "Moody Centre"}}::vertex, {"id": 1125899906842748, "label": "Station", "properties": {"line": ["Millennium"], "name": "Inlet Centre"}}::vertex, {"id": 1125899906842742, "label": "Station", "properties": {"line": ["Millennium"], "name": "Coquitlam Central"}}::vertex, {"id": 1125899906842756, "label": "Station", "properties": {"line": ["Millennium"], "name": "Lincoln"}}::vertex, {"id": 1125899906842752, "label": "Station", "properties": {"line": ["Millennium"], "name": "Lafarge Lake-Douglas"}}::vertex] 90         | [2, 2, 3, 3, 3, 3, 5, 2, 4, 2, 2, 1, 4, 1, 2, 2, 3, 3, 2, 3, 2, 1, 2, 3, 2, 3, 1, 4, 2, 3, 3, 4, 2, 3, 2, 1] | [{"id": 1125899906842769, "label": "Station", "properties": {"line": ["Canada"], "name": "Richmond-Brighouse"}}::vertex, {"id": 1125899906842755, "label": "Station", "properties": {"line": ["Canada"], "name": "Lansdowne"}}::vertex, {"id": 1125899906842733, "label": "Station", "properties": {"line": ["Canada"], "name": "Aberdeen"}}::vertex, {"id": 1125899906842736, "label": "Station", "properties": {"line": ["Canada"], "name": "Bridgeport"}}::vertex, {"id": 1125899906842759, "label": "Station", "properties": {"line": ["Canada"], "name": "Marine Drive"}}::vertex, {"id": 1125899906842754, "label": "Station", "properties": {"line": ["Canada"], "name": "Langara-49th Avenue"}}::vertex, {"id": 1125899906842764, "label": "Station", "properties": {"line": ["Canada"], "name": "Oakridge-41st Avenue"}}::vertex, {"id": 1125899906842750, "label": "Station", "properties": {"line": ["Canada"], "name": "King Edward"}}::vertex, {"id": 1125899906842737, "label": "Station", "properties": {"line": ["Canada"], "name": "Broadway-City Hall"}}::vertex, {"id": 1125899906842765, "label": "Station", "properties": {"line": ["Canada"], "name": "Olympic Village"}}::vertex, {"id": 1125899906842782, "label": "Station", "properties": {"line": ["Canada"], "name": "Yaletown-Roundhouse"}}::vertex, {"id": 1125899906842779, "label": "Station", "properties": {"line": ["Canada"], "name": "Vancouver City Centre"}}::vertex, {"id": 1125899906842781, "label": "Station", "properties": {"line": ["Canada", "Expo"], "name": "Waterfront"}}::vertex, {"id": 1125899906842739, "label": "Station", "properties": {"line": ["Expo"], "name": "Burrard"}}::vertex, {"id": 1125899906842746, "label": "Station", "properties": {"line": ["Expo"], "name": "Granville"}}::vertex, {"id": 1125899906842776, "label": "Station", "properties": {"line": ["Expo"], "name": "Stadium-Chinatown"}}::vertex, {"id": 1125899906842758, "label": "Station", "properties": {"line": ["Expo"], "name": "Main Street-Science World"}}::vertex, {"id": 1125899906842741, "label": "Station", "properties": {"line": ["Expo", "Millennium"], "name": "Commercial-Broadway"}}::vertex, {"id": 1125899906842762, "label": "Station", "properties": {"line": ["Expo"], "name": "Nanaimo"}}::vertex, {"id": 1125899906842732, "label": "Station", "properties": {"line": ["Expo"], "name": "29th Avenue"}}::vertex, {"id": 1125899906842749, "label": "Station", "properties": {"line": ["Expo"], "name": "Joyce-Collingwood"}}::vertex, {"id": 1125899906842766, "label": "Station", "properties": {"line": ["Expo"], "name": "Patterson"}}::vertex, {"id": 1125899906842760, "label": "Station", "properties": {"line": ["Expo"], "name": "Metrotown"}}::vertex, {"id": 1125899906842770, "label": "Station", "properties": {"line": ["Expo"], "name": "Royal Oak"}}::vertex, {"id": 1125899906842743, "label": "Station", "properties": {"line": ["Expo"], "name": "Edmonds"}}::vertex, {"id": 1125899906842731, "label": "Station", "properties": {"line": ["Expo"], "name": "22nd Street"}}::vertex, {"id": 1125899906842763, "label": "Station", "properties": {"line": ["Expo"], "name": "New Westminster"}}::vertex, {"id": 1125899906842740, "label": "Station", "properties": {"line": ["Expo"], "name": "Columbia"}}::vertex, {"id": 1125899906842772, "label": "Station", "properties": {"line": ["Expo"], "name": "Sapperton"}}::vertex, {"id": 1125899906842734, "label": "Station", "properties": {"line": ["Expo"], "name": "Braid"}}::vertex, {"id": 1125899906842757, "label": "Station", "properties": {"line": ["Expo", "Millennium"], "name": "Lougheed Town Centre"}}::vertex, {"id": 1125899906842738, "label": "Station", "properties": {"line": ["Millennium"], "name": "Burquitlam"}}::vertex, {"id": 1125899906842761, "label": "Station", "properties": {"line":["Millennium"], "name": "Moody Centre"}}::vertex, {"id": 1125899906842748, "label": "Station", "properties": {"line": ["Millennium"], "name": "Inlet Centre"}}::vertex, {"id": 1125899906842742, "label": "Station", "properties": {"line": ["Millennium"], "name": "Coquitlam Central"}}::vertex, {"id": 1125899906842756, "label": "Station", "properties": {"line": ["Millennium"], "name": "Lincoln"}}::vertex, {"id": 1125899906842752, "label": "Station", "properties": {"line": ["Millennium"], "name": "Lafarge Lake-Douglas"}}::vertex](2 rows)

There are 2 different routes in total that can be taken. From this information, we can deduce that the optimal path takes 77 minutes using the Canada line until 'Waterfront' station, exchanging to the Expo line until 'Commercial-Broadway' station, and finally exchanging to the Millennium line until the final destination.

Efficiency may depend of different factors, whether it be cost, number of line exchanges made, and so on. Play around with the conditions to fit your specifications and see which path is the most efficient in terms of your definition!

Visualizing the Data

In this section, we will utilize AgeViewer to visualize the graphs that we have created in order to better understand the data.

Let's first view our entire map using the following query:

SELECT *FROM cypher('SkyTrain', $$    MATCH (n)-[r]-(m)    RETURN n, r, m$$) AS (n agtype, r agtype, m agtype);

The displayed graph may initially show an unorganized view of the nodes and relationships; however, by changing the layout options (or dragging each node to a specific position manually), we are able to better organize them. For this example, I have used the 'Klay' layout to view the path in a straight line.

Query Result

To view station names and their connections from specified lines, run the following query:

SELECT *FROM cypher('SkyTrain', $$    MATCH (n)-[r]-(m)    WHERE 'Canada' IN n.line    RETURN n, r, m$$) AS (n agtype, r agtype, m agtype);

You should get a graph similar to the one below. Just like in the 'Exploring the Data' section, you will be able to view the station names that belong to the 'Canada' line, this time along with their connected routes. Add the AND clause to further filter the results, for example, replace the line with the WHERE clause to WHERE 'Canada' IN n.line AND 'Expo' IN n.line and you will see that only the 'Waterfront' station and its connections get outputted.

Query Result

Finally, running the query below will show the most optimal path from 'Richmond-Brighouse' station to 'Lafarge Lake-Douglas' station.

SELECT paths, relFROM cypher('SkyTrain', $$    MATCH paths = (a:Station {name: 'Richmond-Brighouse'})-[:Route*]-(b:Station {name: 'Lafarge Lake-Douglas'})    WITH paths, relationships(paths) AS rels    UNWIND rels AS rel    WITH paths AS paths,        rel AS rel,        collect(rel.time) AS times,        sum(rel.time) AS total_time    RETURN paths, rel, times, total_time$$) AS (paths agtype, rel agtype, times agtype, total_time agtype)ORDER BY total_timeLIMIT 1;

This query is slightly altered from the previous query in the 'Finding the Optimal Path' section. The relationships are included in the output this time, and only the top row of the result is chosen as it will be the most time efficient path.

Query Result

As you can see, this graph visualization is much clearer than the table result and you can easily tell the path of the routes. Go ahead and try out your own queries and with your own data as well to apply the knowledge you have learnt! That is all for the basics of route trafficking in Apache AGE.

Conclusion

In this guide, we've explored how to route traffic using Apache AGE. We began by discussing the importance of setting up data that is relevant to a practical application, and used the SkyTrain system in Vancouver as an example dataset. We then walked through the steps involved in setting up the data, including creating nodes for each station and relationships between them to represent the connections between stations. Finally, we looked at different methods on producing and visualizing tables and graphs that will be of use upon deciding on the most optimal path.

With the skills and knowledge you've gained from this guide, you'll be well-equipped to take on any traffic routing challenge that comes your way. Start using Apache AGE today and become a traffic routing wizard!


Original Link: https://dev.to/xk_woon/become-a-traffic-wizard-a-beginners-guide-to-routing-traffic-with-apache-age-1kg6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To