Course work - graph visualizer. I am writing in Swing. To represent the graph using JUNG. Here is the search code in width (a separate class is made so that you can work with the algorithm step by step, but with a little multithread sign is familiar).

public class BFS { public enum Color { WHITE, GRAY, BLACK } private Graph graph; private Queue<Vertex> queue; private List<Vertex> adjacencyList; private Vertex u, v; private Iterator<Vertex> iter; public BFS(Graph graph) { this.graph = graph; queue = new LinkedList<>(); } public void solution(Vertex firstVertex) { for (Vertex vertex : graph.getVertices()) { vertex.setDistance(-1); vertex.setColor(Color.WHITE); } u = firstVertex; u.setColor(Color.GRAY); u.setDistance(0); queue.add(u); while (!queue.isEmpty()) { u = queue.remove(); for (Iterator<Vertex> i = graph.getSuccessors(u).iterator(); i.hasNext(); ) { v = i.next(); if (v.getColor() == Color.WHITE) { v.setColor(Color.GRAY); v.setDistance(u.getDistance() + 1); queue.add(v); } } u.setColor(Color.BLACK); } } } 

Extra tried to remove. Search does not work. At all. Sometimes he paints some vertices. The first run is the same for each vertex. But if you run a search from the top, from which it was previously launched, the result is different than it was before. It is also interesting that when replacing a color test with another, which should give an identical result, for example: if (v.getDistance() < 0) , or store a dictionary of vertices and boolean values ​​that indicate whether the corresponding vertex was previously visited, the result is different. If you make more significant structural changes (keep your list of vertices, and your adjacency lists) that should give the same results, they give different results, but they are still incorrect. According to the results: in the process of the algorithm, one vertex is processed several times.

    0