implementato connected components
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
import exceptions.InvalidKeyException;
|
||||
import graph.ComponentsBFS;
|
||||
import graph.Edge;
|
||||
import graph.Graph;
|
||||
import graph.Vertex;
|
||||
import partition.ListPartition;
|
||||
import partition.Partition;
|
||||
import position.NodePositionList;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 01/06/2014
|
||||
* Time: 17:32
|
||||
*/
|
||||
public class General {
|
||||
|
||||
public static <V,E> Partition<Vertex<V>> findConnectedComponentsl(Graph<V,E> G){
|
||||
|
||||
|
||||
Partition<Vertex<V>> p = new ListPartition<Vertex<V>>();
|
||||
|
||||
Object VISITED = new Object();
|
||||
Object UNVISITED = new Object();
|
||||
Object STATUS =
|
||||
for (Vertex<V> v : G.vertices()) {
|
||||
v.put()
|
||||
}
|
||||
|
||||
|
||||
|
||||
layers.add(0, new NodePositionList<Vertex<V>>());
|
||||
layers.get(0).addLast(v);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (!layers.get(i).isEmpty()) {
|
||||
layers.add(i + 1, new NodePositionList<Vertex<V>>());
|
||||
|
||||
for (Vertex<V> vertexInLayer : layers.get(i)) {
|
||||
for (Edge<E> e : graph.incidentEdges(vertexInLayer)) {
|
||||
if (!isVisited(e)) {
|
||||
visit(e);
|
||||
Vertex<V> w = graph.opposite(vertexInLayer, e);
|
||||
if (!isVisited(w)) {
|
||||
traverseDiscovery(e, vertexInLayer);
|
||||
|
||||
if (isDone())
|
||||
break;
|
||||
|
||||
visit(w);
|
||||
layers.get(i + 1).addLast(w);
|
||||
|
||||
if (isDone())
|
||||
break;
|
||||
} else {
|
||||
traverseCross(e, v);
|
||||
if (isDone())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isDone())
|
||||
finishVisit(vertexInLayer);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return result();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
73
test_2_prova_intercorso/Other_Functions.java
Normal file
73
test_2_prova_intercorso/Other_Functions.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import arraylist.IndexList;
|
||||
import exceptions.InvalidKeyException;
|
||||
import graph.ComponentsBFS;
|
||||
import graph.Edge;
|
||||
import graph.Graph;
|
||||
import graph.Vertex;
|
||||
import partition.ListPartition;
|
||||
import partition.Partition;
|
||||
import position.NodePositionList;
|
||||
import position.PositionList;
|
||||
import set.Set;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 01/06/2014
|
||||
* Time: 17:32
|
||||
*/
|
||||
public class Other_Functions {
|
||||
|
||||
public static <V,E> Partition<Vertex<V>> findConnectedComponents(Graph<V,E> G) {
|
||||
|
||||
|
||||
Partition<Vertex<V>> p = new ListPartition<Vertex<V>>();
|
||||
|
||||
Object VISITED = new Object();
|
||||
Object UNVISITED = new Object();
|
||||
Object STATUS = new Object();
|
||||
|
||||
for (Vertex<V> v : G.vertices()) {
|
||||
v.put(STATUS, UNVISITED);
|
||||
}
|
||||
|
||||
for (Vertex<V> v : G.vertices()) {
|
||||
|
||||
if (v.get(STATUS).equals(UNVISITED)) {
|
||||
|
||||
IndexList<PositionList<Vertex<V>>> layers = new ArrayIndexList<PositionList<Vertex<V>>>();
|
||||
|
||||
layers.add(0, new NodePositionList<Vertex<V>>());
|
||||
layers.get(0).addLast(v);
|
||||
v.put(STATUS, VISITED);
|
||||
Set<Vertex<V>> s = p.makeSet(v);
|
||||
|
||||
int i = 0;
|
||||
while (!layers.get(i).isEmpty()) {
|
||||
layers.add(i + 1, new NodePositionList<Vertex<V>>());
|
||||
|
||||
for (Vertex<V> vertexInLayer : layers.get(i)) {
|
||||
for (Edge<E> e : G.incidentEdges(vertexInLayer)) {
|
||||
if (e.get(STATUS).equals(UNVISITED)) {
|
||||
e.put(STATUS, VISITED);
|
||||
Vertex<V> w = G.opposite(vertexInLayer, e);
|
||||
if (w.get(STATUS).equals(UNVISITED)) {
|
||||
w.put(STATUS, VISITED);
|
||||
p.union(s, p.makeSet(w));
|
||||
layers.get(i + 1).addLast(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user