A graph consists of a finite set of vertices or nodes and a set of edges connecting these vertices. Two vertices are said to be adjacent if they are connected to each other by the same edge.
Some basic definitions related to graphs are given below. You can refer to Figure 1 for examples.
Fig 1. Visualization of Terminology of Graphs (Image by Author)
Fig 2. Animation of BFS traversal of a graph (Image by Author)
Traversing or searching is one of the fundamental operations which can be performed on graphs. In breadth-first search (BFS), we start at a particular vertex and explore all of its neighbours at the present depth before moving on to the vertices in the next level. Unlike trees, graphs can contain cycles (a path where the first and last vertices are the same). Hence, we have to keep track of the visited vertices. When implementing BFS, we use a queue data structure.
Figure 2 denotes the animation of a BFS traversal of an example graph. Note how vertices are discovered (yellow) and get visited (red).
#computer-science #data-structures #data-science #algorithms #graph