我正在尝试使用HashMap实现基本的图形数据结构,当我尝试使用getneighbours方法时,出现此错误,提示类型不兼容
public class Graph<T> implements Iterable<T>{
private Map<T, LinkedList<T>> adjList;
private int numberOfVertices;
private int numberOfEdges;
private Graph graph;
// Intialization
public Graph() {
adjList = new HashMap<T, LinkedList<T>>();
numberOfVertices = numberOfEdges = 0;
}
// Returns number of vertices
public int getNumberOfVertices() {
return numberOfVertices;
}
// Returns number of edges
public int getNumberOfEdges() {
return numberOfEdges;
}
// Add vertex to graph, if its a valid vertex and not duplicate
public void addVertex(T V) {
if(hasVertex(V)) throw new IllegalArgumentException("Vertex Duplicate");
if(ValidateVertex(V)) {
adjList.put(V, new LinkedList<T>());
numberOfVertices += 1;
}
}
// List of T objects
public void addVertices(List<T> vertices) {
graph = new Graph();
for(T vertex:vertices) {
graph.addVertex(vertex);
}
}
// Array of T objects
public void addVertices(T[] vertices) {
graph = new Graph();
for(T vertex: vertices) {
graph.addVertex(vertex);
}
}
// Checks if the vertex is present in the graph or not
public boolean hasVertex(T V) {
return adjList.containsKey(V);
}
// Validates a given vertex
private boolean ValidateVertex(T V) {
if(V == null || (int)V < 0) throw new IllegalArgumentException("Vertex "+ V + "is not valid");
return true;
}
// Checks if there is a edge between vertex1 and vertex2
public boolean hasEdge(T Vertex1, T Vertex2) {
ValidateVertex(Vertex1);
ValidateVertex(Vertex2);
return adjList.get(Vertex1).contains(Vertex2);
}
// Adds edge between vertex1 and vertex2, if there is no edge and also if the vertexes are valid
public void addEdge(T Vertex1, T Vertex2) {
if(!hasVertex(Vertex1)) addVertex(Vertex1);
if(!hasEdge(Vertex1, Vertex2))
adjList.get(Vertex1).add(Vertex2);
numberOfEdges+=1;
}
// Retures a collection, LinkedList containing neighbour vertices of given vertex
public LinkedList<T> getNeighbours(T Vertex) {
ValidateVertex(Vertex);
if(adjList.isEmpty()) throw new IllegalArgumentException("The graph is emppty, Add some vertices and edges ");
return adjList.get(Vertex);
}
// Checks if the given vertices are neighbours
public boolean isNeighbour(T Vertex1, T Vertex2) {
return hasEdge(Vertex1, Vertex2);
}
// Removes a vertex from the graph, and returns that element
public T removeVertex(T Vertex1) {
if(!hasVertex(Vertex1)) throw new IllegalArgumentException("No such Vertex "+ Vertex1+" to remove");
T remove_element = Vertex1;
adjList.remove(Vertex1);
numberOfVertices-= 1;
return remove_element;
}
// Removes a edge from the graph , if there is one
public void removeEdge(T Vertex1, T Vertex2) {
if(!hasEdge(Vertex1,Vertex2))throw new IllegalArgumentException("No Edge bewteen the vertices to remove");
adjList.remove(Vertex1, Vertex2);
numberOfEdges-= 1;
}
// Returns the size of the graph , which is number of vertices
public int size() {
return numberOfVertices;
}
// Returns an iterator, to iterate over the vertices
@Override
public Iterator<T> iterator() {
return adjList.keySet().iterator();
}
// String reprsentation of the graph
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(T Vertex: adjList.keySet()) {
sb.append(Vertex.toString()+ " -> ");
for(T neighbourVertex: adjList.get(Vertex))
sb.append(neighbourVertex.toString()+ " ");
sb.append("\n");
}
return sb.toString();
}
使用上述图形类
public class BreadthFirstSearch {
public void breadthFirstSearch(Graph graph, int source) {
Queue<Integer> vertices_q = new PriorityQueue<Integer>();
Map<Integer,Boolean> visited = new HashMap<Integer,Boolean>();
vertices_q.add(source);
System.out.print(source+" ");
while(!vertices_q.isEmpty()) {
int vertex = vertices_q.poll();
当我尝试时,下面的行显示错误,不兼容的类型 使用LinkedList邻居= graph.getneighbours(vertex); 然后像(int neighbour:neighbours)那样遍历邻居 效果很好
for(Integer neighbour: graph.getNeighbours(vertex)){
}
}
}
public static void main(String[] args) {
BreadthFirstSearch bfs = new BreadthFirstSearch();
Graph<Integer> graph = new Graph<Integer>();
int[] vertices = new int[]{1,2,3,4,5};
graph.addVertices(Arrays.stream(vertices).boxed().collect(Collectors.toList()));
graph.addEdge(1,2);graph.addEdge(2,1);
graph.addEdge(1,5);graph.addEdge(5,1);
graph.addEdge(2,5);graph.addEdge(5,2);
graph.addEdge(2,3);graph.addEdge(3,2);
graph.addEdge(2,4);graph.addEdge(4,2);
graph.addEdge(3,4);graph.addEdge(4,3);
graph.addEdge(4,5);graph.addEdge(5,4);
// Bread First Search
bfs.breadthFirstSearch(graph,1);
}
}