# Calculate the Girth of a Graph

## Introduction to Girth of a Graph

The girth of a graph is the length of the shortest cycle in the graph. In other words, it is the smallest number of edges that form a closed loop. Calculating the girth of a graph is an important task in graph theory, as it helps us understand the graph's structure and properties.

## Real-world Examples and Scenarios

Calculating the girth of a graph has multiple applications in real-world scenarios. Some examples include:

- In transportation networks, the girth of a graph can help identify potential traffic bottlenecks or areas with a higher likelihood of congestion.
- In social network analysis, the girth can provide insights into the structure of social networks and help identify communities or cliques.
- In computer networks, the girth can be used to analyze the resilience and robustness of the network topology.

## Real-world Scenario and Technical Problem

Let's consider a transportation network in a city. The city's road network can be modeled as a graph, where intersections are represented as vertices and roads as edges. The city officials want to identify the areas with potential traffic bottlenecks to plan for road expansions or new infrastructure projects.

## Problem Statement and Formal Definition

Given a graph G(V, E), where V is the set of vertices representing intersections, and E is the set of edges representing roads, find the girth of the graph, i.e., the length of the shortest cycle in the graph.

## Tying the Problem Statement with the Real-world Scenario

By solving this problem, we can determine the smallest number of roads that form a closed loop in the city's road network. This information can then be used by city officials to identify potential traffic bottlenecks and plan for road expansions or new infrastructure projects.

## Solution to the Problem

We can solve this problem using a Breadth-First Search (BFS) algorithm. The BFS algorithm explores the vertices of a graph in breadthward motion, visiting all the neighbors of a vertex before moving on to the next level of vertices.

To find the girth of a graph, we can perform BFS from each vertex in the graph and keep track of the shortest cycle found so far.

## Solving the Problem Step by Step with the Real-world Scenario

- Initialize the girth as infinity.
- For each vertex in the graph, perform BFS and keep track of the shortest cycle found.
- Update the girth if a shorter cycle is found.

## Actual Code Solution with High-level Comments

```
from collections import deque
def bfs(g, src):
# Initialize the distance array and set the distance of the source vertex to 0
dist = [-1] * len(g)
dist[src] = 0
# Initialize the BFS queue and add the source vertex
q = deque([src])
# Perform BFS
while q:
v = q.popleft()
for neighbor in g[v]:
if dist[neighbor] == -1:
dist[neighbor] = dist[v] + 1
q.append(neighbor)
elif dist[neighbor] >= dist[v]:
# A cycle is found, return its length
return dist[v] + dist[neighbor] + 1
return float('inf')
def girth(g):
min_cycle = float('inf')
for v in range(len(g)):
min_cycle = min(min_cycle, bfs(g, v))
return min_cycle
```

## Calling the Functions with Actual Values

Let's consider a sample road network represented as an adjacency list:

```
road_network = [
[1, 2],
[0, 2, 3],
[0, 1, 3],
[1, 2]
]
```

Calculate the girth of the road network:

```
print(girth(road_network)) # Output: 3
```

## Explaining the Code Solution with Intuitions and Analogies

The `bfs`

function takes the graph and a source vertex as input and performs BFS. It keeps track of the distances from the source vertex to all other vertices. When a cycle is detected, the length of the cycle is returned. Otherwise, infinity is returned.

The `girth`

function calculates the girth of the graph by iterating through all vertices and performing BFS from each vertex. It keeps track of the shortest cycle found so far and returns the girth when all vertices have been processed.

## Solving Similar Real-world Problems

The same solution can be applied to other real-world problems where the girth of a graph is relevant, such as social network analysis or computer networks. By calculating the girth of the graph, we can gain insights into the structure of the network and identify potential areas for improvement or optimization.