Questions tagged [searching]
79 questions
15
votes
9 answers
How to find 5 repeated values in O(n) time?
Suppose you have an array of size $n \geq 6$ containing integers from $1$ to $n − 5$, inclusive, with exactly five repeated. I need to propose an algorithm that can find the repeated numbers in $O(n)$ time. I cannot, for the life of me, think of…
darylnak
- 275
- 2
- 7
9
votes
1 answer
Connection between KMP prefix function and string matching automaton
Let $A_P = (Q,\Sigma,\delta,0,\{m\})$ the string matching automaton for pattern $P \in \Sigma^m$, that is
$Q = \{0,1,\dots,m\}$
$\delta(q,a) = \sigma_P(P_{0,q}\cdot a)$ for all $q\in Q$ and $a\in \Sigma$
with $\sigma_P(w)$ the length of the…
Bob
- 93
- 5
8
votes
1 answer
Complexities of basic operations of searching and sorting algorithms
Wiki has a good cheat sheet, but however it does not involve no. of comparisons or swaps. (though no. of swaps is usually decides its complexity). So I created the following. Is the following info is correct ? Please let me know if there is any…
avi
- 1,473
- 4
- 24
- 39
7
votes
2 answers
Check if adding an edge to a DAG results in a cycle
On the begining: It is a programming contest problem, but not from on-going one. Unfortunatelly, I can't provide any link to this task, because it is not publically available. It was from one of the Polish local programming contest in 2011 organised…
J. Abraham
- 183
- 1
- 7
7
votes
1 answer
Building cycle in rectangle
I have to build a cycle with fixed length $n$ that includes exactly $k$ corners inside $w$ x $h$ rectangle.
For example:
$w = 5\\h=3$
$n = 12\\k = 6$
I have already found out that I need at least $4$ corners and number of corners and number of…
A J
- 83
- 5
6
votes
0 answers
When does greediness guarantee optimality?
I was wondering if there is any theoretical results characterizing under what condition does greedy algorithm actually finds the optimal solution.
Here is a motivating example. Suppose you are trying to find
$$\min_{x \in \mathbb{Z}^2} f(x)$$
where…
ChubbyRuby
- 61
- 1
6
votes
1 answer
What other involutions are there besides xor?
There is a classic problem of finding the only number that occurs an odd number of times in a list. The solution is to xor everything and the result is the requested number.
The key properties used here are the involution of xor (i.e. a number's…
Paul92
- 528
- 3
- 6
6
votes
2 answers
Understanding Levin's Universal Search
I am having troubles understanding Levin's universal search method. In Scholarpedia, http://www.scholarpedia.org/article/Universal_search, it is claimed that “If there exists a program $p$, of length $l(p)$, that can solve the problem in time $t$,…
Don Arturo
- 91
- 1
- 4
4
votes
1 answer
Comparing A* search to Simulated Annealing
Good Afternoon,
I am comparing A* search to Simulated Annealing for an assignment, mainly the algorithms, memory complexity, choice of next actions, and optimality. Now, I am not 100% sure about my answer, and was wondering if someone could give me…
noob
- 41
- 2
4
votes
1 answer
How to prove that average complexity is N/2 for linear search in the unsorted array
All tutorials on algorithms show the complexity for the linear search in the unsorted array in the average case as N/2. I understand that the average case means the items in the list are randomly distributed.
Can anyone show how I would arrive at…
Max Koretskyi
- 325
- 1
- 4
- 14
4
votes
1 answer
Does this A-Star heuristic already exist?
I've been thinking about the A* algorithm recently. For context, A* is a graph-navigating algorithm most often used to solve problems that go "What is the shortest path from point A to point B?". It's based on a distance-estimating heuristic you…
Narrateur du chaos
- 247
- 1
- 9
3
votes
5 answers
Chess Knight minimum moves to destination on an infinite board
There are tones of solutions for Knights tour or shortest path for Knights movement from source cell to destination cell. most of the solutions are using BFS which seems the best algorithm.
Here is my implementation using HashMap:
public class…
Amir-Mousavi
- 268
- 2
- 13
3
votes
1 answer
Average-case complexity of linear search where half of the elements in the array are duplicates
I know that for an array of size n distinct elements, the Average Case complexity for linear search is as follows:
A(n) = $\frac{n + 1}{2}$
However, I am having trouble coming up with the Average Case complexity in the case where half of the…
M. Twain
- 33
- 3
3
votes
1 answer
A* 8-puzzle problem worst case memory usage
We are testing the A* algorithm with Hamming and Manhattan on the 8-puzzle (and its natural generalization n-puzzle) problem.
We have to answer the following question but I can't figure out what it should be.
Our assignment is derived from this.…
Principis
- 31
- 2
3
votes
2 answers
Graphs: Dectect a sink in $\mathcal{O}(V)$
Given a directed conected graph which representation is its adjacency matrix $A$, design an algorithm to detect a sink in $\mathcal{O}(V)$ time, being $V$ the number of vertices.
As definitions can vary, in this context, a sink is defined as a…
Álvaro G. Tenorio
- 335
- 2
- 15