Advertisement
aniket123422

Untitled

Dec 6th, 2022
562
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.08 KB | Source Code | 1 0
  1. // tsp_tour
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. void primMST(vector<vector<pair<int,int>>> &adj, vector<vector<int>> &mst)
  7. {
  8.     int V = adj.size();
  9.    
  10.     vector<int> parent(V,-1);
  11.     vector<int> key(V,INT_MAX);
  12.     vector<int> visited(V,0);
  13.  
  14.     priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
  15.  
  16.     key[0] = 0;
  17.     pq.push({0,0});
  18.  
  19.     while(!pq.empty()){
  20.  
  21.         pair<int,int> p = pq.top();
  22.         pq.pop();
  23.  
  24.         int wt = p.first;
  25.         int u = p.second;
  26.  
  27.         if(visited[u])
  28.             continue;
  29.        
  30.         visited[u] = 1;
  31.  
  32.         for(auto v : adj[u]){
  33.             if(!visited[v.first] && v.second < key[v.first]){
  34.                 key[v.first] = v.second;
  35.                 pq.push({key[v.first],v.first});
  36.                 parent[v.first] = u;
  37.             }
  38.         }
  39.     }
  40.  
  41.     for(int i = 1; i < V; i++){
  42.         mst[parent[i]].push_back(i);
  43.     }
  44.  
  45.  
  46.     return;
  47.  
  48. }
  49.  
  50. void dfs(vector<vector<int>> &mst, vector<int> &visited, vector<int> &tsp, int u){
  51.    
  52.     visited[u] = true;
  53.     tsp.push_back(u);
  54.  
  55.     for(auto v : mst[u]){
  56.         if(!visited[v]){
  57.             dfs(mst,visited,tsp,v);
  58.         }
  59.     }
  60. }
  61.  
  62. int main(){
  63.  
  64.     #ifndef ONLINE_JUDGE
  65.         freopen("../input.txt", "r", stdin);
  66.         freopen("../output.txt", "w", stdout);
  67.     #endif
  68.  
  69.     int n,e;
  70.     cin >> n >> e;
  71.  
  72.     vector<vector<pair<int,int>>> adj(n);
  73.  
  74.     for(int i = 0; i < e; i++){
  75.         int u,v,w;
  76.         cin >> u >> v >> w;
  77.  
  78.         adj[u].push_back({v,w});
  79.         adj[v].push_back({u,w});
  80.     }
  81.  
  82.     vector<vector<int>> mst(n);
  83.  
  84.     primMST(adj,mst);
  85.  
  86.     vector<int> visited(n,0);
  87.  
  88.     vector<int> tsp;
  89.     dfs(mst,visited,tsp,0);
  90.  
  91.     tsp.push_back(0);
  92.  
  93.     for(auto i : tsp){
  94.         cout << i << " ";
  95.     }
  96.  
  97. }
  98.  
  99. // vertex cover
  100.  
  101. #include<bits/stdc++.h>
  102. using namespace std;
  103.  
  104. void printVertexCover(vector<vector<int>> &adj){
  105.  
  106.     int V = adj.size();
  107.     vector<bool> visited(V,false);
  108.  
  109.     for(int u = 0; u < V; u++){
  110.         if(!visited[u]){
  111.             for(auto v : adj[u]){
  112.                 if(!visited[v]){
  113.                     visited[u] = true;
  114.                     visited[v] = true;
  115.                     break;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.  
  121.     for(int i = 0; i < V; i++){
  122.         if(visited[i]){
  123.             cout << i << " ";
  124.         }
  125.     }
  126. }
  127.  
  128. int main(){
  129.  
  130.     #ifndef ONLINE_JUDGE
  131.         freopen("../input.txt", "r", stdin);
  132.         freopen("../output.txt", "w", stdout);
  133.     #endif
  134.  
  135.     int n,e;
  136.     cin >> n >> e;
  137.  
  138.     vector<vector<int>> adj(n);
  139.  
  140.     for(int i = 0; i < e; i++){
  141.         int u,v;
  142.         cin >> u >> v;
  143.  
  144.         adj[u].push_back(v);
  145.         adj[v].push_back(u);
  146.     }
  147.  
  148.     printVertexCover(adj);
  149.     adj.clear();
  150.  
  151. }
  152.  
  153. // Navie
  154. #include <bits/stdc++.h>
  155. using namespace std;
  156.  
  157. void NaiveSearchPattern(string txt, string pat)
  158. {
  159.     int n = txt.size();
  160.     int m = pat.size();
  161.  
  162.     for(int i = 0; i <= n-m; i++){
  163.        
  164.         int j;
  165.  
  166.         for(j = 0; j < m; j++)
  167.             if(txt[i + j] != pat[j])
  168.                 break;
  169.  
  170.         if(j == m)
  171.             cout << "Pattern found at index " << i << endl;
  172.     }
  173. }
  174.  
  175. int main()
  176. {
  177.    
  178.     #ifndef ONLINE_JUDGE
  179.         freopen("../input.txt", "r", stdin);
  180.         freopen("../output.txt", "w", stdout);
  181.     #endif
  182.  
  183.     string str,pat;
  184.     cin >> str >> pat;
  185.  
  186.     NaiveSearchPattern(str,pat);
  187. }
  188.  
  189. // Automata
  190.  
  191. #include <bits/stdc++.h>
  192. using namespace std;
  193.  
  194. int NUM_OF_CHARS = 256;
  195.  
  196. vector<vector<int>> computeTF(string ptr) {
  197.     int m = ptr.size();
  198.     vector<vector<int>> TFtable(m + 1, vector<int>(NUM_OF_CHARS));
  199.  
  200.     for (int i = 0; i < NUM_OF_CHARS; i++) TFtable[0][i] = 0;
  201.     TFtable[0][ptr[0]] = 1;
  202.  
  203.     int lps = 0;
  204.     for (int i = 1; i <= m; i++) {
  205.         for (int x = 0; x < NUM_OF_CHARS; x++) TFtable[i][x] = TFtable[lps][x];
  206.  
  207.         TFtable[i][ptr[i]] = i + 1;
  208.         if (lps < m) lps = TFtable[lps][ptr[i]];
  209.     }
  210.     return TFtable;
  211. }
  212.  
  213. void finiteAutomataStringMatcher(string txt, string ptr) {
  214.     int n = txt.size();
  215.     int m = ptr.size();
  216.     vector<vector<int>> TF = computeTF(ptr);
  217.     int j = 0;
  218.     for (int i = 0; i < n; i++) {
  219.         j = TF[j][txt[i]];
  220.         if (j == m) {
  221.             cout << "Pattern found with shift " << i - m + 1 << "\n";
  222.         }
  223.     }
  224. }
  225.  
  226. int main() {
  227.     string txt, ptr;
  228.     cin >> txt >> ptr;
  229.  
  230.     finiteAutomataStringMatcher(txt, ptr);
  231.  
  232.     return 0;
  233. }
  234.  
  235.  
  236. // kmp
  237.  
  238. #include <bits/stdc++.h>
  239. using namespace std;
  240.  
  241. void computeLPSArray(char *pat, int M, int *lps);
  242.  
  243. void KMPSearch(char *pat, char *txt)
  244. {
  245.     int M = strlen(pat);
  246.     int N = strlen(txt);
  247.  
  248.     int lps[M];
  249.  
  250.     computeLPSArray(pat, M, lps);
  251.  
  252.     int i = 0;
  253.     int j = 0;
  254.     while ((N - i) >= (M - j))
  255.     {
  256.         if (pat[j] == txt[i])
  257.         {
  258.             j++;
  259.             i++;
  260.         }
  261.  
  262.         if (j == M)
  263.         {
  264.             printf("Found pattern at index %d ", i - j);
  265.             j = lps[j - 1];
  266.         }
  267.  
  268.         else if (i < N && pat[j] != txt[i])
  269.         {
  270.             if (j != 0)
  271.                 j = lps[j - 1];
  272.             else
  273.                 i = i + 1;
  274.         }
  275.     }
  276. }
  277.  
  278. void computeLPSArray(char *pat, int M, int *lps)
  279. {
  280.  
  281.     int len = 0;
  282.  
  283.     lps[0] = 0;
  284.  
  285.     int i = 1;
  286.     while (i < M)
  287.     {
  288.         if (pat[i] == pat[len])
  289.         {
  290.             len++;
  291.             lps[i] = len;
  292.             i++;
  293.         }
  294.         else
  295.         {
  296.             if (len != 0)
  297.             {
  298.                 len = lps[len - 1];
  299.             }
  300.             else
  301.             {
  302.                 lps[i] = 0;
  303.                 i++;
  304.             }
  305.         }
  306.     }
  307. }
  308.  
  309. int main()
  310. {
  311.     char txt[] = "ABABDABACDABABCABAB";
  312.     char pat[] = "ABABCABAB";
  313.     KMPSearch(pat, txt);
  314.     return 0;
  315. }
  316.  
  317. /// avl_deletion
  318. #include <bits/stdc++.h>
  319. using namespace std;
  320.  
  321. struct Node {
  322.     int key;
  323.     Node *left, *right;
  324.     int height;
  325.     Node(int x) {
  326.         key = x;
  327.         left = NULL;
  328.         right = NULL;
  329.         height = 1;
  330.     }
  331. };
  332.  
  333. int height(Node *x) {
  334.     if (!x) return 0;
  335.     return x->height;
  336. }
  337.  
  338. int getBalanceFactor(Node *N) {
  339.     if (!N) return 0;
  340.     return height(N->left) - height(N->right);
  341. }
  342.  
  343. // left rotate function
  344.  
  345. Node *leftRotate(Node *x) {
  346.     Node *y = x->right;
  347.     Node *T2 = y->left;
  348.  
  349.     y->left = x;
  350.     x->right = T2;
  351.  
  352.     x->height = 1 + max(height(x->left), height(x->right));
  353.     y->height = 1 + max(height(y->left), height(y->right));
  354.     return y;
  355. }
  356.  
  357. Node *rightRotate(Node *y) {
  358.     Node *x = y->left;
  359.     Node *T2 = x->right;
  360.  
  361.     x->right = y;
  362.     y->left = T2;
  363.  
  364.     x->height = 1 + max(height(x->left), height(x->right));
  365.     y->height = 1 + max(height(y->left), height(y->right));
  366.  
  367.     return x;
  368. }
  369.  
  370. Node *insert(Node *node, int key) {
  371.     if (!node) return new Node(key);
  372.     if (key < node->key)
  373.         node->left = insert(node->left, key);
  374.     else if (key > node->key)
  375.         node->right = insert(node->right, key);
  376.     else
  377.         return node;
  378.  
  379.     node->height = 1 + max(height(node->left), height(node->right));
  380.     int balanceFactor = getBalanceFactor(node);
  381.  
  382.     if (balanceFactor > 1 && key < node->left->key) {
  383.         return rightRotate(node);
  384.     }
  385.  
  386.     if (balanceFactor < -1 && key > node->right->key) {
  387.         return leftRotate(node);
  388.     }
  389.  
  390.     if (balanceFactor > 1 && key > node->left->key) {
  391.         node->left = leftRotate(node->left);
  392.         return rightRotate(node);
  393.     }
  394.     if (balanceFactor < -1 && key < node->right->key) {
  395.         node->right = rightRotate(node->right);
  396.         return leftRotate(node);
  397.     }
  398.     return node;
  399. }
  400.  
  401. void preOrder(Node *node) {
  402.     if (!node) return;
  403.     cout << node->key << " ";
  404.     preOrder(node->left);
  405.     preOrder(node->right);
  406. }
  407.  
  408. Node *minValueNode(Node *root) {
  409.     Node *curr = root;
  410.     while (curr->left) {
  411.         curr = curr->left;
  412.     }
  413.     return curr;
  414. }
  415.  
  416. Node *deleteNode(Node *node, int key) {
  417.     if (!node) return node;
  418.     if (key < node->key)
  419.         deleteNode(node->left, key);
  420.     else if (key > node->key)
  421.         deleteNode(node->right, key);
  422.     else {
  423.         if (node->left == NULL || node->right == NULL) {
  424.             Node *temp = node->left ? node->left : node->right;
  425.             if (temp == NULL) {
  426.                 temp = node;
  427.                 node = NULL;
  428.             } else {
  429.                 *node = *temp;
  430.             }
  431.             free(temp);
  432.         } else {
  433.             Node *temp = minValueNode(node->right);
  434.             node->key = temp->key;
  435.             node->right = deleteNode(node->right, temp->key);
  436.         }
  437.     }
  438.  
  439.     if (!node) return node;
  440.  
  441.     node->height = 1 + max(height(node->right), height(node->left));
  442.  
  443.     int balanceFactor = getBalanceFactor(node);
  444.  
  445.     if (balanceFactor > 1 && getBalanceFactor(node->left) >= 0) {
  446.         return rightRotate(node);
  447.     }
  448.  
  449.     if (balanceFactor > 1 && getBalanceFactor(node->left) < 0) {
  450.         node->left = leftRotate(node->left);
  451.         return rightRotate(node);
  452.     }
  453.  
  454.     if (balanceFactor < -1 && getBalanceFactor(node->right) < 0) {
  455.         return leftRotate(node);
  456.     }
  457.     if (balanceFactor < -1 && getBalanceFactor(node->right) >= 0) {
  458.         node->right = rightRotate(node->right);
  459.         return leftRotate(node);
  460.     }
  461.     return node;
  462. }
  463.  
  464. int main() {
  465.     Node *root = NULL;
  466.     root = insert(root, 9);
  467.     root = insert(root, 5);
  468.     root = insert(root, 10);
  469.     root = insert(root, 0);
  470.     root = insert(root, 6);
  471.     root = insert(root, 11);
  472.     root = insert(root, -1);
  473.     root = insert(root, 1);
  474.     root = insert(root, 2);
  475.  
  476.     cout << "Preorder traversal of the "
  477.             "constructed AVL tree is \n";
  478.     preOrder(root);
  479.     cout << "\n";
  480.     deleteNode(root, 10);
  481.  
  482.     preOrder(root);
  483.     return 0;
  484. }
  485.  
  486. //avl insertion
  487. #include <bits/stdc++.h>
  488. using namespace std;
  489.  
  490. struct Node {
  491.     int key;
  492.     Node *left, *right;
  493.     int height;
  494.     Node(int x) {
  495.         key = x;
  496.         left = NULL;
  497.         right = NULL;
  498.         height = 1;
  499.     }
  500. };
  501.  
  502. int height(Node *x) {
  503.     if (!x) return 0;
  504.     return x->height;
  505. }
  506.  
  507. int getBalanceFactor(Node *N) {
  508.     if (!N) return 0;
  509.     return height(N->left) - height(N->right);
  510. }
  511.  
  512. // left rotate function
  513.  
  514. Node *leftRotate(Node *x) {
  515.     Node *y = x->right;
  516.     Node *T2 = y->left;
  517.  
  518.     y->left = x;
  519.     x->right = T2;
  520.  
  521.     x->height = 1 + max(height(x->left), height(x->right));
  522.     y->height = 1 + max(height(y->left), height(y->right));
  523.     return y;
  524. }
  525.  
  526. Node *rightRotate(Node *y) {
  527.     Node *x = y->left;
  528.     Node *T2 = x->right;
  529.  
  530.     x->right = y;
  531.     y->left = T2;
  532.  
  533.     x->height = 1 + max(height(x->left), height(x->right));
  534.     y->height = 1 + max(height(y->left), height(y->right));
  535.  
  536.     return x;
  537. }
  538.  
  539. Node *insert(Node *node, int key) {
  540.     if (!node) return new Node(key);
  541.     if (key < node->key)
  542.         node->left = insert(node->left, key);
  543.     else if (key > node->key)
  544.         node->right = insert(node->right, key);
  545.     else
  546.         return node;
  547.  
  548.     node->height = 1 + max(height(node->left), height(node->right));
  549.     int balanceFactor = getBalanceFactor(node);
  550.  
  551.     if (balanceFactor > 1 && key < node->left->key) {
  552.         return rightRotate(node);
  553.     }
  554.  
  555.     if (balanceFactor < -1 && key > node->right->key) {
  556.         return leftRotate(node);
  557.     }
  558.  
  559.     if (balanceFactor > 1 && key > node->left->key) {
  560.         node->left = leftRotate(node->left);
  561.         return rightRotate(node);
  562.     }
  563.     if (balanceFactor < -1 && key < node->right->key) {
  564.         node->right = rightRotate(node->right);
  565.         return leftRotate(node);
  566.     }
  567.     return node;
  568. }
  569.  
  570. void preOrder(Node *node) {
  571.     if (!node) return;
  572.     cout << node->key << " ";
  573.     preOrder(node->left);
  574.     preOrder(node->right);
  575. }
  576.  
  577. int main() {
  578.     Node *root = NULL;
  579.     root = insert(root, 10);
  580.  
  581.     root = insert(root, 20);
  582.  
  583.     root = insert(root, 30);
  584.     root = insert(root, 40);
  585.     root = insert(root, 50);
  586.     root = insert(root, 25);
  587.  
  588.     cout << "Preorder traversal of the "
  589.             "constructed AVL tree is \n";
  590.     preOrder(root);
  591.     return 0;
  592. }
  593.  
  594.  
  595. // binomial heap
  596.  
  597.  
  598. #include <bits/stdc++.h>
  599. using namespace std;
  600.  
  601. struct Node
  602. {
  603.     int data, degree;
  604.     Node *child, *sibling, *parent;
  605. };
  606.  
  607. Node *newNode(int key)
  608. {
  609.     Node *temp = new Node;
  610.     temp->data = key;
  611.     temp->degree = 0;
  612.     temp->child = temp->parent = temp->sibling = NULL;
  613.     return temp;
  614. }
  615.  
  616. Node *mergeBinomialTrees(Node *b1, Node *b2)
  617. {
  618.  
  619.     if (b1->data > b2->data)
  620.         swap(b1, b2);
  621.  
  622.     b2->parent = b1;
  623.     b2->sibling = b1->child;
  624.     b1->child = b2;
  625.     b1->degree++;
  626.  
  627.     return b1;
  628. }
  629.  
  630. list<Node *> unionBionomialHeap(list<Node *> l1,
  631.                                 list<Node *> l2)
  632. {
  633.  
  634.     list<Node *> _new;
  635.     list<Node *>::iterator it = l1.begin();
  636.     list<Node *>::iterator ot = l2.begin();
  637.     while (it != l1.end() && ot != l2.end())
  638.     {
  639.  
  640.         if ((*it)->degree <= (*ot)->degree)
  641.         {
  642.             _new.push_back(*it);
  643.             it++;
  644.         }
  645.  
  646.         else
  647.         {
  648.             _new.push_back(*ot);
  649.             ot++;
  650.         }
  651.     }
  652.  
  653.     while (it != l1.end())
  654.     {
  655.         _new.push_back(*it);
  656.         it++;
  657.     }
  658.  
  659.     while (ot != l2.end())
  660.     {
  661.         _new.push_back(*ot);
  662.         ot++;
  663.     }
  664.     return _new;
  665. }
  666.  
  667. list<Node *> adjust(list<Node *> _heap)
  668. {
  669.     if (_heap.size() <= 1)
  670.         return _heap;
  671.     list<Node *> new_heap;
  672.     list<Node *>::iterator it1, it2, it3;
  673.     it1 = it2 = it3 = _heap.begin();
  674.  
  675.     if (_heap.size() == 2)
  676.     {
  677.         it2 = it1;
  678.         it2++;
  679.         it3 = _heap.end();
  680.     }
  681.     else
  682.     {
  683.         it2++;
  684.         it3 = it2;
  685.         it3++;
  686.     }
  687.     while (it1 != _heap.end())
  688.     {
  689.  
  690.         if (it2 == _heap.end())
  691.             it1++;
  692.  
  693.         else if ((*it1)->degree < (*it2)->degree)
  694.         {
  695.             it1++;
  696.             it2++;
  697.             if (it3 != _heap.end())
  698.                 it3++;
  699.         }
  700.  
  701.         else if (it3 != _heap.end() &&
  702.                  (*it1)->degree == (*it2)->degree &&
  703.                  (*it1)->degree == (*it3)->degree)
  704.         {
  705.             it1++;
  706.             it2++;
  707.             it3++;
  708.         }
  709.  
  710.         else if ((*it1)->degree == (*it2)->degree)
  711.         {
  712.             Node *temp;
  713.             *it1 = mergeBinomialTrees(*it1, *it2);
  714.             it2 = _heap.erase(it2);
  715.             if (it3 != _heap.end())
  716.                 it3++;
  717.         }
  718.     }
  719.     return _heap;
  720. }
  721.  
  722. list<Node *> insertATreeInHeap(list<Node *> _heap,
  723.                                Node *tree)
  724. {
  725.  
  726.     list<Node *> temp;
  727.  
  728.     temp.push_back(tree);
  729.  
  730.     temp = unionBionomialHeap(_heap, temp);
  731.  
  732.     return adjust(temp);
  733. }
  734.  
  735. list<Node *> removeMinFromTreeReturnBHeap(Node *tree)
  736. {
  737.     list<Node *> heap;
  738.     Node *temp = tree->child;
  739.     Node *lo;
  740.  
  741.     while (temp)
  742.     {
  743.         lo = temp;
  744.         temp = temp->sibling;
  745.         lo->sibling = NULL;
  746.         heap.push_front(lo);
  747.     }
  748.     return heap;
  749. }
  750.  
  751. list<Node *> insert(list<Node *> _head, int key)
  752. {
  753.     Node *temp = newNode(key);
  754.     return insertATreeInHeap(_head, temp);
  755. }
  756.  
  757. Node *getMin(list<Node *> _heap)
  758. {
  759.     list<Node *>::iterator it = _heap.begin();
  760.     Node *temp = *it;
  761.     while (it != _heap.end())
  762.     {
  763.         if ((*it)->data < temp->data)
  764.             temp = *it;
  765.         it++;
  766.     }
  767.     return temp;
  768. }
  769.  
  770. list<Node *> extractMin(list<Node *> _heap)
  771. {
  772.     list<Node *> new_heap, lo;
  773.     Node *temp;
  774.  
  775.     temp = getMin(_heap);
  776.     list<Node *>::iterator it;
  777.     it = _heap.begin();
  778.     while (it != _heap.end())
  779.     {
  780.         if (*it != temp)
  781.         {
  782.  
  783.             new_heap.push_back(*it);
  784.         }
  785.         it++;
  786.     }
  787.     lo = removeMinFromTreeReturnBHeap(temp);
  788.     new_heap = unionBionomialHeap(new_heap, lo);
  789.     new_heap = adjust(new_heap);
  790.     return new_heap;
  791. }
  792.  
  793. void printTree(Node *h)
  794. {
  795.     while (h)
  796.     {
  797.         cout << h->data << " ";
  798.         printTree(h->child);
  799.         h = h->sibling;
  800.     }
  801. }
  802.  
  803. void printHeap(list<Node *> _heap)
  804. {
  805.     list<Node *>::iterator it;
  806.     it = _heap.begin();
  807.     while (it != _heap.end())
  808.     {
  809.         printTree(*it);
  810.         it++;
  811.     }
  812. }
  813.  
  814. int main()
  815. {
  816.     int ch, key;
  817.     list<Node *> _heap;
  818.  
  819.     _heap = insert(_heap, 10);
  820.     _heap = insert(_heap, 20);
  821.     _heap = insert(_heap, 30);
  822.  
  823.     cout << "Heap elements after insertion:\n";
  824.     printHeap(_heap);
  825.  
  826.     Node *temp = getMin(_heap);
  827.     cout << "\nMinimum element of heap "
  828.          << temp->data << "\n";
  829.  
  830.     _heap = extractMin(_heap);
  831.     cout << "Heap after deletion of minimum element\n";
  832.     printHeap(_heap);
  833.  
  834.     return 0;
  835. }
  836.  
  837.  
  838. // fibonacci heap
  839. #include <bits/stdc++.h>
  840. using namespace std;
  841.  
  842. struct node
  843. {
  844.     node *parent;
  845.     node *child;
  846.     node *left;
  847.     node *right;
  848.     int key;
  849.     int degree;
  850.     char mark;
  851.     char c;
  852. };
  853.  
  854. struct node *mini = NULL;
  855.  
  856. int no_of_nodes = 0;
  857.  
  858. void insertion(int val)
  859. {
  860.     struct node *new_node = new node();
  861.     new_node->key = val;
  862.     new_node->degree = 0;
  863.     new_node->mark = 'W';
  864.     new_node->c = 'N';
  865.     new_node->parent = NULL;
  866.     new_node->child = NULL;
  867.     new_node->left = new_node;
  868.     new_node->right = new_node;
  869.     if (mini != NULL)
  870.     {
  871.         (mini->left)->right = new_node;
  872.         new_node->right = mini;
  873.         new_node->left = mini->left;
  874.         mini->left = new_node;
  875.         if (new_node->key < mini->key)
  876.             mini = new_node;
  877.     }
  878.     else
  879.     {
  880.         mini = new_node;
  881.     }
  882.     no_of_nodes++;
  883. }
  884.  
  885. void Fibonnaci_link(struct node *ptr2, struct node *ptr1)
  886. {
  887.     (ptr2->left)->right = ptr2->right;
  888.     (ptr2->right)->left = ptr2->left;
  889.     if (ptr1->right == ptr1)
  890.         mini = ptr1;
  891.     ptr2->left = ptr2;
  892.     ptr2->right = ptr2;
  893.     ptr2->parent = ptr1;
  894.     if (ptr1->child == NULL)
  895.         ptr1->child = ptr2;
  896.     ptr2->right = ptr1->child;
  897.     ptr2->left = (ptr1->child)->left;
  898.     ((ptr1->child)->left)->right = ptr2;
  899.     (ptr1->child)->left = ptr2;
  900.     if (ptr2->key < (ptr1->child)->key)
  901.         ptr1->child = ptr2;
  902.     ptr1->degree++;
  903. }
  904.  
  905. void Consolidate()
  906. {
  907.     int temp1;
  908.     float temp2 = (log(no_of_nodes)) / (log(2));
  909.     int temp3 = temp2;
  910.     struct node *arr[temp3 + 1];
  911.     for (int i = 0; i <= temp3; i++)
  912.         arr[i] = NULL;
  913.     node *ptr1 = mini;
  914.     node *ptr2;
  915.     node *ptr3;
  916.     node *ptr4 = ptr1;
  917.     do
  918.     {
  919.         ptr4 = ptr4->right;
  920.         temp1 = ptr1->degree;
  921.         while (arr[temp1] != NULL)
  922.         {
  923.             ptr2 = arr[temp1];
  924.             if (ptr1->key > ptr2->key)
  925.             {
  926.                 ptr3 = ptr1;
  927.                 ptr1 = ptr2;
  928.                 ptr2 = ptr3;
  929.             }
  930.             if (ptr2 == mini)
  931.                 mini = ptr1;
  932.             Fibonnaci_link(ptr2, ptr1);
  933.             if (ptr1->right == ptr1)
  934.                 mini = ptr1;
  935.             arr[temp1] = NULL;
  936.             temp1++;
  937.         }
  938.         arr[temp1] = ptr1;
  939.         ptr1 = ptr1->right;
  940.     } while (ptr1 != mini);
  941.     mini = NULL;
  942.     for (int j = 0; j <= temp3; j++)
  943.     {
  944.         if (arr[j] != NULL)
  945.         {
  946.             arr[j]->left = arr[j];
  947.             arr[j]->right = arr[j];
  948.             if (mini != NULL)
  949.             {
  950.                 (mini->left)->right = arr[j];
  951.                 arr[j]->right = mini;
  952.                 arr[j]->left = mini->left;
  953.                 mini->left = arr[j];
  954.                 if (arr[j]->key < mini->key)
  955.                     mini = arr[j];
  956.             }
  957.             else
  958.             {
  959.                 mini = arr[j];
  960.             }
  961.             if (mini == NULL)
  962.                 mini = arr[j];
  963.             else if (arr[j]->key < mini->key)
  964.                 mini = arr[j];
  965.         }
  966.     }
  967. }
  968.  
  969. void Extract_min()
  970. {
  971.     if (mini == NULL)
  972.         cout << "The heap is empty" << endl;
  973.     else
  974.     {
  975.         node *temp = mini;
  976.         node *pntr;
  977.         pntr = temp;
  978.         node *x = NULL;
  979.         if (temp->child != NULL)
  980.         {
  981.  
  982.             x = temp->child;
  983.             do
  984.             {
  985.                 pntr = x->right;
  986.                 (mini->left)->right = x;
  987.                 x->right = mini;
  988.                 x->left = mini->left;
  989.                 mini->left = x;
  990.                 if (x->key < mini->key)
  991.                     mini = x;
  992.                 x->parent = NULL;
  993.                 x = pntr;
  994.             } while (pntr != temp->child);
  995.         }
  996.         (temp->left)->right = temp->right;
  997.         (temp->right)->left = temp->left;
  998.         mini = temp->right;
  999.         if (temp == temp->right && temp->child == NULL)
  1000.             mini = NULL;
  1001.         else
  1002.         {
  1003.             mini = temp->right;
  1004.             Consolidate();
  1005.         }
  1006.         no_of_nodes--;
  1007.     }
  1008. }
  1009.  
  1010. void Cut(struct node *found, struct node *temp)
  1011. {
  1012.     if (found == found->right)
  1013.         temp->child = NULL;
  1014.  
  1015.     (found->left)->right = found->right;
  1016.     (found->right)->left = found->left;
  1017.     if (found == temp->child)
  1018.         temp->child = found->right;
  1019.  
  1020.     temp->degree = temp->degree - 1;
  1021.     found->right = found;
  1022.     found->left = found;
  1023.     (mini->left)->right = found;
  1024.     found->right = mini;
  1025.     found->left = mini->left;
  1026.     mini->left = found;
  1027.     found->parent = NULL;
  1028.     found->mark = 'B';
  1029. }
  1030.  
  1031. void Cascase_cut(struct node *temp)
  1032. {
  1033.     node *ptr5 = temp->parent;
  1034.     if (ptr5 != NULL)
  1035.     {
  1036.         if (temp->mark == 'W')
  1037.         {
  1038.             temp->mark = 'B';
  1039.         }
  1040.         else
  1041.         {
  1042.             Cut(temp, ptr5);
  1043.             Cascase_cut(ptr5);
  1044.         }
  1045.     }
  1046. }
  1047.  
  1048. void Decrease_key(struct node *found, int val)
  1049. {
  1050.     if (mini == NULL)
  1051.         cout << "The Heap is Empty" << endl;
  1052.  
  1053.     if (found == NULL)
  1054.         cout << "Node not found in the Heap" << endl;
  1055.  
  1056.     found->key = val;
  1057.  
  1058.     struct node *temp = found->parent;
  1059.     if (temp != NULL && found->key < temp->key)
  1060.     {
  1061.         Cut(found, temp);
  1062.         Cascase_cut(temp);
  1063.     }
  1064.     if (found->key < mini->key)
  1065.         mini = found;
  1066. }
  1067.  
  1068. void Find(struct node *mini, int old_val, int val)
  1069. {
  1070.     struct node *found = NULL;
  1071.     node *temp5 = mini;
  1072.     temp5->c = 'Y';
  1073.     node *found_ptr = NULL;
  1074.     if (temp5->key == old_val)
  1075.     {
  1076.         found_ptr = temp5;
  1077.         temp5->c = 'N';
  1078.         found = found_ptr;
  1079.         Decrease_key(found, val);
  1080.     }
  1081.     if (found_ptr == NULL)
  1082.     {
  1083.         if (temp5->child != NULL)
  1084.             Find(temp5->child, old_val, val);
  1085.         if ((temp5->right)->c != 'Y')
  1086.             Find(temp5->right, old_val, val);
  1087.     }
  1088.     temp5->c = 'N';
  1089.     found = found_ptr;
  1090. }
  1091.  
  1092. void Deletion(int val)
  1093. {
  1094.     if (mini == NULL)
  1095.         cout << "The heap is empty" << endl;
  1096.     else
  1097.     {
  1098.  
  1099.         Find(mini, val, 0);
  1100.  
  1101.         Extract_min();
  1102.         cout << "Key Deleted" << endl;
  1103.     }
  1104. }
  1105.  
  1106. void display()
  1107. {
  1108.     node *ptr = mini;
  1109.     if (ptr == NULL)
  1110.         cout << "The Heap is Empty" << endl;
  1111.  
  1112.     else
  1113.     {
  1114.         cout << "The root nodes of Heap are: " << endl;
  1115.         do
  1116.         {
  1117.             cout << ptr->key;
  1118.             ptr = ptr->right;
  1119.             if (ptr != mini)
  1120.             {
  1121.                 cout << "-->";
  1122.             }
  1123.         } while (ptr != mini && ptr->right != NULL);
  1124.         cout << endl
  1125.              << "The heap has " << no_of_nodes << " nodes" << endl
  1126.              << endl;
  1127.     }
  1128. }
  1129.  
  1130. int main()
  1131. {
  1132.     cout << "Creating an initial heap" << endl;
  1133.     insertion(5);
  1134.     insertion(2);
  1135.     insertion(8);
  1136.  
  1137.     display();
  1138.  
  1139.     cout << "Extracting min" << endl;
  1140.     Extract_min();
  1141.     display();
  1142.  
  1143.     cout << "Decrease value of 8 to 7" << endl;
  1144.     Find(mini, 8, 7);
  1145.     display();
  1146.  
  1147.     cout << "Delete the node 7" << endl;
  1148.     Deletion(7);
  1149.     display();
  1150.  
  1151.     return 0;
  1152. }
  1153.  
  1154. // rabin karp
  1155.  
  1156. #include <bits/stdc++.h>
  1157. using namespace std;
  1158.  
  1159. // d is the number of characters in the input alphabet
  1160. #define d 256
  1161.  
  1162. // mod is a prime number used for modulo arithmetic
  1163. #define mod INT_MAX
  1164.  
  1165. void RabinKarpSearchPattern(string txt, string pat)
  1166. {
  1167.     int n = txt.size();
  1168.     int m = pat.size();
  1169.  
  1170.     int hashp = 0; // hash value for pattern
  1171.     int hasht = 0; // hash value for txt
  1172.    
  1173.     int h = 1;
  1174.  
  1175.     // h = d^(m-1)
  1176.     for(int i = 0; i < m - 1; i++)
  1177.         h = (h * d) % mod;
  1178.  
  1179.     for(int i = 0; i < m; i++){
  1180.         hashp = (d * hashp + pat[i]) % mod;
  1181.         hasht = (d * hasht + txt[i]) % mod;
  1182.     }
  1183.  
  1184.     // Slide the pattern over text one by one
  1185.     for(int i = 0; i <= n-m; i++){
  1186.  
  1187.         if(hashp == hasht){
  1188.  
  1189.             int j;
  1190.  
  1191.             for(j = 0; j < m; j++){
  1192.                 if(txt[i + j] != pat[j]){
  1193.                     break;
  1194.                 }
  1195.             }
  1196.  
  1197.             if(j == m)
  1198.                 cout << "Pattern found at index " << i << endl;
  1199.         }
  1200.  
  1201.         // Calculate hash value for next window of text:
  1202.         if(i < n - m){
  1203.             hasht = (d*(hasht - txt[i]*h) + txt[i+m]) % mod;
  1204.  
  1205.             if(hasht < 0)
  1206.                 hasht = (hasht + mod);
  1207.         }
  1208.     }
  1209. }
  1210.  
  1211. int main()
  1212. {
  1213.     #ifndef ONLINE_JUDGE
  1214.         freopen("../input.txt", "r", stdin);
  1215.         freopen("../output.txt", "w", stdout);
  1216.     #endif
  1217.  
  1218.     string str,pat;
  1219.     cin >> str >> pat;
  1220.  
  1221.     RabinKarpSearchPattern(str,pat);
  1222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement