1

So pretty much in this assignment I have to add a routine to the EdgeList and AdjMatrix objects class to return a breadth first spanning tree. The signature(s) would be:

In EdgelList: EdgeList BFSpanning();

In AdjMatrix: EdgeList BFSpanning();

But I'm really confused,do I just write the BFSpanning method inside both those classes? can anyone give me a hard structure for me? I can't seem to come up writing the correct method.

Here are my adjmatrix class:

public class AdjMatrix extends Object implements Cloneable{
  private int size;
  private long[][] m;

  public Object clone(){
    int i, j;
    AdjMatrix ret;
    ret = new AdjMatrix();
    ret.setSize(this.getSize());
    for (i=0; (i < this.getSize()); i=i+1){
      for (j=0; (j < this.getSize()); j=j+1){
        ret.setUndirected(i, j, this.getEdge(i, j));
      }
    }
    return (ret);
  }

  public void setSize(int size) throws BadSizeException{
    int i, j;
    if (this.sizeOK(size)) {
      this.size = size;
      m = new long [size][];
      for (i=0; (i < size); i=i+1){
        m[i] = new long[size];
        for (j=0; (j < size); j=j+1){
          m[i][j] = -1;
        }
      }
    } else {
      throw new BadSizeException();
    }
  }

  public int getSize(){
    return(this.size);
  }

  public void setUndirected(int x, int y, long val) throws BadIndexException {
    if (this.OK(x, y)) {
      m[x][y] = val;
      m[y][x] = val;
    } else {
      throw new BadIndexException();
    }
  }

  public void setDirected(int x, int y, long val) throws BadIndexException {
    if (this.OK(x, y)) {
      m[x][y] = val;
    } else {
      throw new BadIndexException();
    }
  }
  public long getEdge(int x, int y) throws BadIndexException {
    long ret = -1;
    if (this.OK(x, y)) {
      ret = m[x][y];
    } else {
      throw new BadIndexException();
    }
    return (ret);
  }

  public boolean sizeOK(int size) {
    return (size > 0);
  }
  public boolean OK(int x, int y) {
    return (((x >=0) && (x < this.size)) &&
            ((y >=0) && (y < this.size)));
  }
}

Here is my edglist class:

public class EdgeList extends Object implements Cloneable{
  private int nodeNumber;
  private Edges[] nodes;

  public Object clone(){
    int i, j;
    EdgeList ret;
    ret = new EdgeList();
    ret.setNodeNumber(this.nodeNumber);
    for (i=0; (i < this.getNodeNumber()); i=i+1){
      ret.nodes[i] = (Edges) (this.nodes[i].clone());
    }
    return (ret);
  }

  public void setNodeNumber(int nodes) throws BadNodeNumberException{
    int i;
    if (nodes > 0) {
      this.nodeNumber = nodes;
      this.nodes = new Edges[nodes];
      for (i=0; (i < nodes); i = i + 1){
        this.nodes[i] = new Edges();
        this.nodes[i].setSize(nodes);
      }
    } else {
      throw new BadNodeNumberException();
    }
  }

  public int getNodeNumber(){
    return(this.nodeNumber);
  }

  public void addEdge(int x, int y, long val) throws BadIndexException, DuplicateEdgeException {
    int i;
    if (this.OK(x, y)) {
      this.nodes[x].add(y, val);
    } else {
      throw new BadIndexException();
    }
  }

  public Edges getEdges(int i) throws BadIndexException {
    Edges ret = null;
    if ((i >= 0) && (i < this.nodeNumber)) {
      ret = (Edges)(nodes[i].clone());
    } else {
      throw new BadIndexException();
    }
    return (ret);
  }

  public boolean OK(int x, int y) {
    return (((x >=0) && (x < this.nodeNumber)) &&
            ((y >=0) && (y < this.nodeNumber)));
  }
}

Also don't know if you need to see this class edges:

public class Edges extends Object implements Cloneable{
  private int size;
  private int place;
  private Edge[] edges;
  public Object clone(){
    Edges ret;
    int i;
    ret = new Edges();
    ret.setSize(this.size);
    for (i=0; (i < this.getSize()); i=i+1){
      ret.add(this.edges[i].getNode(), this.edges[i].getValue());
    }
    return (ret);
  }

  public void setSize(int size){
    edges = new Edge[size];
    this.size = 0;
    this.place = 0;
  }
  public void add(int node, long value) throws DuplicateEdgeException{
    int i;
    boolean ok;
    Edge e;
    ok = ((node  > 0) && (node < edges.length)) && (size < edges.length);
    for (i=0; (i < this.size); i = i + 1) {
      ok = ok && (this.edges[i].getNode() != node);
    }
    if (ok) {
      e = new Edge();
      e.setNode(node);
      e.setValue(value);
      this.edges[this.size] = e;
      this.size = this.size + 1;
    } else {
      throw new DuplicateEdgeException();
    }
  }

  public Edge getCurrent(){
    Edge ret = null;
    if (size != 0) {
      ret =(Edge) (this.edges[place].clone());
    } else {}
    return (ret);
  }

  public void next(){
    this.place = (this.place + 1) % this.size;
  }

  public int getSize(){
    return (this.edges.length);
  }  
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joe liang
  • 31
  • 4

1 Answers1

0

Ask your instructor for clarification if you don't understand. That being said I can offer a guess:

Yes, you are to write a method on each class. They both return a tree, which is a subset of the graph and has all of the vertices. How they actually do this will be vastly different. The goal of the assignment is likely to show that storing the same data in different ways requires different solutions to the same question. Also you may find that one method is easier to write than the other, this will show that certain data formats make answering certain questions easier or harder.

Louis Zelus
  • 133
  • 13
  • Hey Louis I finished wrote it, do you mind take a look at my code. Can I email or you email me, I uploaded the code in github. can you help if you have time? – Joe liang Jul 08 '15 at 14:45