使用有向图java的邻接列表结构

时间:2015-01-13 10:46:37

标签: java graph transpose adjacency-list

我只使用邻接列表实现图形转置的问题,没有边缘列表。图的每个顶点都有一个存储相邻顶点的位置列表。当您必须转置图形时,必须删除存储在顶点A列表中的每个元素并将其用作源顶点。所以,如果我们有这种情况  A - >乙 B删除后,将旧源Vertex A添加到自己的相邻列表中 我们有结果:  B - >甲

当我更新列表时,我覆盖了存储的最后一个信息,所以我丢失了顶点之间的连接。

public class Vertice implements Comparable<Vertice>{

    public Vertice(int valore){
        this.valore = valore;
        this.color = Color.WHITE;
        this.distanza = 0;
        this.padre = null;
        adiacenze = new NodePositionList<Vertice>();
    }

    public int getValore() {
        return valore;
    }

    public void setValore(int valore) {
        this.valore = valore;
    }


    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }


    public Position<Vertice> getPadre() {
        return padre;
    }

    public void setPadre(Position<Vertice> padre) {
        this.padre = padre;
    }


    public int getDistanza() {
        return distanza;
    }

    public void setDistanza(int distanza) {
        this.distanza = distanza;
    }


    public enum Color{
        BLACK, WHITE, GREY;
    }

    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("v_" + valore  );
        return sb.toString();
    }

    public boolean equals(Vertice v){
        return this.compareTo(v) == 0;
    }

    @Override
    public int compareTo(Vertice o) {
        return this.valore - o.getValore();
    }

    public int getFin() {
        return fin;
    }

    public void setFin(int fin) {
        this.fin = fin;
    }

    public Position<Vertice> getRiferimentoVertice() {
        return riferimentoVertice;
    }

    public void setRiferimentoVertice(Position<Vertice> riferimentoVertice) {
        this.riferimentoVertice = riferimentoVertice;
    }


    public Iterable<Vertice> getAdiacenze() {
        return adiacenze;
    }

    public void setAdiacenze(NodePositionList<Vertice> adiacenze) {
        this.adiacenze = adiacenze;
    }

    public Position<Vertice> addNodoAdiacente(Vertice v){ 
        Position<Vertice> newVertice = adiacenze.addLast(v);
        v.setRiferimentoAsAdiacenza(newVertice);
        return newVertice;
    }


    public Position<Vertice> removeNodoAdiacente(Position<Vertice> v){
        return adiacenze.remove(v);
    }


    public Position<Vertice> getRiferimentoAsAdiacenza() {
        return riferimentoAsAdiacenza;
    }

    public void setRiferimentoAsAdiacenza(Position<Vertice> riferimentoAsAdiacenza) {
        this.riferimentoAsAdiacenza = riferimentoAsAdiacenza;
    }


    public boolean hasAdiacent(){
        return adiacenze.size() == 0;
    }

    public boolean hasRedEdge(){
        return redEdge;
    }

    public void setHasRedEdge(boolean redEdge){
        this.redEdge = redEdge;
    }


    private boolean redEdge; 

    private int valore;
    private Color color;
    private Position<Vertice> padre;
    private int distanza;
    private int fin;
    private Position<Vertice> riferimentoVertice;
    private Position<Vertice> riferimentoAsAdiacenza;
    private NodePositionList<Vertice> adiacenze; 

这里是&#34; Grafo.java&#34;类

public class Grafo {



    public Grafo(int numV){
        verticiGrafo = new NodePositionList<Vertice>();
        listaArchi = new NodePositionList<Arco>();
        this.V = numV;
    }


    public Position<Vertice> addVertice(Vertice v){
        Position<Vertice> newAdded = verticiGrafo.addLast(v);
        v.setRiferimentoVertice(newAdded);
        return newAdded;
    }

    public NodePositionList<Vertice> getVertici(){
        return verticiGrafo;
    } 

    public Position<Arco> addArco(Arco a){
        a.getSorgente().element().addNodoAdiacente(a.getDestinazione().element());
        Position<Arco> newAdded = listaArchi.addLast(a);
        a.setRiferimentoArco(newAdded);
        return newAdded;
    }


    public Position<Arco> connect(Position<Vertice> sorgente, Position<Vertice> destinazione){
        Arco a = new Arco(sorgente, destinazione);
        Vertice v_sorgente = sorgente.element();
        Vertice v_destinazione = destinazione.element();
        v_sorgente.addNodoAdiacente(v_destinazione);
        Position<Arco> newAdded = listaArchi.addLast(a);
        a.setRiferimentoArco(newAdded);
        return newAdded;
    }

    public Position<Arco> removeArco(Position<Arco> a){
        a.element().getSorgente().element().removeNodoAdiacente(a.element().getDestinazione());
        return listaArchi.remove(a);
    }

    public Iterable<Arco> archi(){
        return listaArchi;
    }

    public void invertiArco(Position<Arco> a){
        Arco a1 = a.element();
        Position<Vertice> temp = a1.getDestinazione();
        a1.setDestinazione(a1.getSorgente());
        a1.setSorgente(temp);
        Vertice sorgente = a1.getSorgente().element();
        Vertice destinatario = a1.getDestinazione().element();
        sorgente.addNodoAdiacente(destinatario);
    }

    public Position<Arco> getArcoByVertici(Position<Vertice> sorgente, Position<Vertice> destinazione){
        for(Arco a: listaArchi){
            if(a.getColor() == EdgeColor.NONE){
                Vertice dest = a.getDestinazione().element();
                Vertice sorg = a.getSorgente().element();
                if(sorg.equals(sorgente.element()) && dest.equals(destinazione.element())){
                    return a.getRiferimentoArco();
                }
            }
        }
        return null;
    }

    public void removeAllAdiacenze(){
        for(Vertice v: this.getVertici()){
            v.setAdiacenze( new NodePositionList<Vertice>());
        }
    }

    public void setColorArco(Position<Arco> a, EdgeColor ec){
        a.element().setColor(ec);
    }

    public NodePositionList<Arco> getListaArchi() {
        return listaArchi;
    }


    public void setListaArchi(NodePositionList<Arco> listaArchi) {
        this.listaArchi = listaArchi;
    }

    private NodePositionList<Vertice> verticiGrafo;
    private NodePositionList<Arco> listaArchi; //lista archi uscenti


    public final int V;
}

这里的&#34; Ricerca.java&#34;我试图制作的课程

public static Grafo traspostaGrafo(Grafo g){
    NodePositionList<Vertice> npl = g.getVertici();
    Position<Vertice> u = npl.first();
    while (u != npl.getTail()){
        u.element().setColor(Color.WHITE);
        NodePositionList<Vertice> adiacenti = g.getListaAdiacenti(u.element().getValore());
        Position<Vertice> adiacente = adiacenti.first();
        while (adiacente != adiacenti.getTail() && adiacente.element().getFlag() == 0){
            g.addNodoAdiacente(adiacente.element(), u.element());
            g.removeNodoAdiacente(adiacente);   
            adiacente = adiacenti.next(adiacente);
            u.element().setFlag(1);
        }
    }
    return g
}

0 个答案:

没有答案