## 送书读书会丨网络拓扑的处理

12.4.1 描述一个网络拓扑

1、无向图

``graph site_a {     core1 -- access1;     core2 -- access2;     core1 -- core2;}``

12-7 无向图

2、有向图

``digraph site_b {     core1 [shape=box];     core2 [shape=box];     access1;     access2;     access1 -> core1 -> core2 -> access2;}``

12-8 有向图

3、多重图

``graph site_c {      core1 -- core2 [label="10GE*8",color="red",fontsize=9.0];      core1 -- core2 [label="100GE*2",color="blue",fontsize=9.0];}``

12-9 多重图

12.4.2 最短路径的计算

``\$ pip install networkxCollecting networkx  Using cached networkx-2.0.zipCollecting decorator>=4.1.0 (from networkx)  Using cached decorator-4.1.2-py2.py3-none-any.whlInstalling collected packages: decorator, networkx  Running setup.py install for networkx ... doneSuccessfully installed decorator-4.1.2 networkx-2.0``

1、用networkx描述拓扑

``#!/usr/bin/env python#coding:utf-8import networkx as nxnodes = ["BJ", "SH", "GZ", "HZ","NJ","WH","XA"]G = nx.Graph()for node in nodes:    G.add_node(node)edges = [("BJ", "SH"),         ("BJ", "GZ"),         ("SH", "GZ"),         ("HZ", "SH"),         ("HZ", "GZ"),         ("NJ", "SH"),         ("NJ", "BJ"),         ("WH", "SH"),         ("WH", "BJ"),         ("XA", "GZ"),         ("XA", "BJ"),]G.add_edges_from(edges)``

12-10 拓扑图示意图

4、最简单的路径计算

``>>> nx.shortest_path(G,"WH","HZ")['WH', 'SH', 'HZ']``

5、给链路添加cost

``#!/usr/bin/env python#coding:utf-8import networkx as nxnodes = ["BJ", "SH", "GZ", "HZ","NJ","WH","XA"]G = nx.Graph()for node in nodes:    G.add_node(node)edges = [("BJ", "SH",1200),         ("BJ", "GZ",2500),         ("SH", "GZ",1300),         ("HZ", "SH",280),         ("HZ", "GZ",1000),         ("NJ", "SH",300),         ("NJ", "BJ",900),         ("WH", "SH",800),         ("WH", "BJ",850),         ("XA", "GZ",2600),         ("XA", "BJ",2000),]G.add_weighted_edges_from(edges)``

``>>> nx.shortest_path(G,"XA","HZ")['XA', 'GZ', 'HZ']``

``>>> nx.shortest_path(G,"XA","HZ",weight="weight")['XA', 'BJ', 'SH', 'HZ']``

``>>> G["HZ"]["SH"]["weight"]=1000000>>> nx.shortest_path(G,"XA","HZ",weight="weight")['XA', 'GZ', 'HZ']``

6、等价路径的计算

``>>> list(nx.all_shortest_paths(G, "WH", "GZ",weight=None))[['WH', 'BJ', 'GZ'], ['WH', 'SH', 'GZ']]``

networkx中还提供了很多关于最短路径的方法，读者可以参考以下的链接：

https://networkx.github.io/documentation/stable/reference/algorithms/shortest_paths.html。

12.4.3 可用路径的计算

1、获得可用路径

12-11 三角形拓扑

``>>> for path in nx.all_simple_paths(G, "XA", "HZ"):...     print(path)...['XA', 'BJ', 'NJ', 'SH', 'GZ', 'HZ']['XA', 'BJ', 'NJ', 'SH', 'HZ']['XA', 'BJ', 'WH', 'SH', 'GZ', 'HZ']['XA', 'BJ', 'WH', 'SH', 'HZ']['XA', 'BJ', 'GZ', 'HZ']['XA', 'BJ', 'GZ', 'SH', 'HZ']['XA', 'BJ', 'SH', 'GZ', 'HZ']['XA', 'BJ', 'SH', 'HZ']['XA', 'GZ', 'HZ']['XA', 'GZ', 'BJ', 'NJ', 'SH', 'HZ']['XA', 'GZ', 'BJ', 'WH', 'SH', 'HZ']['XA', 'GZ', 'BJ', 'SH', 'HZ']['XA', 'GZ', 'SH', 'HZ']``

2、获得部分可用路径

``>>> for path in nx.all_simple_paths(G, "XA", "HZ", 3):...     print(path)...['XA', 'BJ', 'GZ', 'HZ']['XA', 'BJ', 'SH', 'HZ']['XA', 'GZ', 'HZ']['XA', 'GZ', 'SH', 'HZ']``

3、基于链路cost计算的可用路径

``>>> def get_path_weight(G, path):...     _weight = 0...     for edge in nx.utils.pairwise(path):...         _weight += G.edges[edge[0],edge[1]]["weight"]...     return _weight...>>> for path in  nx.shortest_simple_paths(G, "XA", "HZ",weight="weight"):...     print(path, get_path_weight(G, path))...['XA', 'BJ', 'SH', 'HZ'] 3480['XA', 'BJ', 'NJ', 'SH', 'HZ'] 3480['XA', 'GZ', 'HZ'] 3600['XA', 'BJ', 'WH', 'SH', 'HZ'] 3930['XA', 'GZ', 'SH', 'HZ'] 4180['XA', 'BJ', 'GZ', 'HZ'] 5500['XA', 'BJ', 'GZ', 'SH', 'HZ'] 6080['XA', 'GZ', 'BJ', 'SH', 'HZ'] 6580['XA', 'GZ', 'BJ', 'NJ', 'SH', 'HZ'] 6580['XA', 'GZ', 'BJ', 'WH', 'SH', 'HZ'] 7030``

``>>> from itertools import count>>> counter = count(1)>>> for c, path in zip(counter, nx.shortest_simple_paths(G, "XA", "HZ", weight="weight")):...     if c > 4:...         break...     print(path, get_path_weight(G, path))...['XA', 'BJ', 'SH', 'HZ'] 3480['XA', 'BJ', 'NJ', 'SH', 'HZ'] 3480['XA', 'GZ', 'HZ'] 3600['XA', 'BJ', 'WH', 'SH', 'HZ'] 3930``

1）网络运维自动化资深专家撰写，8位专家联袂推荐，网络工程师转型必备指南

2）以场景与实践驱动，涵盖NetDevOps理念、常用工具、编程基础、网络运维常用Python模块、网络设备的数据处理等

*本期嘉宾分享内容受众偏向于网络工程师、做网络开发和SDN开发的程序员。欢迎感兴趣的同学前来免费听讲。

0条评论