<commit_before>---
layout: post
title: "Graphs With JGraphT"
date: 2020-08-08 16:22
comments: true
categories: [ java, graphs]
---

Hey ho! What crazy times to be alive! But here we go for a new post on Graphs. To be more precise [JGrahT](https://jgrapht.org/) library, a Java library I I used quite heavily recently in my last company. In this post I'm going to use one of the Scoring algorithms provided by the library to solve a SEO related problem. The goals is to serve as an introduction to the library and some basic usage of it. Check it out (Word of waning, cod heavy post)! 

<!--more-->

# Graphs

[Graph Theory](https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)) was one of the most intimidating topics at the university for some. When you see drawings of graphs, all makes sense, until you have to do some code yourself. Then the challenges begins. Then you leave the university and may never see graphs again. I had better luck.

At [Omio](https://omio.com/) though, the idea of graphs is simply the natural way to go. Omio allow people to travel from A to B, and then you have a graph!

**DISCLAIMER:** *This post has no connection with Omio, nor reflects the way systems were developed there. This post is just inspired in a hypothetical scenario related to travel and solving the problem of bots crawling.* 

After using [Neo4j](https://paulosuzart.github.io/blog/2019/02/01/crawling-with-golang/), it was JGraphT that conquered me. It was a critical piece that allowed us to solve a 6 year long issue with Internal Linking. You can read more about this topic in this blog post: [Nailing Internal Linking with Graph Theory](https://medium.com/omio-engineering/nailing-seo-internal-linking-with-graph-theory-2c45544a024d).

# The challenge

Enough small talks. Time to see some code. Let's give a problem statement so we introduce JGraphT and the ingredients to solve the problem:

> Given a set of routes (pair of locations here represented just by their names), link from the home page of your website at most `n` pages so that upon visit, a bot can grasp page relevance, now that they are linked from the home page.

Well, in real life problem is slightly more complex. But for our pet project we just want to link from the home page, the pages that will cause the bots to "walk" the least possible in order to cover most of the graph.

**Then the Graph**

The graph can be assembled out of the list of routes that have a page representing each of them:
<commit_msg>Added TOC<commit_after>---
layout: post
title: "Graphs With JGraphT"
date: 2020-08-08 16:22
comments: true
categories: [ java, graphs]
---

Hey ho! What crazy times to be alive! But here we go for a new post on Graphs. To be more precise [JGrahT](https://jgrapht.org/) library, a Java library I I used quite heavily recently in my last company. In this post I'm going to use one of the Scoring algorithms provided by the library to solve a SEO related problem. The goals is to serve as an introduction to the library and some basic usage of it. Check it out (Word of waning, cod heavy post)! 

<!--more-->

- [Graphs](#graphs)
- [The challenge](#the-challenge)
- [Conclusion](#conclusion)
- [Appendix](#appendix)
  * [Import / Export](#import--export)
  * [Edges and Vertex](#edges-and-vertex)

# Graphs

[Graph Theory](https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)) was one of the most intimidating topics at the university for some. When you see drawings of graphs, all makes sense, until you have to do some code yourself. Then the challenges begins. Then you leave the university and may never see graphs again. I had better luck.

At [Omio](https://omio.com/) though, the idea of graphs is simply the natural way to go. Omio allow people to travel from A to B, and then you have a graph!

**DISCLAIMER:** *This post has no connection with Omio, nor reflects the way systems were developed there. This post is just inspired in a hypothetical scenario related to travel and solving the problem of bots crawling.* 

After using [Neo4j](https://paulosuzart.github.io/blog/2019/02/01/crawling-with-golang/), it was JGraphT that conquered me. It was a critical piece that allowed us to solve a 6 year long issue with Internal Linking. You can read more about this topic in this blog post: [Nailing Internal Linking with Graph Theory](https://medium.com/omio-engineering/nailing-seo-internal-linking-with-graph-theory-2c45544a024d).

# The challenge

Enough small talks. Time to see some code. Let's give a problem statement so we introduce JGraphT and the ingredients to solve the problem:

> Given a set of routes (pair of locations here represented just by their names), link from the home page of your website at most `n` pages so that upon visit, a bot can grasp page relevance, now that they are linked from the home page.

Well, in real life problem is slightly more complex. But for our pet project we just want to link from the home page, the pages that will cause the bots to "walk" the least possible in order to cover most of the graph.

**Then the Graph**

The graph can be assembled out of the list of routes that have a page representing each of them:

{'fraction_non_alphanumeric': 0.06960556844547564, 'fraction_numerical': 0.011987625676720804, 'mean_word_length': 4.131944444444445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4878610', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1358, 'n_words': 803}