Comepletata la ListPartion aggiungendo alcuni metodi a OrderedListset

This commit is contained in:
2014-05-20 18:49:37 +02:00
parent 4420be5086
commit 75199b5c96
4 changed files with 215 additions and 68 deletions

View File

@@ -0,0 +1,48 @@
package com.xgiovio;
import partition.ListPartition;
import position.NodePositionList;
import set.OrderedListSet;
import set.Set;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 18/05/2014
* Time: 15:58
*/
public class ListPartitionTest {
public static void main(String[] args) {
ListPartition<String > a = new ListPartition<String>() ;
Set<String> a1 = a.makeSet("1");
System.out.println(a);
Set<String> a2 = a.makeSet("2");
System.out.println(a);
Set<String> a3 = a.makeSet("3");
System.out.println(a);
Set<String> a4 = a.makeSet("4");
System.out.println(a);
Set<String> out;
out = a.union(a1,a2);
System.out.println(a);
out = a.union(out,a3);
System.out.println(a);
out = a.union(out,a4);
System.out.println(a);
}
}

View File

@@ -15,25 +15,29 @@ import java.util.Iterator;
* @author Roberto Tamassia, Eric Zamore * @author Roberto Tamassia, Eric Zamore
*/ */
/*
public class AdjacencyListGraph<V,E> implements Graph<V,E> { public class AdjacencyListGraph<V,E> implements Graph<V,E> {
protected NodePositionList<Vertex<V>> VList; // container for vertices protected NodePositionList<Vertex<V>> VList; // container for vertices
protected NodePositionList<Edge<E>> EList; // container for edges protected NodePositionList<Edge<E>> EList; // container for edges
/** Default constructor that creates an empty graph */
// Default constructor that creates an empty graph
public AdjacencyListGraph() { public AdjacencyListGraph() {
VList = new NodePositionList<Vertex<V>>(); VList = new NodePositionList<Vertex<V>>();
EList = new NodePositionList<Edge<E>>(); EList = new NodePositionList<Edge<E>>();
} }
/** Return an iterator over the vertices of the graph */
// Return an iterator over the vertices of the graph
public Iterable<Vertex<V>> vertices() { public Iterable<Vertex<V>> vertices() {
return VList; return VList;
} }
/** Return an iterator over the edges of the graph */
// Return an iterator over the edges of the graph
public Iterable<Edge<E>> edges() { public Iterable<Edge<E>> edges() {
return EList; return EList;
} }
/** Replace the element a given position (vertex or edge) with a new
element and return the old element */ // Replace the element a given position (vertex or edge) with a new element and return the old element
public Object replace(Position p, Object o) public Object replace(Position p, Object o)
throws InvalidPositionException { throws InvalidPositionException {
MyPosition pp = checkPosition(p); MyPosition pp = checkPosition(p);
@@ -41,20 +45,23 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
pp.setElement(o); pp.setElement(o);
return temp; return temp;
} }
/** Return an iterator over the edges incident on a vertex */
// Return an iterator over the edges incident on a vertex
public Iterable<Edge<E>> incidentEdges(Vertex<V> v) public Iterable<Edge<E>> incidentEdges(Vertex<V> v)
throws InvalidPositionException { throws InvalidPositionException {
MyVertex<V> vv = checkVertex(v); MyVertex<V> vv = checkVertex(v);
return vv.incidentEdges(); return vv.incidentEdges();
} }
/** Return the endvertices of a edge in an array of length 2 */
// Return the endvertices of a edge in an array of length 2
public Vertex<V>[] endVertices(Edge<E> e) public Vertex<V>[] endVertices(Edge<E> e)
throws InvalidPositionException { throws InvalidPositionException {
MyEdge<E> ee = checkEdge(e); MyEdge<E> ee = checkEdge(e);
return ee.endVertices(); return ee.endVertices();
} }
/** Return the other endvertex of an incident edge */
// Return the other endvertex of an incident edge
public Vertex<V> opposite(Vertex<V> v, Edge<E> e) public Vertex<V> opposite(Vertex<V> v, Edge<E> e)
throws InvalidPositionException { throws InvalidPositionException {
checkVertex(v); checkVertex(v);
@@ -67,7 +74,8 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
else else
throw new InvalidPositionException("No such vertex exists"); throw new InvalidPositionException("No such vertex exists");
} }
/** Test whether two vertices are adjacent */
// Test whether two vertices are adjacent
public boolean areAdjacent(Vertex<V> u, Vertex<V> v) public boolean areAdjacent(Vertex<V> u, Vertex<V> v)
throws InvalidPositionException { throws InvalidPositionException {
// search the incidence list of the vertex with smaller degree // search the incidence list of the vertex with smaller degree
@@ -86,7 +94,8 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
} }
return false; return false;
} }
/** Insert and return a new vertex with a given element */
// Insert and return a new vertex with a given element
public Vertex<V> insertVertex(V o) { public Vertex<V> insertVertex(V o) {
MyVertex<V> vv = new MyVertex<V>(o); MyVertex<V> vv = new MyVertex<V>(o);
VList.addLast(vv); VList.addLast(vv);
@@ -94,8 +103,8 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
vv.setLocation(p); vv.setLocation(p);
return vv; return vv;
} }
/** Insert and return a new edge with a given element between two
vertices */ // Insert and return a new edge with a given element between two vertices
public Edge<E> insertEdge(Vertex<V> v, Vertex<V> w, E o) public Edge<E> insertEdge(Vertex<V> v, Vertex<V> w, E o)
throws InvalidPositionException { throws InvalidPositionException {
MyVertex<V> vv = checkVertex(v); MyVertex<V> vv = checkVertex(v);
@@ -109,8 +118,8 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
ee.setLocation(pe); ee.setLocation(pe);
return ee; return ee;
} }
/** Remove a vertex and all its incident edges and return the
element stored at the removed vertex */ // Remove a vertex and all its incident edges and return the element stored at the removed vertex
public V removeVertex(Vertex<V> v) public V removeVertex(Vertex<V> v)
throws InvalidPositionException { throws InvalidPositionException {
MyVertex<V> vv = checkVertex(v); MyVertex<V> vv = checkVertex(v);
@@ -123,7 +132,8 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
VList.remove(vv.location()); VList.remove(vv.location());
return v.element(); return v.element();
} }
/** Remove an edge and return its element */
// Remove an edge and return its element
public E removeEdge(Edge<E> e) public E removeEdge(Edge<E> e)
throws InvalidPositionException { throws InvalidPositionException {
MyEdge<E> ee = checkEdge(e); MyEdge<E> ee = checkEdge(e);
@@ -138,13 +148,13 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
// Auxiliary methods // Auxiliary methods
/** Return the degree of a given vertex */ // Return the degree of a given vertex
public int degree(Vertex<V> v) { public int degree(Vertex<V> v) {
MyVertex<V> vv = checkVertex(v); MyVertex<V> vv = checkVertex(v);
return vv.degree(); return vv.degree();
} }
/** Determines whether a given position is valid. */ // Determines whether a given position is valid.
protected MyPosition checkPosition(Position p) protected MyPosition checkPosition(Position p)
throws InvalidPositionException { throws InvalidPositionException {
if (p == null || !(p instanceof MyPosition)) if (p == null || !(p instanceof MyPosition))
@@ -152,7 +162,7 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
return (MyPosition) p; return (MyPosition) p;
} }
/** Determines whether a given vertex is valid. */ // Determines whether a given vertex is valid.
protected MyVertex<V> checkVertex(Vertex<V> v) protected MyVertex<V> checkVertex(Vertex<V> v)
throws InvalidPositionException { throws InvalidPositionException {
if (v == null || !(v instanceof MyVertex)) if (v == null || !(v instanceof MyVertex))
@@ -160,7 +170,7 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
return (MyVertex<V>) v; return (MyVertex<V>) v;
} }
/** Determines whether a given edge is valid. */ // Determines whether a given edge is valid.
protected MyEdge<E> checkEdge(Edge<E> e) protected MyEdge<E> checkEdge(Edge<E> e)
throws InvalidPositionException { throws InvalidPositionException {
if (e == null || !(e instanceof MyEdge)) if (e == null || !(e instanceof MyEdge))
@@ -168,24 +178,22 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
return (MyEdge<E>) e; return (MyEdge<E>) e;
} }
/** Implementation of a decorable position by means of a hash // Implementation of a decorable position by means of a hash table.
* table. */
protected static class MyPosition<T> protected static class MyPosition<T>
extends HashTableMap<Object,Object> implements DecorablePosition<T> { extends HashTableMap<Object,Object> implements DecorablePosition<T> {
/** The element stored at this position. */ // The element stored at this position.
protected T elem; protected T elem;
/** Returns the element stored at this position. */ // Returns the element stored at this position.
public T element() { public T element() {
return elem; return elem;
} }
/** Sets the element stored at this position. */ // Sets the element stored at this position.
public void setElement(T o) { public void setElement(T o) {
elem = o; elem = o;
} }
} }
/** Returns a string representation of the vertex and edge lists, // Returns a string representation of the vertex and edge lists, separated by a newline.
* separated by a newline. */
public String toString() { public String toString() {
return VList.toString() + "\n" + EList.toString(); return VList.toString() + "\n" + EList.toString();
} }
@@ -212,70 +220,65 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
return temp; return temp;
} }
/** Implementation of a vertex for an undirected adjacency list //Implementation of a vertex for an undirected adjacency list
* graph. Each vertex stores its incidence container and position //graph. Each vertex stores its incidence container and position
* in the vertex container of the graph. */ //in the vertex container of the graph.
protected class MyVertex<V> protected class MyVertex<V>
extends MyPosition<V> implements Vertex<V> { extends MyPosition<V> implements Vertex<V> {
/** Incidence container of the vertex. */ // Incidence container of the vertex.
protected PositionList<Edge<E>> incEdges; protected PositionList<Edge<E>> incEdges;
/** Position of the vertex in the vertex container of the graph. */ // Position of the vertex in the vertex container of the graph.
protected Position<Vertex<V>> loc; protected Position<Vertex<V>> loc;
/** Constructs the vertex with the given element. */ // Constructs the vertex with the given element.
MyVertex(V o) { MyVertex(V o) {
elem = o; elem = o;
incEdges = new NodePositionList<Edge<E>>(); incEdges = new NodePositionList<Edge<E>>();
} }
/** Return the degree of a given vertex */ // Return the degree of a given vertex
public int degree() { public int degree() {
return incEdges.size(); return incEdges.size();
} }
/** Returns the incident edges on this vertex. */ // Returns the incident edges on this vertex.
public Iterable<Edge<E>> incidentEdges() { public Iterable<Edge<E>> incidentEdges() {
return incEdges; return incEdges;
} }
/** Inserts an edge into the incidence container of this vertex. */ // Inserts an edge into the incidence container of this vertex.
public Position<Edge<E>> insertIncidence(Edge<E> e) { public Position<Edge<E>> insertIncidence(Edge<E> e) {
incEdges.addLast(e); incEdges.addLast(e);
return incEdges.last(); return incEdges.last();
} }
/** Removes an edge from the incidence container of this vertex. */ //Removes an edge from the incidence container of this vertex.
public void removeIncidence(Position<Edge<E>> p) { public void removeIncidence(Position<Edge<E>> p) {
incEdges.remove(p); incEdges.remove(p);
} }
/** Returns the position of this vertex in the vertex container of // Returns the position of this vertex in the vertex container of the graph.
* the graph. */
public Position<Vertex<V>> location() { public Position<Vertex<V>> location() {
return loc; return loc;
} }
/** Sets the position of this vertex in the vertex container of // Sets the position of this vertex in the vertex container of the graph.
* the graph. */
public void setLocation(Position<Vertex<V>> p) { public void setLocation(Position<Vertex<V>> p) {
loc = p; loc = p;
} }
/** Returns a string representation of the element stored at this // Returns a string representation of the element stored at this vertex.
* vertex. */
public String toString() { public String toString() {
return elem.toString(); return elem.toString();
} }
} }
/** Implementation of an edge for an undirected adjacency list // Implementation of an edge for an undirected adjacency list
* graph. Each edge stores its endpoints (end vertices), its // graph. Each edge stores its endpoints (end vertices), its
* positions within the incidence containers of its endpoints, and // positions within the incidence containers of its endpoints, and
* position in the edge container of the graph. */ // position in the edge container of the graph.
protected class MyEdge<E> extends MyPosition<E> implements Edge<E> { protected class MyEdge<E> extends MyPosition<E> implements Edge<E> {
/** The end vertices of the edge. */ // The end vertices of the edge.
protected MyVertex<V>[] endVertices; protected MyVertex<V>[] endVertices;
/** The positions of the entries for the edge in the incidence // The positions of the entries for the edge in the incidence containers of the end vertices.
* containers of the end vertices. */
protected Position<Edge<E>>[] Inc; protected Position<Edge<E>>[] Inc;
/** The position of the edge in the edge container of the // The position of the edge in the edge container of the graph.
* graph. */
protected Position<Edge<E>> loc; protected Position<Edge<E>> loc;
/** Constructs an edge with the given endpoints and elements. */ // Constructs an edge with the given endpoints and elements.
MyEdge (Vertex<V> v, Vertex<V> w, E o) { MyEdge (Vertex<V> v, Vertex<V> w, E o) {
elem = o; elem = o;
endVertices = (MyVertex<V>[]) new MyVertex[2]; endVertices = (MyVertex<V>[]) new MyVertex[2];
@@ -283,38 +286,37 @@ public class AdjacencyListGraph<V,E> implements Graph<V,E> {
endVertices[1] = (MyVertex<V>)w; endVertices[1] = (MyVertex<V>)w;
Inc = (Position<Edge<E>>[]) new Position[2]; Inc = (Position<Edge<E>>[]) new Position[2];
} }
/** Returns the end vertices of the edge. There are always two
* elements in the returned array. */ // Returns the end vertices of the edge. There are always two elements in the returned array.
public MyVertex<V>[] endVertices() { public MyVertex<V>[] endVertices() {
return endVertices; return endVertices;
} }
/** Returns the positions of the edge in the incidence containers
* of its end vertices. The returned array always contains two // Returns the positions of the edge in the incidence containers
* elements. */ // of its end vertices. The returned array always contains two
// elements.
public Position<Edge<E>>[] incidences() { public Position<Edge<E>>[] incidences() {
return Inc; return Inc;
} }
/** Sets the positions of the edge in the incidence containers of // Sets the positions of the edge in the incidence containers of its end vertices.
* its end vertices. */
public void setIncidences(Position<Edge<E>> pv, Position<Edge<E>> pw) { public void setIncidences(Position<Edge<E>> pv, Position<Edge<E>> pw) {
Inc[0] = pv; Inc[0] = pv;
Inc[1] = pw; Inc[1] = pw;
} }
/** Returns the position of the edge in the edge container of the // Returns the position of the edge in the edge container of the graph.
* graph. */
public Position<Edge<E>> location() { public Position<Edge<E>> location() {
return loc; return loc;
} }
/** Sets the position of the edge in the edge container of the // Sets the position of the edge in the edge container of the graph.
* graph. */
public void setLocation(Position<Edge<E>> p) { public void setLocation(Position<Edge<E>> p) {
loc = p; loc = p;
} }
/** Returns a string representation of the edge via a tuple of // Returns a string representation of the edge via a tuple of vertices.
* vertices. */
public String toString() { public String toString() {
return element() + "(" + endVertices[0].toString() + return element() + "(" + endVertices[0].toString() +
"," + endVertices[1].toString() + ")"; "," + endVertices[1].toString() + ")";
} }
} }
} }
*/

View File

@@ -1,6 +1,14 @@
package partition; package partition;
import map.HashTableMap;
import position.NodePositionList;
import position.PositionList;
import set.OrderedListSet;
import set.Set; import set.Set;
import utility.DefaultComparator;
import java.security.InvalidKeyException;
import java.util.Iterator;
/** /**
* Created with xgiovio.macbookair. * Created with xgiovio.macbookair.
@@ -10,30 +18,91 @@ import set.Set;
*/ */
public class ListPartition <E> implements Partition<E> { public class ListPartition <E> implements Partition<E> {
private HashTableMap<E,Set<E>> elementi;
private PositionList<Set<E>> partizione;
public ListPartition () {
elementi = new HashTableMap<E, Set<E>>();
partizione = new NodePositionList<Set<E>>();
}
@Override @Override
public int size() { public int size() {
return 0; return elementi.size();
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return false; return (size() == 0 );
} }
@Override @Override
public Set<E> makeSet(E x) { public Set<E> makeSet(E x) {
try {
OrderedListSet<E> o = new OrderedListSet<E>(x, new DefaultComparator<E>());
elementi.put(x, o);
partizione.addLast(o);
o.setLocation(partizione.last());
return o;
}
catch (InvalidKeyException e){
}
return null; return null;
} }
@Override @Override
public Set<E> union(Set<E> A, Set<E> B) { public Set<E> union(Set<E> A, Set<E> B) {
try {
if (A.size() > B.size()){
OrderedListSet<E> AA = (OrderedListSet<E>) A;
OrderedListSet<E> BB = (OrderedListSet<E>) B;
AA.fastUnion(BB);
partizione.remove(BB.location());
BB.setLocation(null);
Iterator<E> it = BB.list().iterator();
for (;it.hasNext();)
elementi.put(it.next(),AA);
return AA;
} else {
OrderedListSet<E> AA = (OrderedListSet<E>) B;
OrderedListSet<E> BB = (OrderedListSet<E>) A;
AA.fastUnion(BB);
partizione.remove(BB.location());
BB.setLocation(null);
Iterator<E> it = BB.list().iterator();
for (;it.hasNext();)
elementi.put(it.next(),AA);
return AA;
}
}
catch (InvalidKeyException e){
}
return null; return null;
} }
@Override @Override
public Set<E> find(E x) { public Set<E> find(E x) {
try {
return elementi.get(x);
}
catch (InvalidKeyException e){
}
return null; return null;
} }
@Override
public String toString() {
return partizione.toString();
}
} }

View File

@@ -1,11 +1,13 @@
package set; package set;
import position.NodePositionList; import position.NodePositionList;
import position.Position;
import position.PositionList; import position.PositionList;
import utility.DefaultComparator; import utility.DefaultComparator;
import utility.merge.MergeTemplate; import utility.merge.MergeTemplate;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
/** /**
* Created with xgiovio.macbookair. * Created with xgiovio.macbookair.
@@ -17,6 +19,7 @@ import java.util.Comparator;
public class OrderedListSet <E> implements Set<E> { public class OrderedListSet <E> implements Set<E> {
private Comparator<E> c; private Comparator<E> c;
private PositionList<E> L; private PositionList<E> L;
private Position<Set<E>> loc;
@@ -99,8 +102,33 @@ public class OrderedListSet <E> implements Set<E> {
public Set<E> fastUnion(Set<E> B){
OrderedListSet<E> BB = (OrderedListSet<E>) B;
Iterator<E> it = BB.L.iterator();
for (;it.hasNext();)
L.addLast(it.next());
return this;
}
public E fastInsert(E x) {
L.addLast(x);
return x;
}
public Position<Set<E>> location (){
return loc;
}
public void setLocation (Position<Set<E>> in){
loc = in;
}
public PositionList<E> list (){
return L;
}
//////////////////////// inner class override methods from merge template ///// //////////////////////// inner class override methods from merge template /////