homework5/~$cial Graphs.docx homework5/Graph.javahomework5/Graph.java// UMUC CMSC 350 // Class Graph - Defines an undirected graph // Adapted by Ioan from: // Liang - Introduction to Java Programming, 9th Edition (Code Examples of Chapter 30 Graphs and Applications) // Source code of the examples available at: // http://www.cs.armstrong.edu/liang/intro9e/examplesource.html import java.util.*; publicclassGraph<V>{ protectedList<V> vertices =newArrayList<V>();// Store vertices protectedList<List<Integer>> neighbors =newArrayList<List<Integer>>();// Adjacency lists /** Construct an empty graph */ protectedGraph(){ } /** Construct a graph from edges and vertices stored in arrays */ protectedGraph(int[][] edges, V[] vertices){ for(int i =0; i < vertices.length; i++) this.vertices.add(vertices[i]); createAdjacencyLists(edges, vertices.length); } /** Construct a graph from edges and vertices stored in List */ protectedGraph(List<Edge> edges,List<V> vertices){ for(int i =0; i < vertices.size(); i++) this.vertices.add(vertices.get(i)); createAdjacencyLists(edges, vertices.size()); } /** Construct a graph for integer vertices 0, 1, 2 and edge list */ protectedGraph(List<Edge> edges,int numberOfVertices){ for(int i =0; i < numberOfVertices; i++) vertices.add((V)(newInteger(i)));// vertices is {0, 1, ...} createAdjacencyLists(edges, numberOfVertices); } /** Construct a graph from integer vertices 0, 1, and edge array */ protectedGraph(int[][] edges,int numberOfVertices){ for(int i =0; i < numberOfVertices; i++) vertices.add((V)(newInteger(i)));// vertices is {0, 1, ...} createAdjacencyLists(edges, numberOfVertices); } /** Create adjacency lists for each vertex */ privatevoid createAdjacencyLists(int[][] edges,int numberOfVertices){ // Create a linked list for(int i =0; i < numberOfVertices; i++){ neighbors.add(newArrayList<Integer>()); } for(int i =0; i < edges.length; i++){ int u = edges[i][0]; int v = edges[i][1]; neighbors.get(u).add(v); } } /** Create adjacency lists for each vertex */ privatevoid createAdjacencyLists(List<Edge> edges,int numberOfVertices){ // Create a linked list for each vertex for(int i =0; i < numberOfVertices; i++){ neighbors.add(newArrayList<Integer>()); } for(Edge edge: edges){ neighbors.get(edge.u).add(edge.v); } } /** Return the number of vertices in the graph */ publicint getSize(){ return vertices.size(); } /** Return the vertices in the graph */ publicList<V> getVertices(){ return vertices; } /** Return the object for the specified vertex */ public V getVertex(int index){ return vertices.get(index); } /** Return the index for the specified vertex object */ publicint getIndex(V v){ return vertices.indexOf(v); } /** Return the neighbors of the specified vertex */ publicList<Integer> getNeighbors(int index){ return neighbors.get(index); } /** Return the degree for a specified vertex */ publicint getDegree(int v){ return neighbors.get(v).size(); } /** ...