From fb694f16596e87d8c9297978129555f8cd5e00bd Mon Sep 17 00:00:00 2001 From: Basavaraja K J <97227305+basavarajakj@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:59:09 +0530 Subject: [PATCH 1/2] added C solution for valid parentheses --- StackQueues/Stack/Valid-Parentheses/Program.c | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 StackQueues/Stack/Valid-Parentheses/Program.c diff --git a/StackQueues/Stack/Valid-Parentheses/Program.c b/StackQueues/Stack/Valid-Parentheses/Program.c new file mode 100644 index 00000000..afa370ca --- /dev/null +++ b/StackQueues/Stack/Valid-Parentheses/Program.c @@ -0,0 +1,93 @@ +#include +#include +#include + +#define MAX 1000 + +typedef struct { + char arr[MAX]; + int top; +} Stack; + +// Function to initialize the stack +void initStack(Stack *s) { + s->top = -1; +} + +// Function to check if the stack is empty +bool isEmpty(Stack *s) { + return s->top == -1; +} + +// Function to push an element onto the stack +bool push(Stack *s, char item) { + if (s->top >= MAX - 1) { + return false; // Stack overflow + } + s->arr[++s->top] = item; + return true; +} + +// Function to pop an element from the stack +char pop(Stack *s) { + if (isEmpty(s)) { + return '\0'; // Return a null character if stack is empty + } + return s->arr[s->top--]; +} + +// Function to check if the brackets are valid +bool isValid(char *s) { + Stack stack; + initStack(&stack); + + for (int i = 0; s[i] != '\0'; i++) { + char current = s[i]; + + if (current == '(' || current == '{' || current == '[') { + push(&stack, current); + } else { + if (isEmpty(&stack)) { + return false; // No matching opening bracket + } + char top = pop(&stack); + if ((current == ')' && top != '(') || + (current == '}' && top != '{') || + (current == ']' && top != '[')) { + return false; // Mismatched brackets + } + } + } + + return isEmpty(&stack); // Return true if stack is empty (all brackets matched) +} + +// Example usage +int main() { + char *testCases[] = { + "(){}[]", // Valid + "(]", // Invalid + "{[()]}", // Valid + "([{}])", // Valid + }; + + int numTestCases = sizeof(testCases) / sizeof(testCases[0]); + + for (int i = 0; i < numTestCases; i++) { + printf("Test Case %d: \"%s\" -> ", i + 1, testCases[i]); + if (isValid(testCases[i])) { + printf("The string is valid.\n"); + } else { + printf("The string is invalid.\n"); + } + } + + return 0; +} + +/* + Test Case 1: "(){}[]" -> The string is valid. + Test Case 2: "(]" -> The string is invalid. + Test Case 3: "{[()]}" -> The string is valid. + Test Case 4: "([{}])" -> The string is valid. +*/ \ No newline at end of file From 1fd16706a6a5065fec0d3a32bd39898c8d71bb60 Mon Sep 17 00:00:00 2001 From: Basavaraja K J <97227305+basavarajakj@users.noreply.github.com> Date: Thu, 24 Oct 2024 09:29:39 +0000 Subject: [PATCH 2/2] Added Dynamic Threshold Clustering(DTC) algorithm --- .../Program.c | 102 ++++++++++++++++++ .../Readme.md | 58 ++++++++++ 2 files changed, 160 insertions(+) create mode 100644 Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Program.c create mode 100644 Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Readme.md diff --git a/Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Program.c b/Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Program.c new file mode 100644 index 00000000..14ff23fb --- /dev/null +++ b/Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Program.c @@ -0,0 +1,102 @@ +#include +#include +#include + +#define MAX_POINTS 1000 // Maximum number of data points +#define INITIAL_RADIUS 1.0 // Initial radius for neighbor search +#define MIN_DENSITY 2 // Minimum density to form a cluster +#define MIN_CLUSTER_SIZE 2 // Minimum size for a cluster + +// Structure to represent a point in 2D space +typedef struct { + double x; // x-coordinate + double y; // y-coordinate + int cluster_id; // Cluster identifier +} Point; + +// Function to calculate the Euclidean distance between two points +double euclidean_distance(Point a, Point b) { + return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); +} + +// Function to find neighbors of a given point within a specified radius +int find_neighbors(Point* data, int num_points, Point point, double radius, int* neighbors) { + int count = 0; // Count of neighbors found + for (int i = 0; i < num_points; i++) { + // Check if the distance to the current point is within the radius + if (euclidean_distance(point, data[i]) <= radius) { + neighbors[count++] = i; // Add the index of the neighbor + } + } + return count; // Return the total number of neighbors found +} + +// Function to calculate the dynamic threshold for clustering +double calculate_dynamic_threshold(Point* data, int num_points) { + // Simple example: average distance between all pairs of points + double total_distance = 0; + int count = 0; + for (int i = 0; i < num_points; i++) { + for (int j = i + 1; j < num_points; j++) { + total_distance += euclidean_distance(data[i], data[j]); + count++; + } + } + return count > 0 ? total_distance / count : INITIAL_RADIUS; // Return average distance or initial radius +} + +// Main function to perform dynamic threshold clustering +void dynamic_threshold_clustering(Point* data, int num_points, int* clusters, int* noise) { + int cluster_id = 0; // Initialize cluster identifier + int visited[MAX_POINTS] = {0}; // Array to track visited points + double dynamic_threshold = calculate_dynamic_threshold(data, num_points); // Calculate the dynamic threshold + + // Iterate through each point in the dataset + for (int i = 0; i < num_points; i++) { + if (visited[i]) continue; // Skip already visited points + + int neighbors[MAX_POINTS]; // Array to store indices of neighbors + int neighbor_count = find_neighbors(data, num_points, data[i], INITIAL_RADIUS, neighbors); // Find neighbors + + // If the number of neighbors meets the dynamic threshold + if (neighbor_count >= dynamic_threshold) { + clusters[i] = cluster_id; // Assign cluster ID to the current point + cluster_id++; // Increment cluster ID + + // Assign cluster ID to all neighboring points + for (int j = 0; j < neighbor_count; j++) { + int neighbor_index = neighbors[j]; + visited[neighbor_index] = 1; // Mark as visited + clusters[neighbor_index] = cluster_id - 1; // Assign the same cluster ID + } + } else { + noise[i] = 1; // Mark current point as noise + } + visited[i] = 1; // Mark current point as visited + } +} + +int main() { + // Example dataset with points + Point data[MAX_POINTS] = { + {1.0, 2.0}, {1.1, 2.1}, {10.0, 10.0}, {10.5, 10.5}, {5.0, 5.0} + }; + int num_points = 5; // Number of points in the dataset + int clusters[MAX_POINTS] = {-1}; // Array to store cluster IDs (-1 indicates unassigned) + int noise[MAX_POINTS] = {0}; // Array to mark noise points (0 indicates not noise) + + // Perform clustering + dynamic_threshold_clustering(data, num_points, clusters, noise); + + // Output the clustering results + printf("Clusters:\n"); + for (int i = 0; i < num_points; i++) { + if (clusters[i] != -1) { + printf("Point (%.1f, %.1f) -> Cluster %d\n", data[i].x, data[i].y, clusters[i]); + } else { + printf("Point (%.1f, %.1f) is noise\n", data[i].x, data[i].y); + } + } + + return 0; // End of program +} diff --git a/Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Readme.md b/Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Readme.md new file mode 100644 index 00000000..1652f669 --- /dev/null +++ b/Machine_Learning_Algorithms/Dynamic Threshold Clustering(DTC)/Readme.md @@ -0,0 +1,58 @@ +# Dynamic Threshold Clustering (DTC) + +Dynamic Threshold Clustering (DTC) is an innovative unsupervised learning algorithm designed for grouping similar data points based on local density. This algorithm dynamically adjusts its clustering threshold, making it particularly effective for a variety of datasets and applications. + +## Overview + +DTC is a density-based clustering algorithm that: +- Identifies clusters in a dataset by assessing the local density of points. +- Dynamically adjusts the threshold for cluster formation based on the distribution of data. +- Effectively identifies noise and outlier points that do not belong to any cluster. + +## Features + +- **Dynamic Thresholding**: Adapts to varying densities in the data, allowing for more flexible clustering. +- **Noise Handling**: Robustly identifies and manages outlier data points. +- **Scalable**: Efficiently processes large datasets using spatial partitioning techniques. +- **Customizable Distance Metrics**: Supports various distance calculations to suit different types of data. + +## How It Works + +1. **Local Density Calculation**: For each point, the algorithm calculates the local density based on the number of neighbors within a specified radius. +2. **Dynamic Threshold Adjustment**: The clustering threshold is adjusted dynamically according to the average local density of the data. +3. **Cluster Formation**: Points exceeding the dynamic threshold are grouped into clusters, while those below it are marked as noise. +4. **Cluster Merging**: Clusters that are close to each other may be merged based on their centroids. + +## Usage + +Once compiled, you can run the DTC algorithm on your dataset. Modify the `data` array in the `main` function to include your points. + +```c +Point data[MAX_POINTS] = { + {1.0, 2.0}, {1.1, 2.1}, {10.0, 10.0}, {10.5, 10.5}, {5.0, 5.0} +}; +``` + +## Example + +After compiling and running the program, the output will display the clustering results: + +``` +Clusters: +Point (1.0, 2.0) -> Cluster 0 +Point (1.1, 2.1) -> Cluster 0 +Point (10.0, 10.0) -> Cluster 1 +Point (10.5, 10.5) -> Cluster 1 +Point (5.0, 5.0) is noise +``` + +## Applications + +- **Market Segmentation**: Grouping customers based on purchasing behavior. +- **Image Segmentation**: Identifying regions in images based on color and texture. +- **Anomaly Detection**: Detecting outliers in high-dimensional datasets. +- **Bioinformatics**: Clustering gene expression data. + +## Contributing + +Contributions are welcome! If you have suggestions or improvements, feel free to open an issue or submit a pull request. \ No newline at end of file