简体   繁体   English

Java中的数据结构无向图

[英]Data Structure Undirected Graph in Java

Assume an undirected graph where edges have int values. 假设其中边具有int值的无向图。

Example Graph : 图表示例

A---B---C
    |
    D

Example Edge Values : 边值示例

A,B : 1
B,C : 2
B,D : 2

I am writing a program that does the following: Given a node, return all edges in which the node is involved together with the associated value. 我正在编写一个执行以下操作的程序:给定一个节点,返回该节点所涉及的所有边以及关联的值。 For example: IN: B → OUT: (C,2), (D,2). 例如:IN:B→OUT:(C,2),(D,2)。 Or IN: C → OUT: (B,2). 或IN:C→OUT:(B,2)。 I am looking for an efficient data structure to do that. 我正在寻找一个有效的数据结构来做到这一点。 The problem is not derived from graph theory, I just used that to explain it. 这个问题并非来自图论,我只是用它来解释它。 I am not interested in shortest paths etc., just this kind of data store. 我对最短路径等不感兴趣,仅对这种数据存储感兴趣。

The only thing I could quickly come up with: 我唯一能很快想到的是:

HashMap<String,Tuple>

where 哪里

class Tuple{
    String node;
    int value;
}

I would then put each edge in this set twice, for example (pseudocode): 然后,我将每个边缘放入该集合两次,例如(伪代码):

hm.add(A, new Tuple(B,1))
hm.add(B, new Tuple(A,1))

Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点?

How about Guava's ValueGraph ? 番石榴的ValueGraph怎么ValueGraph

https://github.com/google/guava/wiki/GraphsExplained https://github.com/google/guava/wiki/GraphsExplained

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM