-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa6bd86
commit ebeef75
Showing
6 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.idea | ||
.idea/ | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package iterator; | ||
|
||
public interface IteratorInterface<E> { | ||
boolean hasNext(); | ||
E next(); | ||
} |