Algorithm for what should be a simple problem

  • Thread starter Thread starter SpiffyEh
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary

Discussion Overview

The discussion revolves around finding an index \( i \) in a sorted sequence of distinct integers such that \( a_i = i \). Participants are exploring algorithmic approaches to solve this problem efficiently, specifically aiming for an \( O(\log n) \) solution.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Technical explanation

Main Points Raised

  • One participant suggests using a tree structure to approach the problem by splitting the sequence in half with each iteration.
  • Another participant notes that halving the sequence repeatedly results in \( O(\log n) \) but expresses uncertainty about how this leads to a solution.
  • A third participant outlines a recursive method, proposing to check the middle element of the list and decide whether to search in the lower or upper half based on the comparison of the middle element and its index.
  • The example provided illustrates the recursive process, showing how to narrow down the search based on the values of the elements and their indices.

Areas of Agreement / Disagreement

Participants are generally exploring similar approaches to the problem, but there is no consensus on the best method or whether the proposed solutions are correct. Uncertainty remains regarding the effectiveness of the outlined strategies.

Contextual Notes

Some assumptions about the properties of the sequence (e.g., distinct integers, sorted order) are implicit in the discussion. The mathematical steps in the proposed solutions are not fully resolved, leaving room for further exploration.

Who May Find This Useful

This discussion may be useful for students or individuals interested in algorithm design, particularly those studying binary search techniques and recursive problem-solving strategies.

SpiffyEh
Messages
191
Reaction score
0

Homework Statement



Suppose that you are given a sorted sequence of distinct integers {a1, a2, . . . , an}.
Give an O(lg n) algorithm to determine whether there exists an i index such as
ai = i. For example, in {−10,−3, 3, 5, 7}, a3 = 3. In {2, 3, 4, 5, 6, 7}, there is no
such i.

Homework Equations





The Attempt at a Solution


could this be done using a tree? For example splitting the problem in half with every iteration and checking to see if it is in the end? Or am I approaching this wrong?
 
Physics news on Phys.org
O(ln) usually implies a divide-conquer approach
Try the half eah sequence and see what O() that has
 
i know halving it repeatedly gives O(log n) but I don't see how that gets me anywhere
 
Here's a kind of rough outline. You have a sorted list of n numbers. Look at the middle of the list, and see whether an/2 == n/2. If so, you're done.

If an/2 < n/2, look in the lower half of the list.
If an/2 > n/2, look in the upper half of the list.

Do the above recursively, stopping when the size of the list is 1.

For example, suppose the list is {−10,−3, 3, 5, 7, 8 11, 12}. (I added a few numbers to your first example.)

Here n = 8

a4 = 5, so we should look in the lower half of the list; i.e., a1 through a4.

Now n = 4
a2 = -3, so we should look in the upper half of this list; namely, at a3 and a4

Now n = 2, and we see that a3 == 3 and we're done.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
33
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
6K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K