Graphs are one of the most fundamental concepts in Computer Science. And they play the part perfectly in solving a tremendous number of problems. In this article, we’re going to bring into the light an algorithm question that at first glance one may not even think that graphs can be of any use to drive a solution for. But first, let’s have a look at our journey today. Please kindly note, we won’t go deep with the Graphs concepts in this article, rather, a shallow introduction over that and then to focus on its applications to tackle arrays algorithms questions.

Road Map

  1. A quick introduction to Graphs — Structure, Paths, and Cycles
  2. The problem to solve — Finding the minimum required swaps to sort an array
  3. Coding time
  4. Always unit test your algorithms

An Introduction to Graphs

  1. The structure of a graph

A graph is a data structure that in its most compact form is made up of a single vertex (also called a node). Here’s a simple representation of it:

Image for post

Figure 1 — A single node graph

We can obviously extend this graph by adding more properties to it. For example, another connected node to this where the direction between the nodes is not important, just like the below:

Image for post

Figure 2 — An undirected graph (direction does not matter)

Now, if we simply add a direction to the edge between the two nodes above, we would then turn it into a directed graph which also is important for the problem we are going to solve shortly. Here’s how it looks like:

Image for post

Figure 2 — A directed graph (direction matters)

It really sounds easy so far, yet, we need to cover the Paths and Cycles of a graph so we get our backpack ready to solve our algorithm question.

_2. _Paths

To understand the path theoretically, we need to first have a touch on the Walks within a graph. So let’s see what that is:

**2.1. Walk **A Walk is basically a sequence of edges where each edge, except the first one, starts with a node (vertex) where the previous edge ended_. _For example, the following graph demonstrates a walk:

Image for post

Figure 3 — A walk on a graph

Here, if we traverse through A > B > C that would give us a walk — same if we travel A > B > C > D, or even, A > B > C > D > C.

The crucial note to remember here is that a Walk could have repeated edges and that would not have any interference with the definition of a Walk.

**2.2. Path — **A path is simply a walk where you would never go through an edge more than once. Hence, all the edges and vertices are distinct. Going back to the previous example, A > B > C would give us a path but not

A > B > C > D > C.

**2.3. Cycle — **A cycle can be detected when the _first _and the last node on a path are the same. That would give distinct edges but not distinct nodes which would lead to a Cycle. The following figure shows us a cycle of

A > B > C > D > A.

Image for post

Figure 4 — Representing a cycle for A > B > C > D > A

Up to this point, we have covered all we needed to know so we can tackle the algorithm question we have. So without any further details let’s read on to find out more about the question.

#arrays #graph #programming #algorithms

Graphs everywhere -  Finding the minimum swaps required to sort an array
2.40 GEEK