Java 有向图初始化 公共类节点{ 私有最终整数顶点; 私有最终哈希集节点; 公共节点(int索引){ 这个指数=指数; this.nodes=new HashSet(); } 受保护的void addOutgoingEdge(节点a){ 节点。添加(a); } 公共类定向图{ 私有映射顶点; 公共DirectedGraph(字符串str){ this.vertices=new HashMap(); str=str.replaceAll(“[a\\s\\[]”,即“”); 字符串[]边=str.split(“]”); 用于(字符串边:边){ String[]points=edge.split(“,”); int[]整数点=新的int[2]; 整数点[1]=整数.parseInt(点[1]); 节点传入=新节点(整数点[0]); 节点传出=新节点(整数点[1]); //需要构建地图并在此处添加边 } }`在这里输入代码` 受保护的void addEdge(节点原点、节点目标){ 来源地:addOutgoingEdge(目的地); }
我在有向图上滚动时遇到了一些问题 我有两个类:一个节点类和一个DirectedGraph类。DirectedGraph类将传入一个字符串来构造一个图。但是,我似乎无法初始化DirectedGraph类中的GraphNode和相应的边。我知道我将使用一个映射来获取唯一的键,但是值(传出边)把我搞砸了。我知道我离得很近,我只是一直在滑倒 示例输出类似于1------>24------>35------>4Java 有向图初始化 公共类节点{ 私有最终整数顶点; 私有最终哈希集节点; 公共节点(int索引){ 这个指数=指数; this.nodes=new HashSet(); } 受保护的void addOutgoingEdge(节点a){ 节点。添加(a); } 公共类定向图{ 私有映射顶点; 公共DirectedGraph(字符串str){ this.vertices=new HashMap(); str=str.replaceAll(“[a\\s\\[]”,即“”); 字符串[]边=str.split(“]”); 用于(字符串边:边){ String[]points=edge.split(“,”); int[]整数点=新的int[2]; 整数点[1]=整数.parseInt(点[1]); 节点传入=新节点(整数点[0]); 节点传出=新节点(整数点[1]); //需要构建地图并在此处添加边 } }`在这里输入代码` 受保护的void addEdge(节点原点、节点目标){ 来源地:addOutgoingEdge(目的地); },java,data-structures,directed-graph,Java,Data Structures,Directed Graph,我在有向图上滚动时遇到了一些问题 我有两个类:一个节点类和一个DirectedGraph类。DirectedGraph类将传入一个字符串来构造一个图。但是,我似乎无法初始化DirectedGraph类中的GraphNode和相应的边。我知道我将使用一个映射来获取唯一的键,但是值(传出边)把我搞砸了。我知道我离得很近,我只是一直在滑倒 示例输出类似于1------>24------>35------>4 其中左列是顶点,右列是输出边。我希望得到任何提示。以下是我的课程:嗨,我为您编写了一个解决方案
其中左列是顶点,右列是输出边。我希望得到任何提示。以下是我的课程:嗨,我为您编写了一个解决方案,我对您的代码进行了一点重构,我重点关注了关系的创建:
public class Node {
private final int vertex;
private final HashSet<Node> nodes;
public Node(int index) {
this.index = index;
this.nodes = new HashSet<Node>();
}
protected void addOutgoingEdge(Node a) {
nodes.add(a);
}
public class DirectedGraph {
private Map<Integer, Node> vertices;
public DirectedGraph(String str) {
this.vertices = new HashMap<Integer, Node>();
str = str.replaceAll("[a\\s\\[]", "");
String[] edges = str.split("]");
for (String edge : edges) {
String[] points = edge.split(",");
int[] integerPoint = new int[2];
integerPoint[1] = Integer.parseInt(points[1]);
Node incoming = new Node(integerPoint[0]);
Node outgoing = new Node(integerPoint[1]);
// Need to construct the map and add edges here
}
}` enter code here`
protected void addEdge(Node origin, Node destination) {
origin.addOutgoingEdge(destination);
}
package com.stackoverflow.graph;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
/**
*此类管理有向图的创建,以及与构建有向图相关的所有方法
*
*/
公共类定向图{
地图图;
公共DirectedGraph(){
this.graph=new HashMap();
}
void createRelationship(节点输入、节点输出){
列表输出=graph.get(输入);
//如果是新节点,则创建其输出列表(与之有关系的所有节点)
如果(输出==null)
输出=新的ArrayList();
//检查节点是否不在列表中,以避免重复
如果(!outputs.contains(输出)){
输出。添加(输出);
图形输出(输入、输出);
}
}
void printGraph(){
迭代器it=this.graph.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
列表输出=(列表)pair.getValue();
用于(节点:输出){
System.out.print(pair.getKey().toString()+“-->”+节点+”;
}
it.remove();//避免ConcurrentModificationException
}
}
}
------------------
包com.stackoverflow.graph;
公共类节点{
//这是实现所需的唯一字段
私有整数值;
公共节点(int值){
这个值=值;
}
公共整数getValue(){
返回值;
}
公共无效设置值(整数值){
这个值=值;
}
@凌驾
公共字符串toString(){
返回值.toString();
}
}
----------------------
包com.stackoverflow.graph;
公共类应用程序{
公共静态void main(字符串[]args){
DirectedGraph图形=新的DirectedGraph();
//这是最终结果,在进行解析时,您必须准备以下代码以这种方式运行:
Node n1=新节点(1);//取解析后的值,而不是硬编码的值1,2,3,4,5
节点n2=新节点(2);
节点n3=新节点(3);
节点n4=新节点(4);
节点n5=新节点(5);
//将此代码放入循环以创建每条边
关系图(n1,n2);
关系图(n3,n4);
关系图(n3,n5);
//重复关系,但未添加
关系图(n3,n5);
printGraph();
}
}
输出:
1-->2 3-->4 3-->5您使用的是哪一个库?jGraph?没有库。只是标准输出。一个节点可以有多个边吗?为了测试应用程序,请您举一个输入示例。是的,一个节点可以有多个边。输入看起来像一个字符串,“[a1,a2][a6 a6,a10][a10,a11][a33,a22]”我已经解析出字符串并将数字转换为整数。我感谢您的努力。我会对此稍加修改。谢谢。当然,如果您在解析方面有问题,请告诉我。
package com.stackoverflow.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* This class manages the Directed Graph creation, and all the methods related to build a Directed Graph
*
*/
public class DirectedGraph {
Map<Node, List<Node>> graph;
public DirectedGraph() {
this.graph = new HashMap<Node, List<Node>>();
}
void createRelationship(Node input, Node output) {
List<Node> outputs = graph.get(input);
//If is a new node create their output list, (all the nodes to which it has relationship)
if (outputs == null)
outputs = new ArrayList<Node>();
//Check that the node is not in the list, to avoid duplication
if (!outputs.contains(output)){
outputs.add(output);
graph.put(input,outputs);
}
}
void printGraph(){
Iterator it = this.graph.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
List<Node> outputs = (List<Node>) pair.getValue();
for(Node node : outputs){
System.out.print(pair.getKey().toString() + "-->" + node + " ");
}
it.remove(); // avoids a ConcurrentModificationException
}
}
}
------------------
package com.stackoverflow.graph;
public class Node {
//This is the only field you need for your implementation
private Integer value;
public Node(int value){
this.value = value;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
@Override
public String toString() {
return value.toString();
}
}
----------------------
package com.stackoverflow.graph;
public class App {
public static void main(String[] args) {
DirectedGraph graph = new DirectedGraph();
//This is the final result, with your parse you must prepare this code to run in this way:
Node n1 = new Node(1);//Instead of the hardcoded values 1,2,3,4,5 take the parsed ones
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
Node n5 = new Node(5);
//put this code into a loop to create each edge
graph.createRelationship(n1, n2);
graph.createRelationship(n3, n4);
graph.createRelationship(n3, n5);
//Duplicate relationship but not added
graph.createRelationship(n3, n5);
graph.printGraph();
}
}