java 有向加权图的邻接表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1990215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adjacency list of a directed weighted graph
提问by denchr
I am using adjacency lists to represent a directed weighted graph and based on the example code provided by thisSO question, I have created the following:
我正在使用邻接列表来表示有向加权图,并基于此SO 问题提供的示例代码,我创建了以下内容:
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
public class _Graph {
private Map<String, LinkedHashSet<HashMap<String, Integer>>> map = new HashMap<String, LinkedHashSet<HashMap<String, Integer>>>();
public void addEdge(String node1, String node2, int dist) {
LinkedHashSet<HashMap<String, Integer>> adjacent = map.get(node1);
HashMap<String, Integer> innerMap = new HashMap<String, Integer>();
if(adjacent==null) {
adjacent = new LinkedHashSet<HashMap<String, Integer>>();
map.put(node1, adjacent);
}
innerMap.put(node2, dist);
adjacent.add(innerMap);
}
public boolean isConnected(String node1, String node2) {
Set<HashMap<String, Integer>> adjacent = map.get(node1);
if(adjacent==null) {
return false;
}
return adjacent.contains(node2);
}
public LinkedList<HashMap<String, Integer>> adjacentNodes(String node) {
LinkedHashSet<HashMap<String, Integer>> adjacent = map.get(node);
if(adjacent==null) {
return new LinkedList<HashMap<String, Integer>>();
}
return new LinkedList<HashMap<String, Integer>>(adjacent);
}
}
I am having trouble making the isConnectedmethod to work properly. Am I using a wrong data structure to represent the graph here (Map<String, LinkedHashSet<HashMap<String, Integer>>>)? The hashmap will hold the name of the connected node and the distance to it:
我无法使该isConnected方法正常工作。我是否使用错误的数据结构来表示此处的图形 ( Map<String, LinkedHashSet<HashMap<String, Integer>>>)?hashmap 将保存连接节点的名称和到它的距离:
Map<startNode, LinkedHashSet<HashMap<endNode, distanceToEndNode>>>
- Basically how can I check if a node
belongs to the adjacency list of a
given base node? I think the problem
is reduced to iterating properly
over the
adjacentSet<HashMap<String, Integer>>structure, or is my reasoning wrong? - In my second method
adjacentNodes(String node)I am returning a linked list containing the maps (in a set structure) of the connected nodes and their distances. How could I efficiently iterate to see all connections of a any given node?
- 基本上如何检查节点是否属于给定基节点的邻接列表?我认为问题归结为在
adjacentSet<HashMap<String, Integer>>结构上正确迭代,还是我的推理错误? - 在我的第二种方法中,
adjacentNodes(String node)我返回一个链表,其中包含连接节点的地图(在一个集合结构中)及其距离。我如何有效地迭代以查看任何给定节点的所有连接?
回答by Keith Randall
I think LinkedHashSetis not needed here, you can represent the graph with just a Map<String, Map<String, Integer>>.
我认为LinkedHashSet这里不需要,您可以只用一个Map<String, Map<String, Integer>>.
isConnectedis basically what you already have:
isConnected基本上是你已经拥有的:
public boolean isConnected(String node1, String node2) {
Map<String, Integer> adjacent = map.get(node1);
if(adjacent==null) {
return false;
}
return adjacent.containsKey(node2);
}
adjacentNodesjust needs to pull out the entries in the hash set for the source node
adjacentNodes只需要为源节点提取哈希集中的条目
public Collection<Map.Entry<String, Integer>> adjacentNodes(String node) {
Map<String, Integer> adjacent = map.get(node);
if(adjacent==null) {
return new ArrayList<Map.Entry<String, Integer>>();
}
return adjacent.entrySet();
}

