Learn the theory behind how to deal with streaming graph data by using Dynamic PageRank
Tags: memgraph, graphdatabase, database, algorithms
Published: 2023-01-19 15:57:50

## Introduction

[*PageRank*](https://en.wikipedia.org/wiki/PageRank) is one of those iconic algorithms that have changed the world of technology forever. You probably know Google's story - after designing a brilliant algorithm that is able to measure the influence of a webpage simply by looking at who has a link toward the targeted webpage, Google has changed the course of history within a web-browsing niche.

## Where can PageRank be used?

*PageRank* can be used as an influence measurement in many different applications when working with highly interconnected data. For example, one can wonder who is an influential individual in the connected data of **social network** interactions. Others might want to investigate **transactions** made by a particular individual in some **financial data warehouses**. Having an influence measurement, in that case, can lead toward finding who is collaborating with a suspicious number of entities, which could be interpreted as fraudulent behavior. Finally, an extremely intersected **internet network** may require influence measurement to pinpoint the specific place where a data transfer bottleneck could occur. 

All these examples have one thing in common: they can be solved/investigated with proper influence measurement information. Organizing data as connected entities within a **graph** opens up the possibility of using graph algorithms that are specifically designed for such problems. This is what the world's largest companies have been doing for years. However, the real world demands solving problems that are further ahead in the process of reaching the goal. 

Large and constantly evolving data quickly makes common approaches obscure. While going through the following sections, think about your data, question yourself whether it is large or not, and whether it evolves with time or it's static. If you have **large** and **dynamic data**, this article can help you dive into building an influence measurement that constantly updates whenever new data is available. 

## How to use PageRank on dynamic data?

An obvious and trivial solution to the previous problem would be: whenever a **new data point** or connection comes into our platform, simply recalculate the influence measurement with the *PageRank* algorithm. This is very simple, and it works. However, the story changes over time when your business becomes more popular. You begin to have more work and therefore datapoints keep coming at a faster pace, accumulated data keeps rising in size and your algorithm fails to deliver valuable information **efficiently**. 

To prevent that from happening, the next section will describe the method for **incrementally updating** PageRank measurements whenever a new data point hits the platform.

Imagine a case where data is ingested via **streaming**, and you demand an almost real-time answer to what is the influence of some entity.

## Incremental PageRank on streaming data

I am going to describe the algorithm with the most simple and yet genius idea of how to update influence/ranking when new information is added into the system. The work from [**Twitter**](https://en.wikipedia.org/wiki/Twitter) employees described in "[Fast Incremental and Personalized PageRank](http://snap.stanford.edu/class/cs224w-readings/bahmani10pagerank.pdf)" by [Bahmani](https://scholar.google.com/citations?user=v-hyE4MAAAAJ&hl=en) et al. can be simply put like this: 
1. **Approximatively** calculate the influence measurement on your current, **static data**.
2. When a new connection/data point comes into the system, **recalculate measurement** only on data that is **affected by the update**. 

The first one is trivial. There are numerous techniques to empower the *PageRank* algorithm on static data. One of them is approximative.

#### Approximative PageRank

Intuitively, a PageRank is the **probability** to end up in a certain node starting from a randomly picked one. Implementing that exact idea is going to lead toward the approximative solution to the problem. If we sample *R* different [**random walks**](https://www.cs.yale.edu/homes/spielman/561/lect10-18.pdf) of average length 1/ε, where ε notes the probability of finishing a random walk, we have enough information to approximate **centrality/influence**.

Now, for a node v, let's introduce variable Xᵥ that denotes the number of times that v is **stored** in some random walk. If n is a number of total nodes, by the definition that was mentioned previously, PageRank can be approximated with the next formula: 

![memgraph-tutorial-page-rank-formula](https://public-assets.memgraph.com/dynamic-page-rank-on-streaming-data/memgraph-tutorial-page-rank-formula.png)

![memgraph-tutorial-walks](https://public-assets.memgraph.com/dynamic-page-rank-on-streaming-data/memgraph-tutorial-walks.png)
<center>Illustration of different walks starting from the node "1".</center>

#### Incremental update

When saying, "*recalculate measurement only on data which is affected by the update*" one wonders what specific data points should be updated once new data hits the system. We can think about it like this: after creating a **connection**, or a **new data point**, we are going to trigger an update on PageRank values for each node.

Calculation of parameters depends on previously calculated walks, therefore we need to **store them beforehand**. After we recalculate routes, we can easily derive new PageRank values from the **formula above**. Walks that need to be changed are affected by new graph entities. If a connection is added, all walks that are going **through the connection's nodes** need to be changed (the image below shows how walk #3 updates once a new edge arrives). As mentioned, after these changes, the calculation of PageRank is trivial by reusing the stored information and formula above.

![memgraph-tutorial-walks-new-edge](https://public-assets.memgraph.com/dynamic-page-rank-on-streaming-data/memgraph-tutorial-walks-new-edge.png)
<center>When a new edge arrives in the graph, walks are adapted accordingly.</center>


## Conclusion

The idea of this article is to describe how a **smart algorithm** can spare a large number of computations and deliver valuable insights into data faster than before. With the current progress and rise in **streaming data**, such insight can solve troubles for a huge amount of companies. 

Our team of engineers is currently tackling the problem of graph analytics algorithms on **real-time data**. If you want to discuss how to apply **online/streaming algorithms** on connected data, feel free to join our **[Discord server](https://memgr.ph/join-discord)** and message us.

**MAGE** shares his wisdom on a [**Twitter** channel](https://twitter.com/intent/follow?screen_name=memgraphmage). Get to know him better by following him 🐦

![memgraph-tutorial-twitter-page-rank](https://public-assets.memgraph.com/dynamic-page-rank-on-streaming-data/memgraph-tutorial-twitter-page-rank.png)

Last but not least, check out [**MAGE**](https://github.com/memgraph/mage) and don't hesitate to give a star ⭐ or contribute with new ideas.

[![Read more about graph algorithms on memgraph.com](https://public-assets.memgraph.com/external/memgraph-read-more-gradient-1200.png)](https://memgraph.com/blog?topics=Graph+Algorithms&utm_source=devto&utm_medium=referral&utm_campaign=blog_repost&utm_content=banner#list)