Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

Compute the Graph Center and Periphery

Introduction

In graph theory, the concepts of graph center and periphery are used to determine the central and peripheral nodes in a network. These measures help us understand the importance of nodes in a network and have various real-world applications like social network analysis, communication networks, transportation networks, and more. In this lesson, we will learn how to compute the graph center and periphery, along with their real-world applications and examples.

Graph Center and Periphery

The graph center is a set of nodes with the minimum eccentricity in a graph. The eccentricity of a node is the maximum shortest path length between the node and any other node in the graph. In simpler terms, the graph center comprises the nodes that are closest to all other nodes in the graph.

On the other hand, the graph periphery is a set of nodes with the maximum eccentricity in a graph. These nodes are the farthest from all other nodes in the graph.

Real-world Examples

  1. In social network analysis, the graph center can represent the most influential people in a network, while the graph periphery represents the least influential people.
  2. In communication networks, the graph center can be used to identify the nodes with the highest traffic capacity, and the periphery can help identify potential bottlenecks.
  3. In transportation networks, the graph center can help identify the most accessible locations, while the graph periphery can indicate the least accessible locations.

Real-world Scenario

Let's consider a scenario where we have a transportation network of several cities connected by roads. We want to find the most centrally located city (graph center) to build a new airport and the most remote city (graph periphery) to build a new power plant.

Problem Statement

Given a connected, undirected graph G = (V, E) with V as the set of vertices (cities) and E as the set of edges (roads), find the graph center and periphery of G.

Solution

We can solve this problem by performing the following steps:

  1. Compute the shortest path between all pairs of nodes using an algorithm like Floyd-Warshall.
  2. Calculate the eccentricity for each node.
  3. Find the nodes with minimum eccentricity (graph center) and maximum eccentricity (graph periphery).

Let's implement this solution using Python.

import sys

def floyd_warshall(graph):
    # Initialize the distance matrix
    distance = [[sys.maxsize for _ in range(len(graph))] for _ in range(len(graph))]
    for i in range(len(graph)):
        for j in range(len(graph)):
            if i == j:
                distance[i][j] = 0
            elif graph[i][j] != 0:
                distance[i][j] = graph[i][j]

    # Compute shortest paths
    for k in range(len(graph)):
        for i in range(len(graph)):
            for j in range(len(graph)):
                distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])

    return distance

def find_center_and_periphery(graph):
    # Compute the shortest path between all pairs of nodes
    distance = floyd_warshall(graph)

    # Compute eccentricity for each node
    eccentricity = [max(row) for row in distance]

    # Find graph center and periphery
    center = [i for i, e in enumerate(eccentricity) if e == min(eccentricity)]
    periphery = [i for i, e in enumerate(eccentricity) if e == max(eccentricity)]

    return center, periphery

# Example graph (adjacency matrix)
graph = [
    [0, 1, 1, 0, 0],
    [1, 0, 1, 1, 0],
    [1, 1, 0, 1, 1],
    [0, 1, 1, 0, 1],
    [0, 0, 1, 1, 0]
]

center, periphery = find_center_and_periphery(graph)
print("Graph center:", center)
print("Graph periphery:", periphery)

This code first computes the shortest path between all pairs of nodes using the floyd_warshall function. Then, it calculates the eccentricity for each node and finds the graph center and periphery using the find_center_and_periphery function.

In the given example, the output should be:

Graph center: [2]
Graph periphery: [0, 4]

This means that city 2 is the most centrally located city, and cities 0 and 4 are the most remote cities.

Conclusion

In this lesson, we learned how to compute the graph center and periphery using the Floyd-Warshall algorithm and their real-world applications. The solution provided can be applied to various scenarios like social network analysis, communication networks, and transportation networks to find the most central and peripheral nodes.