Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

Determine the Eccentricity of Nodes in a Graph

Introduction

The eccentricity of a node in a graph is a measure of the maximum distance between that node and any other node in the graph. In simple terms, it tells us the longest path we have to take to reach any other node in the graph from the given node. It is a useful metric in various real-world applications like social network analysis, transportation network planning, and computer network topology analysis.

Real-world Examples and Scenarios

  1. In social network analysis, eccentricity can help us identify influential people with a large reach or those who are well-connected in the network.
  2. In transportation network planning, eccentricity can be useful in determining the efficiency of transportation hubs, like airports or train stations, and identifying areas that need better connectivity.
  3. In computer network topology analysis, eccentricity can help us understand the resilience of a network and identify critical nodes that, if removed, would significantly disrupt the network.

Real-world Scenario: Social Network Analysis

Let's consider a social network where nodes represent people, and edges represent friendships between them. We want to determine the eccentricity of each person in the network to identify the most well-connected individuals.

Problem Statement

Given an undirected graph G(V, E) where V is the set of nodes representing people and E is the set of edges representing friendships, determine the eccentricity of each node in the graph.

Tying Problem Statement with Real-world Scenario

This problem statement is directly related to our real-world scenario of social network analysis, as it aims to determine the eccentricity of each person in the network.

Solution

We can solve this problem using the Breadth-First Search (BFS) algorithm. BFS allows us to find the shortest paths between a starting node and all other nodes in the graph. By running BFS for each node in the graph and finding the maximum distance to any other node, we can determine the eccentricity of each node.

Step-by-step Solution with Real-world Scenario

  1. For each person (node) in the social network: a. Run BFS from that person (starting node) to find the shortest paths to all other nodes. b. Find the maximum distance to any other node. c. The maximum distance found in step (b) is the eccentricity of the person (node).

Actual Code Solution

Here's a Python implementation of the solution using BFS:

from collections import deque

# Function to perform BFS traversal from the starting node and return the maximum distance
def bfs(graph, start):
    # Initialize the visited nodes and the distances
    visited = set()
    distances = {start: 0}
    queue = deque([start])

    # Perform BFS traversal
    while queue:
        current_node = queue.popleft()
        visited.add(current_node)

        for neighbor in graph[current_node]:
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append(neighbor)
                distances[neighbor] = distances[current_node] + 1

    # Return the maximum distance
    return max(distances.values())

# Function to find the eccentricity of each node in the graph
def find_eccentricity(graph):
    eccentricities = {}
    for node in graph:
        eccentricities[node] = bfs(graph, node)
    return eccentricities

Calling the Functions with Actual Values

Let's create a sample social network graph and find the eccentricity of each person:

# Sample social network graph
graph = {
    'A': ['B', 'C'],
    'B': ['A', 'C', 'D'],
    'C': ['A', 'B', 'D'],
    'D': ['B', 'C', 'E'],
    'E': ['D']
}

# Find the eccentricity of each person in the social network
eccentricities = find_eccentricity(graph)
print(eccentricities)

Output:

{'A': 2, 'B': 2, 'C': 2, 'D': 2, 'E': 3}

Explaining the Code Solution with Intuitions and Analogies

The bfs function takes a graph and a starting node as input and performs a Breadth-First Search traversal from the starting node. It keeps track of the visited nodes and the distances from the starting node to every other node in the graph. Once the traversal is complete, it returns the maximum distance.

The find_eccentricity function iterates through each node in the graph and calls the bfs function to find the maximum distance from that node to any other node. It stores these distances in a dictionary, with the nodes as the keys and their eccentricities as the values.

Solving Other Similar Real-world Problems

The same solution can be applied to other real-world problems where we need to find the eccentricity of nodes in a graph, such as transportation network planning and computer network topology analysis. The only modification required would be to represent the problem as an undirected graph and tailor the input graph as per the specific domain.