From 71cea16bcb0f0b27f4b888306d1e32fcdc13d95a Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sun, 1 Jun 2014 18:47:19 +0200 Subject: [PATCH] implementato connected components --- test_2_prova_intercorso/General.java | 82 -------------------- test_2_prova_intercorso/Other_Functions.java | 73 +++++++++++++++++ 2 files changed, 73 insertions(+), 82 deletions(-) delete mode 100644 test_2_prova_intercorso/General.java create mode 100644 test_2_prova_intercorso/Other_Functions.java diff --git a/test_2_prova_intercorso/General.java b/test_2_prova_intercorso/General.java deleted file mode 100644 index 11636de..0000000 --- a/test_2_prova_intercorso/General.java +++ /dev/null @@ -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 Partition> findConnectedComponentsl(Graph G){ - - - Partition> p = new ListPartition>(); - - Object VISITED = new Object(); - Object UNVISITED = new Object(); - Object STATUS = - for (Vertex v : G.vertices()) { - v.put() - } - - - - layers.add(0, new NodePositionList>()); - layers.get(0).addLast(v); - } - - int i = 0; - while (!layers.get(i).isEmpty()) { - layers.add(i + 1, new NodePositionList>()); - - for (Vertex vertexInLayer : layers.get(i)) { - for (Edge e : graph.incidentEdges(vertexInLayer)) { - if (!isVisited(e)) { - visit(e); - Vertex 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(); - } - - - - } - - - -} diff --git a/test_2_prova_intercorso/Other_Functions.java b/test_2_prova_intercorso/Other_Functions.java new file mode 100644 index 0000000..f7fe06e --- /dev/null +++ b/test_2_prova_intercorso/Other_Functions.java @@ -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 Partition> findConnectedComponents(Graph G) { + + + Partition> p = new ListPartition>(); + + Object VISITED = new Object(); + Object UNVISITED = new Object(); + Object STATUS = new Object(); + + for (Vertex v : G.vertices()) { + v.put(STATUS, UNVISITED); + } + + for (Vertex v : G.vertices()) { + + if (v.get(STATUS).equals(UNVISITED)) { + + IndexList>> layers = new ArrayIndexList>>(); + + layers.add(0, new NodePositionList>()); + layers.get(0).addLast(v); + v.put(STATUS, VISITED); + Set> s = p.makeSet(v); + + int i = 0; + while (!layers.get(i).isEmpty()) { + layers.add(i + 1, new NodePositionList>()); + + for (Vertex vertexInLayer : layers.get(i)) { + for (Edge e : G.incidentEdges(vertexInLayer)) { + if (e.get(STATUS).equals(UNVISITED)) { + e.put(STATUS, VISITED); + Vertex 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; + } + +}