In this section, we’ll discuss a couple of simple examples. We’ll try to relate the examples with the definition given above.
1. One Connected Component
In this example, the given undirected graph has one connected component:
Let’s name this graph G1(V, E). Here V = {V1, V2, V3, V4, V5, V6} denotes the vertex set and E ={E1, E2, E3, E4, E5, E6, E7} denotes the edge set of G1. The graph G1 has one connected component, let’s name it C1, which contains all the vertices of G1. Now let’s check whether the set C1 holds to the definition or not.
According to the definition, the vertices in the set C1 should reach one another via a path. We’re choosing two random vertices V1 and V6:
The vertices V1 and V6 satisfied the definition, and we could do the same with other vertex pairs in C1 as well.
2. More Than One Connected Component
In this example, the undirected graph has three connected components:
Let’s name this graph as G2(V, E), where V = {V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12}, and E = {E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11}. The graph G2 has 3 connected components: C1 = {V1, V2, V3, V4, V5, V6}, C2 = {V7, V8, V9} and C3 = {V10, V11, V12}.
Now, let’s see whether connected components C1, C2, and C3 satisfy the definition or not. We’ll randomly pick a pair from each C1, C2, and C3 set.
From the set C1, let’s pick the vertices V4 and V6.
Now let’s pick the vertices V8 and V9 from the set C2.
Finally, let’s pick the vertices V11 and V12 from the set C3.
So from these simple demonstrations, it is clear that C1, C2, and C3 follow the connected component definition.
As we have already discussed the definition and demonstrated a couple of examples of the connected components, it’s a good time to list out some of the important properties that connected component always holds.
First of all, the connected component set is always non-empty.
Moreover, if there is more than one connected component for a given graph then the union of connected components will give the set of all vertices of the given graph.
For example G2:
C1 ∪ C2 ∪ C3 = {V1, V2, V3, V4, V5, V6} ∪ {V7, V8, V9} ∪ {V10, V11, V12} = {V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12} = V].
Finally, connected component sets are pairwise disjoint. That means if we take the intersection between two different connected component sets then the intersection will be equals to an empty set or a null set.
Let’s consider the connected components of graph G2 again. In G2, let’s check this property:
C1 ∩ C2 ∩ C3 = {V1, V2, V3, V4, V5, V6} ∩ {V7, V8, V9} ∩ {V10, V11, V12} ={∅}
Given an undirected graph, it’s important to find out the number of connected components to analyze the structure of the graph – it has many real-life applications. We can use either DFS or BFS for this task.
In this section, we’ll discuss a DFS-based algorithm that gives us the number of connected components for a given undirected graph:
Let’s run the algorithm on a sample graph:
The red vertex denotes that it is not visited. The green vertex denotes it is visited by the algorithm:
We can pick any vertex from the vertex list to start the algorithm. Let’s pick V1.
The algorithm checks whether it is visited or not. In this case, V1 is not visited. So it calls DFS(V, V1).
Within the DFS(), first, it labels the vertex V1 as visited and searches for the adjacent vertices of V1. All the adjacent vertices are also marked as visited. When DFS finishes visiting all the adjacent vertices of V1, the Component_Count becomes 1, and the status of vertices are updated:
Again, the algorithm picks any random vertex. Let’s pick V4 this time.
It checks whether V4 is already visited or not. As it is not visited so the algorithm calls DFS(V, V4). Again the algorithm marks the vertex V4 mark as visited, and DFS searches for its adjacent vertices and marks them as visited. Now the Component_Count becomes 2, and the status of the vertex list is updated again:
The algorithm continues and chooses V6, checks the status, and calls DFS(V, V6). The vertex V6 and its adjacent vertices are labeled as visited and the Component_Count increases to 3. The algorithm updates the vertex list status:
Finally, the algorithm chooses V8, calls DFS(V, V8), and makes V8 as visited. The vertex V8 doesn’t have any adjacent vertices so DFS returns and Component_Count increases to 4. Finally, the algorithm updates the status of the vertex list:
As the algorithm finished traversing all the vertices of the graph G3, it terminates and returns the value of Component_Count which equals the number of connected components in G3. In this case, the algorithms find four connected components in G3:
We used four different colours to illustrate the connected components in G3, namely: C1 = {V1, V2, V3}, C2 = {V4, V5}, C3 = {V6, V7}, C4 = {V8}.
65 videos|120 docs|94 tests
|
|
Explore Courses for Civil Engineering (CE) exam
|