Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在 Rust 中创建一个带有字符串节点的未加权、无向图。我一直在尝试使用petgraph和上from_edges提供的构造方法UnGraph,但我无法这样做,因为&str它不是有效的 NodeIndex。有没有更简单的方法来做到这一点?
petgraph
from_edges
UnGraph
&str
您可以使用 a HashMapin ,其中键是节点,值是Vec连接到键的节点的 a。为方便起见String,用作节点的主要类型。另请注意,无向图仅意味着您需要将两个节点都添加到相邻列表中。在示例时尚中:
HashMap
Vec
String
{ "start": ["a", "b"], "a" : ["b", "d", "end"], "b": ["a", "end"], "d" : [], "end": [] }
type Graph = HashMap<String, Vec<String>>; impl Graph { ... }