Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Questions
Topics :
1. Graph
2. Linked List
3. Dynamic Programming
4. Sorting And Searching
5. Tree / Binary Search Tree
6. Number Theory
7. BIT Manipulation
8. String / Array
Graph
1. Binary Search
2. Search an element in a sorted and rotated array
3. Bubble Sort
4. Insertion Sort
5. Merge Sort
6. Heap Sort (Binary Heap)
7. Quick Sort
8. Interpolation Search
9. Find Kth Smallest/Largest Element In Unsorted Array
10. Given a sorted array and a number x, find the pair in array
whose sum is closest to x
Tree / Binary Search Tree
1. Modular Exponentiation
2. Modular multiplicative inverse
3. Primality Test | Set 2 (Fermat Method)
4. Euler’s Totient Function
5. Sieve of Eratosthenes
6. Convex Hull
7. Basic and Extended Euclidean algorithms
8. Segmented Sieve
9. Chinese remainder theorem
10. Lucas Theorem
BIT Manipulation
BFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two
categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so to make sure that we cover every
vertex, we can also run the BFS algorithm on every node
For example, in the following graph, we start traversal from vertex 2. When we come to
vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we
don’t mark visited vertices, then 2 will be processed again and it will become a non-
terminating process. A Breadth First Traversal of the following graph is 2, 0, 3, 1.
void Graph::BFS(int s) s = queue.front();
{ cout << s << " ";
// Mark all the vertices as not visited queue.pop_front();
bool *visited = new bool[V];
for(int i = 0; i < V; i++) // Get all adjacent vertices of the
visited[i] = false; dequeued
// vertex s. If a adjacent has not been
// Create a queue for BFS visited,
list<int> queue; // then mark it visited and enqueue it
// Mark the current node as visited and for (i = adj[s].begin(); i != adj[s].end();
enqueue it ++i)
visited[s] = true; {
queue.push_back(s); if (!visited[*i])
{
// 'i' will be used to get all adjacent visited[*i] = true;
list<int>::iterator i; queue.push_back(*i);
while(!queue.empty()) }
{ }
// Dequeue a vertex from queue and }
print it }
Depth First Search or DFS for a Graph
Depth First Traversal (or Search) for a graph is similar to Depth
First Traversal of a tree. The only catch here is, unlike trees,
graphs may contain cycles, so we may come to the same node
again. To avoid processing a node more than once, we use a
boolean visited array.
DFS pseudocode (recursive implementation)
The pseudocode for DFS is shown below. In the init() function, notice that we run the
DFS function on every node. This is because the graph might have two different
disconnected parts so to make sure that we cover every vertex, we can also run the
DFS algorithm on every node.
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}