Skip to content

Commit 1d211ed

Browse files
committed
Add graph centrality algorithms to compute central and median nodes in a graph
- Implement `floyd_warshall_algorithm` to compute all-pairs shortest paths using the Floyd-Warshall algorithm. - Implement `find_central_and_median_node` to calculate eccentricity and harmonic closeness centrality for each node. - Add helper functions: - `initialize_distance_matrix` for initializing the distance matrix and validating edge weights. - `calculate_node_centrality` for computing eccentricity and closeness of a single node. - Include comprehensive module-level docstrings explaining: - The mathematical problem and its significance. - Descriptions of the algorithms used. - Pseudo-code for the Floyd-Warshall algorithm. - References to relevant Wikipedia articles. - Add detailed type hints for all functions and variables to enhance code clarity and support static type checking. - Refactor code to improve readability and maintainability: - Use descriptive variable names. - Introduce intermediate variables for complex expressions. - Add comments explaining key steps and computations. - Ensure compliance with PEP 8 standards: - Adjust line lengths to not exceed 88 characters. - Reformat long strings and comments. - Fix issues identified by code linters like `ruff`. - Handle exceptions properly: - Assign exception messages to variables before raising, adhering to best practices. - Include doctests for various graph scenarios to verify correctness: - Single-node graphs. - Two-node graphs with positive weights. - Fully connected graphs. - Directed acyclic graphs (DAGs). - Disconnected graphs. - Graphs with zero or negative edge weights (raising exceptions). - Cyclic graphs. - Sparse graphs.
1 parent 2d671df commit 1d211ed

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

graphs/dijkstra_algorithm.py

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44

55
import math
66
import sys
7+
from dataclasses import dataclass, field
8+
9+
10+
@dataclass
11+
class Edge:
12+
destination: int
13+
weight: float
14+
15+
16+
@dataclass
17+
class Node:
18+
node_id: int
19+
distance: float = float("inf") # Initialize with "infinity" distance
20+
parent: int | None = None
21+
edges: list[Edge] = field(default_factory=list)
22+
723

824
# For storing the vertex set to retrieve node with the lowest distance
925

0 commit comments

Comments
 (0)