Understanding Travelling Salesman Time Complexity [closed] - traveling-salesman

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have read from multiple sources and from my understanding of the algorithm that it runs in 2^N time. My question is what causes TSP to achieve this run time? I can't seem to find a pseudo-code so i can examine it.

The algorithm you mean is likely the inclusion-exclusion:
Find the shortest path though the following state space using A*:
the state is defined by the partition of the set of cities to the 'visited' set, 'unvisited' set and the 'current' node.
a valid transition is one that moves one node from 'current' to 'visited' and one from the 'unvisited' to the 'current' set. Its cost is equal the distance from the old 'current' to the new 'current'.
the starting state is: no city is 'visited', an arbitrary city is 'current'.
a finishing state is: no city is 'unvisited', any city is 'current'.
The time complexity of inclusion-exclusion is given by the number of states: there is exactly one 'current' city (factor of n) and all other cities are either visited or unvisited (factor of 2^n).
The 'A*' algorithm will enter each state at most once. For each state, it will explore at most 'n' other nodes and push them into the priority queue. The priority queue will take at most 'O(n)' time to perform its operation.
Thus, the running time is O(2^n * n * n * O(n)) = O(2^n * poly(n)). Further insight shows that O(2^n * poly(n)) is equal to O(2^n).

Related

finding the maximum in a HashSet<u32> in rust? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
how can i find the maximum or minimum value in a HashSet without looping through all the elements in it? Is there any function or one liner way to do so ?
You can do something like this:
let hs: HashSet<u32> = HashSet::from_iter(vec![54, 23, 55, 6, 3, 100]);
let min_value = *hs.iter().min().unwrap();
let max_value = *hs.iter().max().unwrap();
That way you won't have to loop yourself, but internally that's exactly what is going to happen. It's not possible to find the min or max value in a hash set (or a hash map, for that matter), without checking all the values.
BTW there is another (relatively common) set type, BTreeSet, which keeps its values ordered. That would allow you to simply get the first or the last element (via iter().next() or iter().next_back()) and you've got you min/max value w/o any extra work. Performance-wise that would be much faster of course.

is it possible that hash function produces the same hash value for two different inputs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
is it possible that hash function produces the same hash value for two different inputs ? I am not mistaken I have read that hash function is one way encryption, so we cannot go back to the input using the result, and read that even if we change a bit of the ducoment that is directed to be hadhed will give another hash value, so how does collusion happen? and how is it possible for a hash function to produce the same value for two different inputs?
Yes. The range of any hash function is limited by the number of bits. For example, if we were to use a hash function outputting 8 bits, then we only have 256 possible outputs; so if we have more than 256 possible inputs then we are guaranteed to have a collision.
If we use a hash function with a larger range then it becomes much less likely to have a collision; but cannot make it impossible because our inputs are unlimited.
The existence of collisions is part of the reason why these functions are one way: a given output could have been generated from an unlimited number of different inputs.
We should also use a good cryptographic hash function to help avoid collisions from similar input.
You may want to look at the "SHAttered" example of 2 different PDF files that have the same SHA1 hash.

Solving Acceleration for Time with a limited velocity [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm working on a calculator in Excel for interstellar travel times. I'm currently solving an equation for Acceleration for Time to arrival like so:
=sqrt(distance*2/acceleration)
which seems to work fine for me, except that if I give it a large enough acceleration and long enough distance, I get a maximum velocity back that is higher than the speed of light.
What I want to do is add in some limiting factor into the formula that limits the velocity to some number, but I have no idea how to do this in the mathematics (disclaimer: I'm a writer and artist, so I suck badly at math). I think I need to integrate something like V=min(C,d/t) where C is the speed of light, but I don't know how to integrate that into my function. Since the rest of this works without having to chart out periods of time, I'd prefer a solution in the formula rather than some roundabout recursive chart trickery. Any ideas?
The right solution is of course to use the relativistic equation for the velocity after "constant acceleration" (which doesn't exist when you get near the speed of light). I suspect you mean "constant apparent acceleration" (what the passengers in the rocket feel). In that case, relativistically,
v = c * tanh(asinh(F*t/m*c))
Where
v = velocity
F = force
t = time
m = mass
c = speed of light
Then you can write F = m * a so acceleration = F / m
which you can write in Excel (after defining the constant C_ = 3E8 )
= C_ * TANH(ASINH(acceleration*time/C_)
This will initially give you linear acceleration as expected - then it will taper off and never quite reach the speed of light:
It seems to me that this equation is "the right one" for your particular application - you are not really trying to be super accurate, just have something that at least doesn't go faster than the speed of light, and transitions smoothly. In reality, what a rocket motor can do at these very high velocities, how the mass of the rocket is changing - all those things make the math a lot more complicated.
update if you want to achieve a result like the above but only have "distance" and "acceleration", we need to be a little bit tricky. Of course distance is something that depends on your frame of reference - it's different for the people in the rocket vs a stationary observer. So we are going to throw "real physics" out of the window for a minute and do something else. The straight red line in my plot represents "how fast you would be going if you kept accelerating" - this is the velocity of your initial calculation.
You can convert that to the "real" velocity with a simple
limitedVelocity = C_ * TANH(ASINH(calculatedVelocity / C_))
This is more in keeping with the question you asked, and allows you to stay in the framework you had (where you know "acceleration" and "distance" - whatever those mean in your world.)
Relativity. Blows your mind.
Afterthought
An accelerating space ship is in a non inertial frame of reference. The clock on board runs at a different speed (slower) than the "clock in the universe". Inside the spaceship the distance to their destination appears to shrink (Lorentz contraction) as they go faster. All this means that the "real" calculation depends on factors and assumptions that were not explicitly stated in the question. But since this is about an "interstellar travel calculator" by a self-professed non-physicist I think it is better not to turn this into a second year Physics of General Relativity course.
You can use an IF statement:
IF(logical_test, [value_if_true], [value_if_false])
As in:
=IF( sqrt(distance*2/acceleration) > C , C , sqrt(distance*2/acceleration) )

What's the difference between collision resistance and preimage resistance? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
For hash function, what's the difference for collision protection and preimage protection?
from wikipedia: http://en.wikipedia.org/wiki/Cryptographic_hash_function
Properties
Most cryptographic hash functions are designed to take a string of any
length as input and produce a fixed-length hash value. A cryptographic
hash function must be able to withstand all known types of
cryptanalytic attack. As a minimum, it must have the following
properties:
Preimage resistance Given a hash h it should be difficult to find any message m such that h = hash(m). This concept is related to that of one-way
function. Functions that lack this property are vulnerable to preimage
attacks.
Second-preimage resistance Given an input m1 it should be difficult to find another input m2 — where m1 != m2 — such that hash( m1 ) = hash( m2 ). This property is
sometimes referred to as weak collision resistance, and functions that
lack this property are vulnerable to second-preimage attacks.
Collision resistance It should be difficult to find two different messages m1 and m2 such that
hash( m1 ) = hash( m2 ). Such a pair is called a cryptographic hash
collision. This property is sometimes referred to as strong collision
resistance. It requires a hash value at least twice as long as that
required for preimage-resistance, otherwise collisions may be found by
a birthday attack.

Why is this an invalid Turing machine? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
Whilst doing exam revision I am having trouble answering the following question from the book, "An Introduction to the Theory of Computation" by Sipser. Unfortunately there's no solution to this question in the book.
Explain why the following is not a legitimate Turing machine.
M = {
The input is a polynomial p over variables x1, ..., xn
Try all possible settings of x1, ..., xn to integer values
Evaluate p on all of these settings
If any of these settings evaluates to 0, accept; otherwise reject.
}
This is driving me crazy! I suspect it is because the set of integers is infinite? Does this somehow exceed the alphabet's allowable size?
Although this is quite an informal way of describing a Turing machine, I'd say the problem is one of the following:
otherwise reject - i agree with Welbog on that. Since you have a countably infinite set of possible settings, the machine can never know whether a setting on which it evaluates to 0 is still to come, and will loop forever if it doesn't find any - only when such a setting is encountered, the machine may stop. That last statement is useless and will never be true, unless of course you limit the machine to a finite set of integers.
The code order: I would read this pseudocode as "first write all possible settings down, then evaluate p on each one" and there's your problem:
Again, by having an infinite set of possible settings, not even the first part will ever terminate, because there never is a last setting to write down and continue with the next step. In this case, not even can the machine never say "there is no 0 setting", but it can never even start evaluating to find one. This, too, would be solved by limiting the integer set.
Anyway, i don't think the problem is the alphabet's size. You wouldn't use an infinite alphabet since your integers can be written in decimal / binary / etc, and those only use a (very) finite alphabet.
I'm a bit rusty on turing machines, but I believe your reasoning is correct, ie the set of integers is infinite therefore you cannot compute them all. I am not sure how to prove this theoretically though.
However, the easiest way to get your head around Turing machines is to remember "Anything a real computer can compute, a Turing machine can also compute.". So, if you can write a program that given a polynomial can solve your 3 questions, you will be able to find a Turing machine which can also do it.
I think the problem is with the very last part: otherwise reject.
According to countable set basics, any vector space over a countable set is countable itself. In your case, you have a vector space over the integers of size n, which is countable. So your set of integers is countable and therefore it is possible to try every combination of them. (That is to say without missing any combination.)
Also, computing the result of p on a given set of inputs is also possible.
And entering an accepting state when p evaluates to 0 is also possible.
However, since there is an infinite number of input vectors, you can never reject the input. Therefore no Turing machine can follow all of the rules defined in the question. Without that last rule, it is possible.

Resources