star_topology

This commit is contained in:
test 2022-06-16 21:44:51 +02:00
parent c8db7c9940
commit 81ef4b724d

View File

@ -1,6 +1,8 @@
import random
import networkx as nx
from statistics import mean
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib import animation
@ -79,6 +81,30 @@ def bus_network(n=30) -> Graph:
return network
def rank_avg(edges, digits=2):
ranks = {}
for e in edges:
ranks[e.node_a] = ranks.get(e.node_a, 0) + 1
ranks[e.node_b] = ranks.get(e.node_b, 0) + 1
return round(mean(ranks.values()), digits)
def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float]:
node_count = cluster_count + cluster_count * starsize + 1
nodes = [Node() for _ in range(node_count)]
edges = []
for x in range(cluster_count):
center_node = x * starsize + x
edges += [Edge(nodes[center_node], nodes[i], 1.0) for i in range(center_node + 1, center_node + starsize + 1)]
edges.append(Edge(nodes[-1], nodes[center_node], 1.0))
network = Graph()
network.add_edges(edges)
return network, rank_avg(edges)
def main():
network = Graph()
nodes = [Node(True), Node(), Node(), Node(True), Node()]
@ -97,6 +123,9 @@ def main():
bus = bus_network()
do_graph_animation('bus.gif', bus, 5)
star, star_avg_rank = star_network()
do_graph_animation('star.gif', star, 5)
if __name__ == "__main__":
main()