DEV Community

Muhammad Farooq
Muhammad Farooq

Posted on

Understanding Graph Database and Cypher

Introduction

What is graph

A graph is a collection of nodes and relationships (edges) between them. Sometimes nodes are called vertices and relationships are called edges.

Representation

Image showing a graph

A circle typically represents a Node, while an arrowed line indicates an Edge connecting one Node to another.

The Nodes colored in red are labeled as "Females," while the blue Nodes are labeled as "Males."
The connection between them is labeled as "Follows" and is directed from one Node to another.
By assigning labels, the graph becomes easier to handle, allowing for the display, coloring, or manipulation of Nodes or relationships with specific labels.

Properties and Paths

We have the freedom to assign as much information as we desire to each node and relationship in the graph. This data is stored as properties.

To illustrate, let's consider the node named "Adam" which possesses the following properties:

{Name: Adam, Age: 25}

The property list functions similar to a key-value store, where the property name acts as the key, and the corresponding value can be a number (INT or FLOAT), a string, a list, or a BOOLEAN. Some graphs even contain nodes with several megabytes of information per node or edge.

In our graph visualization, we have chosen to display Adam's Name property on the node. However, we have the flexibility to showcase any property of the node based on our preference.

Furthermore, just like nodes, edges can also have unique properties. For example, we can store information such as the duration of Fia's following of Dee within the edge that connects them in the graph.

Paths

A Path is a sequence of alternating nodes and edges.

Nodes can exist with any number of outgoing or incoming edges. If we want to make the relationship bi-directional we must have two edges.

Representing Nodes, Edges, Labels, Properties and Paths.

OpenCypher makes clever use ASCII Art to make using the language easy to use.

  • Nodes are always represented as ( ), brackets - to look like circles.
  • Properties of nodes (or edges) are inside { } curly brackets separated by commas.
  • The property name and value are separated by : colon as in { Age: 30, Eyes: 'Brown' }
  • Edges are represented by [ ] square brackets and their direction is shown with a >
  • Labels are preceded by : colon as in :Female or :Follows

Here are progressively more detailed Pattern examples

*() - [ ] -> () *

  • this is the base notation of a path and matches every pair of connected nodes and their connecting edge

Visit Apache AGE:

Top comments (0)