Skip to main content

Posts

Showing posts from January, 2023

Breadth First Search (BFS)

  Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a ‘search key’) and explores the neighbor nodes first, before moving to the next level neighbors. BFS uses a queue data structure to hold the next nodes to visit. The first node visited is the root node, and then it visits all the neighbor nodes at the present depth level from left to right, and adds them to the queue. Then it visits the next level nodes and continues the same process until it reaches the end of the tree or graph. BFS can be used to solve many problems in graph theory, for example: Finding the shortest path between two nodes u and v, with the condition that the path is the shortest among all possible paths. This can be done using a predecessor subgraph. Testing a graph for bipartiteness. Finding all connected components in an undirected graph. Finding the level of each node in a t

Depth first Search (DFS)

  Depth-first search (DFS) is a method for traversing a graph or tree data structure. It involves exploring as far as possible along each branch before backtracking. The algorithm starts at the root node (or a selected node) and explores as far as possible along each branch before backtracking. The basic idea behind DFS is to go deep in each branch before exploring siblings. This is done by using a stack data structure to keep track of the current vertex and the vertices to be visited next. The algorithm explores a vertex, marks it as visited, and adds it to the stack. It then selects the next vertex to visit by popping the top element from the stack. If the popped vertex has unvisited vertices, it pushes them onto the stack and continues exploring. One of the main advantages of DFS is its simplicity. It is easy to implement and requires minimal memory. It can also be used to solve problems such as finding the connected components of a graph and testing if a graph is bipartite. In addi

Binary Search

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing the search interval in half. The basic idea is to use the information that the array is sorted and reduce the time complexity to O(log n). The algorithm begins by comparing the middle element of the array with the target value. If the target value matches the middle element, its position in the array is returned. If the target value is less than the middle element, the algorithm repeats its action on the sub-array to the left of the middle element. If the target value is greater than the middle element, the algorithm repeats its action on the sub-array to the right of the middle element. The algorithm will then continue to the next step until the target value is found or the search interval is empty. The search interval is initially the whole array. If the target value is less than the middle element of the current array, the algorithm will search the left half of th

Linear Search

Linear search is a method for finding a specific value in a list or array by iterating through each element in the list or array until the desired value is found. This method is also known as a sequential search. It is a simple search algorithm that is easy to understand and implement. The basic idea behind linear search is to start at the first element in the list or array and compare it to the desired value. If the element is not the desired value, the algorithm moves on to the next element and continues this process until the desired value is found or the end of the list or array is reached. One advantage of linear search is that it is easy to implement and understand, making it a good choice for small lists or arrays. Additionally, linear search does not require any pre-processing of the data and can be applied to any data type. However, the main disadvantage of linear search is its time complexity. The time it takes to find a specific value in a list or array using linear search i