mac->arch
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
package network;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
public interface Graph<Vertex, Wheight> extends Iterable<Vertex> {
|
||||
|
||||
public boolean containsVertex(Vertex vertex);
|
||||
|
||||
public boolean addVertex(Vertex vertex);
|
||||
|
||||
public boolean removeVertex(Vertex vertex);
|
||||
|
||||
public int edgeSize();
|
||||
|
||||
public Wheight getEdgeWheight(Vertex v1, Vertex v2);
|
||||
|
||||
public boolean containsEdge(Vertex v1, Vertex v2);
|
||||
|
||||
public boolean addEdge(Vertex v1, Vertex v2, Double weight);
|
||||
|
||||
public boolean removeEdge(Vertex v1, Vertex v2);
|
||||
|
||||
public boolean isEmpty();
|
||||
|
||||
public int size();
|
||||
|
||||
public Set<Vertex> neighbors (Vertex v);
|
||||
|
||||
public Iterator<Vertex> iterator();
|
||||
|
||||
public Iterator<Vertex> breadthFirstIterator(Vertex v);
|
||||
|
||||
public Iterator<Vertex> depthFisrtIterator (Vertex v);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package network;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
public interface UnweightGraph<Vertex> extends Iterable<Vertex> {
|
||||
|
||||
// FATTO
|
||||
public boolean containsVertex(Vertex vertex);
|
||||
|
||||
// FATTO
|
||||
public boolean addVertex(Vertex vertex);
|
||||
|
||||
// FATTO
|
||||
public boolean removeVertex(Vertex vertex);
|
||||
|
||||
// FATTO
|
||||
public int edgeSize();
|
||||
|
||||
// FATTO
|
||||
public boolean containsEdge(Vertex v1, Vertex v2);
|
||||
|
||||
// FATTO
|
||||
public boolean addEdge(Vertex v1, Vertex v2);
|
||||
|
||||
// FATTO
|
||||
public boolean removeEdge(Vertex v1, Vertex v2);
|
||||
|
||||
// FATTO
|
||||
public boolean isEmpty();
|
||||
|
||||
// FATTO
|
||||
public int size();
|
||||
|
||||
// FATTO
|
||||
public Set<Vertex> neighbors (Vertex v);
|
||||
|
||||
// FATTO
|
||||
public Iterator<Vertex> iterator();
|
||||
|
||||
// FATTO
|
||||
public Iterator<Vertex> breadthFirstIterator (Vertex v);
|
||||
|
||||
|
||||
public Iterator<Vertex> depthFisrtIterator (Vertex v);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
package network.network;
|
||||
|
||||
import network.Graph;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Network<Vertex extends Comparable<? super Vertex>> implements Graph<Vertex, Double>{
|
||||
|
||||
protected TreeMap<Vertex, TreeMap<Vertex, Double>> adjacencyMap;
|
||||
|
||||
public Network() {
|
||||
this.adjacencyMap = new TreeMap<Vertex, TreeMap<Vertex, Double>>();
|
||||
}
|
||||
|
||||
public Network(Network<Vertex> network) {
|
||||
this.adjacencyMap = new TreeMap<Vertex, TreeMap<Vertex, Double>>(network.adjacencyMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (object == null) return false;
|
||||
if (object == this) return true;
|
||||
if (!(object instanceof Network)) return false;
|
||||
|
||||
Network<?> network = (Network<?>) object;
|
||||
|
||||
return adjacencyMap.equals(network.adjacencyMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsVertex(Vertex vertex) {
|
||||
return adjacencyMap.containsKey(vertex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addVertex(Vertex vertex) {
|
||||
// Null Check
|
||||
if (vertex == null) throw new NullPointerException();
|
||||
// Se l'elemento è già presente
|
||||
if (containsVertex(vertex)) return false;
|
||||
// Inserimento
|
||||
adjacencyMap.put(vertex, new TreeMap<Vertex, Double>());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeVertex(Vertex vertex) {
|
||||
// Null check
|
||||
if (vertex == null) throw new NullPointerException();
|
||||
// Se l'elemento non è presente
|
||||
if (!containsVertex(vertex)) return false;
|
||||
// Rimozione del vertice dalle liste di adiacenza
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
adjacencyMap.get(v).remove(vertex);
|
||||
}
|
||||
// Rimozione del vertice dalle chiavi
|
||||
adjacencyMap.remove(vertex);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Restituisce il numero di archi presenti all'interno del grafo
|
||||
*/
|
||||
@Override
|
||||
public int edgeSize() {
|
||||
// Variabile contatore
|
||||
int edges = 0;
|
||||
// Conto degli archi
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
edges += adjacencyMap.get(v).size();
|
||||
}
|
||||
return edges;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getEdgeWheight(Vertex v1, Vertex v2) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null) throw new NullPointerException();
|
||||
// Verifichiamo se ambedue sono presenti nel grafo, se non ci sono allora return null.
|
||||
if (!(containsVertex(v1) && containsVertex(v2))) return null;
|
||||
// Otteniamo la lista di adiacenza
|
||||
Map<Vertex, Double> tmp = adjacencyMap.get(v1);
|
||||
// Se il vertex2 non presenta il vertice2 allora non esiste il peso
|
||||
if (!tmp.containsKey(v2)) return null;
|
||||
return tmp.get(v2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsEdge(Vertex v1, Vertex v2) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null) throw new NullPointerException();
|
||||
// Verifichiamo che ambedue siano nel grafico
|
||||
if (!(containsVertex(v1) && containsVertex(v2))) return false;
|
||||
// Otteniamo la lista di adiacenza
|
||||
Map<Vertex, Double> tmp = adjacencyMap.get(v1);
|
||||
return tmp.containsKey(v2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addEdge(Vertex v1, Vertex v2, Double weight) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null || weight == null) throw new NullPointerException();
|
||||
// Verifichiamo che ambedue siano nel grafico
|
||||
if (!(containsVertex(v1) && containsVertex(v2))) return false;
|
||||
// Verifichiamo che non esista l'arco. Se esiste allora non fa nulla. Se non esiste lo aggiunge.
|
||||
if (containsEdge(v1, v2)) return false;
|
||||
// Aggiunta dell'edge
|
||||
Map<Vertex, Double> tmp = adjacencyMap.get(v1);
|
||||
tmp.put(v2, weight);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeEdge(Vertex v1, Vertex v2) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null) throw new NullPointerException();
|
||||
// Verifichiamo che ambedue siano nel grafico
|
||||
if (!(containsVertex(v1) && containsVertex(v2))) return false;
|
||||
// Verifichiamo se l'arco esiste oppure no.
|
||||
if (!containsEdge(v1, v2)) return false;
|
||||
// Rimozione dell'edge
|
||||
Map<Vertex, Double> tmp = adjacencyMap.get(v1);
|
||||
tmp.remove(v2);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return adjacencyMap.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return adjacencyMap.keySet().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Vertex> neighbors(Vertex v) {
|
||||
// Null check
|
||||
if (v == null) throw new NullPointerException();
|
||||
// Presenza del vertice nel grafo
|
||||
if (!containsVertex(v)) return null;
|
||||
// Se il vertice è presente, allora restituisco il tutto
|
||||
return new TreeSet<Vertex>(adjacencyMap.get(v).keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Vertex> iterator() {
|
||||
return new NetworkIterator();
|
||||
}
|
||||
|
||||
protected class NetworkIterator implements Iterator<Vertex> {
|
||||
|
||||
// Variabili
|
||||
protected Iterator<Vertex> iterator;
|
||||
protected Vertex currentVertex;
|
||||
|
||||
// Costruttore
|
||||
public NetworkIterator() {
|
||||
this.iterator = adjacencyMap.keySet().iterator();
|
||||
}
|
||||
|
||||
// hasNext
|
||||
public boolean hasNext() {
|
||||
return this.iterator.hasNext();
|
||||
}
|
||||
|
||||
// next
|
||||
public Vertex next() {
|
||||
this.currentVertex = iterator.next();
|
||||
return this.currentVertex;
|
||||
}
|
||||
|
||||
// remove
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Vertex> breadthFirstIterator(Vertex vertex) {
|
||||
if (!this.adjacencyMap.containsKey(vertex)) throw new IllegalArgumentException();
|
||||
return new BreadthFirstIterator(vertex);
|
||||
}
|
||||
|
||||
protected class BreadthFirstIterator implements Iterator<Vertex> {
|
||||
|
||||
// Variabili
|
||||
protected Queue<Vertex> queue;
|
||||
protected HashMap<Vertex, Boolean> research;
|
||||
protected Vertex currentVertex;
|
||||
|
||||
// Costruttore
|
||||
public BreadthFirstIterator(Vertex vertex) {
|
||||
this.queue = new LinkedList<Vertex>();
|
||||
this.research = new HashMap<Vertex, Boolean>();
|
||||
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
research.put(v, false);
|
||||
}
|
||||
|
||||
queue.add(vertex);
|
||||
research.put(vertex, true);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return !(queue.isEmpty());
|
||||
}
|
||||
|
||||
public Vertex next() {
|
||||
currentVertex = queue.remove();
|
||||
|
||||
Set<Vertex> neighborsSet = adjacencyMap.get(currentVertex).keySet();
|
||||
|
||||
for (Vertex v : neighborsSet) {
|
||||
if (research.get(v)) continue;
|
||||
research.put(v, true);
|
||||
queue.add(v);
|
||||
}
|
||||
|
||||
return currentVertex;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Vertex> depthFisrtIterator(Vertex v) {
|
||||
return new DepthFirstIterator(v);
|
||||
}
|
||||
|
||||
protected class DepthFirstIterator implements Iterator<Vertex>{
|
||||
|
||||
// Variabili
|
||||
protected Stack<Vertex> stack = new Stack<Vertex>();
|
||||
protected HashMap<Vertex, Boolean> reached;
|
||||
Vertex current;
|
||||
|
||||
// Costruttore
|
||||
public DepthFirstIterator(Vertex vertex) {
|
||||
this.stack = new Stack<Vertex>();
|
||||
this.reached = new HashMap<Vertex, Boolean>();
|
||||
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
this.reached.put(v, false);
|
||||
}
|
||||
|
||||
this.stack.add(vertex);
|
||||
this.reached.put(vertex, true);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return !stack.isEmpty();
|
||||
}
|
||||
|
||||
public Vertex next() {
|
||||
this.current = this.stack.pop();
|
||||
|
||||
Set<Vertex> neighborsSet = adjacencyMap.get(this.current).keySet();
|
||||
for (Vertex v : neighborsSet) {
|
||||
if (this.reached.get(v)) {
|
||||
if (this.stack.remove(v)) stack.add(v);
|
||||
} else {
|
||||
this.stack.add(v);
|
||||
this.reached.put(v, true);
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ESERCIZI
|
||||
|
||||
/*
|
||||
* Un grafo è detto fortementeConnesso quando per ogni vertice
|
||||
* si può raggiungere un qualsiasi altro vertice del grafo
|
||||
*/
|
||||
public boolean isStronglyConnected() {
|
||||
for (Vertex SS : adjacencyMap.keySet()) {
|
||||
Iterator<Vertex> it = this.breadthFirstIterator(SS);
|
||||
int counter = 0;
|
||||
while (it.hasNext()) {
|
||||
counter++;
|
||||
it.next();
|
||||
}
|
||||
if (counter < adjacencyMap.size()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package network.network;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import network.UnweightGraph;
|
||||
|
||||
public class UnweightNetwork<Vertex> implements UnweightGraph<Vertex> {
|
||||
|
||||
// Lista di adiacenza
|
||||
private HashMap<Vertex, HashSet<Vertex>> adjacencyMap;
|
||||
|
||||
// Costruttore
|
||||
public UnweightNetwork() {
|
||||
adjacencyMap = new HashMap<Vertex, HashSet<Vertex>>();
|
||||
}
|
||||
|
||||
public UnweightNetwork(UnweightNetwork<Vertex> network) {
|
||||
adjacencyMap = new HashMap<Vertex, HashSet<Vertex>>(network.adjacencyMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (object == null) return false;
|
||||
if (object == this) return true;
|
||||
if (!(object instanceof UnweightNetwork<?>)) return false;
|
||||
|
||||
UnweightNetwork<?> unweightNetwork = (UnweightNetwork<?>) object;
|
||||
|
||||
return this.adjacencyMap.equals(unweightNetwork.adjacencyMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.adjacencyMap.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.adjacencyMap.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int edgeSize() {
|
||||
int counter = 0;
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
HashSet<Vertex> neighbours = adjacencyMap.get(v);
|
||||
counter += neighbours.size();
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsVertex(Vertex vertex) {
|
||||
return adjacencyMap.containsKey(vertex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addVertex(Vertex vertex) {
|
||||
// Null check
|
||||
if (vertex == null) throw new NullPointerException();
|
||||
// Controllo la presenza di un vertice già presente all'interno del grafo
|
||||
if (containsVertex(vertex)) return false;
|
||||
// Aggiunta di un vertice e della sua lista di adiacenza
|
||||
HashSet<Vertex> vertexAdjacencyMap = new HashSet<Vertex>();
|
||||
adjacencyMap.put(vertex, vertexAdjacencyMap);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeVertex(Vertex vertex) {
|
||||
// Null check
|
||||
if (vertex == null) throw new NullPointerException();
|
||||
// Controllo la presenza di un vertice già presente
|
||||
if (!(containsVertex(vertex))) return false;
|
||||
// Rimozione del vertice stesso
|
||||
adjacencyMap.remove(vertex);
|
||||
// Rimozione del vertice in altre liste di adiacenza
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
HashSet<Vertex> neighboorsAdjacencyMap = adjacencyMap.get(v);
|
||||
neighboorsAdjacencyMap.remove(vertex);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsEdge(Vertex v1, Vertex v2) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null) throw new NullPointerException();
|
||||
// Contorllo la presenza dei vertici che siano già esistenti.
|
||||
if (!((containsVertex(v1)) && (containsVertex(v2)))) return false;
|
||||
// Data la presenza dei due vertici continua pure il tutto.
|
||||
// Acquisiamo la lista di adiacenza del vertice 1
|
||||
HashSet<Vertex> neighboorsAdjacencyMap = adjacencyMap.get(v1);
|
||||
// Controlliamo che sia presente v2 nella lista di adiacenza di V1
|
||||
return neighboorsAdjacencyMap.contains(v2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeEdge(Vertex v1, Vertex v2) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null) throw new NullPointerException();
|
||||
// Contorllo la presenza dei vertici che siano già esistenti.
|
||||
if (!((containsVertex(v1)) && (containsVertex(v2)))) return false;
|
||||
// Data la presenza dei due vertici continua pure il tutto.
|
||||
// Acquisiamo la lista di adiacenza del vertice 1
|
||||
HashSet<Vertex> neighboorsAdjacencyMap = adjacencyMap.get(v1);
|
||||
// Controlliamo che sia presente v2 nella lista di adiacenza di V1
|
||||
if (!neighboorsAdjacencyMap.contains(v2)) return false; // V2 non è contenuto nella lista di adicenza di v2 pertanto non può essere rimosso perchè l'arco non esiste
|
||||
// Data la presenza del vertice in v2, procediamo alla rimozione di v2.
|
||||
neighboorsAdjacencyMap.remove(v2);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addEdge(Vertex v1, Vertex v2) {
|
||||
// Null check
|
||||
if (v1 == null || v2 == null) throw new NullPointerException();
|
||||
// Controllo la presenza dei due vertici
|
||||
if (!(containsVertex(v1) && containsVertex(v2))) return false;
|
||||
// Data la presenza dei due vertici continua ad aggiungere l'arco tra i due vertici
|
||||
// Arco v1 -> v2, quindi v2 deve essere aggiunto dentro alla lista di adiacenza di v1
|
||||
HashSet<Vertex> neighboorsAdjacencyMap = adjacencyMap.get(v1);
|
||||
return neighboorsAdjacencyMap.add(v2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Vertex> neighbors (Vertex v){
|
||||
// Null check
|
||||
if (v == null) throw new NullPointerException();
|
||||
// Verifichiamo che il vertice esista
|
||||
if (!(containsVertex(v))) return null;
|
||||
// Data la presenza di v1, si restituisca la lista di adiacenza
|
||||
return adjacencyMap.get(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Vertex> iterator(){
|
||||
return new UnweightNetworkIterator();
|
||||
}
|
||||
|
||||
protected class UnweightNetworkIterator implements Iterator<Vertex> {
|
||||
|
||||
// VARIABILI
|
||||
private Vertex currentVertex;
|
||||
private Iterator<Vertex> iterator;
|
||||
|
||||
// Costruttore
|
||||
public UnweightNetworkIterator() {
|
||||
iterator = adjacencyMap.keySet().iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vertex next() {
|
||||
currentVertex = iterator.next();
|
||||
return currentVertex;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Vertex> breadthFirstIterator (Vertex vertex){
|
||||
if (!this.adjacencyMap.containsKey(vertex)) throw new IllegalArgumentException();
|
||||
return new BreadthFirstIterator(vertex);
|
||||
}
|
||||
|
||||
protected class BreadthFirstIterator implements Iterator<Vertex> {
|
||||
|
||||
// Variabili
|
||||
protected Queue<Vertex> queue;
|
||||
protected HashMap<Vertex, Boolean> research;
|
||||
protected Vertex currentVertex;
|
||||
|
||||
// Costruttore
|
||||
public BreadthFirstIterator(Vertex vertex) {
|
||||
this.queue = new LinkedList<Vertex>();
|
||||
this.research = new HashMap<Vertex, Boolean>();
|
||||
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
this.research.put(v, false);
|
||||
}
|
||||
|
||||
this.queue.add(vertex);
|
||||
this.research.put(vertex, true);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return !this.queue.isEmpty();
|
||||
}
|
||||
|
||||
public Vertex next() {
|
||||
|
||||
currentVertex = this.queue.remove();
|
||||
|
||||
Set<Vertex> neighborsSet = adjacencyMap.get(currentVertex);
|
||||
|
||||
for (Vertex v : neighborsSet) {
|
||||
if (this.research.get(v)) continue;
|
||||
this.research.put(v, true);
|
||||
queue.add(v);
|
||||
}
|
||||
|
||||
return currentVertex;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Iterator<Vertex> depthFisrtIterator (Vertex v) {
|
||||
if (!this.adjacencyMap.containsKey(v)) throw new IllegalArgumentException();
|
||||
return null; //TODO
|
||||
}
|
||||
|
||||
protected class DepthFirstIterator implements Iterator<Vertex>{
|
||||
|
||||
// Variabili
|
||||
protected Stack<Vertex> stack = new Stack<Vertex>();
|
||||
protected HashMap<Vertex, Boolean> reached;
|
||||
Vertex current;
|
||||
|
||||
// Costruttore
|
||||
public DepthFirstIterator(Vertex vertex) {
|
||||
this.stack = new Stack<Vertex>();
|
||||
this.reached = new HashMap<Vertex, Boolean>();
|
||||
|
||||
for (Vertex v : adjacencyMap.keySet()) {
|
||||
this.reached.put(v, false);
|
||||
}
|
||||
|
||||
this.stack.add(vertex);
|
||||
this.reached.put(vertex, true);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return !stack.isEmpty();
|
||||
}
|
||||
|
||||
public Vertex next() {
|
||||
this.current = this.stack.pop();
|
||||
|
||||
Set<Vertex> neighborsSet = adjacencyMap.get(this.current);
|
||||
for (Vertex v : neighborsSet) {
|
||||
if (this.reached.get(v)) {
|
||||
if (this.stack.remove(v)) stack.add(v);
|
||||
} else {
|
||||
this.stack.add(v);
|
||||
this.reached.put(v, true);
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package network.undirected_network;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import network.network.Network;
|
||||
|
||||
public class UndirectedNetwork<Vertex extends Comparable<? super Vertex>> extends Network<Vertex> {
|
||||
|
||||
public UndirectedNetwork() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UndirectedNetwork(UndirectedNetwork<Vertex> network) {
|
||||
super(network);
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
if (object == null) return false;
|
||||
if (object == this) return true;
|
||||
if (!(object instanceof UndirectedNetwork)) return false;
|
||||
|
||||
UndirectedNetwork<?> undirectedNetwork = (UndirectedNetwork<?>) object;
|
||||
|
||||
return this.adjacencyMap.equals(undirectedNetwork.adjacencyMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addEdge(Vertex v1, Vertex v2, Double weight) {
|
||||
return super.addEdge(v1, v2, weight) && super.addEdge(v2, v1, weight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeEdge(Vertex v1, Vertex v2) {
|
||||
return super.removeEdge(v1, v2) && super.removeEdge(v2, v1);
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
Vertex v = adjacencyMap.firstKey();
|
||||
Iterator<Vertex> it = new BreadthFirstIterator(v);
|
||||
int counter = 0;
|
||||
while (it.hasNext()) counter++;
|
||||
if (counter < adjacencyMap.size()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package parziale.p231110;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Esercizio {
|
||||
|
||||
/*
|
||||
* Scrivere un metodo generico statico che prende in input
|
||||
* un array di elementi e restituisce il
|
||||
* numero di oggetti presenti all’interno dell’array che
|
||||
* non presentano duplicazioni. Quindi
|
||||
* utilizzare il metodo sul tipo Integer.
|
||||
* Esempio: se l’array fosse {10, 6, 5, 6, 7, 8, 6, 8, 10, 12, 10},
|
||||
* il metodo dovrebbe restituire
|
||||
* l’intero 3 (in grassetto i 3 elementi che non presentano duplicazioni).
|
||||
*/
|
||||
|
||||
public static int numeroOccorrenzeSingole(int[] array) {
|
||||
if (array == null) throw new NullPointerException();
|
||||
Map<Integer, Integer> lista = new HashMap<Integer, Integer>();
|
||||
for (Integer i : array) {
|
||||
if (lista.get(i) == null) lista.put(i, 1);
|
||||
else lista.put(i, lista.get(i) + 1);
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
for (Integer i : lista.keySet()) {
|
||||
if (lista.get(i) == 1) counter++;
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package parziale.p231110;
|
||||
|
||||
import java.lang.Integer;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class Libreria {
|
||||
|
||||
/*
|
||||
* Libreria che rappresenta una libreria
|
||||
* come una lista di libri senza duplicati e
|
||||
* supporta le seguenti operazioni.
|
||||
*/
|
||||
|
||||
Set<Libro> libreria = new HashSet<Libro>();
|
||||
|
||||
/*
|
||||
* Metodo n.1
|
||||
* inserire nella libreria un nuovo libro, se non è già presente,
|
||||
* dati in input l’ISBN e l’anno
|
||||
* di pubblicazione;
|
||||
*/
|
||||
|
||||
public boolean aggiungiLibro(Libro libro) {
|
||||
if (libro == null) throw new NullPointerException();
|
||||
return libreria.add(libro);
|
||||
}
|
||||
|
||||
/*
|
||||
* verificare se la libreria contiene un libro,
|
||||
* dato in input il suo ISBN;
|
||||
*/
|
||||
|
||||
public boolean contieneLibro(String ISBN) {
|
||||
return libreria.contains(new Libro(ISBN, 0));
|
||||
}
|
||||
|
||||
/*
|
||||
* cancellare dalla libreria un libro, se presente,
|
||||
* dato in input il suo ISBN;
|
||||
*/
|
||||
|
||||
public boolean rimuoviLibro(String ISBN) {
|
||||
return libreria.remove(new Libro(ISBN, 0));
|
||||
}
|
||||
|
||||
/*
|
||||
* ordinare la lista dei libri in ordine
|
||||
* crescente rispetto al codice ISBN;
|
||||
*/
|
||||
|
||||
public List<Libro> listaOrdinata() {
|
||||
List<Libro> listaOrdinata = new ArrayList<Libro>(libreria);
|
||||
if (libreria.isEmpty()) return listaOrdinata;
|
||||
Collections.sort(listaOrdinata, new Comparator<Libro>() {
|
||||
@Override
|
||||
public int compare(Libro l1, Libro l2) {
|
||||
String ISBN1 = l1.ISBN();
|
||||
String ISBN2 = l2.ISBN();
|
||||
|
||||
return ISBN1.compareTo(ISBN2);
|
||||
}
|
||||
});
|
||||
return listaOrdinata;
|
||||
}
|
||||
|
||||
/*
|
||||
* ordinare la lista dei libri in ordine decrescente
|
||||
* rispetto all’anno di prima pubblicazione;
|
||||
*/
|
||||
|
||||
public List<Libro> listaOrdinataPerAnnoDec() {
|
||||
List<Libro> listaOrdinata = new ArrayList<Libro>(libreria);
|
||||
if (libreria.isEmpty()) return listaOrdinata;
|
||||
Collections.sort(listaOrdinata, new Comparator<Libro>() {
|
||||
@Override
|
||||
public int compare(Libro l1, Libro l2) {
|
||||
int anno1 = l1.annoPubblicazione();
|
||||
int anno2 = l2.annoPubblicazione();
|
||||
|
||||
return Integer.compare(anno2, anno1);
|
||||
}
|
||||
});
|
||||
return listaOrdinata;
|
||||
}
|
||||
|
||||
/*
|
||||
* calcolare restituire in output, per ogni anno di
|
||||
* pubblicazione compreso tra il 2000 e il
|
||||
* 2023, il relativo numero di libri contenuti nella libreria.
|
||||
*/
|
||||
|
||||
public Map<Integer, Integer> numeroLibriTra2000e2023() {
|
||||
// Anno Libro (Integer), Numero di occorrenze (Integer)
|
||||
Map<Integer, Integer> mappaOrdinata = new TreeMap<Integer, Integer>();
|
||||
for (Libro libro : libreria) {
|
||||
int anno = libro.annoPubblicazione();
|
||||
if (!(anno >= 2000 && anno <= 2023)) continue;
|
||||
if (mappaOrdinata.get(anno) == null) mappaOrdinata.put(anno, 1);
|
||||
else mappaOrdinata.put(anno, 1 + mappaOrdinata.get(anno));
|
||||
}
|
||||
return mappaOrdinata;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package parziale.p231110;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Libro{
|
||||
|
||||
// Metodi di instanza
|
||||
private String ISBN;
|
||||
private int annoPubblicazione;
|
||||
|
||||
// Metodo costruttore
|
||||
public Libro(
|
||||
String ISBN,
|
||||
int annoPubblicazione) {
|
||||
this.ISBN = ISBN;
|
||||
this.annoPubblicazione = annoPubblicazione;
|
||||
}
|
||||
|
||||
// Metodi getter
|
||||
public String ISBN() {
|
||||
return ISBN;
|
||||
}
|
||||
|
||||
public int annoPubblicazione() {
|
||||
return annoPubblicazione;
|
||||
}
|
||||
|
||||
// Metodi di oggetto (fondamentali per HashSet usato in Libreria)
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
if (obj == this) return true;
|
||||
if (!(obj instanceof Libro)) return false;
|
||||
|
||||
Libro extObj = (Libro) obj;
|
||||
|
||||
return this.ISBN.equals(extObj.ISBN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(ISBN);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user