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);
}
我有一點有些麻煩滾動的球對我有向圖向圖初始化
我有兩類:一類節點和創建DirectedGraph類。 DirectedGraph類將傳入一個字符串來構造圖。但是,我似乎無法初始化GraphNode和我的DirectedGraph類中的相應邊。我知道我將使用Map來獲得唯一的鍵值,但值(傳出邊緣)正在讓我興奮。我知道我非常接近,我只是繼續滑倒。
樣例輸出將如1 -----> 2 4 -----> 3 5 ------> 4 其中,左列是頂點,右列是輸出邊緣。我會很感激任何提示。這裏是我的課:
您正在使用哪個庫? jGraph? – sirandy
沒有庫..只是標準輸出。 – esmith08734
一個節點可以有多條邊嗎?還可以舉一個輸入示例嗎?,以測試該應用。 – sirandy