diff --git a/iterator/.gitignore b/iterator/.gitignore index 9fb18b4..ed378d1 100644 --- a/iterator/.gitignore +++ b/iterator/.gitignore @@ -1,2 +1,2 @@ -.idea +.idea/ out diff --git a/iterator/src/MainGrafo2.java b/iterator/src/MainGrafo2.java index 34de84f..be033b7 100644 --- a/iterator/src/MainGrafo2.java +++ b/iterator/src/MainGrafo2.java @@ -5,6 +5,7 @@ import grafo.dirigido.Aresta; import grafo.dirigido.Grafo; import grafo.dirigido.Vertice; +import iterator.IteratorInterface; import model.Aluno; public class MainGrafo2 { @@ -76,17 +77,19 @@ public static void main(String args[]){ g.addAresta("4", "3", 1); g.addAresta("5", "2", 1); g.addAresta("5", "1", 1); - g.addAresta("1", "2", 1); - - g.BFS("6"); - g.BFSDistance("5"); - - System.out.println(); - g.DFS("6"); - - - g.clear(); - System.out.println("Grafo foi esvaziado: " + g); + g.addAresta("1", "2", 1); + + IteratorInterface it = g.getBFSIterator("6"); + System.out.println("BFS:"); + while (it.hasNext()) {Van + System.out.println(it.next()); + } + + it = g.getDFSIterator("6"); + System.out.println("DFS:"); + while (it.hasNext()) { + System.out.println(it.next()); + } } diff --git a/iterator/src/grafo/dirigido/Grafo.java b/iterator/src/grafo/dirigido/Grafo.java index 3673677..ef80288 100644 --- a/iterator/src/grafo/dirigido/Grafo.java +++ b/iterator/src/grafo/dirigido/Grafo.java @@ -1,6 +1,10 @@ package grafo.dirigido; +import iterator.BFSIterator; +import iterator.DFSIterator; +import iterator.IteratorInterface; + import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -510,5 +514,12 @@ public Grafo getSubGrafo(T carga){ return grafo; } + public IteratorInterface getBFSIterator(T carga) { + return new BFSIterator(this.vertices, this.arestas, carga); + } + + public IteratorInterface getDFSIterator(T carga) { + return new DFSIterator(this.vertices, this.arestas, carga); + } } diff --git a/iterator/src/iterator/BFSIterator.java b/iterator/src/iterator/BFSIterator.java new file mode 100644 index 0000000..c277631 --- /dev/null +++ b/iterator/src/iterator/BFSIterator.java @@ -0,0 +1,67 @@ +package iterator; + +import grafo.dirigido.Aresta; +import grafo.dirigido.Vertice; + +import java.util.*; + +public class BFSIterator implements IteratorInterface{ + + private final List> vertices; + private final List> edges; + private final Queue> queue = new LinkedList<>(); + private final Set> visited = new HashSet<>(); + + public BFSIterator(List> vertices, List> arestas, T id) { + this.edges = arestas; + this.vertices = vertices; + Vertice v = getVertice(id); + + if(v != null){ + this.queue.add(v); + this.visited.add(v); + } else { + throw new NoSuchElementException(); + } + + } + @Override + public boolean hasNext() { + return !queue.isEmpty(); + } + + @Override + public Vertice next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + Vertice next = queue.remove(); + for (Vertice adjacent : this.getAdjacent(next)){ + if(!this.visited.contains(adjacent)){ + this.queue.add(adjacent); + this.visited.add(adjacent); + } + } + return next; + } + + private Vertice getVertice(T data){ + + for (Vertice u : vertices) { + if ( u.getCarga().equals( data )) + return u; + } + return null; + } + + private List> getAdjacent( Vertice vertice ){ + List> vertex = new ArrayList<>(); + + for(Aresta arco: edges){ + if( arco.getOrigem().equals(vertice)) + vertex.add((Vertice) arco.getDestino()); + } + + return vertex; + } +} \ No newline at end of file diff --git a/iterator/src/iterator/DFSIterator.java b/iterator/src/iterator/DFSIterator.java new file mode 100644 index 0000000..396f617 --- /dev/null +++ b/iterator/src/iterator/DFSIterator.java @@ -0,0 +1,69 @@ +package iterator; + +import grafo.dirigido.Aresta; +import grafo.dirigido.Vertice; + +import java.util.*; + +public class DFSIterator implements IteratorInterface{ + + private final List> vertices; + private final List> edges; + private final Stack> stack = new Stack<>(); + private final Set> visited = new HashSet<>(); + + public DFSIterator(List> vertices, List> edges, T data) { + this.edges = edges; + this.vertices = vertices; + Vertice v = getVertice(data); + + if(v != null){ + this.stack.add(v); + this.visited.add(v); + } else { + throw new NoSuchElementException(); + } + + } + @Override + public boolean hasNext() { + return !stack.isEmpty(); + } + + @Override + public Vertice next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + Vertice next = stack.pop(); + + for (Vertice adjacent : this.getAdjacent(next)){ + if(!this.visited.contains(adjacent)){ + this.stack.add(adjacent); + this.visited.add(adjacent); + } + } + + return next; + } + + private Vertice getVertice(T carga){ + + for (Vertice u : vertices) { + if ( u.getCarga().equals( carga )) + return u; + } + return null; + } + + private List> getAdjacent(Vertice vertice ){ + List> vertex = new ArrayList<>(); + + for(Aresta arco: edges){ + if( arco.getOrigem().equals(vertice)) + vertex.add(arco.getDestino()); + } + + return vertex; + } +} \ No newline at end of file diff --git a/iterator/src/iterator/IteratorInterface.java b/iterator/src/iterator/IteratorInterface.java new file mode 100644 index 0000000..c2432b1 --- /dev/null +++ b/iterator/src/iterator/IteratorInterface.java @@ -0,0 +1,6 @@ +package iterator; + +public interface IteratorInterface { + boolean hasNext(); + E next(); +}