Memgraph logo
Back to blog
Memgraph Cypher Implementation: Flexibility and Advanced Traversals

Memgraph Cypher Implementation: Flexibility and Advanced Traversals

By Katarina Supe
7 min readJune 24, 2024

Cypher query language is an intuitive and flexible graph query language. With Cypher, you express what to retrieve but not how to retrieve it, allowing you to focus on the problem domain instead of worrying about the syntax.

Memgraph enhances basic Cypher with additional constructs to improve the user experience. Custom query modules let users define custom procedures to fill gaps in the current Cypher implementation.

This blog post explores the constructs in Memgraph’s Cypher implementation that make it flexible, easy to understand, and use.

Powerful Deep Path Traversals

Handling deep path traversals is crucial for use cases like cybersecurity, fraud detection, and identity and access management. Memgraph’s core includes depth-first search, breadth-first search, weighted shortest path, and all shortest paths—each with customizable Cypher syntax.

depth-first search

With Memgraph, you can perform inline filtering and limit path length, enhancing execution speed, expressivity, and efficiency. This flexibility allows you to write queries in one line and limit your search for the quickest results, which is vital when time is of the essence.

Example: Breadth-First Search

MATCH path=(:City {name: 'London'})-[r:ROAD *BFS ..2 (r, n | r.continent = 'Europe')]->(:City)
RETURN path;

Example: All Shortest Paths

MATCH path=(n {id: 0})-[relationships:CloseTo *ALLSHORTEST (r, n | 1) total_weight]-(m {id: 9})
RETURN nodes(path), total_weight;

weighted shortest path

For more on Memgraph’s deep path traversals, refer to Deep Path Traversal Capabilities. It explains how these built-in algorithms work, provides usage examples, and offers optimization tips for enhancing performance in large-scale graph environments.

Beyond these, various popular graph algorithms like PageRank or community detection are implemented in the MAGE library.

Extend Cypher With Out-Of-The-Box Graph Algorithms

Memgraph Advanced Graph Extensions (MAGE) library extends Cypher with graph algorithms and utility modules. The CALL clause invokes procedures, while YIELD and RETURN clauses handle the results, simplifying the use of advanced graph algorithms.

Example: PageRank Algorithm

CALL pagerank.get()
YIELD node, rank
RETURN node, rank;

Common Graph Algorithms in MAGE

  • PageRank - Represents the likelihood of a random click leading to a particular page. MAGE offers a dynamic version of this algorithm.

  • Betweenness Centrality - Determines node importance based on frequency on shortest paths, indicating its influence on information flow within the network. MAGE offers a dynamic version of this algorithm.

  • Community Detection - Computes graph communities where nodes connect more intensely within than outside the community using the Louvain method. MAGE offers the dynamic version of this algorithm.

  • Node2vec - Maps nodes to a low-dimensional space preserving network neighborhoods. MAGE offers the dynamic version of this algorithm.

Integrations and Utility Modules

MAGE integrates with powerful libraries like cuGraph, Elasticsearch, igraph and NetworkX.

It also offers utility modules such as date, distance_calculator, graph_util, path, migrate, or periodic. These utility modules were developed in response to user feedback, recognizing the need for more functionalities in Cypher. For instance, the periodic module efficiently deletes all nodes and relationships from the database, inspiring the new DROP GRAPH query.

While the MAGE library offers an extensive array of procedures, it may not cover every specific need you have for your Cypher queries. No worries—custom query modules are available to fill gaps and meet your unique requirements.

Custom Query Modules

Using Memgraph’s APIs for Python, C/C++, or Rust allows you to create custom procedures to call in Cypher. MAGE is a collection of pre-prepared custom query modules that showcase the capabilities of our APIs.

If you need an algorithm or utility procedure unavailable in MAGE, you can implement it in your preferred language. Prototyping in Python is the easiest way to start, and if you require better performance, switching to a C++ implementation will provide a speed boost.

You can implement both read and write procedures.

Example: Simple Read Procedure in Python

import  mgp
 
@mgp.read_proc
def total_cost(context: mgp.ProcCtx,
               city: str,
               adults: int,
               children: mgp.Nullable[int] = None
               ) -> mgp.Record(Total_cost_per_night=mgp.Nullable[float]):
 
    for vertex in context.graph.vertices:
        if vertex.properties["name"] == city and isinstance(vertex.properties.get("cost_per_night_USD"), float):
            cost_per_night = vertex.properties.get("cost_per_night_USD")
            total_cost = cost_per_night * adults
            if children is not None:
                total_cost += cost_per_night / 2 * children
            return mgp.Record(Total_cost_per_night=total_cost)
 
    return mgp.Record(Total_cost_per_night=None)

If you’d like to learn more about it, visit our Custom query modules docs and follow the guides we prepared.

What’s Next for Cypher?

Cypher query language is constantly expanding in Memgraph, and we look forward to providing more value to Memgraph’s users with every new iteration. Its flexibility and expressiveness allow us to get the most out of our database and help users easily utilize advanced graph analytics.

If you feel something is missing in Memgraph’s current implementation, please open an issue on our GitHub. For workarounds, refer to our Memgraph docs or join our Discord server to connect with our community support team.

Further Reading

As you might have heard, the GQL standard has been released. We're thrilled to see GQL come to life, especially given its similarities to Cypher. The Memgraph team is exploring ways to support and add value for users interested in GQL. In the meantime, here are some resources to help you learn Cypher.

Join us on Discord!
Find other developers performing graph analytics in real time with Memgraph.
© 2024 Memgraph Ltd. All rights reserved.