For working professionals
For fresh graduates
More
Data Structure Tutorial: Every…
1. Data Structure
2. Types of Linked Lists
3. Array vs Linked Lists in Data Structure
4. Stack vs. Queue Explained
5. Singly Linked List
6. Circular doubly linked list
7. Circular Linked List
8. Stack Implementation Using Array
9. Circular Queue in Data Structure
10. Dequeue in Data Structures
11. Bubble Sort Algorithm
12. Insertion Sort Algorithm
13. Shell Sort Algorithm
14. Radix Sort
15. Counting Sort Algorithm
16. Trees in Data Structure
17. Tree Traversal in Data Structure
18. Inorder Traversal
19. Optimal Binary Search Trees
20. AVL Tree
21. Red-Black Tree
22. B+ Tree in Data Structure
23. Expression Tree
24. Adjacency Matrix
25. Spanning Tree in Data Structure
26. Kruskal Algorithm
27. Prim's Algorithm in Data Structure
28. Bellman Ford Algorithm
29. Ford-Fulkerson Algorithm
30. Trie Data Structure
31. Floyd Warshall Algorithm
32. Rabin Karp Algorithm
33. What Is Dynamic Programming?
34. Longest Common Subsequence
35. Fractional Knapsack Problem
36. Greedy Algorithm
37. Longest Increasing Subsequence
38. Matrix Chain Multiplication
39. Subset Sum Problem
40. Backtracking Algorithm
41. Huffman Coding Algorithm
42. Tower of Hanoi
43. Stack vs Heap
44. Asymptotic Analysis
45. Binomial Distribution
46. Coin Change Problem
47. Fibonacci Heap
48. Skip List in Data Structure
49. Sparse Matrix
50. Splay Tree
51. Queue in Data Structure
52. Stack in Data Structure
53. Time and Space Complexity
54. Linked List in Data Structure
55. Stack And Queue: Roles & Functions
56. Doubly Linked List
57. Strongly Connected Components
Now Reading
58. Bucket Sort Algorithm
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.
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 distinction between connected components and strongly connected components lies in the directionality of edges:
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.
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.
In competitive programming, SCCs are often used to solve problems involving directed graphs. They are particularly useful in scenarios such as:
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 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.
Comprehending the difference between weak and strongly connected components is essential:
The primary difference lies in the consideration of edge direction:
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.
Double implementation of the depth-first search method is the foundation of Kosaraju's algorithm.
Kosaraju's algorithm executes in O(V+E), or linear time.
There are three phases in all.
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.
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()
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.
Subgraphs in a directed graph where each vertex is reachable from every other vertex form strongly connected components (SCCs).
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.
In competitive programming, strongly connected components are used to solve problems involving directed graphs, such as finding cycles, optimizing routes, and analyzing dependencies.
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.
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.
Strong connected components consider the direction of edges, requiring mutual reachability. Weak connected components ignore edge direction, requiring only connectivity without considering direction.
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.
The strongly connected core is the largest strongly connected subgraph within a directed graph, containing the maximum number of mutually reachable vertices.
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.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.