当前位置: 首页> 文旅> 旅游 > 构建有向图的函数

构建有向图的函数

时间:2025/8/27 12:52:03来源:https://blog.csdn.net/xixixixixixixi21/article/details/140595749 浏览次数:0次

构建有向图的函数,同时图结构的点和边上都支持添加属性

在Java中,你可以使用HashMap作为图节点的属性容器,并使用邻接表(Adjacency List)存储图。这里是一个简单的例子:

Java
import java.util.*;class GraphNode {int id;Map<String, Object> attributes;GraphNode(int id) {this.id = id;this.attributes = new HashMap<>();}// 添加属性方法void addAttribute(String key, Object value) {attributes.put(key, value);}
}class Edge {int source;int destination;Map<String, Object> edgeAttributes;Edge(int source, int destination) {this.source = source;this.destination = destination;this.edgeAttributes = new HashMap<>();}// 添加属性方法void addEdgeAttribute(String key, Object value) {edgeAttributes.put(key, value);}
}class DirectedGraph {private List<GraphNode> nodes;private Map<Integer, List<Edge>> edges;DirectedGraph() {this.nodes = new ArrayList<>();this.edges = new HashMap<>();}// 添加节点和边的示例void addNode(int id, String attrKey, Object attrValue) {GraphNode node = new GraphNode(id);node.addAttribute(attrKey, attrValue);nodes.add(node);}void.addEdge(int srcId, int dstId, String edgeAttrKey, Object edgeAttrValue) {if (!edges.containsKey(srcId)) {edges.put(srcId, new ArrayList<>());}Edge edge = new Edge(srcId, dstId);edge.addEdgeAttribute(edgeAttrKey, edgeAttrValue);edges.get(srcId).add(edge);}
}

在Python中,我们可以使用字典(dict)来表示节点和边的属性。这里是相应的实现:

Python
class Node:def __init__(self, id):self.id = idself.attributes = {}# 添加属性方法def add_attribute(self, key, value):self.attributes[key] = valueclass Edge:def __init__(self, source, destination):self.source = sourceself.destination = destinationself.edge_attributes = {}# 添加属性方法def add_edge_attribute(self, key, value):self.edge_attributes[key] = valueclass Graph:def __init__(self):self.nodes = []self.edges = {}# 添加节点和边的示例def add_node(self, id, attr_key, attr_value):node = Node(id)node.add_attribute(attr_key, attr_value)self.nodes.append(node)def add_edge(self, src_id, dst_id, edge_attr_key, edge_attr_value):if src_id not in self.edges:self.edges[src_id] = []edge = Edge(src_id, dst_id)edge.add_edge_attribute(edge_attr_key, edge_attr_value)self.edges[src_id].append(edge)# 使用示例
graph = Graph()
graph.add_node(1, 'attr', 'value')
graph.add_edge(1, 2, 'edge_attr', 'value')C语言中,由于原生数据结构没有直接对应于PythonJava的灵活性,可以使用结构体和链表来实现。这里给出一个简化的版本:C
#include <stdio.h>
#include <stdlib.h>typedef struct Node {int id;char *attributes;  // 如果需要保存键值对,可以考虑使用struct map或其他第三方库
} Node;typedef struct Edge {int source;int destination;char *edge_attributes;  // 同上
} Edge;typedef struct Graph {Node **nodes;int *edges;size_t num_nodes;size_t num_edges;
} Graph;// 添加节点、边等操作...
void add_node(Graph *g, int id, const char *attr_key, const char *attr_value) {// 实现节点创建和属性添加
}void add_edge(Graph *g, int src_id, int dst_id, const char *edge_attr_key, const char *edge_attr_value) {// 实现边的创建和属性添加
}int main() {Graph graph;// 初始化并添加节点和边...return 0;
}
关键字:构建有向图的函数

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: