Learning Sites:

To learn Click Here

Some Problems About Linked List

Dynamic Memory Allocation:

Code Snippet:

1 2 3 4 5 6 7 8 |
int dim; cin>>dim; string **s = new string *[dim]; s[0] = new string[100]; s[1] = new string[100]; s[0][1]="ajksdhjkasdh"; s[1][0]="sswd"; cout<<s[1][0]<<" "<<s[0][1]<<"\n"; |

BellmanFord Algorithm:

Take all the edge in a struct as:

1 |
Struct Edge{int source,destination,cost}; |

Relax all the edge for (node-1)(e.g. total number of node of the vertex) number of time.

After that check one more time if all the edges are relaxed(minimized valued from source) or not.

If there found any edge not minimized then negative cycle exists.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
BellmanFord(int node,int edge) { bool negativecycle = false; for(int i=1;i<=node-1;i++) { for(int j=0;j<edge;j++) { if(cost[e[j].destination]>cost[e[j].src]+e[j].weight) cost[e[j].destination] = cost[e[j].src]+e[j].weight } } for(int j=0;j<edge;j++) { if(cost[e[j].destination]>cost[e[j].src]+e[j].weight) negativecycle = 1; } } |

kadane’s algorithm:

Implementation:

For 1D array:

- take 2 variable : max_end_here = 0 && max_so_far = 0
- for all the elements of the 1d array starting from the beginning
- add elements with max_end_here and compare it with 0 and max_so_far
- if max_end_here< 0 then assign max_end_here 0
- if max_end_here > max_so_far than update max_so_far
- after doing it for all the elements of the array return max_so_far

Code Snippet:

1 2 3 4 5 6 7 8 9 10 |
for(int i=0;i<N;i++) { max_end_here+=line[i]; if(max_end_here<0) max_end_here = 0; if(max_end_here>max_so_far) max_so_far = max_end_here; } |

For 2D array:

Travelling Salesman Problem:

Let the given set of vertices be {1, 2, 3, 4,….n}. Let us consider 1 as starting and ending point of output. For every other vertex i (other than 1), we find the minimum cost path with 1 as the starting point, i as the ending point and all vertices appearing exactly once. Let the cost of this path be cost(i), the cost of corresponding Cycle would be cost(i) + dist(i, 1) where dist(i, 1) is the distance from i to 1. Finally, we return the minimum of all [cost(i) + dist(i, 1)] values. This looks simple so far. Now the question is how to get cost(i)?

To calculate cost(i) using Dynamic Programming, we need to have some recursive relation in terms of sub-problems. Let us define a term *C(S, i) be the cost of the minimum cost path visiting each vertex in set S exactly once, starting at 1 and ending at i*.

We start with all subsets of size 2 and calculate C(S, i) for all subsets where S is the subset, then we calculate C(S, i) for all subsets S of size 3 and so on. Note that 1 must be present in every subset.

collected from geeksforgeeks.com

Topological Sort

We can modify DFS to find Topological Sorting of a graph. In DFS, we start from a vertex, we first print it and then recursively call DFS for its adjacent vertices. In topological sorting, we use a temporary stack. We don’t print the vertex immediately, we first recursively call topological sorting for all its adjacent vertices, then push it to a stack. Finally, print contents of stack. Note that a vertex is pushed to stack only when all of its adjacent vertices (and their adjacent vertices and so on) are already in stack.

collected from geeksforgeeks.com

Articulation Point Finding Algorithm:

There are two ways to solve the problem.

One is using dfs on the graph excluding one vertex each time and check whether all the vertices are visited or not. If for exclusion of any vertex causes to unreachable to any vertex then that vertex is counted as articulation point. This is O(V*(V+E)) time complexity approach.

There is another approach called tarjan’s algorithm which is a linear time approach O(V+E) time complexity algorithm.

Approach is checking the following two conditions:

1)if any vertex is root and has more than one child then its an AP.

2)if any vertex is not root then search for any of its child which has the property that rooted sub graph of that child (the sub graph where that child node is the root) does not contain any node which has a back edge to any of the ancestors of the parent node(so if that child node is removed all of the nodes of that sub graph is got a way to be connected to the main graph).

To check this two array is kept to trace the discovery time and low of the nodes.

The implementation of the algorithms in its base form is given here.

**Swapping:**

Swapping without using third variable:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { int a, b; printf("Enter two integers to swap\n"); scanf("%d%d", &a, &b); a = a + b; b = a - b; a = a - b; printf("a = %d\nb = %d\n",a,b); return 0; } |

Swap two numbers using pointers:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> #include <iostream> using namespace std; int main() { int x, y, *a, *b, temp; scanf("%d%d", &x, &y); a = &x; b = &y; temp = *b; *b = *a; *a = temp; cout<<x<<" "<<y<<"\n"; return 0; } |

Swap using call by reference:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> #include <iostream> using namespace std; void swap(int *a,int *b) { int tmp; tmp=*a; *a=*b; *b=tmp; } int main() { int x, y; scanf("%d%d", &x, &y); swap(&x,&y); cout<<x<<" "<<y<<"\n"; return 0; } |

Swap using bitwise XOR:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> #include <iostream> using namespace std; int main() { int x, y; scanf("%d%d", &x, &y); x=x^y; y=x^y; x=x^y; cout<<x<<" "<<y<<"\n"; return 0; } |

**how to get integer from comma separated string in c++:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <vector> #include <string> #include <sstream> std::string str = "1,2,3,4,5,6"; std::vector<int> vect; std::stringstream ss(str); int i; while (ss >> i) { vect.push_back(i); if (ss.peek() == ',') ss.ignore(); } |

for learning more about this click here .