DSA Theory

Breadth First Search or BFS for a Graph

One of the basic graphs traversal techniques is Breadth First Search (BFS). It starts with a node and first moves all around it. Their neighboring are walked once all adjacent have been visited. Unlike DFS, this visits closest vertices before others in a different sense. We mostly move vertically level by level. Many well-known graph algorithms, including Prim’s algorithm, Kahn’s Algorithm, and Dijkstra’s shortest path, depend on BFS. BFS itself finds shortest path in an unweighted graph, detects cycle in both directed and undirected graphs, and many more issues. Starting from a given source, the program investigates all reachable vertices from the supplied source. It bears resemblance to a tree’s Breadth-First Traversal. Like tree, we start with the supplied source (in tree, we start with root) and move vertices level by level employing a queue data structure. The sole catch here is that graphs could include cycles, unlike trees, thus we might find the same node once again. We employ a boolean visited array to prevent repeatedly processing a node. Enqueue the specified source vertex and mark it as visited initially. BFS of the whole Graph maybe disconnected In case of a disconnected graph, the aforementioned code prints just those vertices that are reachable from the source, thereby depending on a source it accepts as input. Now let us discuss the graph maybe disconnected and the method that displays all vertices without any source. The concept is straightforward: instead of requesting BFS for a single vertex, we call the above designed BFS one by one for all non-visited vertices. Breadth-First Search (BFS) Algorithm: A Complexity Study Time Complexity of the BFS Algorithm: O(V + E)BFS investigates every graph edge and vertex. Under worst circumstances, it visits every edge and vertex once. BFS’s time complexity is thus O(V + E), where V and E are the provided graph’s vertex and edge counts respectively. BFS Algorithm Auxiliary Space: O(V)To mark the vertices that must be visited, BFS employs a queue. The queue may, in the worst situation include every vertex in the graph. BFS has therefore O(V) as its space complexity.BFS uses in graphs:In computer science and graph theory, BFS finds uses including: Data packet routing in network protocols benefits from BFS’s ability to determine the shortest path between two nodes in a network. Breadth First Search (BFS) for a Graph FAQs First question: What is BFS and how operates?BFS is a graph traversal method whereby a graph methodically explored visiting all the vertices at a particular level then proceeding to the following level. Beginning at a starting vertex, it enqueues it into a queue and notes it as visited. It then visits a vertex from the queue, dequeues all of her unvisited neighbors into the queue, This process keeps on till the queue runs empty. Second question: For what uses BFS finds appropriate?BFS finds the shortest path in an unweighted graph, detects cycles in a graph, topologically sorts a directed acyclic graph (DAG), identifies related components in a graph, and solves mazes and Sudoku puzzles among other things. What is BFS’s temporal complexity in question three?BFS’s temporal complexity is O(V + E), where V is the graph’s vertex count and E its edge count. Fourth question: How complexly space BFS uses?BFS employs a queue to monitor the vertices that must be visited, so its space complexity is O(V). The benefits of BFS use include question five.For an unweighted graph, BFS is straightforward to apply and effective for determining the shortest path. It ensures furthermore that every vertex in the graph gets visited.

Breadth First Search or BFS for a Graph Read More »

Floyd’s Cycle Finding Algorithm

Overview Floyd’s cycle detection approach is a pointer algorithm that traverses the sequence at varying speeds using just two points. The application of this approach in Linked Lists and its results are described in the paper. Finding out if the linked list has a cycle or not is the goal. The head node’s two pointers are kept first. You move one of the pointers by one step and the other by two steps at each iteration. The tortoise and the hare are your two pointers. One of the two scenarios will eventually occur: Hare will arrive at the linked list’s tail (null), indicating that it is cycle-free.When a tortoise meets a hare, a cycle is in place. The following is the most logical way to interpret this: The hare walks two nodes and the tortoise walks one node at each stage of the algorithm. In actuality, the hare approaches after the tortoise has been gone for one unit of distance. In actuality, the hare moves one step and the tortoise remains still with each step. The Hare-Tortoise algorithm, also known as Floyd’s cycle finding algorithm, is a pointer algorithm that employs just two pointers and traverses the sequence at varying speeds. A loop in a linked list can be located using this approach. Two pointers are used, one of which moves twice as quickly as the other. The two are referred to as the slow pointer and the quick pointer, respectively. One of these things will happen as you navigate the linked list: There is no loop in the linked list if the Fast pointer reaches the end (NULL).There is a loop in the linked list because the fast pointer eventually catches the slow pointer again. Floyd’s Cycle Finding Algorithm: The two pointers, slow and fast, supposed to begin at the top of the linked list. What is the operation of Floyd’s Algorithm? Two pointers—slow and fast—are started from the head of the linked list by the algorithm. One node at a time, we progress slowly, and two nodes at a time, we move quickly. They will undoubtedly meet if there is a loop. The following facts make this strategy effective: The fast pointer needs to be inside the loop when the slow pointer enters it.We may observe that the distance between slow and fast pointers increases by one with each iteration if we look at how they move. The distance between the two pointers grows after each repetition in which the slow pointer advances one step and the fast pointer advances two. The distance between the slow and fast pointers increases k+1 after one iteration if the slow pointer is initially k from a specific point in the cycle. This distance becomes k+2 after two iterations, and so forth. This distance will eventually match the cycle length n as they keep moving within the cycle. Since both points are traveling within the same cycle and the distance is now encircling the cycle, they will collide. Please see the page “How does Floyd’s Algorithm work?” for further information on the algorithm’s operation and proof. Locate a Cycle’s Beginning Node in a Linked List Use the following procedures to determine a cycle’s beginning node in a linked list: The meeting point (assuming a cycle exists) where the slow and fast pointers intersect inside the cycle can be found using the procedure above.Reset one pointer (slow) to the list’s head after identifying the cycle. At the meeting spot, keep the other pointer (quick).One step at a time, move both pointers. The cycle begins at the node where they reunite. Benefits: It uses extremely little memory because it does not require additional space (such as a hash table) to record visited nodes.The middle element of a linked list can also be found using an adaptation of this approach. Learn about the Two Pointers method. Let me start by introducing a challenging technique from the field of problem-solving. Determine the middle node in a linked list. List data structures should known to you. Otherwise, kindly learn it before proceeding.Our circles are therefore nodes of a linked list based on the images above. The middle ones on the list are the red ones. Both nodes (3) and (4) can be regarded as middle nodes if the list is even in length. However, let’s assume that only node (4) regarded as a middle. You have to locate the red circle (a midpoint) and bring it back. Please take note that you are unaware of the list’s length (number of nodes), foxy fella. The solution’s complexity should be as little as feasible. Counting every node by iterating them would be the most obvious solution. After that, go over it again, stopping at half of the count. Lovely! You are currently halfway through the list. The issue has resolved! However, is it truly a quick fix? You see, you have to go through your list nearly twice. What if I told you that there is only one traversal that can solve it? Two Pointers is the name of the trick. A second pointer must introduced. Your default iterator, which advances one step at a time, is the first one. Your slow tortoise is it. Your hare is the second one. It takes two steps all at once.

Floyd’s Cycle Finding Algorithm Read More »