# Graph Algorithms Cheat Sheet

Know Thy Complexities! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them. Algorithms need not be boring or hard to use. In fact, algorithms surround you in many ways that you might not have thought about, and you use them every day to perform important tasks. However, you need to be able to use algorithms without having to become a mathematician. Programming languages enable you to describe.

### 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.

### Graph Algorithms Cheat Sheet

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

### Graph Algorithms Cheat Sheet Answers

• 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.

### 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.

## 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

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

### Algorithms For Dummies 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.

## 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.

### 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.

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)

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

### Calculate PageRank for all nodes

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.

### C++ Stl Algorithm Cheat Sheet

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

### Specify an alias for results

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

## Conclusion

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

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.