Domanda

Ho definito un bordo personalizzato e un tipo di vertice da utilizzare in un grafico sparso non orientato. Il problema è che il grafico sta aggiungendo più bordi che non voglio. Ad esempio, considerando il codice seguente:

UndirectedSparseGraph<Vertex, Edge> graphX = new UndirectedSparseGraph<Vertex, Edge>();
graphX.addEdge(new Edge("1#2"), new Vertex("1"), new Vertex("2"));
graphX.addEdge(new Edge("1#2"), new Vertex("1"), new Vertex("2"));
graphX.addEdge(new Edge("2#1"), new Vertex("2"), new Vertex("1"));
graphX.addEdge(new Edge("1#3"), new Vertex("1"), new Vertex("3"));
graphX.addEdge(new Edge("1#4"), new Vertex("1"), new Vertex("4"));

Ho aggiunto intenzionalmente due bordi simili (i primi). Ho sovrascritto un metodo uguale per entrambe le classi che ho creato, cioè, Edge e Vertex, ma il grafico si presume come i bordi poiché i vertici sono diversi e ne aggiungono tutti. Ecco l'output:

Vertices:1,4,1,1,2,1,1,2,2,3
Edges:1#3[1,3] 1#4[1,4] 1#2[1,2] 1#2[1,2] 2#1[2,1] 

Allora, cosa sto facendo di sbagliato?

Ps. Cordiali saluti ecco le classi che ho creato:

public class Vertex {

    private String id;
    //More info in the future

    public Vertex(String id){
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object obj){
        return ((Vertex) obj).id.equals(this.id);
    }

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

}

public class Edge {

    private String id;
    private double weight;

    public Edge(String id, double weight){
        this.id = id;
        this.weight = weight;
    }

    public Edge(String id){
        this.id = id;
        this.weight = -1;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getWeight() {
        return weight;
    }

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

    @Override
    public boolean equals(Object obj){
        return ((Edge) obj).id.equals(this.id);
    }

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

}

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top