1. Home
Data Structure

Data Structure Tutorial: Everything You Need to Know

Learn all about data structures with our comprehensive tutorial. Master the fundamentals and advance your skills in organizing and managing data efficiently.

  • 60
  • 14
right-top-arrow

Tutorial Playlist

58 Lessons
57

The Power of Strongly Connected Components in Graph Theory

Updated on 26/08/2024444 Views

Introduction

In the fascinating world of graph theory, the concept of strongly connected components is important to understand the structure and functionality of directed graphs. 

In this blog, the focus is on the essence of strongly connected components, differences from other components, applications in competitive programming, and related concepts like strongly connected theorem, weakly connected components, and strongly connected core.

What are Strongly Connected Components?

Strongly connected components are subgraphs in a directed graph where every vertex is reachable from every other vertex within the same subgraph.

Example: Consider the directed graph with vertices A, B, C, and D, and edges A→B, B→C, C→A, and C→D. The subgraph containing A, B, and C is an SCC because you can reach any vertex from any other vertex within this subgraph. However, D does not belong to this SCC because it is not reachable from A or B, nor can you reach A or B from D.

The Difference Between Connected and Strongly Connected Components

The distinction between connected components and strongly connected components lies in the directionality of edges:

  • Connected Components (Undirected Graphs): In an undirected graph, a connected component is a subgraph where any two vertices are connected by a path, regardless of the direction of traversal.

Example: In an undirected graph with vertices 1, 2, 3, and 4, and edges 1-2, 2-3, and 3-4, the entire graph is one connected component because you can traverse between any two vertices ignoring direction.

  • Strongly Connected Components (Directed Graphs): In a directed graph, a strongly connected component requires mutual reachability; you must be able to traverse from any vertex to every other vertex and back, adhering to the direction of edges.

Example: In a directed graph with vertices X, Y, Z, and edges X→Y, Y→Z, and Z→X, this subgraph is a strongly connected component. However, if there was an additional vertex W with an edge Z→W, W would not be part of the SCC because you cannot return to Z from W.

Strongly Connected Components in Competitive Programming

In competitive programming, SCCs are often used to solve problems involving directed graphs. They are particularly useful in scenarios such as:

  • Cycle Detection: Identifying cycles within a graph, as every cycle is an SCC.Example: If a graph contains vertices 1, 2, 3, and edges 1→2, 2→3, 3→1, then the subgraph {1, 2, 3} forms a cycle and is an SCC.
  • Graph Optimization: Simplifying complex graphs into more manageable subgraphs.Example: In a complex graph with multiple interconnected SCCs, identifying these components helps in reducing the problem size by treating each SCC as a single node.
  • Dependency Analysis: Analyzing dependencies between modules or components in software engineering.

Example: In a software project, if modules A, B, and C depend on each other circularly, identifying them as an SCC helps in understanding their mutual dependencies and organizing the project structure efficiently.

Tarjan's strongly connected components algorithm and Kosaraju's are commonly employed to find SCCs efficiently, enabling programmers to tackle problems involving large and complex graphs.

The Strongly Connected Theorem

The strongly connected theorem states that any directed graph can be disintegrated into its strongly connected components, which form a unique partition of the graph. This theorem is fundamental because it allows us to break down a complex directed graph into simpler, manageable pieces, each of which is an SCC.

Strong and Weak Connected Components

Comprehending the difference between weak and strongly connected components is essential:

  • Strong Connected Components: These are subgraphs where every vertex is mutually reachable via directed edges.Example: In a directed graph with vertices {1, 2, 3, 4} and edges {1→2, 2→3, 3→1}, the subgraph {1, 2, 3} is a strongly connected component.
  • Weak Connected Components: These are subgraphs where vertices are connected if we ignore the direction of edges. In other words, if replacing all directed edges with undirected edges results in a connected subgraph, it is a weakly connected component.The Difference Between Strong and Weak Connected Components

The primary difference lies in the consideration of edge direction:

  • Strongly Connected Components: Require mutual reachability considering the direction of edges.
  • Weakly Connected Components: Only require connectivity, ignoring the direction of edges. Applications of Strongly Connected Components
  • Applications for vehicle routing 
  • Maps 
  • Model-checking in official verification

Strongly Connected Graph Example

Take a look at the graph below.

The graph above's highly related elements are:

As you can see, each vertex in the first strongly connected component has a directed path that allows it to reach every other vertex.

The Kosaraju Algorithm can be utilized to locate these elements.

The Kosaraju Algorithm

Double implementation of the depth-first search method is the foundation of Kosaraju's algorithm.

Kosaraju's Complexity of Algorithm

Kosaraju's algorithm executes in O(V+E), or linear time.

There are three phases in all.

Search the entire graph for depth-first.

Starting with vertex-0, we will visit each of its offspring vertices and mark those that have been visited as completed. A vertex should be pushed to the stack if it leads to a vertex that has previously been visited.

As an illustration, move from vertex-0 to vertex-1, vertex-2, and finally vertex-3. Put vertex-3, the source vertex, into the stack since it leads to vertex-0, which has already been visited. 

Visit the child vertices of the previous vertex (vertex-2), which are vertices-4,-5,-6, and-7, in that order. Vertex-7 has nowhere to go, so push it into the stack.

Visit the child vertices of the previous vertex (vertex-6). However, since every one of its descendant vertices has been visited, add it to the stack.

In the same way, a final stack is made.

Turn the original graph around.

Utilize the inverted graph to conduct a depth-first search.

Start at the stack's uppermost vertex. Go over each of its offspring's vertices. One strongly connected component is produced after the already visited vertex is reached.

Take vertex-0 out of the stack, for instance. Navigate through each of the child vertices of vertex-0 (vertex-0, vertex-1, vertex-2, and vertex-3 in that order) and mark them as visited. Since vertex-3's child has already been visited, the visited vertices come together to form a single, tightly connected component.

If you have already visited the top vertex, go to the stack and pop it. If not, select the top vertex in the stack and follow the previously described steps to navigate through its child vertices.

Therefore, the elements that are highly related are:

Here’s a Python Example:

from collections import defaultdict

class Graph:

def __init__(self, vertex):

        self.V = vertex

        self.graph = defaultdict(list)

# Add edge into the graph

    def add_edge(self, s, d):

        self.graph[s].append(d)

# dfs

    def dfs(self, d, visited_vertex):

        visited_vertex[d] = True

        print(d, end='')

        for i in self.graph[d]:

            if not visited_vertex[i]:

                self.dfs(i, visited_vertex)

def fill_order(self, d, visited_vertex, stack):

        visited_vertex[d] = True

        for i in self.graph[d]:

            if not visited_vertex[i]:

                self.fill_order(i, visited_vertex, stack)

        stack = stack.append(d)

# transpose the matrix

    def transpose(self):

        g = Graph(self.V)

for i in self.graph:

            for j in self.graph[i]:

                g.add_edge(j, i)

        return g

# Print stongly connected components

    def print_scc(self):

        stack = []

        visited_vertex = [False] * (self.V)

for i in range(self.V):

            if not visited_vertex[i]:

                self.fill_order(i, visited_vertex, stack)

gr = self.transpose()

visited_vertex = [False] * (self.V)

while stack:

            i = stack.pop()

            if not visited_vertex[i]:

                gr.dfs(i, visited_vertex)

                print("")

g = Graph(8)

g.add_edge(0, 1)

g.add_edge(1, 2)

g.add_edge(2, 3)

g.add_edge(2, 4)

g.add_edge(3, 0)

g.add_edge(4, 5)

g.add_edge(5, 6)

g.add_edge(6, 4)

g.add_edge(6, 7)

print("Strongly Connected Components:")

g.print_scc()

Conclusion

Strongly connected components are the cornerstone concept in graph theory. They provide deep insights into the structure and dynamics of directed graphs.

From their theoretical foundation to practical applications in competitive programming and software engineering, they are indispensable tools used to analyze and optimize complex networks. By understanding the differences between strong and weak connected components and their related concepts, one can unlock new potentials in solving graph-related problems efficiently and effectively.

Frequently Asked Questions:

  1. What are strong connection components?

Subgraphs in a directed graph where each vertex is reachable from every other vertex form strongly connected components (SCCs).

  1. What is the difference between connected vs. strongly connected components?

In an undirected graph, a connected component is a subgraph where any two vertices are connected by a path. In a directed graph, a strongly connected component is a subgraph where every vertex is reachable from every other vertex within that subgraph.

  1. What are strongly connected components in competitive programming?

In competitive programming, strongly connected components are used to solve problems involving directed graphs, such as finding cycles, optimizing routes, and analyzing dependencies.

  1. What is the strongly connected theorem?

The theorem of strong connectivity states that a directed graph can be broken down into its strongly connected components, which form the graph's unique partition.

  1. What are strong and weak connected components?

Strong connected components refer to subgraphs where every vertex is mutually reachable via directed edges. Weak connected components refer to subgraphs where vertices are connected if the direction of edges is ignored.

  1. What is the difference between strong and weak connected components?

Strong connected components consider the direction of edges, requiring mutual reachability. Weak connected components ignore edge direction, requiring only connectivity without considering direction.

  1. Is strongly connected component a cycle?

A strongly connected component can be a cycle if each vertex is reachable from every other vertex in a circular manner, but SCCs can also be more complex structures than simple cycles.

  1. What is the strongly connected core?

The strongly connected core is the largest strongly connected subgraph within a directed graph, containing the maximum number of mutually reachable vertices.

  1. What are weakly connected components?

Weakly connected components are subgraphs of a directed graph where vertices are connected if edge direction is ignored, meaning there is an undirected path between any two vertices within the subgraph.

Rohan Vats

Rohan Vats

Software Engineering Manager @ upGrad. Assionate about building large scale web apps with delightful experiences. In pursuit of transforming engi…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...