Advertisement
Shiam7777777

Untitled

May 21st, 2019
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.93 KB | None | 0 0
  1.                     //  dfs
  2.  
  3. #include<iostream>
  4. #include<queue>
  5. #include<vector>
  6. using namespace std;
  7. const int white=0,grey=1,black=2;
  8. const int INF=100000,Nil=-1;
  9. int timecnt=0;
  10. struct node{
  11.     int color,discovery,done,parent;
  12. };
  13. void dfs(vector<vector<int>> &graph,int nodes, vector<node> &Prop);
  14. void dfs_visit(vector<vector<int>> &graph,int u, vector<node> &Prop);
  15. int main(){
  16.     int nodes,edges;
  17.     cout<<"Enter the number nodes : ";
  18.     cin>>nodes;
  19.     cout<<"Enter the number of edges : ";
  20.     cin>>edges ;
  21.     vector<vector<int>> graph(nodes+1,vector<int>());
  22.     vector<node> Prop(nodes+1,{white,INF,INF,-1});
  23.     cout<<"Enter all the edges :-"<<endl;
  24.     for(int i=0; i<edges; i++){
  25.         int u,v;
  26.         cin>>u>>v;
  27.         graph[u].push_back(v);
  28.     }
  29.     cout<<"Dfs order : ";
  30.     dfs(graph,nodes,Prop);
  31.  
  32.     return 0;
  33. }
  34. void dfs(vector<vector<int>> &graph,int nodes, vector<node> &Prop){
  35.  
  36.     for(int i=0; i<=nodes ; i++){
  37.         Prop[i].color=white ;
  38.         Prop[i].parent=Nil;
  39.     }
  40.     timecnt=0;
  41.     for(int i=1; i<=nodes ; i++){
  42.         if(Prop[i].color==white){
  43.             dfs_visit(graph,i,Prop);
  44.         }
  45.     }
  46.  
  47.  
  48. }
  49. void dfs_visit(vector<vector<int>> &graph,int u, vector<node> &Prop){
  50.     cout<<" ->"<<u;
  51.     timecnt++;
  52.     Prop[u].discovery=timecnt;
  53.     Prop[u].color=grey;
  54.     for(int i=0,sz=graph[u].size(); i<sz ; i++){
  55.         int n=graph[u][i];
  56.         if(Prop[n].color==white){
  57.             Prop[n].parent=u;
  58.             dfs_visit(graph,n,Prop);
  59.         }
  60.  
  61.     }
  62.     Prop[u].color=black;
  63.     timecnt++;
  64.     Prop[u].done=timecnt;
  65.  
  66. }
  67.  
  68. //                      bfs
  69.  
  70. #include<bits/stdc++.h>
  71. using namespace std;
  72. #define pii pair<int,int>
  73. int fx[]={1,-1,0,0};
  74. int fy[]={0,0,1,-1};
  75. int cell[201][201];
  76. int d[201][201];
  77. int vis[201][201];
  78. int row,col;
  79. void bfs(int sx, int sy)
  80. {
  81.     memset(vis,0,sizeof vis);
  82.     vis[sx][sy] = 1;
  83.     queue < pii > q;
  84.     q.push(pii(sx,sy));
  85.     while(!q.empty())
  86.     {
  87.         pii top = q.front();
  88.         cout<<top.first<<" "<<top.second<<" ";
  89.         q.pop();
  90.         for(int k=0;k<4;k++)
  91.         {
  92.             int tx=top.first+fx[k];
  93.             int ty=top.second+fy[k];
  94.             if(tx>=0 && tx < row && ty>=0 && ty<col && cell[tx][ty] != -1 && vis[tx][ty] == 0)
  95.             {
  96.                 vis[tx][ty] = 1;
  97.                 d[tx][ty] = d[top.first][top.second] + 1;
  98.                 q.push(pii(tx,ty));
  99.             }
  100.         }
  101.     }
  102. }
  103. main()
  104. {
  105.     int n,e;
  106.     while(cin>>n>>e)
  107.     {
  108.         memset(cell,-1, sizeof cell);
  109.         row = n, col = n;
  110.         for(int i=0;i<e;i++)
  111.         {
  112.             int x,y;
  113.             cin>>x>>y;
  114.             cell[x][y] = 1;
  115.             if(i == e-1)
  116.                 bfs(x,y);
  117.         }
  118.     }
  119.     return 0;
  120. }
  121.  
  122. //          Floyd Warshall
  123.  
  124. #include<bits/stdc++.h>
  125. #define V 4
  126. #define INF 999999999
  127. using namespace std;
  128. void printSolution(int dist[][V]);
  129. void floydWarshall (int graph[][V])
  130. {
  131.  
  132.     int dist[V][V], i, j, k;
  133.  
  134.     for (i = 0; i < V; i++)
  135.         for (j = 0; j < V; j++)
  136.             dist[i][j] = graph[i][j];
  137.  
  138.     for (k = 0; k < V; k++)
  139.     {
  140.  
  141.         for (i = 0; i < V; i++)
  142.         {
  143.  
  144.             for (j = 0; j < V; j++)
  145.             {
  146.  
  147.                 if (dist[i][k] + dist[k][j] < dist[i][j])
  148.                     dist[i][j] = dist[i][k] + dist[k][j];
  149.             }
  150.         }
  151.     }
  152.  
  153.     printSolution(dist);
  154. }
  155.  
  156. void printSolution(int dist[][V])
  157. {
  158.  
  159.     for (int i = 0; i < V; i++)
  160.     {
  161.         for (int j = 0; j < V; j++)
  162.         {
  163.             if (dist[i][j] == INF)
  164.                 cout<<"INF"<<"\t";
  165.             else
  166.                 cout<<(dist[i][j])<<"\t";
  167.         }
  168.         printf("\n");
  169.     }
  170. }
  171.  
  172. int main()
  173. {
  174.  
  175.     int graph[V][V] = { {0, 5, INF, 10},
  176.                         {INF, 0, 3, INF},
  177.                         {INF, INF, 0, 1},
  178.                         {INF, INF, INF, 0}
  179.                     };
  180.  
  181.     floydWarshall(graph);
  182.     return 0;
  183. }
  184.  
  185. //              Dijktra's
  186.  
  187. #include<bits/stdc++.h>
  188. using namespace std;
  189. # define INF 0x3f3f3f3f
  190.  
  191. typedef pair<int, int> iPair;
  192.  
  193. class Graph
  194. {
  195.     int V;
  196.  
  197.     list< pair<int, int> > *adj;
  198.  
  199. public:
  200.     Graph(int V);
  201.  
  202.     void addEdge(int u, int v, int w);
  203.  
  204.     void shortestPath(int s);
  205. };
  206.  
  207. Graph::Graph(int V)
  208. {
  209.     this->V = V;
  210.     adj = new list<iPair> [V];
  211. }
  212.  
  213. void Graph::addEdge(int u, int v, int w)
  214. {
  215.     adj[u].push_back(make_pair(v, w));
  216.     adj[v].push_back(make_pair(u, w));
  217. }
  218.  
  219. void Graph::shortestPath(int src)
  220. {
  221.  
  222.     priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
  223.  
  224.     vector<int> dist(V, INF);
  225.  
  226.     pq.push(make_pair(0, src));
  227.     dist[src] = 0;
  228.  
  229.     while (!pq.empty())
  230.     {
  231.  
  232.         int u = pq.top().second;
  233.         pq.pop();
  234.  
  235.         list< pair<int, int> >::iterator i;
  236.         for (i = adj[u].begin(); i != adj[u].end(); ++i)
  237.         {
  238.             int v = (*i).first;
  239.             int weight = (*i).second;
  240.  
  241.             if (dist[v] > dist[u] + weight)
  242.             {
  243.                 dist[v] = dist[u] + weight;
  244.                 pq.push(make_pair(dist[v], v));
  245.             }
  246.         }
  247.     }
  248.  
  249.     for (int i = 0; i < V; ++i)
  250.         printf("%d \t\t %d\n", i, dist[i]);
  251. }
  252.  
  253. int main()
  254. {
  255.     int V = 9;
  256.     Graph g(V);
  257.  
  258.     g.addEdge(0, 1, 4);
  259.     g.addEdge(0, 7, 8);
  260.     g.addEdge(1, 2, 8);
  261.     g.addEdge(1, 7, 11);
  262.     g.addEdge(2, 3, 7);
  263.     g.addEdge(2, 8, 2);
  264.     g.addEdge(2, 5, 4);
  265.     g.addEdge(3, 4, 9);
  266.     g.addEdge(3, 5, 14);
  267.     g.addEdge(4, 5, 10);
  268.     g.addEdge(5, 6, 2);
  269.     g.addEdge(6, 7, 1);
  270.     g.addEdge(6, 8, 6);
  271.     g.addEdge(7, 8, 7);
  272.  
  273.     g.shortestPath(0);
  274.  
  275.     return 0;
  276. }
  277.  
  278. //          Heap_sort
  279.  
  280. #include <iostream>
  281.  
  282. using namespace std;
  283.  
  284. void heapSort(int a[], int n, int h);
  285. void buildMaxHeap(int a[], int n, int h);
  286. void maxHeapify(int a[], int h, int i);
  287. int left(int i);
  288. int right(int i);
  289.  
  290. int main(){
  291.     int a[20], n, h;
  292.     cout << "How many numbers?: ";
  293.     cin >> n;
  294.     h = n;
  295.     cout << "\nEnter numbers: ";
  296.     for(int i = 1; i <= n; ++i)
  297.         cin >> a[i];
  298.     heapSort(a, n, h);
  299.     cout << "\nOutput numbers in sorted order: ";
  300.     for(int i = 1; i <= n; ++i)
  301.         cout << " " << a[i];
  302.     return 0;
  303. }
  304.  
  305. void heapSort(int a[], int n, int h){
  306.     buildMaxHeap(a, n, h);
  307.     for(int i = n; i >= 2; --i){
  308.         swap(a[1], a[i]);
  309.         h--;
  310.         maxHeapify(a, h, 1);
  311.     }
  312. }
  313.  
  314. void buildMaxHeap(int a[], int n, int h){
  315.     h = n;
  316.     for(int i = (n/2); i >= 1; --i)
  317.         maxHeapify(a, h, i);
  318. }
  319.  
  320. void maxHeapify(int a[], int h, int i){
  321.     int l = left(i);
  322.     int r = right(i);
  323.     int largest;
  324.     if(l <= h && a[l] > a[i])
  325.         largest = l;
  326.     else
  327.         largest = i;
  328.     if(r <= h && a[r] > a[largest])
  329.         largest = r;
  330.     if(largest != i){
  331.         swap(a[i], a[largest]);
  332.         maxHeapify(a, h, largest);
  333.     }
  334. }
  335.  
  336. int left(int i){
  337.     return (2 * i);
  338. }
  339.  
  340. int right(int i){
  341.     return (2 * i + 1);
  342. }
  343.  
  344. //          Insertion Sort
  345.  
  346. #include <bits/stdc++.h>
  347. using namespace std;
  348.  
  349. void insertionSort(int arr[], int n)
  350. {
  351.     int i, key, j;
  352.     for (i = 1; i < n; i++)
  353.     {
  354.         key = arr[i];
  355.         j = i - 1;
  356.  
  357.         while (j >= 0 && arr[j] > key)
  358.         {
  359.             arr[j + 1] = arr[j];
  360.             j = j - 1;
  361.         }
  362.         arr[j + 1] = key;
  363.     }
  364. }
  365.  
  366. void printArray(int arr[], int n)
  367. {
  368.     int i;
  369.     for (i = 0; i < n; i++)
  370.         cout << arr[i] << " ";
  371.     cout << endl;
  372. }
  373.  
  374. int main()
  375. {
  376.     int arr[] = { 12, 11, 13, 5, 6 };
  377.     int n = sizeof(arr) / sizeof(arr[0]);
  378.  
  379.     insertionSort(arr, n);
  380.     printArray(arr, n);
  381.  
  382.     return 0;
  383. }
  384.  
  385. //          LCS
  386.  
  387. #include<iostream>
  388. #include<cstring>
  389. #include<cstdlib>
  390. using namespace std;
  391.  
  392. void lcs( char *X, char *Y, int m, int n )
  393. {
  394. int L[m+1][n+1];
  395.  
  396. for (int i=0; i<=m; i++)
  397. {
  398.     for (int j=0; j<=n; j++)
  399.     {
  400.     if (i == 0 || j == 0)
  401.         L[i][j] = 0;
  402.     else if (X[i-1] == Y[j-1])
  403.         L[i][j] = L[i-1][j-1] + 1;
  404.     else
  405.         L[i][j] = max(L[i-1][j], L[i][j-1]);
  406.     }
  407. }
  408.  
  409.  
  410. int index = L[m][n];
  411.  
  412.  
  413. char lcs[index+1];
  414. lcs[index] = '\0';
  415.  
  416. int i = m, j = n;
  417. while (i > 0 && j > 0)
  418. {
  419.     if (X[i-1] == Y[j-1])
  420.     {
  421.         lcs[index-1] = X[i-1];
  422.         i--; j--; index--;
  423.     }
  424.  
  425.     else if (L[i-1][j] > L[i][j-1])
  426.         i--;
  427.     else
  428.         j--;
  429. }
  430.  
  431. cout << "LCS of " << X << " and " << Y << " is " << lcs;
  432. }
  433.  
  434. int main()
  435. {
  436. char X[] = "AGGTAB";
  437. char Y[] = "GXTXAYB";
  438. int m = strlen(X);
  439. int n = strlen(Y);
  440. lcs(X, Y, m, n);
  441. return 0;
  442. }
  443.  
  444. //          Merge_Sort
  445.  
  446. #include<stdlib.h>
  447. #include<stdio.h>
  448.  
  449. void merge(int arr[], int l, int m, int r)
  450. {
  451.     int i, j, k;
  452.     int n1 = m - l + 1;
  453.     int n2 = r - m;
  454.  
  455.     int L[n1], R[n2];
  456.  
  457.     for (i = 0; i < n1; i++)
  458.         L[i] = arr[l + i];
  459.     for (j = 0; j < n2; j++)
  460.         R[j] = arr[m + 1+ j];
  461.  
  462.     i = 0;
  463.     j = 0;
  464.     k = l;
  465.     while (i < n1 && j < n2)
  466.     {
  467.         if (L[i] <= R[j])
  468.         {
  469.             arr[k] = L[i];
  470.             i++;
  471.         }
  472.         else
  473.         {
  474.             arr[k] = R[j];
  475.             j++;
  476.         }
  477.         k++;
  478.     }
  479.  
  480.     while (i < n1)
  481.     {
  482.         arr[k] = L[i];
  483.         i++;
  484.         k++;
  485.     }
  486.  
  487.     while (j < n2)
  488.     {
  489.         arr[k] = R[j];
  490.         j++;
  491.         k++;
  492.     }
  493. }
  494.  
  495. void mergeSort(int arr[], int l, int r)
  496. {
  497.     if (l < r)
  498.     {
  499.         int m = l+(r-l)/2;
  500.  
  501.         mergeSort(arr, l, m);
  502.         mergeSort(arr, m+1, r);
  503.  
  504.         merge(arr, l, m, r);
  505.     }
  506. }
  507.  
  508. void printArray(int A[], int size)
  509. {
  510.     int i;
  511.     for (i=0; i < size; i++)
  512.         printf("%d ", A[i]);
  513.     printf("\n");
  514. }
  515.  
  516. int main()
  517. {
  518.     int arr[] = {12, 11, 13, 5, 6, 7};
  519.     int arr_size = sizeof(arr)/sizeof(arr[0]);
  520.  
  521.     printf("Given array is \n");
  522.     printArray(arr, arr_size);
  523.  
  524.     mergeSort(arr, 0, arr_size - 1);
  525.  
  526.     printf("\nSorted array is \n");
  527.     printArray(arr, arr_size);
  528.     return 0;
  529. }
  530.  
  531. //          Quick_Sort
  532.  
  533. #include<stdio.h>
  534.  
  535. void swap(int* a, int* b)
  536. {
  537.     int t = *a;
  538.     *a = *b;
  539.     *b = t;
  540. }
  541.  
  542. int partition (int arr[], int low, int high)
  543. {
  544.     int pivot = arr[high];
  545.     int i = (low - 1);
  546.     for (int j = low; j <= high- 1; j++)
  547.     {
  548.         if (arr[j] <= pivot)
  549.         {
  550.             i++;
  551.             swap(&arr[i], &arr[j]);
  552.         }
  553.     }
  554.     swap(&arr[i + 1], &arr[high]);
  555.     return (i + 1);
  556. }
  557.  
  558. void quickSort(int arr[], int low, int high)
  559. {
  560.     if (low < high)
  561.     {
  562.         int pi = partition(arr, low, high);
  563.  
  564.         quickSort(arr, low, pi - 1);
  565.         quickSort(arr, pi + 1, high);
  566.     }
  567. }
  568.  
  569. void printArray(int arr[], int size)
  570. {
  571.     int i;
  572.     for (i=0; i < size; i++)
  573.         printf("%d ", arr[i]);
  574.     printf("\n");
  575. }
  576.  
  577. int main()
  578. {
  579.     int arr[] = {10, 7, 8, 9, 1, 5};
  580.     int n = sizeof(arr)/sizeof(arr[0]);
  581.     quickSort(arr, 0, n-1);
  582.     printf("Sorted array: \t");
  583.     printArray(arr, n);
  584.     return 0;
  585. }
  586.  
  587. //          Activity Selection
  588.  
  589. #include<bits/stdc++.h>
  590. using namespace std;
  591. bool custom_sort(pair<long long,long long> a, pair<long long,long long> b){
  592.     return a.second<b.second;
  593. }
  594.  
  595. int main(){
  596.     long long int n;
  597.     cout<<"Number of activity : ";
  598.     cin>>n;
  599.     cout<<"Enter all activities :-"<<endl;
  600.     vector< pair<long long,long long> > activities ,ans;
  601.     for(int i=0; i<n ; i++){
  602.         long long st, sp;
  603.         cin>>st>>sp;
  604.         activities.push_back(make_pair(st,sp));
  605.     }
  606.     sort(activities.begin(),activities.end(),custom_sort);
  607.     ans.push_back(make_pair(activities[0].first,activities[0].second));
  608.     long long sp=activities[0].second;
  609.     for(int i=1; i<n;i++){
  610.         if(activities[i].first>=sp){
  611.             ans.push_back(activities[i]);
  612.             sp=activities[i].second;
  613.         }
  614.     }
  615.     cout<<"All possible activities are :- "<<endl;
  616.  
  617.     for( auto i : ans ){
  618.         cout<<i.first<<" "<<i.second<<endl;
  619.     }
  620.  
  621.     return 0;
  622. }
  623.  
  624. //              Binary Search
  625.  
  626. #include <stdio.h>
  627.  
  628. int binarySearch(int arr[], int l, int r, int x)
  629. {
  630.     while (l <= r) {
  631.         int m = l + (r - l) / 2;
  632.  
  633.         if (arr[m] == x)
  634.             return m;
  635.  
  636.         if (arr[m] < x)
  637.             l = m + 1;
  638.  
  639.         else
  640.             r = m - 1;
  641.     }
  642.  
  643.     return -1;
  644. }
  645.  
  646. int main(void)
  647. {
  648.     int arr[] = { 2, 3, 4, 10, 40 };
  649.     int n = sizeof(arr) / sizeof(arr[0]);
  650.     int x = 10;
  651.     int result = binarySearch(arr, 0, n - 1, x);
  652.     (result == -1) ? printf("Element is not present"
  653.                             " in array")
  654.                 : printf("Element is present at "
  655.                             "index %d",
  656.                             result);
  657.     return 0;
  658. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement