From d7c3c07930536934507429503d9de8e5369111a8 Mon Sep 17 00:00:00 2001 From: Jitin Saxena Date: Fri, 3 Oct 2025 19:17:51 +0530 Subject: [PATCH 1/3] prism algo --- CPP/data_structures/graphs/graph/prism.cpp | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 CPP/data_structures/graphs/graph/prism.cpp diff --git a/CPP/data_structures/graphs/graph/prism.cpp b/CPP/data_structures/graphs/graph/prism.cpp new file mode 100644 index 00000000..25f24136 --- /dev/null +++ b/CPP/data_structures/graphs/graph/prism.cpp @@ -0,0 +1,89 @@ +#include +using namespace std; + +/* +====================================================== +Prim's Algorithm: Minimum Spanning Tree (MST) +====================================================== +Description: +Prim's algorithm finds the Minimum Spanning Tree (MST) of a weighted +undirected graph. The MST is a subset of edges connecting all vertices +with the minimum total edge weight, without forming cycles. + +Approach: +1. Start with a single vertex (we start with vertex 0). +2. Pick the smallest edge connecting a vertex in MST to a vertex outside MST. +3. Include that edge and repeat until all vertices are included in MST. + +Time Complexity: +- O(V^2), where V is the number of vertices (using adjacency matrix). +- Using priority queues (min-heap) can reduce it to O(E log V). + +Space Complexity: +- O(V^2) for adjacency matrix. +====================================================== +*/ + +const int INF = 1e9; // Infinity value for initialization + +int main() { + int n; // Number of vertices + cout << "Enter number of vertices: "; + cin >> n; + + // Input adjacency matrix + vector> graph(n, vector(n)); + cout << "Enter adjacency matrix (0 for no edge):\n"; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cin >> graph[i][j]; + } + } + + // key[i] -> Minimum weight edge to include vertex i in MST + vector key(n, INF); + + // inMST[i] -> True if vertex i is included in MST + vector inMST(n, false); + + // parent[i] -> Stores the parent of vertex i in MST + vector parent(n, -1); + + // Start from vertex 0 + key[0] = 0; + + // MST will have n vertices + for (int count = 0; count < n - 1; count++) { + // Step 1: Pick the minimum key vertex from the set of vertices not yet included in MST + int u = -1; + int minKey = INF; + + for (int v = 0; v < n; v++) { + if (!inMST[v] && key[v] < minKey) { + minKey = key[v]; + u = v; + } + } + + // Step 2: Include this vertex in MST + inMST[u] = true; + + // Step 3: Update key and parent for adjacent vertices + for (int v = 0; v < n; v++) { + // If there is an edge u-v and v is not in MST and weight is smaller + if (graph[u][v] != 0 && !inMST[v] && graph[u][v] < key[v]) { + key[v] = graph[u][v]; + parent[v] = u; + } + } + } + + // Print the MST + cout << "\nEdges in the Minimum Spanning Tree (MST):\n"; + cout << "Edge \tWeight\n"; + for (int i = 1; i < n; i++) { + cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n"; + } + + return 0; +} From 81759c8affad6f58f76612ee2462d7d3c6e3cafd Mon Sep 17 00:00:00 2001 From: Jitin Saxena Date: Fri, 3 Oct 2025 19:21:42 +0530 Subject: [PATCH 2/3] prism algo --- CPP/data_structures/graphs/graph/prism.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CPP/data_structures/graphs/graph/prism.cpp b/CPP/data_structures/graphs/graph/prism.cpp index 25f24136..f5df4905 100644 --- a/CPP/data_structures/graphs/graph/prism.cpp +++ b/CPP/data_structures/graphs/graph/prism.cpp @@ -87,3 +87,5 @@ int main() { return 0; } + +//added to prism branch \ No newline at end of file From 1382d67f202b6d317c5ea90810b80643c51266ef Mon Sep 17 00:00:00 2001 From: Jitin Saxena Date: Fri, 3 Oct 2025 19:36:16 +0530 Subject: [PATCH 3/3] Binary Search --- .../searching/Seacrh/binary_search.cpp | 93 +++++++++++-------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/CPP/algorithms/searching/Seacrh/binary_search.cpp b/CPP/algorithms/searching/Seacrh/binary_search.cpp index f1d47e28..8fcc3a2a 100644 --- a/CPP/algorithms/searching/Seacrh/binary_search.cpp +++ b/CPP/algorithms/searching/Seacrh/binary_search.cpp @@ -1,56 +1,73 @@ -#include -using namespace std; +#include +/* +====================================================== +Binary Search Algorithm +====================================================== +Description: +Binary Search is an efficient algorithm to find the position of a target value +in a sorted array. It repeatedly divides the search interval in half. +Key Idea: +1. Start with the entire array as the search interval. +2. Find the middle element. +3. If the middle element is equal to the target, return its index. +4. If the target is smaller, search the left half. +5. If the target is larger, search the right half. +6. Repeat until the target is found or interval is empty. +Time Complexity: O(log n) +Space Complexity: O(1) for iterative implementation +====================================================== +*/ -int binarysearch(int arr[], int size, int key){ - int s = 0; //startingpoint - int e = size-1; //ending point - while(s key){ - e = mid-1; + else if (arr[mid] < target) { + left = mid + 1; // Search in right half } - else{ - s = mid+1; + else { + right = mid - 1; // Search in left half } } - //if not work then we ll return -1 - return -1; -} - - - + return -1; // Target not found +} +int main() { + int n, target; + // Input the size of the array + printf("Enter size of array: "); + scanf("%d", &n); + int arr[n]; + printf("Enter %d elements in sorted order: ", n); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } -int main (){ + // Input the target value + printf("Enter the target value: "); + scanf("%d", &target); - int size; - cout<<"enter the size of the array : "; - cin>>size; - int key; - cout<<"enter the key : "; - cin>>key; + // Call Binary Search + int result = binarySearch(arr, n, target); - int arr[size]; - cout<<"Enter the elements of array : "<>arr[i]; - } - int result = binarysearch(arr,size,key); - if(result == -1){ - cout<<"Element is not present in the array"<