替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
Alternate(s) to making adjaceny list in python with dictionary for graph problems? (like vector<vector<int>> in c++)
在python中,我注意到人们使用defaultdict(list)
或类似性质的东西制作图表。 你如何在python中编写list<int> adj[n]
或vector<vector<int>> adj(n)
?
使用基本上unordered_maps
的字典不会使大型图形的运行时变慢吗?
使用 OOPs 的方式! 取自图形及其表示形式。感谢@DarrylG提供它!
# A class to represent the adjacency list of the node
class AdjNode:
def __init__(self, data):
self.vertex = data
self.next = None
# A class to represent a graph. A graph
# is the list of the adjacency lists.
# Size of the array will be the no. of the
# vertices "V"
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [None] * self.V
# Function to add an edge in an undirected graph
def add_edge(self, src, dest):
# Adding the node to the source node
node = AdjNode(dest)
node.next = self.graph[src]
self.graph[src] = node
# Adding the source node to the destination as
# it is the undirected graph
node = AdjNode(src)
node.next = self.graph[dest]
self.graph[dest] = node
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- std::vector的包装器,使数组的结构看起来像结构的数组
- 呼叫运营商<<临时
- 为什么(-1)%vector::size()总是返回0
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在C++中将类(带有Vector成员)保存为二进制文件
- 编译器如何区分std::vector的构造函数
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- C++重载<<具有typedef'd std::vector
- std::映射<>或std::vector<>在处理大型标志集时