Writing a Java Code to Add Weights to the Directed Graph

The aim is to adapt the implementation of the AdjacencyListGraph class to support weights on edges.

The steps should be the following:

  1. Understand Snippet 6.1 showing how we can implement the adjacency list representation
  2. Adapt the implementation so that the array list can store the weights
ArrayList<Edge>[] adj;
public AdjacencyListWeightedGraph(int nodes) {

this.adj = new ArrayList[nodes];
for (int i = 0; i < nodes; i++)
this.adj[i] = new ArrayList<>();
}
public void addEdge(int u, int v, int weight) {
this.adj[u].add(new Edge(u, v, weight));
}
Snippet 6.2: Implementation of an adjacency list representation of a weighted graph. Source class name: Adjacencylistweightedgraph

Go to https://goo.gl/uoazxy to access this code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.130.199