图形练习传递闭包

时间:2014-11-23 16:47:31

标签: java graph

我必须找到给定图的传递闭包。所以,传递闭包的方法应该是这样的:G = ABCD - > G + = AB AC AD BC BD CD,G是给定图,G +是传递闭包。到目前为止,我的方法从ABCD输出AB AC AD,但如何输出其余的BC BD CD?我创造了:

图表类:

class Graph {

  String id;
  Vertex first;

  Graph (String s, Vertex v) {
     id = s;
     first = v;
  }

  Graph (String s) {
     this (s, null);
  }


  public LinkedList<Edge> transitiveClosure(LinkedList<Vertex> v)
  {

      Graph g = new Graph("G+");
      LinkedList<Edge> edge = new LinkedList<Edge>();
      Edge e = null;      
      Iterator i = v.iterator();

      for(Vertex vertex : v)
      {
         System.out.print(vertex);


        while(i.hasNext()) {          
        g.first = v.getFirst();
        Vertex tmp = v.getFirst();

        tmp =(Vertex) i.next();
        if(tmp == g.first) 
        tmp = (Vertex)i.next();
        e = new Edge(g.first.toString() + tmp);                         
        edge.add(e);
        //v.remove(g.first);


        }
      }


      System.out.println();
      return edge;
      }

顶点类

 class Vertex {

  String id;
  Vertex next;
  Edge first;

  Vertex (String s, Vertex v, Edge e) {
     id = s;
     next = v;
     first = e;
  }

  Vertex (String s) {
     this (s, null, null);
  }

  @Override
  public String toString() {
     return id;
  }

  // TODO!!! Your Vertex methods here!

}

边缘类

 class Edge {

  String id;
  Vertex target;
  Edge next;

  Edge (String s, Vertex v, Edge e) {
     id = s;
     target = v;
     next = e;
  }

  Edge (String s) {
     this (s, null, null);
  }

  @Override
  public String toString() {
     return id;
  }

  // TODO!!! Your Edge methods here!

}

0 个答案:

没有答案