有向图初始化
Directed Graph Initialization
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);
}
我在让球在我的有向图上滚动时遇到了一些麻烦
我有两个 classes:一个节点 class 和一个有向图 class。 DirectedGraph class 会传入一个String来构造一个图。但是,我似乎无法在我的 DirectedGraph class 中初始化 GraphNode 和相应的边。我知道我将使用 Map 来获取唯一键,但值(出边)让我搞砸了。我知道我很接近,我只是不断滑倒。
示例输出类似于 1-----> 2 4 -----> 3 5 ------> 4
其中左列是顶点,右列是出边。我将不胜感激任何提示。这是我的 classes:
你好,我已经为你编写了一个解决方案,我重构了你的代码,我专注于关系的创建:
package com.Whosebug.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.Whosebug.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.Whosebug.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();
}
}
输出:
1-->2 3-->4 3-->5
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);
}
我在让球在我的有向图上滚动时遇到了一些麻烦
我有两个 classes:一个节点 class 和一个有向图 class。 DirectedGraph class 会传入一个String来构造一个图。但是,我似乎无法在我的 DirectedGraph class 中初始化 GraphNode 和相应的边。我知道我将使用 Map 来获取唯一键,但值(出边)让我搞砸了。我知道我很接近,我只是不断滑倒。
示例输出类似于 1-----> 2 4 -----> 3 5 ------> 4 其中左列是顶点,右列是出边。我将不胜感激任何提示。这是我的 classes:
你好,我已经为你编写了一个解决方案,我重构了你的代码,我专注于关系的创建:
package com.Whosebug.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.Whosebug.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.Whosebug.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();
}
}
输出:
1-->2 3-->4 3-->5