We are All Souls

When clients first start working with me they are often feeling overwhelmed, upset, or out of balance. Many of them have forgotten who they really are, judging themselves on the basis of: Whether…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Top 10 Algorithms Programmers Should Know In Graph Data Structure

Graph algorithms are a set of instructions that traverse (visits nodes of a) graph. Some algorithms are used to find a specific node or the path between two given nodes.

Graphs are very useful data structures which can be to model various problems. These algorithms have direct applications on Social Networking sites, State Machine modeling and many more. I have attached my source code for all the algorithms discussed below. You can use it for your referral.

Breadth First Search is a recursive algorithm for searching all the vertices of a graph or tree data structure. Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node and explores all the neighboring nodes. Then, it selects the nearest node and explores all the unexplored nodes. While using BFS for traversal, any node in the graph can be considered as the root node.

An simple algorithm to be remembered for BFS: The Diagrammatic representation of BFS Traversal:

Depth first Search or Depth first traversal is a recursive algorithm for searching all the vertices of a graph or tree data structure. Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.

An simple algorithm to be remembered for DFS: The Diagrammatic representation of DFS Traversal:

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. There can be more than one topological sorting for a graph.

An simple algorithm to be remembered for Topological sort:

2.1 if v is not visited, then

2.2 TopoSort(c, visited, stack)

2.3 done

3. push u into a stack

The Diagrammatic representation of Topological sort

One of the Topological sort for this graph is:

5 -> 4 -> 2 -> 3 -> 1 -> 0

Kahn’s topological sort algorithm is cycle detection algorithm, which also provides an efficient way to print the topological order. Kahn’s topological sort algorithm works by finding vertices with no incoming edges and removing all outgoing edges from these vertices.

Dijkstra’s algorithm allows us to find the shortest path between any two vertices of a graph. It differs from the minimum spanning tree because the shortest distance between two vertices might not include all the vertices of the graph.

It is a single-source shortest path algorithm. Here, single-source means that only one source is given, and we have to find the shortest path from the source to all the nodes.

After applying Dijkstra’s Algorithm:

Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices of a weighted graph. It is similar to Dijkstra’s algorithm but it can detect graphs in which edges can have negative weight cycles.

Dijkstra’s Algorithm (can’t able to detect negative weight cycle) can give an incorrect result because they can go through a negative weight cycle and reduce the path length.

Dijkstra doesn’t work for Graphs with negative weights, Bellman-Ford works for such graphs. Bellman-Ford is also simpler than Dijkstra and suites well for distributed systems.

The Output Of Bellman Ford’s Algorithm:

Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it does not work for the graphs with negative cycles (where the sum of the edges in a cycle is negative). Here, a dynamic programming concept will be used.

Algorithm Behind Floyd-Warshall Algorithm:

Dij(k) ← min ( Dij(k-1), Dik(k-1)+Dkj(k-1) )

Prim’s Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph. Prim’s algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized.

An simple algorithm to be remembered for Prim’s Algorithm:

Output:

Kruskal’s Algorithm is used to find the minimum spanning tree for a connected weighted graph. The main target of the algorithm is to find the subset of edges by using which we can traverse every vertex of the graph. It follows the greedy approach that finds an optimum solution at every stage instead of focusing on a global optimum.

An simple algorithm to be remembered for Prim’s Algorithm:

If we can reach every vertex of a component from every other vertex in that component then it is called a Strongly Connected Component (SCC). Single node is always a SCC. The Brute force method will be Floyd Warshall Algorithm. But for better time complexity, we can use Kosaraju’s Algorithm.

An simple algorithm to be remembered for Prim’s Algorithm:

Output:

These are the most important algorithms one should know as a programmer in graph data structure. I will be uploading detailed explanation of each algorithm in upcoming posts. Stay Tuned!

Did you find the Algorithms listed in this article helpful? If you enjoyed this article, share it with your friends and colleagues! Leave your comments below!

Here’s my another article: Learn Data Structures and Algorithms

Add a comment

Related posts:

Spiritual Transformation Comes With These 6 Disturbing Symptoms

Spiritual transformation is a larger or lesser change in a person’s attitude towards perspective and spirituality. From a simple event to a more complex occurrence of events over time, many things…

Tecsun PL880 Shortwave Radio Receiver

This is a short review of the Tecsun PL880 shortwave radio, one of the best shortwave receivers for SWL available in 2020. Please check this Medium publication: Shortwave Radio Listening, the…

A Stock Short Gone Wrong?

Chinese billionaires accused of corruption who flee China to live in exile in the United States are far from new, but none have been so vocal and at the center of US-China tensions as Gou Wengui…