Trying to come up with simple algorithm of significant time complexity in Java

In summary, the conversation discusses the need for algorithms that will take a significant amount of time for computers to process, rather than being solved quickly. Examples of such algorithms include recursively computing Fibonacci numbers and solving NP problems such as the 3SAT problem and traveling salesman problem, which have a time complexity of O(2n) and can bring computers to their knees when the input size is large.
  • #1
frankfjf
168
0
Hi PF,

I'm working on a program that requires measuring how long it takes a given computer to process a certain task, but am having trouble coming up with algorithms that won't take most computers a trivial amount of time to perform. The only one I've got so far is recursively computing Fibonacci numbers.

Can anyone suggest similarly simple methods that will tie up a computer for more than an instant? It's fine if the method only slows things down for large inputs.
 
Technology news on Phys.org
  • #2
You need an NP problem! Here's a list of some good hard (for a computer) algorithms. Last year I implemented a 3SAT-Solver and brought my computer to it's knees. It wasn't too hard. A 3SAT problem is a boolean satisfiability problem, given a conjunction of clauses

i.e.

(a OR b OR ~c) AND (b OR c OR ~d) AND (...) AND (...) etc

What is an assignment you can give to a, b, c, d, etc that will make the expression evaluate to true?

The traveling salesman problem is another famous one. Given a set of cities with some cost between each one, what is the route that visits all cities once (hamiltonian path) with the least cost. You can represent the cities and connecting paths with a graph and write a graph traversal algorithm to find hamiltonian paths, which is computationally costly in the first place, and you need to find every possible hamiltonian path before you can be sure that you have the one with least overall cost.

These problems have time complexity of O(2n) where n is the size of the input, so you can see that as soon as n gets even remotely big, the time it takes to solve these problems gets very, very, very, very, very big!
 

FAQ: Trying to come up with simple algorithm of significant time complexity in Java

1. What is time complexity and why is it important in algorithm design?

Time complexity is a measure of the amount of time an algorithm takes to run as a function of the input size. It is important in algorithm design because it helps us analyze and compare the efficiency of different algorithms. A lower time complexity means an algorithm will run faster, making it more efficient.

2. How do I determine the time complexity of a Java algorithm?

To determine the time complexity of a Java algorithm, you can look at the number of operations performed in the worst-case scenario. This is often denoted by the "Big O" notation. For example, an algorithm with a time complexity of O(n) means that the number of operations increases linearly with the size of the input.

3. How can I come up with a simple algorithm with significant time complexity in Java?

One approach to coming up with a simple algorithm with significant time complexity in Java is to focus on the input size and try to minimize the number of operations needed to solve the problem. This could involve using efficient data structures and algorithms, avoiding nested loops, and optimizing loops by reducing the number of iterations.

4. Can I improve the time complexity of an existing Java algorithm?

Yes, it is possible to improve the time complexity of an existing Java algorithm. This could involve rethinking the approach to the problem and finding more efficient ways to solve it. Additionally, using different data structures or implementing the algorithm in a different language could also potentially improve its time complexity.

5. How does time complexity affect the performance of a Java program?

The time complexity of a Java program directly affects its performance. A program with a lower time complexity will run faster and more efficiently, while a higher time complexity could result in slower execution and potentially even cause the program to crash when dealing with large inputs. It is important to consider time complexity when designing and implementing algorithms in Java to ensure optimal performance.

Similar threads

Replies
7
Views
3K
Replies
5
Views
13K
Replies
15
Views
2K
Replies
4
Views
800
Replies
1
Views
2K
Replies
2
Views
1K
Back
Top