In this video we break down the BFS algorithm in a visual manner with examples and key intuition. We then show the implementation of the algorithm with code and then finish off the video by demonstrating how you can use the BFS algorithm to solve the Flood Fill problem.
0:45 BFS Intuition/Examples
2:39 BFS Implementation
5:19 Flood Fill Problem
This video wouldn't be possible without the open source manim library created by 3blue1brown: https://github.com/3b1b/manim
Here is link to the repository that contains the code used to generate the animations in this video: https://github.com/nipunramk/Reducible
A week ago we learned about graph data structure. Today we will talk about how we can work with graphs. We will try to find distances between two nodes in a graph. This is one of the main uses of graphs and it’s called graph traversal. There are two main graph algorithms Breadth First Search (BFS) and Depth First Search (DFS) and today we will talk about BFS.
This is how our graph looks like:
Breadth First Search
In our example we will work with an adjacency matrix. This is how matrix represents graph above:
We will start with an input node, then visit all its neighbors which is one edge away. And then visit all their neighbors. Point is to determine how close the node is to the root node.
Function which we will write in a moment will return an object with key value pairs where key will represent node and value how far this node is from the root.
First we will loop over the adjacency matrix (2D array), create as many key value pairs as many nodes we have on the graph. Initially we will assign distance to the infinity which represents lack of connection between the nodes.
Today, let us touch base on some fundamental concepts like search algorithms.
In simple terms, **searching **is a process of looking up a particular data record in the database or in the collection of items. A search typically answers as true or false whether the particular data in which we are referring is found or not and perform the next action accordingly.
Commonly used algorithms for search are:
Let us understand them in detail with some example
Linear Search Algorithm is the simplest and easiest form of the search algorithm. In this algorithm, a sequential search is made over all the items one by one to search for the targeted item. Each item is checked in sequence until the match is found. If the match is found, the searched item is returned otherwise the search continues till the end.
To make it easy to understand, let us see understand linear search using a flow diagram
Linear Search — Data Flow
In _Binary search algorithm, _begins with an interval covering the whole array and diving it in half. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
To make it easy to understand, let us see understand binary search using flow diagram and example as below.
Binary Search — Data Flow
#sorting-algorithms #algorithms #data-structures #search-and-sort #searching-algorithm
The Depth First Search algorithm traverses the graph and explores each adjacent node before backtracking and moving to the next node. It utilizes the stack data structure. Remember, the stack uses the last-in-first-out (LIFO) approach. We push items onto the stack and pop items from the top of the stack. We’ll keep track of the stack and we’ll show the vertices in the order of which they were discovered.
We’ll begin our traversal at vertex A. Vertex A has been discovered, so it’s added to the discovery list. Vertex A is also pushed onto the stack and is marked as visited.
#dfs #algorithms #programming #depth-first-search #computer-science
We have to search for a value **x **in a sorted matrix M. If x **exists, then return its coordinates **(i, j), else return (-1, -1).
Let us consider the above matrix as an example. In this example, we are going to search for the value 12. Since 12 is present in the matrix, the algorithm should return its coordinates (2, 1)
A simple approach is to traverse all the values in the matrix and check if it is equal to 12.
The worst case time complexity of the above algorithm will be
_O(n x m) = O(n²) __when _n = m
The above algorithm behaves worse for large values of n and m. Let us look into the efficient algorithm now.
1\. Start from Top Right position (0, m - 1) in the matrix M 2\. If the value is equal to x return (0, m - 1) 3\. Move one row down if the current value is less than x 4\. Move one column left if the current value is greater than x
Let us apply this algorithm into our matrix M. We are going to search for the value 12 in M
#data-structures #search-sorted-matrix #2d-binary-search #algorithms #matrix-search #algorithms
Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. Based on the type of search operation, these algorithms are generally classified into two categories:
Linear Search: The idea is to traverse the given array arr and find the index at which the element is present. Below are the steps:
#java #searching #algorithms-searching #binary search