简体   繁体   中英

Data Structure Undirected Graph in Java

Assume an undirected graph where edges have int values.

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). Or 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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