A practical understanding of topological sorting and ordering 

The shortest path problem is pivotal in graph theory. It aims at discovering the “most efficient’ or ‘best’ way of moving from x to y, where x and y are both nodes in a given graph. The most efficient or best in this context is evaluated by the lowest sum of edge weights between the path of both vertices. The shortest or quickest path is arrived at by summing the lengths of the individual edges. A best-case scenario is a graph with edges having positive weights. There is also the concept of single-source shortest path problem with s as the source node. For clarity, the source node initiates the transversal within the graph.

Continue reading

Exploring Breadth First Search and Depth First Search in a graph

You might have encountered the words, breadth and depth in real-world scenarios. Breadth implies the complete range of knowledge of any given subject or topic. On the other hand, depth in terms of learning touches on the degree to which a particular subject is magnified or explored. Let’s begin with the breadth first search or the BFS of a given graph. Now BFS does not refer to Best Friends from school but Breadth-First Search. 

Exploring Breadth First Search or Breadth First Traversal 

BFS is an algorithm that is designed to search for a graph or tree data formation. It usually travels in a breadthward motion and utilises a queue as a prompt to identify the next vertex to commence a traversal. If a roadblock is encountered or no adjacent node is found, the tree root or the source node is removed for the queue. The traversal of the graph usually begins with a ‘search key’ or the initialising node. Imagine a hotel with so many floors and rooms as nodes, a breadth-first traversal algorithm is like a cleaning staff that will clean rooms floor by floor. All neighbouring nodes at the current depth or floor with the example above will be visited to clean before moving to the vertices or rooms on the next floor. No node is expected to be revisited as one would not expect hotel staff to clean the same room twice in the same period. Once a room is cleaned, it is ticked on a sheet as a visited while with BFS, the neighbouring reversed node is enqueued or marked as visited, 

Continue reading