Skip to content

Commit

Permalink
Merge branch 'main' into generics
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-dhangar authored Nov 10, 2024
2 parents 8dfc4a6 + ab4e8ec commit ae7d1d6
Show file tree
Hide file tree
Showing 8 changed files with 630 additions and 49 deletions.
129 changes: 129 additions & 0 deletions docs/Rabin-Karp Algorithm/Rabin-Karp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
id: rabin-karp-algorithm
title: "Rabin-Karp Algorithm for Pattern Searching"
sidebar_label: "Rabin-Karp Algorithm"
sidebar_position: 1
description: "Detailed explanation and implementation of the Rabin-Karp algorithm for pattern searching in strings."
tags: [Algorithm, Rabin-Karp, Pattern Matching, String Searching]
---

# Rabin-Karp Algorithm
## Overview
The Rabin-Karp algorithm is a string-searching algorithm that uses hashing to find patterns in text efficiently. It is especially useful for locating multiple occurrences of a pattern within a text and performs well in average cases, though it can degrade in performance if there are hash collisions.

The algorithm's main advantage is its use of a **rolling hash function**, which allows it to re-compute hash values efficiently as it slides over the text.

## Use Cases
- **Plagiarism Detection**: Identifies similar or identical sequences within a document.
- **DNA Sequence Matching**: Finds specific nucleotide patterns within larger DNA sequences.
- **Spam Detection**: Helps locate keywords or patterns within large datasets for filtering purposes.

## Algorithm Details
### Key Concepts
1. **Hashing**: The pattern and text windows are hashed to enable quick comparison.
2. **Rolling Hash**: A technique to compute hash values in constant time by reusing previous hash values as the window slides across the text.
3. **Collision Handling**: To address hash collisions, the algorithm performs an additional string comparison when hashes match.

### Algorithm Complexity
- **Average Time Complexity**: \(O(n + m)\), where \(n\) is the length of the text and \(m\) is the length of the pattern.
- **Worst-Case Complexity**: \(O(n \times m)\) in cases of frequent hash collisions.

## Example Pseudocode
### Rabin-Karp Pattern Search

```cpp
// Given a text T and pattern P, this function finds all occurrences of P in T.

function rabinKarp(text, pattern, prime):
n = length(text)
m = length(pattern)
hashPattern = computeHash(pattern, prime)
hashText = computeHash(text.substring(0, m), prime)

for i from 0 to n - m:
if hashPattern == hashText:
if text.substring(i, i + m) == pattern:
print("Pattern found at index", i)

if i < n - m:
hashText = recomputeHash(text, i, m, hashText, prime)

function computeHash(str, prime):
hashValue = 0
for each character in str:
hashValue = (hashValue * 256 + ASCII(character)) % prime
return hashValue
```
## Code Example in C++: Rabin-Karp Algorithm
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int d = 256; // Number of characters in the input alphabet
void rabinKarpSearch(string text, string pattern, int prime) {
int m = pattern.length();
int n = text.length();
int p = 0; // Hash value for pattern
int t = 0; // Hash value for text
int h = 1;
// Calculate the value of h (d^(m-1)) % prime
for (int i = 0; i < m - 1; i++)
h = (h * d) % prime;
// Calculate the hash value of pattern and first window of text
for (int i = 0; i < m; i++) {
p = (d * p + pattern[i]) % prime;
t = (d * t + text[i]) % prime;
}
// Slide the pattern over text one character at a time
for (int i = 0; i <= n - m; i++) {
// Check the hash values of current window of text and pattern
if (p == t) {
// If the hash values match, check the actual characters
bool match = true;
for (int j = 0; j < m; j++) {
if (text[i + j] != pattern[j]) {
match = false;
break;
}
}
if (match)
cout << "Pattern found at index " << i << endl;
}
// Calculate hash value for the next window of text
if (i < n - m) {
t = (d * (t - text[i] * h) + text[i + m]) % prime;
if (t < 0)
t = (t + prime);
}
}
}
int main() {
string text = "GEEKS FOR GEEKS";
string pattern = "GEEK";
int prime = 101; // A prime number
rabinKarpSearch(text, pattern, prime);
return 0;
}
```

## Explanation of the Code
- Hash Initialization: Calculates initial hash values for both the pattern and the first window of text.
- Hash Comparison: If the hash values match, the algorithm checks the actual text to confirm a match.
- Hash Update: Uses a rolling hash to efficiently update the hash value as it slides through the text, maintaining constant time complexity for each shift.

## Example Walkthrough
Consider the text "GEEKS FOR GEEKS" and the pattern "GEEK". The algorithm computes the initial hash for "GEEK" and matches it against the rolling hash of each substring in the text, sliding character-by-character to check each window.

## Real-World Example
The Rabin-Karp algorithm is widely used in applications requiring fast, efficient pattern matching. Examples include searching for specific words or sequences in large documents or databases, identifying duplicate code snippets, and finding malicious patterns within network packets.

By adding the Rabin-Karp algorithm, we enhance the Algo repository with a fast and robust solution for text searching, useful in both educational contexts and practical applications like data processing and network security.
133 changes: 133 additions & 0 deletions docs/algorithms/Searching Algorithms/PrioritySearch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---

id: priority-search-algorithm
sidebar_position: 5
title: Priority Search algorithm
sidebar_label: Priority Search algorithm

---

## Definition 📖

**Priority search** is a search algorithm that is used for efficiently finding the closest point in a 2D plane to a given query point, using an ordered set of rectangles. It is particularly useful in scenarios where you need to search a large set of rectangles or intervals efficiently. The algorithm works by partitioning a set of 2D rectangles or intervals using priority values, allowing for faster querying than a brute-force search.

## Characteristics ✨

- **Efficient for Rectangles**:
- Priority search is designed specifically for searching within a set of rectangles in a 2D plane, where each rectangle is associated with a priority value.

- **Optimized for Range Queries**:
- The algorithm helps efficiently find the rectangle with the smallest priority value that contains a given point.

- **Use of Data Structures**:
- The algorithm typically uses a combination of data structures such as a priority search tree (a variant of a binary search tree) or a segment tree to optimize the search process.

- **Ordered Rectangles**:
- The rectangles are sorted in terms of their priority values, which enables the search process to be faster and more efficient.

## Time Complexity ⏱️

- **Best Case: `O(log n)`** 🌟
If the target rectangle is found early in the search process, it leads to an efficient query time.

- **Average Case: `O(log n)`** 🔄
In general, the search algorithm operates in logarithmic time complexity, making it faster than brute-force search methods.

- **Worst Case: `O(n)`** 💥
In the worst-case scenario, if the query point lies outside the regions covered by the rectangles or if all rectangles need to be checked, the time complexity could degrade to linear time.

## Space Complexity 💾

- **Space Complexity: `O(n)`**
Priority search typically requires space for storing the rectangles and their associated priority values, along with any auxiliary data structures (such as a tree or heap) used for efficient querying.

## C++ Implementation 💻

Here’s a simple implementation of priority search in C++:

```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Rectangle {
int x1, y1, x2, y2, priority;
};

int prioritySearch(const vector<Rectangle>& rectangles, int targetX, int targetY) {
// Sort rectangles by priority
vector<Rectangle> sortedRectangles = rectangles;
sort(sortedRectangles.begin(), sortedRectangles.end(), [](const Rectangle& a, const Rectangle& b) {
return a.priority < b.priority;
});

// Find the first rectangle that contains the target point
for (const auto& rect : sortedRectangles) {
if (targetX >= rect.x1 && targetX <= rect.x2 && targetY >= rect.y1 && targetY <= rect.y2) {
return rect.priority;
}
}
return -1; // No rectangle contains the point
}

int main() {
vector<Rectangle> rectangles = {
{0, 0, 2, 2, 1},
{1, 1, 3, 3, 2},
{2, 2, 4, 4, 3}
};
int targetX = 2, targetY = 2;

int priority = prioritySearch(rectangles, targetX, targetY);

if (priority != -1)
cout << "Rectangle found with priority " << priority << endl;
else
cout << "No rectangle contains the point" << endl;

return 0;
}
```
## Applications of Priority Search 🌐

**Geographical Information Systems (GIS):**

- Used to find the closest feature (like a region or building) in a GIS, given a point location.

**Computer Graphics:**

- In graphics applications, priority search can be useful for collision detection or determining the nearest object in a 2D plane.

**Database Management:**

- Priority search is used in databases to efficiently query for spatial data that is organized by priority or weight.

**Computer-Aided Design (CAD):**

- Used in CAD tools to find the nearest object or region that meets certain conditions (such as priority).

## Advantages and Disadvantages
### Advantages: ✅

**Efficient for Sorted Data:**
- When rectangles are sorted by priority, the search can be much faster than a brute-force approach, with logarithmic time complexity in the average case.

**Low Memory Overhead:**
- The space complexity is manageable, requiring only space for storing the rectangles and any auxiliary data structures.

**Optimized for Range Queries:**
- Priority search is optimized for finding the nearest object in a range, making it ideal for applications like GIS and CAD.

### Disadvantages: ⚠️

**Requires Sorted Data:**
- The efficiency of the algorithm depends on the rectangles being sorted by priority. If the data is not sorted, additional effort is required to sort it, which can reduce the overall performance.

**Limited to 2D Search:**
- Priority search is primarily designed for 2D spatial queries and may not be suitable for higher-dimensional data without modification.

## Summary
Priority Search is an algorithm used for efficiently finding the closest rectangle to a query point in a 2D plane, where each rectangle is associated with a priority value. It sorts the rectangles by priority and searches for the one that contains the query point.


71 changes: 71 additions & 0 deletions docs/languages/SQL/sql-30.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
id: sql-create-database
sidebar_position: 30
title: "SQL CREATE DATABASE Statement"
sidebar_label: "SQL CREATE DATABASE"
description: "The SQL CREATE DATABASE statement is used to create a new database."
tags: [sql, dbms, database, create database]
---

The `CREATE DATABASE` statement in SQL is used to create a new database. It allows you to specify the name of the database and, in some SQL systems, additional configuration options such as character set, collation, and storage settings.

### Syntax

```sql
CREATE DATABASE database_name;
```

### Key Points
* Database Name: Must be unique within the SQL server.
* Permissions: Creating a database usually requires administrative or specific permissions.
* Optional Clauses: Some SQL systems support additional options like CHARACTER SET and COLLATE to define the database encoding and collation.

### Example

**Example 1: Basic CREATE DATABASE Statement**

Create a database named StoreDB.

```sql
CREATE DATABASE StoreDB;
```

**Example 2: CREATE DATABASE with Character Set and Collation**

Create a database named CustomerDB with specific character set and collation (supported in certain SQL systems like MySQL).

```sql
CREATE DATABASE CustomerDB
CHARACTER SET utf8
COLLATE utf8_general_ci;
```

**Example 3: Checking for Database Existence**

To avoid errors when creating a database that might already exist, use a conditional check (supported in certain databases).

```sql
CREATE DATABASE IF NOT EXISTS InventoryDB;
```

### Viewing Created Databases

After creating a database, you can view a list of databases using:

```sql
SHOW DATABASES; -- MySQL
```

Or, in SQL Server:

```sql
SELECT name
FROM sys.databases;
```

**Notes :**
* Database Creation: The CREATE DATABASE statement is supported in most SQL-based databases like MySQL, PostgreSQL, and SQL Server, but syntax may vary slightly.
* Modifying Databases: Once created, you can alter database settings with ALTER DATABASE.
* Dropping Databases: Use DROP DATABASE database_name; to remove a database, but be cautious as this action is irreversible.

The CREATE DATABASE statement is essential for setting up new databases within an SQL server, laying the foundation for organizing and managing data.
61 changes: 61 additions & 0 deletions docs/languages/SQL/sql-31.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
id: sql-drop-database
sidebar_position: 31
title: "SQL DROP DATABASE Statement"
sidebar_label: "SQL DROP DATABASE"
description: "The SQL DROP DATABASE statement is used to delete an existing database."
tags: [sql, dbms, database, drop database]
---

The `DROP DATABASE` statement in SQL is used to delete an existing database, along with all its tables, data, and associated objects. This action is irreversible, so it should be used with caution.

### Syntax

```sql
DROP DATABASE database_name;
```

### Key Points
* Permanent Deletion: The database and all its data are permanently deleted.
* Permissions Required: Dropping a database generally requires administrative privileges.
* Safety Precaution: Be certain before executing this command, as it cannot be undone.

### Example

**Example 1: Basic DROP DATABASE Statement**

Delete a database named StoreDB.

```sql
DROP DATABASE StoreDB;
```

**Example 2: Conditionally Dropping a Database**

To avoid errors when attempting to drop a database that may not exist, you can use a conditional check (supported in certain databases).

```sql
DROP DATABASE IF EXISTS CustomerDB;
```

### Viewing Available Databases

Before deleting a database, you may want to view a list of existing databases to ensure accuracy:

```sql
SHOW DATABASES; -- MySQL
```

Or, in SQL Server:

```sql
SELECT name
FROM sys.databases;
```

**Notes :**
* Data Loss Warning: Dropping a database will delete all data and schema definitions within it. This action is irreversible.
* Database Backup: It is recommended to create a backup of any critical data before using DROP DATABASE.
* Compatibility: The DROP DATABASE statement is supported across most SQL systems, though syntax may vary slightly.

The DROP DATABASE statement is a powerful command for removing databases when they are no longer needed, but it should be used with caution to prevent accidental data loss.
Loading

0 comments on commit ae7d1d6

Please sign in to comment.