diff --git a/dense_zone/Graph.java b/dense_zone/Graph.java new file mode 100644 index 0000000000000000000000000000000000000000..6a5d893e54789859a64f012c997acd71261d7c2b --- /dev/null +++ b/dense_zone/Graph.java @@ -0,0 +1,287 @@ +package dense_zone; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.*; + +public class Graph<Label> { + private int cardinal; + private Map<Integer, List<Integer>> adjacencyList; + + public Graph(int size) { + cardinal = size; + adjacencyList = new HashMap<>(); + for (int i = 0; i < size; i++) { + adjacencyList.put(i, new ArrayList<>()); + } + } + + // Add an edge to the adjacency list + public void addEdge(int source, int dest) { + adjacencyList.get(source).add(dest); + adjacencyList.get(dest).add(source); // For an undirected graph + } + + // Check if a set of vertices forms a dense zone + public boolean isDenseZone(List<Integer> vertices) { + // Check if all vertices in 'vertices' are connected to each other in the original graph + for (int i = 0; i < vertices.size(); i++) { + int vertex1 = vertices.get(i); + for (int j = i + 1; j < vertices.size(); j++) { + int vertex2 = vertices.get(j); + if (!adjacencyList.get(vertex1).contains(vertex2)) { + return false; + } + } + } + return true; + } + + // Check if two vertices are connected in the graph + public boolean isConnected(int vertex1, int vertex2) { + return adjacencyList.get(vertex1).contains(vertex2); + } + + public List<Set<Integer>> findAllMaximalDenseZones() { + List<Set<Integer>> maximalDenseZones = new ArrayList<>(); + int numVertices = cardinal; + + // Check if the graph contains only one vertex, add it as a maximal dense zone + if (numVertices == 1) { + Set<Integer> singleVertexZone = new HashSet<>(); + singleVertexZone.add(0); // Assuming the single vertex has index 0 + maximalDenseZones.add(singleVertexZone); + return maximalDenseZones; + } + + for (int vertex = 0; vertex < numVertices; vertex++) { + Set<Integer> currentZone = new HashSet<>(); + currentZone.add(vertex); + findMaximalDenseZones(currentZone, maximalDenseZones); + } + + // Filter out single vertices from maximal dense zones + maximalDenseZones.removeIf(zone -> zone.size() == 1); + + // Check and remove subsets + maximalDenseZones = removeSubsets(maximalDenseZones); + + return maximalDenseZones; + } + + private List<Set<Integer>> removeSubsets(List<Set<Integer>> maximalDenseZones) { + List<Set<Integer>> result = new ArrayList<>(maximalDenseZones); + + for (Set<Integer> zone1 : maximalDenseZones) { + for (Set<Integer> zone2 : maximalDenseZones) { + if (zone1 != zone2 && isSubset(new ArrayList<>(zone1), new ArrayList<>(zone2))) { + result.remove(zone1); + break; + } + } + } + + return result; + } + + // Check if one list is a subset of another + public boolean isSubset(List<Integer> subset, List<Integer> set) { + return set.containsAll(subset); + } + + + private void findMaximalDenseZones(Set<Integer> currentZone, List<Set<Integer>> maximalDenseZones) { + int numVertices = cardinal; + int lastAddedVertex = -1; + + for (int vertex : currentZone) { + lastAddedVertex = vertex; + } + + boolean canAddMoreVertices = false; + + for (int vertex = lastAddedVertex + 1; vertex < numVertices; vertex++) { + boolean canAddVertex = true; + for (int zoneVertex : currentZone) { + if (!adjacencyList.get(vertex).contains(zoneVertex)) { + canAddVertex = false; + break; + } + } + + if (canAddVertex) { + Set<Integer> newZone = new HashSet<>(currentZone); + newZone.add(vertex); + findMaximalDenseZones(newZone, maximalDenseZones); + canAddMoreVertices = true; + } + } + + if (!canAddMoreVertices) { + maximalDenseZones.add(currentZone); + } + } + + // Find all maximum dense zones among the maximal dense zones + public List<Set<Integer>> findMaximumDenseZones(List<Set<Integer>> maximalDenseZones) { + List<Set<Integer>> maximumDenseZones = new ArrayList<>(); + int maxSize = 0; + + for (Set<Integer> denseZone : maximalDenseZones) { + if (denseZone.size() > maxSize) { + maximumDenseZones.clear(); + maximumDenseZones.add(new HashSet<>(denseZone)); + maxSize = denseZone.size(); + } else if (denseZone.size() == maxSize) { + maximumDenseZones.add(new HashSet<>(denseZone)); + } + } + + return maximumDenseZones; + } + + + // Find an approximate dense zone using a heuristic + public List<Integer> findApproximateDenseZone() { + Random random = new Random(); + int startPoint = random.nextInt(cardinal); + List<Integer> maxDenseZone = new ArrayList<>(); + maxDenseZone.add(startPoint); + + List<Integer> currentDenseZone = new ArrayList<>(); + currentDenseZone.add(startPoint); + + while (true) { + int bestVertex = -1; + double bestDensity = calculateDensity(currentDenseZone); + + for (int neighbor = 0; neighbor < cardinal; neighbor++) { + if (!currentDenseZone.contains(neighbor)) { + currentDenseZone.add(neighbor); + double newDensity = calculateDensity(currentDenseZone); + + if (newDensity > bestDensity) { + bestDensity = newDensity; + bestVertex = neighbor; + } + + currentDenseZone.remove(currentDenseZone.size() - 1); + } + } + + if (bestVertex == -1) { + break; + } else { + currentDenseZone.add(bestVertex); + } + } + + if (calculateDensity(currentDenseZone) > calculateDensity(maxDenseZone)) { + maxDenseZone = new ArrayList<>(currentDenseZone); + } + + return maxDenseZone; + } + + public double calculateDensity(List<Integer> vertices) { + int numEdges = 0; + for (int i = 0; i < vertices.size(); i++) { + for (int j = i + 1; j < vertices.size(); j++) { + if (isConnected(vertices.get(i), vertices.get(j))) { + numEdges++; + } + } + } + + return (double) numEdges / (vertices.size() * (vertices.size() - 1) / 2); + } + + // Read edges from a file and populate the adjacency list + public void readEdgesFromFile(String filePath) { + try (Scanner scanner = new Scanner(new File(filePath))) { + int numVertices = scanner.nextInt(); + scanner.nextLine(); + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] edgeTokens = line.split(" "); + if (edgeTokens.length == 2) { + int source = Integer.parseInt(edgeTokens[0]); + int dest = Integer.parseInt(edgeTokens[1]); + addEdge(source, dest); + } + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + // Calculate the execution time of a given method + public static long calculateExecutionTime(Runnable methodToMeasure) { + long startTime = System.currentTimeMillis(); + methodToMeasure.run(); + long endTime = System.currentTimeMillis(); + return endTime - startTime; + } + + public static void main(String[] args) { + // Create a Graph object + Graph<Integer> graphObj = new Graph<>(100); // Assuming you know the number of vertices + + // Read edges from a text file and measure execution time + long readTime = calculateExecutionTime(() -> graphObj.readEdgesFromFile("C:\\MASTER 1\\Complexite\\m1-info-complexite-project-1\\dense_zone\\Instances_graphe\\graph100.txt")); + System.out.println("Reading time: " + readTime + " milliseconds"); + + // Measure execution time of isDenseZone + long isDenseZoneTime = calculateExecutionTime(() -> { + List<Integer> X = Arrays.asList(0,1,2,3); + boolean isDenseZone = graphObj.isDenseZone(X); + if (isDenseZone) { + System.out.println("X = " + X + " is a dense zone in G."); + } else { + System.out.println("X = " + X + " is not a dense zone in G."); + } + }); + + + // Measure execution time of findAllMaximalDenseZones + long findAllMaximalDenseZonesTime = calculateExecutionTime(() -> { + List<Set<Integer>> maximalDenseZones = graphObj.findAllMaximalDenseZones(); + System.out.println("All Maximal Dense Zones:"); + for (Set<Integer> denseZone : maximalDenseZones) { + System.out.println(denseZone); + } + }); + + // Measure execution time of findMaximumDenseZones + long findMaximumDenseZonesTime = calculateExecutionTime(() -> { + List<Set<Integer>> maximalDenseZones = graphObj.findAllMaximalDenseZones(); + List<Set<Integer>> maximumDenseZones = graphObj.findMaximumDenseZones(maximalDenseZones); + System.out.println("\nMaximum Dense Zones:"); + for (Set<Integer> denseZone : maximumDenseZones) { + System.out.println(denseZone); + } + }); + + // Measure execution time of findApproximateDenseZone + long approximateDenseZoneTime = calculateExecutionTime(() -> { + List<Integer> approximateDenseZone = graphObj.findApproximateDenseZone(); + System.out.println("\nApproximate Dense Zone: " + approximateDenseZone); + }); + + System.out.println("isDenseZone execution time: " + isDenseZoneTime + " milliseconds"); + System.out.println("findAllMaximalDenseZones execution time: " + findAllMaximalDenseZonesTime + " milliseconds"); + System.out.println("findMaximumDenseZones execution time: " + findMaximumDenseZonesTime + " milliseconds"); + System.out.println("findApproximateDenseZone execution time: " + approximateDenseZoneTime + " milliseconds"); + + } +} + + + + + + + + + + diff --git a/dense_zone/Instances_graphe/graph10.txt b/dense_zone/Instances_graphe/graph10.txt new file mode 100644 index 0000000000000000000000000000000000000000..cc2175682241a4015e19760fab2cb98ac3574faa --- /dev/null +++ b/dense_zone/Instances_graphe/graph10.txt @@ -0,0 +1,11 @@ +10 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph100.txt b/dense_zone/Instances_graphe/graph100.txt new file mode 100644 index 0000000000000000000000000000000000000000..a42b1246aaa3413c95c4568abe403e3181f7cc91 --- /dev/null +++ b/dense_zone/Instances_graphe/graph100.txt @@ -0,0 +1,101 @@ +100 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 +38 40 +39 41 +40 42 +41 43 +42 44 +43 45 +44 46 +45 47 +46 48 +47 49 +50 51 +50 52 +51 52 +51 53 +52 54 +53 55 +54 56 +55 57 +56 58 +57 59 +58 60 +59 61 +60 62 +61 63 +62 64 +63 65 +64 66 +65 67 +66 68 +67 69 +70 71 +70 72 +71 72 +71 73 +72 74 +73 75 +74 76 +75 77 +76 78 +77 79 +80 81 +80 82 +81 82 +81 83 +82 84 +83 85 +84 86 +85 87 +86 88 +87 89 +90 91 +90 92 +91 92 +91 93 +92 94 +93 95 +94 96 +95 97 +96 98 +97 99 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph20.txt b/dense_zone/Instances_graphe/graph20.txt new file mode 100644 index 0000000000000000000000000000000000000000..3fca2da2f7c69434637ef1afac93c0a7c343582c --- /dev/null +++ b/dense_zone/Instances_graphe/graph20.txt @@ -0,0 +1,21 @@ +20 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph30.txt b/dense_zone/Instances_graphe/graph30.txt new file mode 100644 index 0000000000000000000000000000000000000000..16104209c19228efc6ca444db4507c2516810377 --- /dev/null +++ b/dense_zone/Instances_graphe/graph30.txt @@ -0,0 +1,31 @@ +30 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph40.txt b/dense_zone/Instances_graphe/graph40.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9c03bb847aa380df805237e13600d9364445406 --- /dev/null +++ b/dense_zone/Instances_graphe/graph40.txt @@ -0,0 +1,41 @@ +40 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph5.txt b/dense_zone/Instances_graphe/graph5.txt new file mode 100644 index 0000000000000000000000000000000000000000..ceedb05c852faf37e24956f9b99ee86994cb49c1 --- /dev/null +++ b/dense_zone/Instances_graphe/graph5.txt @@ -0,0 +1,6 @@ +5 +0 1 +0 2 +1 2 +1 3 +3 4 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph50.txt b/dense_zone/Instances_graphe/graph50.txt new file mode 100644 index 0000000000000000000000000000000000000000..e551537ca41cefa01c2f249a034c113858f1c51f --- /dev/null +++ b/dense_zone/Instances_graphe/graph50.txt @@ -0,0 +1,51 @@ +50 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 +38 40 +39 41 +40 42 +41 43 +42 44 +43 45 +44 46 +45 47 +46 48 +47 49 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph60.txt b/dense_zone/Instances_graphe/graph60.txt new file mode 100644 index 0000000000000000000000000000000000000000..73e82ffdc2bf4461880e32c52f8b6fda77b5f34d --- /dev/null +++ b/dense_zone/Instances_graphe/graph60.txt @@ -0,0 +1,61 @@ +60 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 +38 40 +39 41 +40 42 +41 43 +42 44 +43 45 +44 46 +45 47 +46 48 +47 49 +50 51 +50 52 +51 52 +51 53 +52 54 +53 55 +54 56 +55 57 +56 58 +57 59 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph70.txt b/dense_zone/Instances_graphe/graph70.txt new file mode 100644 index 0000000000000000000000000000000000000000..c770f9a072c3c486930d416300a93105b5e1047b --- /dev/null +++ b/dense_zone/Instances_graphe/graph70.txt @@ -0,0 +1,71 @@ +70 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 +38 40 +39 41 +40 42 +41 43 +42 44 +43 45 +44 46 +45 47 +46 48 +47 49 +50 51 +50 52 +51 52 +51 53 +52 54 +53 55 +54 56 +55 57 +56 58 +57 59 +58 60 +59 61 +60 62 +61 63 +62 64 +63 65 +64 66 +65 67 +66 68 +67 69 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph80.txt b/dense_zone/Instances_graphe/graph80.txt new file mode 100644 index 0000000000000000000000000000000000000000..427dcf5e198eee57abc77cd59974d677d87a9242 --- /dev/null +++ b/dense_zone/Instances_graphe/graph80.txt @@ -0,0 +1,81 @@ +80 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 +38 40 +39 41 +40 42 +41 43 +42 44 +43 45 +44 46 +45 47 +46 48 +47 49 +50 51 +50 52 +51 52 +51 53 +52 54 +53 55 +54 56 +55 57 +56 58 +57 59 +58 60 +59 61 +60 62 +61 63 +62 64 +63 65 +64 66 +65 67 +66 68 +67 69 +70 71 +70 72 +71 72 +71 73 +72 74 +73 75 +74 76 +75 77 +76 78 +77 79 \ No newline at end of file diff --git a/dense_zone/Instances_graphe/graph90.txt b/dense_zone/Instances_graphe/graph90.txt new file mode 100644 index 0000000000000000000000000000000000000000..d5f1ebe7ce95b41a7ff3f3ff151f25c22485f17e --- /dev/null +++ b/dense_zone/Instances_graphe/graph90.txt @@ -0,0 +1,91 @@ +90 +0 1 +0 2 +1 2 +1 3 +2 4 +3 5 +4 6 +5 7 +6 8 +7 9 +8 10 +9 11 +10 12 +11 13 +12 14 +13 15 +14 16 +15 17 +16 18 +17 19 +18 20 +19 21 +20 22 +21 23 +22 24 +23 25 +24 26 +25 27 +26 28 +27 29 +28 30 +29 31 +30 32 +31 33 +32 34 +33 35 +34 36 +35 37 +36 38 +37 39 +38 40 +39 41 +40 42 +41 43 +42 44 +43 45 +44 46 +45 47 +46 48 +47 49 +50 51 +50 52 +51 52 +51 53 +52 54 +53 55 +54 56 +55 57 +56 58 +57 59 +58 60 +59 61 +60 62 +61 63 +62 64 +63 65 +64 66 +65 67 +66 68 +67 69 +70 71 +70 72 +71 72 +71 73 +72 74 +73 75 +74 76 +75 77 +76 78 +77 79 +80 81 +80 82 +81 82 +81 83 +82 84 +83 85 +84 86 +85 87 +86 88 +87 89 \ No newline at end of file