diff --git a/1bfs.cpp b/1bfs.cpp new file mode 100644 index 0000000..3a94f80 --- /dev/null +++ b/1bfs.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +class Solution { + public: + // Function to return Breadth First Traversal of given graph. + vector bfsOfGraph(int V, vector adj[]) { + int vis[V] = {0}; + vis[0] = 1; + queue q; + // push the initial starting node + q.push(0); + vector bfs; + // iterate till the queue is empty + while(!q.empty()) { + // get the topmost element in the queue + int node = q.front(); + q.pop(); + bfs.push_back(node); + // traverse for all its neighbours + for(auto it : adj[node]) { + // if the neighbour has previously not been visited, + // store in Q and mark as visited + if(!vis[it]) { + vis[it] = 1; + q.push(it); + } + } + } + return bfs; + } +}; + +void addEdge(vector adj[], int u, int v) { + adj[u].push_back(v); + adj[v].push_back(u); +} + +void printAns(vector &ans) { + for (int i = 0; i < ans.size(); i++) { + cout << ans[i] << " "; + } +} + +int main() +{ + vector adj[6]; + + addEdge(adj, 0, 1); + addEdge(adj, 1, 2); + addEdge(adj, 1, 3); + addEdge(adj, 0, 4); + + Solution obj; + vector ans = obj.bfsOfGraph(5, adj); + printAns(ans); + + return 0; +} \ No newline at end of file diff --git a/Searching algorithm b/Searching algorithm new file mode 100644 index 0000000..042ea03 --- /dev/null +++ b/Searching algorithm @@ -0,0 +1,35 @@ +// C++ program for Naive Pattern +// Searching algorithm +#include +using namespace std; + +void search(char* pat, char* txt) +{ + int M = strlen(pat); + int N = strlen(txt); + + /* A loop to slide pat[] one by one */ + for (int i = 0; i <= N - M; i++) { + int j; + + /* For current index i, check for pattern match */ + for (j = 0; j < M; j++) + if (txt[i + j] != pat[j]) + break; + + if (j + == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1] + cout << "Pattern found at index " << i << endl; + } +} + +// Driver's Code +int main() +{ + char txt[] = "AABAACAADAABAAABAA"; + char pat[] = "AABA"; + + // Function call + search(pat, txt); + return 0; +} diff --git a/eggdrop.cpp b/eggdrop.cpp new file mode 100644 index 0000000..e786c7c --- /dev/null +++ b/eggdrop.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +// A utility function to get +// maximum of two integers +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +// Function to get minimum +// number of trials needed in worst +// case with n eggs and k floors +int eggDrop(int n, int k) +{ + // If there are no floors, + // then no trials needed. + // OR if there is one floor, + // one trial needed. + if (k == 1 || k == 0) + return k; + + // We need k trials for one + // egg and k floors + if (n == 1) + return k; + + int min = INT_MAX, x, res; + + // Consider all droppings from + // 1st floor to kth floor and + // return the minimum of these + // values plus 1. + for (x = 1; x <= k; x++) { + res = max( + eggDrop(n - 1, x - 1), + eggDrop(n, k - x)); + if (res < min) + min = res; + } + + return min + 1; +} + +// Driver program to test +// to pront printDups +int main() +{ + int n = 2, k = 10; + cout << "Minimum number of trials " + "in worst case with " + << n << " eggs and " << k + << " floors is " + << eggDrop(n, k) << endl; + return 0; +} + +// This code is contributed +// by Akanksha Rai + diff --git a/knapsack b/knapsack new file mode 100644 index 0000000..ede4104 --- /dev/null +++ b/knapsack @@ -0,0 +1,157 @@ +// C++ program to solve knapsack problem using +// branch and bound +#include +using namespace std; + +// Structure for Item which store weight and corresponding +// value of Item +struct Item +{ + float weight; + int value; +}; + +// Node structure to store information of decision +// tree +struct Node +{ + // level --> Level of node in decision tree (or index + // in arr[] + // profit --> Profit of nodes on path from root to this + // node (including this node) + // bound ---> Upper bound of maximum profit in subtree + // of this node/ + int level, profit, bound; + float weight; +}; + +// Comparison function to sort Item according to +// val/weight ratio +bool cmp(Item a, Item b) +{ + double r1 = (double)a.value / a.weight; + double r2 = (double)b.value / b.weight; + return r1 > r2; +} + +// Returns bound of profit in subtree rooted with u. +// This function mainly uses Greedy solution to find +// an upper bound on maximum profit. +int bound(Node u, int n, int W, Item arr[]) +{ + // if weight overcomes the knapsack capacity, return + // 0 as expected bound + if (u.weight >= W) + return 0; + + // initialize bound on profit by current profit + int profit_bound = u.profit; + + // start including items from index 1 more to current + // item index + int j = u.level + 1; + int totweight = u.weight; + + // checking index condition and knapsack capacity + // condition + while ((j < n) && (totweight + arr[j].weight <= W)) + { + totweight += arr[j].weight; + profit_bound += arr[j].value; + j++; + } + + // If k is not n, include last item partially for + // upper bound on profit + if (j < n) + profit_bound += (W - totweight) * arr[j].value / + arr[j].weight; + + return profit_bound; +} + +// Returns maximum profit we can get with capacity W +int knapsack(int W, Item arr[], int n) +{ + // sorting Item on basis of value per unit + // weight. + sort(arr, arr + n, cmp); + + // make a queue for traversing the node + queue Q; + Node u, v; + + // dummy node at starting + u.level = -1; + u.profit = u.weight = 0; + Q.push(u); + + // One by one extract an item from decision tree + // compute profit of all children of extracted item + // and keep saving maxProfit + int maxProfit = 0; + while (!Q.empty()) + { + // Dequeue a node + u = Q.front(); + Q.pop(); + + // If it is starting node, assign level 0 + if (u.level == -1) + v.level = 0; + + // If there is nothing on next level + if (u.level == n-1) + continue; + + // Else if not last node, then increment level, + // and compute profit of children nodes. + v.level = u.level + 1; + + // Taking current level's item add current + // level's weight and value to node u's + // weight and value + v.weight = u.weight + arr[v.level].weight; + v.profit = u.profit + arr[v.level].value; + + // If cumulated weight is less than W and + // profit is greater than previous profit, + // update maxprofit + if (v.weight <= W && v.profit > maxProfit) + maxProfit = v.profit; + + // Get the upper bound on profit to decide + // whether to add v to Q or not. + v.bound = bound(v, n, W, arr); + + // If bound value is greater than profit, + // then only push into queue for further + // consideration + if (v.bound > maxProfit) + Q.push(v); + + // Do the same thing, but Without taking + // the item in knapsack + v.weight = u.weight; + v.profit = u.profit; + v.bound = bound(v, n, W, arr); + if (v.bound > maxProfit) + Q.push(v); + } + + return maxProfit; +} + +// driver program to test above function +int main() +{ + int W = 10; // Weight of knapsack + Item arr[] = {{2, 40}, {3.14, 50}, {1.98, 100}, + {5, 95}, {3, 30}}; + int n = sizeof(arr) / sizeof(arr[0]); + + cout << "Maximum possible profit = " + << knapsack(W, arr, n); + + return 0; +} diff --git a/merge.c b/merge.c new file mode 100644 index 0000000..97f86ad --- /dev/null +++ b/merge.c @@ -0,0 +1,38 @@ +procedure mergesort( var a as array ) + if ( n == 1 ) return a + + var l1 as array = a[0] ... a[n/2] + var l2 as array = a[n/2+1] ... a[n] + + l1 = mergesort( l1 ) + l2 = mergesort( l2 ) + + return merge( l1, l2 ) +end procedure + +procedure merge( var a as array, var b as array ) + + var c as array + while ( a and b have elements ) + if ( a[0] > b[0] ) + add b[0] to the end of c + remove b[0] from b + else + add a[0] to the end of c + remove a[0] from a + end if + end while + + while ( a has elements ) + add a[0] to the end of c + remove a[0] from a + end while + + while ( b has elements ) + add b[0] to the end of c + remove b[0] from b + end while + + return c + +end procedure diff --git a/strassen.c b/strassen.c new file mode 100644 index 0000000..7aa1405 --- /dev/null +++ b/strassen.c @@ -0,0 +1,51 @@ +#include +int main(){ + int a[2][2], b[2][2], c[2][2], i, j; + int m1, m2, m3, m4 , m5, m6, m7; + + printf("Enter the 4 elements of first matrix: "); + for(i = 0;i < 2; i++) + for(j = 0;j < 2; j++) + scanf("%d", &a[i][j]); + + printf("Enter the 4 elements of second matrix: "); + for(i = 0; i < 2; i++) + for(j = 0;j < 2; j++) + scanf("%d", &b[i][j]); + + printf("\nThe first matrix is\n"); + for(i = 0; i < 2; i++){ + printf("\n"); + for(j = 0; j < 2; j++) + printf("%d\t", a[i][j]); + } + + printf("\nThe second matrix is\n"); + for(i = 0;i < 2; i++){ + printf("\n"); + for(j = 0;j < 2; j++) + printf("%d\t", b[i][j]); + } + + m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]); + m2= (a[1][0] + a[1][1]) * b[0][0]; + m3= a[0][0] * (b[0][1] - b[1][1]); + m4= a[1][1] * (b[1][0] - b[0][0]); + m5= (a[0][0] + a[0][1]) * b[1][1]; + m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]); + m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]); + + c[0][0] = m1 + m4- m5 + m7; + c[0][1] = m3 + m5; + c[1][0] = m2 + m4; + c[1][1] = m1 - m2 + m3 + m6; + + printf("\nAfter multiplication using Strassen's algorithm \n"); + for(i = 0; i < 2 ; i++){ + printf("\n"); + for(j = 0;j < 2; j++) + printf("%d\t", c[i][j]); + } + + return 0; +} diff --git a/unbound knapsack b/unbound knapsack new file mode 100644 index 0000000..0d8f4fd --- /dev/null +++ b/unbound knapsack @@ -0,0 +1,40 @@ +// C++ implementation of the approach +#include +using namespace std; + +// Function to return the maximum required value +float knapSack(int W, float wt[], float val[], int n) +{ + + // maxratio will store the maximum value to weight + // ratio we can have for any item and maxindex + // will store the index of that element + float maxratio = INT_MIN; + int maxindex = 0; + + // Find the maximum ratio + for (int i = 0; i < n; i++) { + if ((val[i] / wt[i]) > maxratio) { + maxratio = (val[i] / wt[i]); + maxindex = i; + } + } + + // The item with the maximum value to + // weight ratio will be put into + // the knapsack repeatedly until full + return (W * maxratio); +} + +// Driver code +int main() +{ + float val[] = { 14, 27, 44, 19 }; + float wt[] = { 6, 7, 9, 8 }; + int n = sizeof(val) / sizeof(val[0]); + int W = 50; + + cout << knapSack(W, wt, val, n); + + return 0; +}