From efa5c20a897afff6aa48f8e516f083f58b2042a0 Mon Sep 17 00:00:00 2001 From: purnimasharma06 Date: Thu, 3 Oct 2024 00:11:46 +0530 Subject: [PATCH 1/2] Removed two lines 1. Removed the SearchAlgorithm Interface and Import 2. Removed the @Override Annotation --- .../thealgorithms/searches/BinarySearch.java | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch.java b/src/main/java/com/thealgorithms/searches/BinarySearch.java index 22096307d144..374d698370c2 100644 --- a/src/main/java/com/thealgorithms/searches/BinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/BinarySearch.java @@ -1,25 +1,20 @@ -package com.thealgorithms.searches; - -import com.thealgorithms.devutils.searches.SearchAlgorithm; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; /** - * Binary search is one of the most popular algorithms The algorithm finds the - * position of a target value within a sorted array + * Binary search is one of the most popular algorithms. + * The algorithm finds the position of a target value within a sorted array. * *

- * Worst-case performance O(log n) Best-case performance O(1) Average - * performance O(log n) Worst-case space complexity O(1) + * Worst-case performance O(log n) Best-case performance O(1) + * Average performance O(log n) Worst-case space complexity O(1) * * @author Varun Upadhyay (https://github.com/varunu28) * @author Podshivalov Nikita (https://github.com/nikitap492) - * @see SearchAlgorithm - * @see IterativeBinarySearch */ -class BinarySearch implements SearchAlgorithm { +public class BinarySearch { /** * @param array is an array where the element should be found @@ -27,7 +22,6 @@ class BinarySearch implements SearchAlgorithm { * @param is any comparable type * @return index of the element */ - @Override public > int find(T[] array, T key) { return search(array, key, 0, array.length - 1); } @@ -43,8 +37,9 @@ public > int find(T[] array, T key) { */ private > int search(T[] array, T key, int left, int right) { if (right < left) { - return -1; // this means that the key not found + return -1; // this means that the key was not found } + // find median int median = (left + right) >>> 1; int comp = key.compareTo(array[median]); @@ -66,7 +61,12 @@ public static void main(String[] args) { int size = 100; int maxElement = 100000; - Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new); + Integer[] integers = IntStream + .generate(() -> r.nextInt(maxElement)) + .limit(size) + .sorted() + .boxed() + .toArray(Integer[]::new); // The element that should be found int shouldBeFound = integers[r.nextInt(size - 1)]; @@ -74,9 +74,20 @@ public static void main(String[] args) { BinarySearch search = new BinarySearch(); int atIndex = search.find(integers, shouldBeFound); - System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size); + System.out.printf( + "Should be found: %d. Found %d at index %d. Array length %d%n", + shouldBeFound, + integers[atIndex], + atIndex, + size + ); int toCheck = Arrays.binarySearch(integers, shouldBeFound); - System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex); + System.out.printf( + "Found by system method at an index: %d. Is equal: %b%n", + toCheck, + toCheck == atIndex + ); } } + From 2ac0993d8805a52d4c3c6dfde5421f72e33a4b9c Mon Sep 17 00:00:00 2001 From: purnimasharma06 Date: Fri, 4 Oct 2024 13:16:27 +0530 Subject: [PATCH 2/2] Added Travelling Salesman Problem --- .../TravellingSalesmanProblem.java | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/main/java/com/thealgorithms/greedyalgorithms/TravellingSalesmanProblem.java diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/TravellingSalesmanProblem.java b/src/main/java/com/thealgorithms/greedyalgorithms/TravellingSalesmanProblem.java new file mode 100644 index 000000000000..16479d91a2df --- /dev/null +++ b/src/main/java/com/thealgorithms/greedyalgorithms/TravellingSalesmanProblem.java @@ -0,0 +1,154 @@ +import java.util.*; +class TSP{ + +static int V = 4; + +// implementation of traveling +// Salesman Problem +static int travllingSalesmanProblem(int graph[][], + int s) +{ + // store all vertex apart + // from source vertex + ArrayList vertex = + new ArrayList(); + + for (int i = 0; i < V; i++) + if (i != s) + vertex.add(i); + + // store minimum weight + // Hamiltonian Cycle. + int min_path = Integer.MAX_VALUE; + do + { + // store current Path weight(cost) + int current_pathweight = 0; + + // compute current path weight + int k = s; + + for (int i = 0; + i < vertex.size(); i++) + { + current_pathweight += + graph[k][vertex.get(i)]; + k = vertex.get(i); + } + current_pathweight += graph[k][s]; + + // update minimum + min_path = Math.min(min_path, + current_pathweight); + + } while (findNextPermutation(vertex)); + + return min_path; +} + +// Function to swap the data +// present in the left and right indices +public static ArrayList swap( + ArrayList data, + int left, int right) +{ + // Swap the data + int temp = data.get(left); + data.set(left, data.get(right)); + data.set(right, temp); + + // Return the updated array + return data; +} + +// Function to reverse the sub-array +// starting from left to the right +// both inclusive +public static ArrayList reverse( + ArrayList data, + int left, int right) +{ + // Reverse the sub-array + while (left < right) + { + int temp = data.get(left); + data.set(left++, + data.get(right)); + data.set(right--, temp); + } + + // Return the updated array + return data; +} + +// Function to find the next permutation +// of the given integer array +public static boolean findNextPermutation( + ArrayList data) +{ + // If the given dataset is empty + // or contains only one element + // next_permutation is not possible + if (data.size() <= 1) + return false; + + int last = data.size() - 2; + + // find the longest non-increasing + // suffix and find the pivot + while (last >= 0) + { + if (data.get(last) < + data.get(last + 1)) + { + break; + } + last--; + } + + // If there is no increasing pair + // there is no higher order permutation + if (last < 0) + return false; + + int nextGreater = data.size() - 1; + + // Find the rightmost successor + // to the pivot + for (int i = data.size() - 1; + i > last; i--) { + if (data.get(i) > + data.get(last)) + { + nextGreater = i; + break; + } + } + + // Swap the successor and + // the pivot + data = swap(data, + nextGreater, last); + + // Reverse the suffix + data = reverse(data, last + 1, + data.size() - 1); + + // Return true as the + // next_permutation is done + return true; +} + +// Driver Code +public static void main(String args[]) +{ + // matrix representation of graph + int graph[][] = {{0, 10, 15, 20}, + {10, 0, 35, 25}, + {15, 35, 0, 30}, + {20, 25, 30, 0}}; + int s = 0; + System.out.println( + travllingSalesmanProblem(graph, s)); +} +}