首页 > 编程语言 >python-graph-study2024-7

python-graph-study2024-7

时间:2024-07-07 09:19:07浏览次数:1  
标签:node study2024 dictionary python graph pos nx edge

 

1. 先打基础

1 基础学习笔记
 

Graph Creation

NetworkX graph objects can be created in one of three ways:

  • Graph generators—standard algorithms to create network topologies.

  • Importing data from preexisting (usually file) sources.

  • Adding edges and nodes explicitly.

 

Edge attributes can be anything:

import math
G.add_edge('y', 'x', function=math.cos)
G.add_node(math.cos)  # any hashable can be a node


已有生成算法生成专门的图

Graph generators such as binomial_graph() and erdos_renyi_graph() are provided in the graph generators subpackage.

 

从文件读入图数据-写图数据到外部文件

For importing network data from formats such as GML, GraphML, edge list text files see the reading and writing graphs subpackage.

 
   

Graph Reporting 和 Algorithms

   

Drawing

1 import matplotlib.pyplot as plt
2 G = nx.cubical_graph()
3 subax1 = plt.subplot(121)
4 nx.draw(G)   # default spring_layout
5 subax2 = plt.subplot(122)
6 nx.draw(G, pos=nx.circular_layout(G), node_color='r', edge_color='b')

 

 

 

 

   

Data Structure

NetworkX uses a “dictionary of dictionaries of dictionaries” as the basic network data structure. This allows fast lookup with reasonable storage for large sparse networks. The keys are nodes so G[u] returns an adjacency dictionary keyed by neighbor to the edge attribute dictionary. A view of the adjacency data structure is provided by the dict-like object G.adj as e.g. for node, nbrsdict in G.adj.items():. The expression G[u][v] returns the edge attribute dictionary itself. A dictionary of lists would have also been possible, but not allow fast edge detection nor convenient storage of edge data.

 

Advantages of dict-of-dicts-of-dicts data structure:

  • Find edges and remove edges with two dictionary look-ups.

  • Prefer to “lists” because of fast lookup with sparse storage.

  • Prefer to “sets” since data can be attached to edge.

    • G[u][v]                   returns the edge attribute dictionary.

    • n in G                 tests if node n is in graph G.

    • for n in G:    iterates through the graph.

    • for nbr in G[n]:   iterates through neighbors.

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
 

ego_graph

 返回给定半径内以节点n为中心的邻居的诱导子图。

   
   

 

2.关于布局

 

1.  记录已有的布局数据——重复绘图时使用上次的布局

举例:

draw_planar——用平面布局画一个平面网络图G。

draw_planar(G**kwargs)

This is a convenience function equivalent to:

nx.draw(G, pos=nx.planar_layout(G), **kwargs)


每次调用该函数时都会计算布局。对于重复绘制,直接调用planar_layout并重用结果会更有效:

1 G = nx.path_graph(5)
2 pos = nx.planar_layout(G)
3 nx.draw(G, pos=pos)  # Draw the original graph
4 # Draw a subgraph, reusing the same node positions
5 nx.draw(G.subgraph([0, 1, 2]), pos=pos, node_color="red")

使用pos记录布局数据后可以复用位置信息。

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

 

标签:node,study2024,dictionary,python,graph,pos,nx,edge
From: https://www.cnblogs.com/yuweng1689/p/18288210

相关文章

  • python 识别图片验证码/滑块验证码准确率极高的 ddddocr 库
    前言验证码的种类有很多,它是常用的一种反爬手段,包括:图片验证码,滑块验证码,等一些常见的验证码场景。识别验证码的python库有很多,用起来也并不简单,这里推荐一个简单实用的识别验证码的库ddddocr(带带弟弟ocr)库.环境准备python版本要求小于等于python3.9版本pip安装pipin......
  • python简单操作
    一.输出九九乘法表row=1whilerow<=9:  col=1  whilecol<=row:    print("%d*%d=%d"%(row,col,row*col),end="\t")    #\t在控制台输出一个制表符,协助文本垂直方向上对齐    #\n换行符\"可输出双引号 ......
  • Shopee虾皮api python获取虾皮购物平台的商品数据信息 数据采集
    虾皮购物(英语:Shopee)是一个电商平台,总公司设在新加坡,归属于SeaGroup(之前称之为Garena),该企业于2009年由李小冬(ForrestLi)创办。虾皮购物于2015年初次在新加坡推出,现阶段已拓展到马来西亚、泰国、印度尼西亚、越南和菲律宾。虾皮购物为全球华人地区的客户提供线上购物和销售......
  • Appium+python自动化(四十二)- 寿终正寝完结篇 - 结尾有惊喜,过时不候(超详解)
    1.简介 按照上一篇的计划,今天给小伙伴们分享执行测试用例,生成测试报告,以及自动化平台。今天这篇分享讲解完。Appium自动化测试框架就要告一段落了。2.执行测试用例&报告生成 测试报告,宏哥已经讲解了testng、HTMLTestRunner、allure等等,今天就在讲解一个新的测试报告BSTest......
  • python socket模块实现上传文件到服务器
    socket模块文件上传案例catserver.py#-*-coding:UTF-8-*-importsocketsock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.bind(('0.0.0.0',8001))#127.0.0.1或查看自己局域网本地IP地址sock.listen(1)conn,addr=sock.accept()#接收......
  • python速通(函数)
    (网页的生成)牛客网的某个网页基本已经写好了,最后一步为了适应手机的尺寸,需要将高度增加一倍。为了适用于多个网页,牛牛希望你能将这个功能定义为一个函数,函数输入网页高度h,输出增加后的结果。defiheight(h):returnh*2h1,h2=map(int,input().split())print(iheight......
  • python随笔day02
    1.arg元组类型和**kwargs字典类型#元组参数:元组类型数据,对传递参数有顺序要求deftest(*args):print(f"args[0]={args[0]},args[1]={args[1]},args[2]={args[2]}")test(1,2,3)#字典参数:字典类型数据,对传递参数没有顺序要求,格式要求key=value值deftest2(**kwargs):......
  • Python学习
    目录7-1jmu-python-判断闰年7-2jmu-python-素数7-3jmu-python-找字符7-4计算表达式(*,//,%)7-5客户评级7-6运输打折问题7-7水仙花数7-8生成输入数的乘方表7-9输出字母在字符串中位置索引7-10通过两个列表构建字典7-11jmu-python-重复元素判定7-12求集合......
  • python练习01
    练习if语句age=int(input('请输入年龄'))ifage<10:print('还是个小屁孩')elif10<age<20:print('青春期叛逆的小孩')elif30<age<40:print('老大不小了,赶紧结婚,小屁孩')elif40<age<50:print('家里有个不听话的小屁孩')elif50<......
  • python数据容器(二)元组
    1.数据容器:tuple(元组)(1)定义t1=(1,"Hello",True)t2=()t3=tuple()print(f"t1的类型是:{type(t1)},内容是:{t1}")print(f"t2的类型是:{type(t2)},内容是:{t2}")print(f"t3的类型是:{type(t3)},内容是:{t3}")运行结果:(2)定义单个元素的元素t1=("hel......