Graph Algorithms Cheat Sheet



Introduction

Cypher is the most widely adopted, fully specified, and open query language for property graph databases. It provides an intuitive and fast way to work with property graphs.

This cheat sheet is one you will want to bookmark as it is part of an ebook! O(n2)O(n^2)O(n2) O(1)O(1)O(1) Selection sort works by repeatedly 'selecting' the. Graph Traversal The most basic graph algorithm that visits nodes of a graph in certain order Used as a subroutine in many other algorithms We will cover two algorithms – Depth-First Search (DFS): uses recursion (stack) – Breadth-First Search (BFS): uses queue Depth-First and Breadth-First Search 17. Cheat-sheet This cheat sheet contains templates for common data structures and algorithms in Python.

This article contains some of the most useful and common Cypher queries along with their explanations. Whenever you are not sure how to write a Cypher query, you can just take a look at this cheat sheet and try again. If you are new to graph databases and Cypher, you can also use this post to get acquainted with some of the features that Cypher and Memgraph have to offer.

1. Matching

Find nodes with specific properties

  • MATCH (c:City): the MATCH clause specifies a node pattern with the label City and assigns the matches to variable c.
  • WHERE c.name = 'London': the WHERE clause filters the matched results to those that have a name property with value London.
  • RETURN c.population_size: the RETURN clause is used to request specific results.

This query can be rewritten without a WHERE clause to achieve the same result.

Find nodes with specific relationships

  • MATCH (city:City)-[:IN]-(country:Country): the MATCH clause specifies a node and relationship pattern with two connected nodes, labeled City and Country, connected by a relationship of type IN.
Graph Algorithms Cheat Sheet

Match labels

This query can be rewritten using the WHERE clause to achieve the same result.

Match multiple labels

This query can be rewritten using the WHERE clause to achieve the same result.

Matching nodes with properties in a range

2. Creating

Create a node

  • c:City: creates a new node with the label City and assigns it to variable c (which can be omitted if it’s not needed).
  • {name: 'Zagreb', population_size: 1000000}: the newly created node has two properties, one with a string value and another with an integer value.

Create nodes with relationships

Graph Algorithms Cheat Sheet Free

The CREATE clause is used to create two new nodes and a directed relationship between them.

Create a relationship between existing nodes

This will create a directed relationship of type IN between two existing nodes. If such a relationship already exists, this query will result in a duplicate. To avoid this, you can use the MERGE clause:

3. Updating

Add or update node properties

Graph Algorithms Cheat Sheet

If you use the SET clause on a property that doesn’t exist it will be created.

Replace all node properties

  • SET c = {name: 'UK' ..}: this SET clause will delete all existing properties and create the newly specified ones.

Update multiple node properties

  • SET c += {name: 'UK' ..}: this SET clause will add new properties and update existing ones if they are already set.

Check if a property exists and update it

Because the WHERE clause contains the statement c.language IS NULL, the node will only be matched if it doesn’t have a language property.

Rename a property

  • WHERE c.official_language IS null: the WHERE clause makes sure that you only create the new property in nodes that don’t have a property with the same name.
  • SET n.official_language = n.language: you are technically not renaming a property but rather creating a new one with a different name and the same value.
  • REMOVE n.language: the REMOVE clause is used to delete the old property.

4. Deleting

Delete a node

  • DELETE r, c: before you can delete a node, all of its relationships need to be deleted first.

This query can be rewritten with the DETACH clause to achieve the same result.

Delete a property

This query will delete the property language from a specific node.

Delete label in every node

This query will delete the label Country from every node.

Delete one of multiple labels

This will delete the label City from every node that has the labels Country and City.

Delete all nodes and relationships

This query will delete the whole database. Download microsoft access wine cellar template free software.

5. Constraints

Create a uniqueness constraint

This query will make sure that every node with the label City has a unique value for the location property.

Create an existence constraint

This query will make sure that every node with the label City has the property name.

Cheat

Check constraints

This query will list all active constraints in the database.

Drop a uniqueness constraint

This query will remove the specified uniqueness constraint.

Drop an existence constraint

This query will remove the specified existence constraint.

6. Graph Algorithms

To find out more about the built-in algorithms in Memgraph, take a look at the reference guide.

Breadth First Search

This query will find all paths of length up to 10 between nodes c1 and c2.

Weighted Shortest Path

The above query will find the shortest path of length up to 10 nodes between nodes c1 and c2. The length restriction parameter is optional.

7. NetworkX

If you want to know which NetworkX algorithms are available in Memgraph, take a look at the reference guide.

Analyze the whole graph

This query will return various information like the number of nodes, number of edges, average degree, etc.

Find weakly connected components (Union Find)

C++ Stl Algorithm Cheat Sheet

This query will search the whole graph for weakly connected components.

Calculate PageRank for all nodes

Graph Algorithms Cheat Sheet

This query will calculate the rank of every node, order them from highest to lowest and return the first 10 results.

8. Other Useful Cypher Queries

Count all nodes

This query will return the number of nodes in the database.

Count all relationships

This query will return the number of relationships in the database.

Limit the number of returned results

  • LIMIT 5: this will limit the number of returned nodes to 5.

Specify an alias for results

Graph Algorithms Pdf

By using AS with the RETURN clause, the property population_size will be returned with an alias.

Conclusion

Graph Algorithms Cheat Sheet

Cypher is an extensive query language with lots of features and this cheat sheet is a great starting point for mastering them.

All Graph Algorithms

If you have any questions about Cypher that aren’t covered in this article, please take a look at our Cypher manual. Feel free to also share your questions on StackOverflow with the tag memgraphdb or on our community forum.