# What Is Connected Graph in Data Structure With Example?

//

Scott Campbell

A connected graph is a fundamental concept in data structures that represents a set of vertices (also known as nodes) and edges. In a connected graph, there is a path between every pair of vertices. This means that starting from any vertex, you can reach any other vertex by following the edges.

Example: Let’s consider the following graph:

This graph consists of 6 vertices labeled as A, B, C, D, E, and F. The edges are represented by the lines connecting the vertices. Now let’s analyze whether this graph is connected or not.

To determine if a graph is connected, we can perform a traversal algorithm such as breadth-first search (BFS) or depth-first search (DFS). These algorithms help us explore all the vertices and determine if there is a path between every pair of vertices.

BFS starts at a given vertex and explores all its neighbors before moving on to the next level of neighbors. It uses a queue data structure to keep track of visited vertices.

Let’s start BFS from vertex A:

• Step 1: Visit vertex A and enqueue it.
• Step 2: Dequeue vertex A and visit its adjacent vertices B and C.
• Step 3: Enqueue vertices B and C.
• Step 4: Dequeue vertex B and visit its adjacent vertex D.
• Step 5: Enqueue vertex D.

The process continues until all the reachable vertices are visited. If BFS visits all the vertices in the graph, it means that the graph is connected. In our example, BFS visits all the vertices (A, B, C, D, E, F), so the graph is connected.

## Depth-First Search (DFS)

DFS explores as far as possible along each branch before backtracking. It uses a stack data structure to keep track of visited vertices.

Let’s start DFS from vertex A:

• Step 1: Visit vertex A and push it onto the stack.
• Step 2: Pop vertex A from the stack and visit its adjacent vertices B and C.
• Step 3: Push vertices B and C onto the stack.
• Step 4: Pop vertex C from the stack and visit its adjacent vertex E.
• Step 5: Push vertex E onto the stack. If DFS visits all the vertices in the graph, it means that the graph is connected. In our example, DFS visits all the vertices (A, B, C, D, E, F), so the graph is connected.

A connected graph is essential in various applications such as network analysis, social network analysis, and routing algorithms. Understanding connected graphs helps us analyze connectivity patterns and find efficient paths between different entities in a system.

In conclusion, a connected graph is a type of graph where there is a path between every pair of vertices. We can use traversal algorithms like BFS or DFS to determine if a graph is connected or not.

In our example with six vertices labeled A to F, both BFS and DFS visit all vertices indicating that it is a connected graph. Connected graphs play a vital role in various real-world scenarios where establishing connectivity between entities is crucial for efficient operations.