This repository contains a Python-based program for simulating and managing a network of routers. It uses Dijkstra's algorithm to find the shortest path between routers, create routing tables, and visualize the network using NetworkX and Matplotlib. The system is designed for educational purposes to understand routing and shortest path algorithms in computer networks.
-
Graph Representation:
- Models routers and connections as a graph where nodes represent routers and edges represent connections.
-
Shortest Path Calculation:
- Uses Dijkstra's algorithm to compute the shortest path between routers.
-
Routing Table Generation:
- Automatically generates routing tables for each router.
-
Router Removal:
- Dynamically removes routers from the network and updates the routing tables.
-
Network Visualization:
- Creates a graphical representation of the network with routers and connections.
-
Flexible Configuration:
- Easily configurable to add new routers, connections, or modify existing ones.
- Python 3.x
- Libraries:
networkx
matplotlib
pandas
Install the required libraries using:
pip install networkx matplotlib pandas
-
Router
Class:- Handles operations related to a single router, including connections and edge costs.
- Implements Dijkstra's algorithm to find the shortest path.
- Maintains and prints routing tables.
- Visualizes the graph structure using NetworkX.
-
Graph
Class:- Manages the entire network of routers.
- Supports adding routers and edges (connections).
-
main
Function:- Demonstrates usage by creating a sample network, calculating shortest paths, and visualizing the graph.
-
Initialize the Graph:
graph = Graph()
-
Add Routers and Connections:
graph.add_edge("a", "b", 7) graph.add_edge("a", "c", 9) graph.add_edge("b", "c", 10)
-
Create Routers:
router = Router("a", graph) router_two = Router("b", graph)
-
Calculate Shortest Path:
router.get_path("f")
-
Print Routing Table:
router.print_routing_table()
-
Remove Router:
router_two.remove_router("c")
-
Visualize Network:
G = nx.Graph() router_two.map_out_graph(G)
Input:
router.get_path("f")
Output:
Start: a
End: f
Path: a->c->f
Cost: 11
Input:
router.print_routing_table()
Output:
from to cost path
0 a b 7 a->b
1 a c 9 a->c
2 a d 20 a->c->d
3 a e 26 a->c->d->e
4 a f 11 a->c->f
- Displays the network graph with nodes (routers) and edges (connections).
- Labels nodes and edges with names and weights, respectively.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or fix.
- Commit your changes and push them to your fork.
- Open a pull request describing your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
Enhance your understanding of routing algorithms and graph visualization with this Router Graph and Shortest Path Finder!