Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Integer> vertex =
new ArrayList<Integer>();

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<Integer> swap(
ArrayList<Integer> 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<Integer> reverse(
ArrayList<Integer> 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<Integer> 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));
}
}
41 changes: 26 additions & 15 deletions src/main/java/com/thealgorithms/searches/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
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.
*
* <p>
* 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
* @param key is an element which should be found
* @param <T> is any comparable type
* @return index of the element
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
return search(array, key, 0, array.length - 1);
}
Expand All @@ -43,8 +37,9 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
*/
private <T extends Comparable<T>> 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]);
Expand All @@ -66,17 +61,33 @@ 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)];

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
);
}
}

Loading