# Determine the Wiener Index of a Graph

## Introduction to the Wiener Index of a Graph

The Wiener Index of a graph is a topological index used in mathematical chemistry to study the molecular structure of chemical compounds. It is defined as the sum of all pairwise shortest path distances between the vertices of the graph. In simpler terms, it represents the total distance between all pairs of vertices in a graph.

The Wiener Index provides insights into various properties of a molecule, such as boiling points, molecular stability, and the efficiency of certain chemical reactions. As such, determining the Wiener Index of a graph is essential for understanding the properties of chemical compounds and optimizing their synthesis.

## Real-world examples and scenarios

Imagine a pharmaceutical company that is developing a new drug. The company needs to analyze various properties of the drug's molecular structure to ensure its efficiency and safety. By calculating the Wiener Index of the molecule's graph representation, the researchers can predict crucial properties and optimize the drug's synthesis process.

Another scenario could be a network analysis of a transportation system, where nodes represent locations, and edges represent routes. The Wiener Index could help in understanding the overall connectivity of the transportation system and aid in optimizing the routing for efficient transportation.

## Problem Scenario: Drug Synthesis in the Pharmaceutical Industry

A pharmaceutical company is developing a new drug, and they need to understand the properties of the drug's molecular structure. They have the molecular graph representation of the drug, and they want to determine its Wiener Index.

## Problem Statement

Given a connected, undirected graph `G(V, E)`

with `V`

vertices and `E`

edges, find the Wiener Index of the graph.

## Tying the problem statement with the real-world scenario

By solving this problem, the pharmaceutical company can predict the properties of the drug's molecular structure and optimize the synthesis process, leading to a more efficient and safe drug.

## Solution to the problem

We can solve this problem by implementing the following steps:

- Compute the shortest path distances between all pairs of vertices using the Floyd-Warshall algorithm.
- Sum up all the pairwise shortest path distances to obtain the Wiener Index.

## Step-by-step solution with the real-world scenario

Let's implement the solution using Python to calculate the Wiener Index of the drug's molecular structure, represented as a graph.

### Actual code solution with high-level comments

```
import sys
# Initialize the graph
def initialize_graph(vertices):
graph = [[sys.maxsize for _ in range(vertices)] for _ in range(vertices)]
for i in range(vertices):
graph[i][i] = 0
return graph
# Add an edge to the graph
def add_edge(graph, u, v, weight):
graph[u][v] = weight
graph[v][u] = weight
# Implement the Floyd-Warshall algorithm to compute shortest path distances
def floyd_warshall(graph):
vertices = len(graph)
for k in range(vertices):
for i in range(vertices):
for j in range(vertices):
graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
# Calculate the Wiener Index of the graph
def wiener_index(graph):
floyd_warshall(graph)
wiener_index = 0
for row in graph:
wiener_index += sum(row)
return wiener_index // 2
# Create the graph for the drug's molecular structure
vertices = 5
edges = [(0, 1, 1), (1, 2, 1), (1, 3, 1), (3, 4, 1)]
graph = initialize_graph(vertices)
# Add the edges to the graph
for edge in edges:
add_edge(graph, edge[0], edge[1], edge[2])
# Calculate the Wiener Index of the drug's molecular structure
drug_wiener_index = wiener_index(graph)
print("Wiener Index of the drug's molecular structure:", drug_wiener_index)
```

### Explanation of the code solution

In the provided code, we first initialize a graph with the given number of vertices. We then add the edges of the graph using the `add_edge`

function. After constructing the graph, we compute the shortest path distances between all pairs of vertices using the Floyd-Warshall algorithm implemented in the `floyd_warshall`

function. Finally, we calculate the Wiener Index using the `wiener_index`

function, which sums up all the pairwise shortest path distances.

## How the solution can solve other real-world problems

The provided solution can also be applied to other real-world problems involving the analysis of graphs, such as network analysis, transportation systems, and social networks. By calculating the Wiener Index, we can gain insights into the overall connectivity and efficiency of these systems and improve their performance.