Skip to content

Commit

Permalink
adds iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinromero committed May 15, 2023
1 parent fa6bd86 commit ebeef75
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 12 deletions.
2 changes: 1 addition & 1 deletion iterator/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
.idea/
out
25 changes: 14 additions & 11 deletions iterator/src/MainGrafo2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}

}

Expand Down
11 changes: 11 additions & 0 deletions iterator/src/grafo/dirigido/Grafo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -510,5 +514,12 @@ public Grafo<T> getSubGrafo(T carga){
return grafo;
}

public IteratorInterface<T> getBFSIterator(T carga) {
return new BFSIterator<T>(this.vertices, this.arestas, carga);
}

public IteratorInterface<T> getDFSIterator(T carga) {
return new DFSIterator<T>(this.vertices, this.arestas, carga);
}

}
67 changes: 67 additions & 0 deletions iterator/src/iterator/BFSIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package iterator;

import grafo.dirigido.Aresta;
import grafo.dirigido.Vertice;

import java.util.*;

public class BFSIterator<T> implements IteratorInterface{

private final List<Vertice<T>> vertices;
private final List<Aresta<T>> edges;
private final Queue<Vertice<T>> queue = new LinkedList<>();
private final Set<Vertice<T>> visited = new HashSet<>();

public BFSIterator(List<Vertice<T>> vertices, List<Aresta<T>> arestas, T id) {
this.edges = arestas;
this.vertices = vertices;
Vertice<T> 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<T> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Vertice<T> next = queue.remove();
for (Vertice<T> adjacent : this.getAdjacent(next)){
if(!this.visited.contains(adjacent)){
this.queue.add(adjacent);
this.visited.add(adjacent);
}
}
return next;
}

private Vertice<T> getVertice(T data){

for (Vertice<T> u : vertices) {
if ( u.getCarga().equals( data ))
return u;
}
return null;
}

private List<Vertice<T>> getAdjacent( Vertice<T> vertice ){
List<Vertice<T>> vertex = new ArrayList<>();

for(Aresta<T> arco: edges){
if( arco.getOrigem().equals(vertice))
vertex.add((Vertice<T>) arco.getDestino());
}

return vertex;
}
}
69 changes: 69 additions & 0 deletions iterator/src/iterator/DFSIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package iterator;

import grafo.dirigido.Aresta;
import grafo.dirigido.Vertice;

import java.util.*;

public class DFSIterator<T> implements IteratorInterface{

private final List<Vertice<T>> vertices;
private final List<Aresta<T>> edges;
private final Stack<Vertice<T>> stack = new Stack<>();
private final Set<Vertice<T>> visited = new HashSet<>();

public DFSIterator(List<Vertice<T>> vertices, List<Aresta<T>> edges, T data) {
this.edges = edges;
this.vertices = vertices;
Vertice<T> 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<T> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Vertice<T> next = stack.pop();

for (Vertice<T> adjacent : this.getAdjacent(next)){
if(!this.visited.contains(adjacent)){
this.stack.add(adjacent);
this.visited.add(adjacent);
}
}

return next;
}

private Vertice<T> getVertice(T carga){

for (Vertice<T> u : vertices) {
if ( u.getCarga().equals( carga ))
return u;
}
return null;
}

private List<Vertice<T>> getAdjacent(Vertice<T> vertice ){
List<Vertice<T>> vertex = new ArrayList<>();

for(Aresta<T> arco: edges){
if( arco.getOrigem().equals(vertice))
vertex.add(arco.getDestino());
}

return vertex;
}
}
6 changes: 6 additions & 0 deletions iterator/src/iterator/IteratorInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package iterator;

public interface IteratorInterface<E> {
boolean hasNext();
E next();
}

0 comments on commit ebeef75

Please sign in to comment.