# Social Network and user recommendations using Graphs and Dijkstra algorithm

Hey! I'm Siddhi. I'm an engineering student studying Computer Science. One of my favorite subjects of my curriculum is Data Structures and the topic I fear the most is Graphs. To overcome this fear, I tried to implement this data structure in a practical application - a social network.

In this blog, I'll explain how I used graphs to implement a social network and how I used the Dijkstra algorithm to provide people you may know user recommendations.

There are 2 main parts:

- Building a social network
- Providing people you may know user recommendations

## What is a social network?

A social network is a structure consisting of users and connections between them.

We can say that a social network is a graph where,

- Nodes are users
- Edges are connections/relations
- Weight is the closeness between the users

### How to determine the closeness between the users?

The weight of the edge between two nodes will decide the closeness between the users. Lower the weight, the closer the two users are. But how to determine the closeness between two users.

To calculate the closeness between two users, I designed a simple algorithm, where

- When the user makes a connection the weight between them is set to some MAX value.
- After that, whenever a user likes the other user's post they become close by 2 weights,
- When user1 comments on user2's post they become close by 4 weights.

This closeness parameter will help us to provide better recommendations.

The less the weight between two users, the more close they are.

So by using graph and this closeness algorithm, the social network can be implemented. Now, the next part is providing user recommendations.

### What is the Dijkstra algorithm?

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph

We will not get into the details of the implementation of the algorithm, but rather focus on what it does and how it can be used in this application.

Let's take an example, we have the following graph

We want to find the shortest path between A and E,

- There are two paths between A and E, which are
**A-B-C-E**and**A-D-E** - The weight of the path
**A-B-C-E**is 3 + 2 + 2 = 7 - The weight of the path
**A-D-E**is 2 + 2 = 4 **Dijkstra algorithm will return the shortest path.****So, in this example, it will return***A-D-E.*

But, the main question is how will we use this algorithm to provide user recommendations?

### How to use Dijkstra to provide user recommendations?

The steps are simple. If we want to find user recommendations for a particular user,

- We will need to find the shortest path from that user to all the other users in the network.
- Sort them according to weight.
- Now we will have the people you many know in ascending order of how likely that user might know them.

Let's understand this with an example,

Finding People you may know for **User A,**

Dijkstra algorithm will find the shortest path to all other users

B - 3, C - 5, D - 12, E - 15

We will drop the connections of the user

**A,**so now we have,C - 5, E - 15

We will sort the list by weight

C - 5, E - 15

Now, we get our result. Here, User A is more likely to know user C as it is the first element in the sorted list.

We are likely to know users who are friends with our closest friends. Such users can be found by using the procedure mentioned above.

### What does the result mean?

These steps will return the users with the least path. The connection weight between the users will be less. The less the connection weight between them, the more likely they know each other. Thus, the recommendations will be useful.

This is how I used graphs and the Dijkstra algorithm to implement a social network and to provide people you may know user recommendations.