Python 实现简单的超图

2024-06-12 19:28
文章标签 python 简单 实现 超图

本文主要是介绍Python 实现简单的超图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1. 超图
  • 2. 代码
  • 3. 参考


1. 超图

在数学中,超图(Hypergraph)是一种广义上的图,是有限集合中最一般的离散结构,在信息科学、生命科学等领域有着广泛的应用。
边(edge)顶点(vertex 或 node) 组成。
超图超边(hyperedge)顶点(vertex) 组成。

类型元素
顶点 + 边
超图顶点 + 超边

普通图,一条边只能连接两个顶点,而超图的一条边可以连接任意数量的顶点。
记超图为 G G G,顶点集合为 V \mathcal{V} V,超边集合为 E \mathcal{E} E,则有
G = ( V , E ) G = (\mathcal{V}, \mathcal{E}) G=(V,E)
如果是加权超图,则:
G = ( V , E , W ) G = (\mathcal{V}, \mathcal{E}, W) G=(V,E,W)
其中 W W W 表示权重。

对于顶点集合 V \mathcal{V} V 中的任意顶点 v v v,可以表示为 v ∈ V v \in \mathcal{V} vV
对于超边集合 E \mathcal{E} E 中的任意超边 e e e,可以表示为 e ∈ E e \in \mathcal{E} eE,且 e ⊂ V e \subset V eV

2. 代码

首先定义顶点类:

class Vertex:def __init__(self, name: str = None, weight: float = None):self.name = nameself.weight = weight

接着定义超边类:

from typing import List
import Vertexclass Edge:def __init__(self, name: str = None, weight: float = None, vertices: List[Vertex] = None):self.name = nameself.weight = weightself.vertices = vertices

最后由定义可得超图类:

from typing import List
import numpy as npimport Edge
import Vertexclass Hypergraph(object):def __init__(self, vertices: List[Vertex] = None, edges: List[Edge] = None):self.vertices = verticesself.edges = edgesif vertices is None or edges is None:self.num_vertices = 0self.num_edges = 0self.incident_matrix = Noneself.vertices_degree = Noneself.edges_degree = Noneself.vertex_degree_diagonal_matrix = Noneself.edge_degree_diagonal_matrix = Noneelse:self.num_vertices = len(vertices)self.num_edges = len(edges)self.incident_matrix = self.calculate_incident_matrix()self.vertices_degree = self.calculate_vertex_degree()self.edges_degree = self.calculate_edge_degree()self.vertex_degree_diagonal_matrix = self.calculate_diagonal_matrix_of_vertex_degree()self.edge_degree_diagonal_matrix = self.calculate_diagonal_matrix_of_edge_degree()def init_hypergraph_from_files(self, dataset_dir: str):self.vertices, self.edges = hypergraph_construction(dataset_dir)self.num_vertices = len(self.vertices)self.num_edges = len(self.edges)self.incident_matrix = self.calculate_incident_matrix()self.vertices_degree = self.calculate_vertex_degree()self.edges_degree = self.calculate_edge_degree()self.vertex_degree_diagonal_matrix = self.calculate_diagonal_matrix_of_vertex_degree()self.edge_degree_diagonal_matrix = self.calculate_diagonal_matrix_of_edge_degree()def calculate_incident_matrix(self):"""Calculate the incident matrix of the hypergraph.:return: The incident matrix of the hypergraph."""incident_matrix = np.zeros(shape=(self.num_vertices, self.num_edges), dtype=int)for i in range(self.num_vertices):vertex = self.vertices[i]for j in range(self.num_edges):edge = self.edges[j]if vertex in edge.vertices:incident_matrix[i, j] = 1return incident_matrixdef calculate_vertex_degree(self):"""Calculate the degree of vertices in the hypergraph.:return: The degree of vertices in the hypergraph."""edge_weights = np.zeros(shape=(self.num_edges,), dtype=np.float64)for i in range(self.num_edges):edge = self.edges[i]edge_weights[i] = edge.weightedge_weights = edge_weights.reshape(-1, 1)vertex_degree_array = np.dot(self.incident_matrix, edge_weights)return vertex_degree_array.reshape(vertex_degree_array.size, )def calculate_edge_degree(self):"""Calculate the degree of edges in the hypergraph.:return: The degree of edges in the hypergraph."""edges_degree = self.incident_matrix.sum(axis=0)return edges_degreedef calculate_diagonal_matrix_of_vertex_degree(self):"""Create a diagonal matrix with the degrees of vertex as the diagonal elements.:return: The diagonal matrix."""return np.diag(self.vertices_degree)def calculate_diagonal_matrix_of_edge_degree(self):"""Create a diagonal matrix with the degrees of edge as the diagonal elements.:return: The diagonal matrix."""return np.diag(self.edges_degree)

3. 参考

  1. 集智百科

这篇关于Python 实现简单的超图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055124

相关文章

浅析Python中的绝对导入与相对导入

《浅析Python中的绝对导入与相对导入》这篇文章主要为大家详细介绍了Python中的绝对导入与相对导入的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1 Imports快速介绍2 import语句的语法2.1 基本使用2.2 导入声明的样式3 绝对import和相对i

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

grom设置全局日志实现执行并打印sql语句

《grom设置全局日志实现执行并打印sql语句》本文主要介绍了grom设置全局日志实现执行并打印sql语句,包括设置日志级别、实现自定义Logger接口以及如何使用GORM的默认logger,通过这些... 目录gorm中的自定义日志gorm中日志的其他操作日志级别Debug自定义 Loggergorm中的

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

springMVC返回Http响应的实现

《springMVC返回Http响应的实现》本文主要介绍了在SpringBoot中使用@Controller、@ResponseBody和@RestController注解进行HTTP响应返回的方法,... 目录一、返回页面二、@Controller和@ResponseBody与RestController

nginx中重定向的实现

《nginx中重定向的实现》本文主要介绍了Nginx中location匹配和rewrite重定向的规则与应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下... 目录一、location1、 location匹配2、 location匹配的分类2.1 精确匹配2