Skip to content

Commit 850185b

Browse files
authored
Merge pull request #2598 from Aditi22Bansal/main
Solved #2592- Fractal Search
2 parents 2d3ebb7 + ad0fffe commit 850185b

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
id: Fractal-Search-Algorithm
3+
title: Fractal Search Algorithm
4+
sidebar_label: Fractal Search Algorithm
5+
tags:
6+
- Advanced
7+
- Search Algorithms
8+
- Fractals
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a detailed explanation and implementation of the Fractal Search Algorithm."
15+
16+
---
17+
18+
## What is the Fractal Search Algorithm?
19+
20+
The Fractal Search Algorithm (FSA) is an advanced search algorithm inspired by the fractal nature of various processes and patterns in nature. It leverages the self-similarity and recursive properties of fractals to efficiently search through complex spaces.
21+
22+
## Algorithm for Fractal Search
23+
24+
1. **Initialization**: Define the search space and initialize the fractal pattern.
25+
2. **Recursive Division**: Recursively divide the search space into smaller subspaces following the fractal pattern.
26+
3. **Search Subspaces**: Evaluate the subspaces to find the target element.
27+
4. **Merge Results**: Combine the results from the subspaces to determine the final position of the target element.
28+
29+
## How does Fractal Search work?
30+
31+
- FSA divides the search space into self-similar subspaces recursively.
32+
- It searches each subspace individually, combining results to identify the target element.
33+
- This approach reduces the search space significantly, improving efficiency.
34+
35+
36+
## Problem Description
37+
38+
Given a complex search space, implement the Fractal Search Algorithm to find the target element. If the element is not present, the algorithm should indicate that as well.
39+
40+
## Examples
41+
42+
**Example 1:**
43+
Input:
44+
search_space = [1, 3, 5, 7, 9]
45+
target = 5
46+
Output: 2
47+
48+
**Example 2:**
49+
Input:
50+
search_space = [2, 4, 6, 8, 10]
51+
target = 7
52+
Output: -1
53+
54+
## Your Task
55+
56+
Complete the function `fractal_search()` which takes a list `search_space` and an integer `target` as input parameters and returns the index of the target element. If the element is not present, return -1.
57+
58+
Expected Time Complexity: $O(\log n)$
59+
Expected Auxiliary Space: $O(n)$
60+
61+
## Constraints
62+
63+
- $1 <= n <= 10^6$
64+
- $1 <= search_space[i] <= 10^9$
65+
- $1 <= target <= 10^9$
66+
67+
## Implementation
68+
69+
```python
70+
import numpy as np
71+
72+
def fractal_search(search_space, target):
73+
def recursive_search(subspace, depth):
74+
if len(subspace) == 0:
75+
return -1
76+
77+
mid_index = len(subspace) // 2
78+
mid_value = subspace[mid_index]
79+
80+
if mid_value == target:
81+
return mid_index
82+
83+
if target < mid_value:
84+
return recursive_search(subspace[:mid_index], depth + 1)
85+
else:
86+
result = recursive_search(subspace[mid_index + 1:], depth + 1)
87+
return mid_index + 1 + result if result != -1 else -1
88+
89+
return recursive_search(search_space, 0)
90+
91+
# Example usage:
92+
search_space = [1, 3, 5, 7, 9]
93+
target = 5
94+
print(fractal_search(search_space, target)) # Output: 2
95+
```
96+
97+
```cpp
98+
#include <iostream>
99+
#include <vector>
100+
101+
int fractal_search(const std::vector<int>& search_space, int target) {
102+
int recursive_search(const std::vector<int>& subspace, int depth) {
103+
if (subspace.empty()) {
104+
return -1;
105+
}
106+
107+
int mid_index = subspace.size() / 2;
108+
int mid_value = subspace[mid_index];
109+
110+
if (mid_value == target) {
111+
return mid_index;
112+
}
113+
114+
if (target < mid_value) {
115+
return recursive_search({subspace.begin(), subspace.begin() + mid_index}, depth + 1);
116+
} else {
117+
int result = recursive_search({subspace.begin() + mid_index + 1, subspace.end()}, depth + 1);
118+
return result != -1 ? mid_index + 1 + result : -1;
119+
}
120+
}
121+
122+
return recursive_search(search_space, 0);
123+
}
124+
125+
// Example usage:
126+
int main() {
127+
std::vector<int> search_space = {1, 3, 5, 7, 9};
128+
int target = 5;
129+
std::cout << fractal_search(search_space, target) << std::endl; // Output: 2
130+
return 0;
131+
}
132+
```
133+
134+
```java
135+
import java.util.List;
136+
137+
public class FractalSearch {
138+
public static int fractalSearch(List<Integer> search_space, int target) {
139+
int recursiveSearch(List<Integer> subspace, int depth) {
140+
if (subspace.isEmpty()) {
141+
return -1;
142+
}
143+
144+
int midIndex = subspace.size() / 2;
145+
int midValue = subspace.get(midIndex);
146+
147+
if (midValue == target) {
148+
return midIndex;
149+
}
150+
151+
if (target < midValue) {
152+
return recursiveSearch(subspace.subList(0, midIndex), depth + 1);
153+
} else {
154+
int result = recursiveSearch(subspace.subList(midIndex + 1, subspace.size()), depth + 1);
155+
return result != -1 ? midIndex + 1 + result : -1;
156+
}
157+
}
158+
159+
return recursiveSearch(search_space, 0);
160+
}
161+
162+
public static void main(String[] args) {
163+
List<Integer> search_space = List.of(1, 3, 5, 7, 9);
164+
int target = 5;
165+
System.out.println(fractalSearch(search_space, target)); // Output: 2
166+
}
167+
}
168+
```
169+
170+
```javascript
171+
function fractalSearch(search_space, target) {
172+
function recursiveSearch(subspace, depth) {
173+
if (subspace.length === 0) {
174+
return -1;
175+
}
176+
177+
const midIndex = Math.floor(subspace.length / 2);
178+
const midValue = subspace[midIndex];
179+
180+
if (midValue === target) {
181+
return midIndex;
182+
}
183+
184+
if (target < midValue) {
185+
return recursiveSearch(subspace.slice(0, midIndex), depth + 1);
186+
} else {
187+
const result = recursiveSearch(subspace.slice(midIndex + 1), depth + 1);
188+
return result !== -1 ? midIndex + 1 + result : -1;
189+
}
190+
}
191+
192+
return recursiveSearch(search_space, 0);
193+
}
194+
195+
// Example usage:
196+
const search_space = [1, 3, 5, 7, 9];
197+
const target = 5;
198+
console.log(fractalSearch(search_space, target)); // Output: 2
199+
```
200+
201+
# Complexity Analysis
202+
### Time Complexity: $O(\log n)$, where $n$ is the number of elements in the search space. The recursive division reduces the search space exponentially.
203+
### Space Complexity: $O(n)$, due to the additional space required for recursive function calls and subspaces.
204+
# Advantages and Disadvantages
205+
## Advantages:
206+
207+
Efficient for large and complex search spaces due to the recursive division.
208+
Exploits the self-similarity of fractals, making it suitable for certain types of data structures.
209+
## Disadvantages:
210+
211+
More complex to implement compared to traditional search algorithms.
212+
Performance may vary depending on the nature of the search space and fractal pattern used.
213+
### References
214+
Wikipedia: Fractal
215+
Research Paper: Fractal Search Algorithm

0 commit comments

Comments
 (0)