2017-04-19 72 views
0
 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 其中,左列是頂點,右列是輸出邊緣。我會很感激任何提示。這裏是我的課:

+0

您正在使用哪個庫? jGraph? – sirandy

+0

沒有庫..只是標準輸出。 – esmith08734

+0

一個節點可以有多條邊嗎?還可以舉一個輸入示例嗎?,以測試該應用。 – sirandy

回答

0

您好我已經編碼爲你解決我重構一點你的代碼,我把重點放在關係創建:

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(); 
    } 
} 

輸出:

1 - > 2 3 - > 4 3 - > 5

+0

我感謝您的努力。我會稍微鼓掌一下。謝謝。 – esmith08734

+0

當然,如果解析出現問題,請告訴我。 – sirandy