简体   繁体   中英

Java implementing weighted graph?

I had written my code, but don't know how to access weight of graph, or how to print its edges in main method, please have look at my code. Please help,actually i trying to implement Dijkstra , but I don't know it is the correct way of including weight in the graph or not. Please help trying to solve it from past three days.

public class Gr {

public class Node{ 
    public int vertex;
    public  int weight ;

    public int getVertex() {return vertex;}
    public int getWeight() {return weight;}
    public Node(int v , int w){
        vertex=v;
        weight=w;
    }

}
private int numVertices=1 ;
private  int numEdges=0 ;
private Map<Integer,ArrayList<Node>> adjListsMap= new HashMap<>();


public int getNumVertices(){
    return  numVertices;
}

public int addVertex(){
    int v = getNumVertices();
    ArrayList<Node> neighbors = new ArrayList<>();
    adjListsMap.put(v,neighbors);
    numVertices++ ;
    return (numVertices-1);
}

//adding edge
public void addEdge(int u , int v,int w ){
    numEdges++ ;
    if(v<numVertices&&u<numVertices){
        (adjListsMap.get(u)).add( new Node(u,w));
        (adjListsMap.get(v)).add(new Node(u,w));

    }
    else {
        throw new IndexOutOfBoundsException();
    }
}

//getting neighbours

public List<Node> getNeighbors(int v ){
    return new ArrayList<>(adjListsMap.get(v));
}



public static void main(String[] args){
    Gr g = new Gr();


        for(int j=1;j<=3;j++)
            g.addVertex();
        for(int k =1;k<=2;k++)
        {   int u= in.nextInt();
            int v = in.nextInt();
            int w = in.nextInt();
            g.addEdge(u,v,w);
        }


    }

}

First note: Usually , Node is the vertex and Edge is an edge. The names you've adopted may cause a lot of confusion.

Answer: It is good practice to use Node and Edge , if you are representing your graph as an Adjacency List . If it is the case, The Node has a label and a list of Edge s. Edge has some kind of reference (in my example, a reference to the Node object) to the destination Node and a weight .

Code example:

Node.java

public class Node {
  private String label;
  private List<Edge> edges;
}

Edge.java

public class Edge {
  private Node destination;
  private double weight;
}

Usage example

public class Main {
    public static void main(String[] args) {
        // creating the graph A --1.0--> B
        Node n = new Node();
        n.setLabel("A");
        Node b = new Node();
        b.setLabel("B");
        Edge e = new Edge();
        e.setDestination(b);
        e.setWeight(1.0);
        n.addEdge(e);

        // returns the destination Node of the first Edge
        a.getEdges().get(0).getDestination(); 
        // returns the weight of the first Edge
        a.getEdges().get(0).getWeight(); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM