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 random
import networkx as nx from statistics import mean
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import networkx as nx
from matplotlib import animation from matplotlib import animation
@ -73,12 +75,36 @@ def do_graph_animation(output_file_name: str, in_graph: Graph, frame_count: int)
def bus_network(n=30) -> Graph: def bus_network(n=30) -> Graph:
network = Graph() network = Graph()
nodes = [Node() for _ in range(n)] nodes = [Node() for _ in range(n)]
edges = [Edge(nodes[i], nodes[i+1], 1.0) for i in range(n-1)] edges = [Edge(nodes[i], nodes[i + 1], 1.0) for i in range(n - 1)]
network.add_edges(edges) network.add_edges(edges)
return network 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(): def main():
network = Graph() network = Graph()
nodes = [Node(True), Node(), Node(), Node(True), Node()] nodes = [Node(True), Node(), Node(), Node(True), Node()]
@ -97,6 +123,9 @@ def main():
bus = bus_network() bus = bus_network()
do_graph_animation('bus.gif', bus, 5) do_graph_animation('bus.gif', bus, 5)
star, star_avg_rank = star_network()
do_graph_animation('star.gif', star, 5)
if __name__ == "__main__": if __name__ == "__main__":
main() main()