Skip to content

Commit 260dbfe

Browse files
Merge development into development-JoseCarlosGarcia
2 parents 44b1af2 + 0c69175 commit 260dbfe

21 files changed

+3212
-1979
lines changed

nbproject/project.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ dist.jlink.dir=${dist.dir}/jlink
3636
dist.jlink.output=${dist.jlink.dir}/jlibgraph
3737
endorsed.classpath=
3838
excludes=
39+
file.reference.slf4j-api-1.7.9.jar=lib/slf4j-api-1.7.9.jar
3940
includes=**
4041
jar.compress=false
41-
javac.classpath=
42+
javac.classpath=\
43+
${file.reference.slf4j-api-1.7.9.jar}
4244
# Space-separated list of extra javac options
4345
javac.compilerargs=
4446
javac.deprecation=true
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (C) 2022 Amaya.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package cu.edu.cujae.graphy.algorithms;
20+
21+
import cu.edu.cujae.graphy.core.Graph;
22+
import cu.edu.cujae.graphy.core.iterators.GraphIterator;
23+
import java.util.HashMap;
24+
import java.util.Set;
25+
import java.util.TreeSet;
26+
27+
/**
28+
* This algorithm checks if a given undirected graph is biconnected.
29+
*
30+
* @author Amaya D. Fuentes;
31+
*/
32+
public class BiconnectivityDetection<T> extends AbstractAlgorithm<Boolean> {
33+
34+
private int V;
35+
private GraphIterator<T> iterator;
36+
37+
public BiconnectivityDetection(Graph<T> graph) {
38+
super(Boolean.FALSE);
39+
40+
if(graph.isDirected()) {
41+
throw new IllegalArgumentException("The graph is directed.");
42+
}
43+
44+
this.V = graph.size();
45+
this.iterator = graph.randomIterator();
46+
}
47+
48+
@Override
49+
public Algorithm<Boolean> apply() {
50+
51+
Set<Integer> visited = new TreeSet<>();
52+
53+
HashMap<Integer, Integer> parent = new HashMap<>();
54+
55+
int discTime = 0;
56+
HashMap<Integer, Integer> disc = new HashMap<>();
57+
HashMap<Integer, Integer> low = new HashMap<>();
58+
59+
boolean articulationPoint = hasArticulationPoint(iterator, visited, parent, disc, low, discTime);
60+
61+
if(!articulationPoint && visited.size() == V) {
62+
setResult(true);
63+
}
64+
65+
return this;
66+
}
67+
68+
69+
70+
/**
71+
* This recursive function checks if a graph has an articulation point through DFS search
72+
*
73+
* @param iterator - The graph's iterator
74+
* @param visited - Contains every visited vertex
75+
* @param parent - The key is the current node's label and the value is its parent's in the DFS tree
76+
* @param low - The key is the node's label and the value is the discovery time of it earliest visited adjacent node
77+
* @param disc - The key is the current node's label and the value is its discovery time
78+
* @param discTime - The discovery time for the current node
79+
* @return true if there is an articulation point, otherwise false
80+
*/
81+
private boolean hasArticulationPoint(GraphIterator<T> iterator, Set<Integer> visited, HashMap<Integer, Integer> parent, HashMap<Integer, Integer> low, HashMap<Integer, Integer> disc, int discTime) {
82+
83+
int children = 0;
84+
int currentLabel = iterator.getLabel();
85+
visited.add(currentLabel);
86+
discTime++;
87+
disc.put(currentLabel, discTime);
88+
low.put(currentLabel, discTime);
89+
90+
for(Integer adjLabel: iterator.getAllAdjacentVertices()) {
91+
iterator.next(adjLabel);
92+
93+
if(!visited.contains(adjLabel)) {
94+
children++;
95+
parent.put(adjLabel, currentLabel);
96+
97+
if(hasArticulationPoint(iterator, visited, parent, low, disc, discTime)){
98+
return true;
99+
}
100+
101+
low.put(currentLabel, Math.min(low.get(currentLabel), low.get(adjLabel)));
102+
103+
if(!parent.containsKey(currentLabel) && children > 1) {
104+
return true;
105+
}
106+
107+
if(parent.containsKey(currentLabel) && low.get(adjLabel) >= disc.get(currentLabel)) {
108+
return true;
109+
}
110+
}
111+
112+
else if(parent.get(currentLabel) != adjLabel) {
113+
low.put(currentLabel, Math.min(low.get(currentLabel), disc.get(adjLabel)));
114+
}
115+
}
116+
117+
return false;
118+
}
119+
120+
121+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (C) 2022 Amaya.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package cu.edu.cujae.graphy.algorithms;
20+
21+
import cu.edu.cujae.graphy.core.Graph;
22+
import cu.edu.cujae.graphy.core.iterators.GraphIterator;
23+
import cu.edu.cujae.graphy.utils.HashTuple;
24+
import java.util.Collection;
25+
import java.util.HashMap;
26+
import java.util.Set;
27+
import java.util.TreeSet;
28+
29+
/**
30+
* The <b>Bron-Kerbosch algorithm</b> is an enumeration algorithm for finding all
31+
* maximal cliques in an undirected graph. That is, it lists all the subsets of
32+
* vertices with the properties that each pair of vertices in one of the listed
33+
* subsets is connected by an edge, and no listed subset can have any vertices
34+
* added to it while preserving its complete connectivity. An isolated vertex is
35+
* not considered a maximal clique.
36+
*
37+
* The variant of the algorithm implemented is the one that involves a "pivot element"
38+
* for efficiency purposes.
39+
*
40+
* @author Amaya D. Fuentes
41+
* @param <T>
42+
*/
43+
public class BronKerboschAlgorithm<T> extends AbstractAlgorithm<Collection<Collection<Integer>>>
44+
{
45+
private Set<Integer> vertices;
46+
private HashMap<Integer, Collection<Integer>> neighbors;
47+
48+
public BronKerboschAlgorithm(Graph<T> graph) {
49+
50+
super(new HashTuple<Collection<Integer>>());
51+
52+
if(graph.isDirected()) {
53+
throw new IllegalArgumentException("The graph is directed");
54+
}
55+
56+
if(graph.size() == 0) {
57+
throw new IllegalArgumentException("The graph is empty.");
58+
}
59+
60+
//initialize class fields
61+
this.vertices = new TreeSet<>();
62+
this.neighbors = new HashMap<>();
63+
64+
//get the vertices and their neighbors, excluding isolated vertices
65+
GraphIterator<T> dfsIterator = (GraphIterator<T>) graph.depthFirstSearchIterator(true);
66+
while(dfsIterator.hasNext()) {
67+
dfsIterator.next();
68+
if(!dfsIterator.getAllAdjacentVertices().isEmpty()) {
69+
vertices.add(dfsIterator.getLabel());
70+
neighbors.put(dfsIterator.getLabel(),dfsIterator.getAllAdjacentVertices());
71+
}
72+
}
73+
}
74+
75+
@Override
76+
public Algorithm<Collection<Collection<Integer>>> apply() {
77+
78+
Set<Integer> P = vertices;
79+
Set<Integer> R = new TreeSet<>();
80+
Set<Integer> X = new TreeSet<>();
81+
82+
maximalCliques(R, P, X);
83+
84+
return this;
85+
}
86+
87+
/**
88+
* Recursive method for finding all maximal cliques (Bron-Kerbosch algorithm
89+
* with pivot).
90+
*
91+
* @param R - The set to fill with the vertices that form a maximal clique
92+
* @param P - The set of candidate vertices to be added to R
93+
* @param X - The set of processed vertices or belonging to a maximal clique
94+
*/
95+
private void maximalCliques(Set<Integer> R, Set<Integer> P, Set<Integer> X) {
96+
97+
if(P.isEmpty() && X.isEmpty()) {
98+
Collection<Collection<Integer>> result = getResult();
99+
result.add(R);
100+
}
101+
else {
102+
Set<Integer> P1 = new TreeSet<>(P);
103+
Integer u = findPivot(unite(P1, X));
104+
for(Integer v: remove(P, u)) {
105+
maximalCliques(unite(R, v), intersect(P1, v), intersect(X, v));
106+
P1.remove(v);
107+
X.add(v);
108+
}
109+
}
110+
}
111+
112+
113+
/**
114+
* Method for finding the element of a set with the higher degree in the
115+
* DFS search tree (the most neighbors)
116+
*
117+
* @param set - The set where the pivot will be found
118+
* @return The element of the set with the highest degree
119+
*/
120+
private Integer findPivot(Set<Integer> set) {
121+
Integer pivot = null;
122+
int maxDegree = 0;
123+
for(Integer vertex: set) {
124+
int degree = neighbors.get(vertex).size();
125+
if(degree >= maxDegree) {
126+
maxDegree = degree;
127+
pivot = vertex;
128+
}
129+
}
130+
return pivot;
131+
132+
}
133+
134+
135+
/**
136+
* Method to get the intersection of a set with the neighbors of a vertex
137+
* without altering the original set.
138+
*
139+
* @param set
140+
* @param vertex
141+
* @return The set corresponding to the intersection
142+
*/
143+
private Set<Integer> intersect(Set<Integer> set, Integer vertex) {
144+
Set<Integer> intersection = new TreeSet<>(set);
145+
intersection.retainAll(neighbors.get(vertex));
146+
return intersection;
147+
}
148+
149+
/**
150+
* Method to get the union of two sets without altering them
151+
*
152+
* @param set1
153+
* @param set2
154+
* @return The set corresponding to the union of set1 and set2
155+
*/
156+
private Set<Integer> unite(Set<Integer> set1, Set<Integer> set2) {
157+
Set<Integer> union = new TreeSet<>(set1);
158+
union.addAll(set2);
159+
return union;
160+
}
161+
162+
/**
163+
* Method to get the union of a set and the neighbors of a vertex without
164+
* altering the original set
165+
*
166+
* @param set
167+
* @param vertex
168+
* @return The set corresponding to the union
169+
*/
170+
private Set<Integer> unite(Set<Integer> set, Integer vertex) {
171+
Set<Integer> union = new TreeSet<>(set);
172+
union.add(vertex);
173+
return union;
174+
}
175+
176+
/**
177+
* Method to get the difference of a set with the neighbors of a vertex
178+
* without altering the original set
179+
*
180+
* @param set
181+
* @param vertex
182+
* @return The set corresponding to the difference
183+
*/
184+
private Set<Integer> remove(Set<Integer> set, Integer vertex) {
185+
Set<Integer> removal = new TreeSet<>(set);
186+
removal.removeAll(neighbors.get(vertex));
187+
return removal;
188+
}
189+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2022 Amaya.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package cu.edu.cujae.graphy.algorithms;
20+
21+
import cu.edu.cujae.graphy.core.Graph;
22+
import cu.edu.cujae.graphy.core.iterators.GraphIterator;
23+
24+
/**
25+
* This algorithm checks if a given graph, whether directed or undirected, is connex.
26+
*
27+
* @author Amaya D. Fuentes
28+
* @param <T>
29+
*/
30+
public class ConnectivityDetection<T> extends AbstractAlgorithm<Boolean> {
31+
32+
private GraphIterator<T> iterator;
33+
private int V;
34+
35+
/**
36+
*
37+
* @param graph The graph on which the algorithm is to be executed
38+
*/
39+
public ConnectivityDetection(Graph<T> graph) {
40+
41+
super(Boolean.FALSE);
42+
this.iterator = (GraphIterator<T>) graph.depthFirstSearchIterator(false);
43+
this.V = graph.size();
44+
45+
}
46+
47+
@Override
48+
public Algorithm<Boolean> apply() {
49+
50+
int count = 0;
51+
while(iterator.hasNext()) {
52+
iterator.next();
53+
count++;
54+
}
55+
56+
if(count == V) {
57+
setResult(true);
58+
}
59+
60+
return this;
61+
}
62+
63+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy