Java 根据相邻节点的坐标构建图形
您可以这样理解: nodeName nodeName's x-coord, nodeName's y-coord x-coord of an adjacent node, y-coord of that adjacent node 诺德奈 诺德南的x坐标,诺德南的y坐标 相邻节点的x坐标,该相邻节点的y坐标 …剩下的只是相邻节点的更多坐标。我试图找出如何将其存储为图形,以便检查路径是否合法。例如,可能nodeA nodeB nodeC是合法的,但nodeA nodeC nodeD不是Java 根据相邻节点的坐标构建图形,java,graph,Java,Graph,您可以这样理解: nodeName nodeName's x-coord, nodeName's y-coord x-coord of an adjacent node, y-coord of that adjacent node 诺德奈 诺德南的x坐标,诺德南的y坐标 相邻节点的x坐标,该相邻节点的y坐标 …剩下的只是相邻节点的更多坐标。我试图找出如何将其存储为图形,以便检查路径是否合法。例如,可能nodeA nodeB nodeC是合法的,但nodeA nodeC nodeD不是 所以,我的
所以,我的最后一个问题是:编写Graph类并通过读取这些数据来填充它的最佳方法是什么 我认为没有必要以某种方式存储节点来确定路径是否合法:您可以在读取下一个节点时检查其合法性。当且仅当下一个节点的坐标与前一个节点的坐标相差不超过1时,下一个节点才是合法的。我认为没有必要以某种方式存储节点以确定路径是否合法:您可以在读取下一个节点时检查其合法性。当且仅当下一个节点的坐标与前一个节点的坐标相差不超过1时,下一个节点才合法。您可以将文件拆分为多组行。每个组描述一个节点。然后解析所有组
Map<Node, List<Node>> neighbors;
Map<String, Node> nodeByCoords;
// Get node by it's coordinates. Create new node, if it doesn't exist.
Node getNode(String coords) {
String[] crds = coords.split(" ");
int x = Integer.parseInt(crds[0]);
int y = Integer.parseInt(crds[1]);
String key = x + " " + y;
if (!nodeByCoords.containsKey(key)) {
Node node = new Node();
node.setX(x);
node.setY(y);
nodeByCoords.put(key, node);
neighbords.put(node, new ArrayList<Node>());
}
return nodeByCoords.get(key);
}
// Create node (if not exists) and add neighbors.
void List<String> readNode(List<String> description) {
Node node = getNode(description.get(1));
node.setName(description.get(0));
for (int i = 2; i < description.size(); i++) {
Node neighbor = getNode(description.get(i));
neighbors.get(node).add(neighbor);
}
}
// Splits lines to groups. Each group describes particular node.
List<List<String>> splitLinesByGroups (String filename) {
BufferedReader reader = new BufferedReader(new FileReader(filename));
List<List<String>> groups = new ArrayList<List<String>>();
List<String> group = new ArrayList<String>();
while (reader.ready()) {
String line = reader.readLine();
if (Character.isLetter(line.charAt())) {
groups.add(group);
group = new ArrayList<String>();
}
group.add(line);
}
groups.add(group);
return groups;
}
// Read file, split it to groups and read nodes from groups.
void readGraph(String filename) {
List<List<String>> groups = splitLineByGroups(filename);
for (List<String> group: groups) {
readNode(group);
}
}
映射邻居;
地图节点库;
//通过节点的坐标获取节点。创建新节点(如果不存在)。
节点getNode(字符串坐标){
字符串[]crds=coords.split(“”);
intx=Integer.parseInt(crds[0]);
int y=Integer.parseInt(crds[1]);
字符串键=x+“”+y;
如果(!nodeByCoords.containsKey(键)){
节点=新节点();
node.setX(x);
node.setY(y);
nodeByCoords.put(键,节点);
put(node,newarraylist());
}
返回nodeByCoords.get(key);
}
//创建节点(如果不存在)并添加邻居。
无效列表读取节点(列表描述){
Node Node=getNode(description.get(1));
node.setName(description.get(0));
对于(int i=2;i
您可以将文件拆分为多行。每个组描述一个节点。然后解析所有组
Map<Node, List<Node>> neighbors;
Map<String, Node> nodeByCoords;
// Get node by it's coordinates. Create new node, if it doesn't exist.
Node getNode(String coords) {
String[] crds = coords.split(" ");
int x = Integer.parseInt(crds[0]);
int y = Integer.parseInt(crds[1]);
String key = x + " " + y;
if (!nodeByCoords.containsKey(key)) {
Node node = new Node();
node.setX(x);
node.setY(y);
nodeByCoords.put(key, node);
neighbords.put(node, new ArrayList<Node>());
}
return nodeByCoords.get(key);
}
// Create node (if not exists) and add neighbors.
void List<String> readNode(List<String> description) {
Node node = getNode(description.get(1));
node.setName(description.get(0));
for (int i = 2; i < description.size(); i++) {
Node neighbor = getNode(description.get(i));
neighbors.get(node).add(neighbor);
}
}
// Splits lines to groups. Each group describes particular node.
List<List<String>> splitLinesByGroups (String filename) {
BufferedReader reader = new BufferedReader(new FileReader(filename));
List<List<String>> groups = new ArrayList<List<String>>();
List<String> group = new ArrayList<String>();
while (reader.ready()) {
String line = reader.readLine();
if (Character.isLetter(line.charAt())) {
groups.add(group);
group = new ArrayList<String>();
}
group.add(line);
}
groups.add(group);
return groups;
}
// Read file, split it to groups and read nodes from groups.
void readGraph(String filename) {
List<List<String>> groups = splitLineByGroups(filename);
for (List<String> group: groups) {
readNode(group);
}
}
映射邻居;
地图节点库;
//通过节点的坐标获取节点。创建新节点(如果不存在)。
节点getNode(字符串坐标){
字符串[]crds=coords.split(“”);
intx=Integer.parseInt(crds[0]);
int y=Integer.parseInt(crds[1]);
字符串键=x+“”+y;
如果(!nodeByCoords.containsKey(键)){
节点=新节点();
node.setX(x);
node.setY(y);
nodeByCoords.put(键,节点);
put(node,newarraylist());
}
返回nodeByCoords.get(key);
}
//创建节点(如果不存在)并添加邻居。
无效列表读取节点(列表描述){
Node Node=getNode(description.get(1));
node.setName(description.get(0));
对于(int i=2;i < /代码> 您可能需要考虑使用< /p>
只需创建的实例并使用节点和边填充即可:
// Define your node, override 'equals' and 'hashCode'
public class Node {
public int x, y;
Node (int _x, int _y) {
x = _x;
y = _y;
}
@Override public boolean equals (Object other) {
if ( (other.x == x)
&& (other.y == y))
return true;
return false;
}
/* Override hashCode also */
}
// Later on, you just add edges and vertices to your graph
SimpleGraph<Node,Edge> sg;
sg.addEdge (...);
sg.addVertex (...);
//定义节点,覆盖'equals'和'hashCode'
公共类节点{
公共整数x,y;
节点(整数x,整数y){
x=x;
y=_y;
}
@重写公共布尔等于(对象其他){
如果((其他.x==x)
&&(其他.y==y))
返回true;
返回false;
}
/*也重写哈希代码*/
}
//稍后,只需将边和顶点添加到图形中
单纯形sg;
sg.附录(…);
sg.addVertex(…);
最后,您可以用来查找路径是否存在:
您可能需要考虑使用
只需创建的实例并使用节点和边填充即可:
// Define your node, override 'equals' and 'hashCode'
public class Node {
public int x, y;
Node (int _x, int _y) {
x = _x;
y = _y;
}
@Override public boolean equals (Object other) {
if ( (other.x == x)
&& (other.y == y))
return true;
return false;
}
/* Override hashCode also */
}
// Later on, you just add edges and vertices to your graph
SimpleGraph<Node,Edge> sg;
sg.addEdge (...);
sg.addVertex (...);
//定义节点,覆盖'equals'和'hashCode'
公共类节点{
公共整数x,y;
节点(整数x,整数y){
x=x;
y=_y;
}
@重写公共布尔等于(对象其他){
如果((其他.x==x)
&&(其他.y==y))
返回true;
返回false;
}
/*也重写哈希代码*/
}
//稍后,只需将边和顶点添加到图形中
单纯形sg;
sg.附录(…);
sg.addVertex(…);
最后,您可以使用查找路径是否存在:路径“合法”是什么意思?@Alexeybrezkin非法路径是指通过在data@AlexeyBerezkin例如,在上面的数据中,可以从节点