生成包含大量对象的图形

时间:2016-10-29 12:35:41

标签: java graph tree

我想生成一个图表,表示在青蛙飞跃游戏中移动青蛙的所有可能性。这是在开始时使用每侧2只青蛙时使用的图表。我还编写了Graph的简单Java表示和生成可能性图的方法。我遇到的问题是该方法非常慢,超过20只青蛙,并且由于它创建的顶点对象的数量而出现“内存不足异常”。有没有更好的方法我可以编写generateTheGraph方法,也许能够使用它还有100多只青蛙?

                                    >>_<<
               |              |                   |                  |
             >_><<         _>><<                >><_<               >><<_
        |            |                     |           |
      _>><<       ><>_<                   >><<_       >_<><
              |             |                     |         |
            ><_><          ><><_                _><><      ><_><
         |         |        |                     |       |       |
       _<>><      ><<>_    ><_<>                <>_><   _<>><   ><<>_    
         |         |      |     |             |     |     |       |
       <_>><     ><<_>  _<><>  ><<_>       <_>><  <><>_ <_>><   ><<_>
                          |                         |
                        <_><>                     <><_>
                          |                         |
                        <<>_>                     <_<>>
                          |                         |
                        <<_>>                     <<_>>

顶点类:

public class Vertex implements Comparable<Vertex> {

private String data;
private List<Edge> neighboursList;
private boolean visited;
private Vertex previousVertex;
private double minDistance = Double.MAX_VALUE;

public Vertex(String name) {
    this.data = name;
    this.neighboursList = new ArrayList<>();
}

public void addNeighbour(Edge edge) {
    this.neighboursList.add(edge);
}

public String getData() {
    return data;
}

public void setName(String name) {
    this.data = name;
}

public List<Edge> getNeighboursList() {
    return neighboursList;
}

public void setNeighboursList(List<Edge> neighboursList) {
    this.neighboursList = neighboursList;
}

public boolean isVisited() {
    return visited;
}

public void setVisited(boolean visited) {
    this.visited = visited;
}

public Vertex getPreviousVertex() {
    return previousVertex;
}

public void setPreviousVertex(Vertex previousVertex) {
    this.previousVertex = previousVertex;
}

public double getMinDistance() {
    return minDistance;
}

public void setMinDistance(double minDistance) {
    this.minDistance = minDistance;
}

public String toString() {
    return this.data;
}

public int compareTo(Vertex otherVertex) {
    return Double.compare(this.minDistance, otherVertex.getMinDistance());
}

边缘类:

public class Edge {

private double weight;
private Vertex startVertex;
private Vertex targetVertex;

public Edge(double weight, Vertex startVertex, Vertex targetVertex) {
    super();
    this.weight = weight;
    this.startVertex = startVertex;
    this.targetVertex = targetVertex;
}

public double getWeight() {
    return weight;
}

public void setWeight(double weight) {
    this.weight = weight;
}

public Vertex getStartVertex() {
    return startVertex;
}

public void setStartVertex(Vertex startVertex) {
    this.startVertex = startVertex;
}

public Vertex getTargetVertex() {
    return targetVertex;
}

public void setTargetVertex(Vertex targetVertex) {
    this.targetVertex = targetVertex;
}

以及生成图表的方法:

private void generateTheGraph(Stack<Vertex> stack, Vertex vertex) {

    int index = vertex.getData().indexOf("_");
    if (vertex.getData().equals(targetedString)) {
        targetVertex = vertex;
    }

    if ((index - 1 >= 0) && vertex.getData().charAt(index - 1) == '>') {
        StringBuilder option = new StringBuilder(vertex.getData());
        option.setCharAt(index, '>');
        option.setCharAt(index - 1, '_');
        Vertex newVertex = new Vertex(option.toString());
        option.setLength(0);
        vertex.addNeighbour(new Edge(1, vertex, newVertex));
        stack.push(newVertex);
        generateTheGraph(stack, newVertex);
    }
    if ((index + 1 < vertex.getData().length()) && vertex.getData().charAt(index + 1) == '<') {
        StringBuilder option = new StringBuilder(vertex.getData());
        option.setCharAt(index, '<');
        option.setCharAt(index + 1, '_');
        Vertex newVertex = new Vertex(option.toString());
        option.setLength(0);
        vertex.addNeighbour(new Edge(1, vertex, new Vertex(option.toString())));
        stack.push(newVertex);
        generateTheGraph(stack, newVertex);
    }
    if ((index - 2 >= 0) && vertex.getData().charAt(index - 2) == '>') {
        StringBuilder option = new StringBuilder(vertex.getData());
        option.setCharAt(index, '>');
        option.setCharAt(index - 2, '_');
        Vertex newVertex = new Vertex(option.toString());
        option.setLength(0);
        vertex.addNeighbour(new Edge(1, vertex, newVertex));
        stack.push(newVertex);
        generateTheGraph(stack, newVertex);
    }
    if ((index + 2 < vertex.getData().length()) && vertex.getData().charAt(index + 2) == '<') {
        StringBuilder option = new StringBuilder(vertex.getData());
        option.setCharAt(index, '<');
        option.setCharAt(index + 2, '_');
        Vertex newVertex = new Vertex(option.toString());
        option.setLength(0);
        vertex.addNeighbour(new Edge(1, vertex, newVertex));
        stack.push(newVertex);
        generateTheGraph(stack, newVertex);
    }
}

0 个答案:

没有答案