1- // articulationpoints.go
2- // description: Provides a function to identify articulation points in a graph.
3- // time complexity: O(|V| + |E|) where |V| is the number of vertices and |E| is the number of edges in the graph
4- // space complexity: O(|V|) where |V| is the number of vertices in the graph
5-
1+ // Package graph provides algorithms to analyze graph structures.
62package graph
73
84import "github.com/TheAlgorithms/Go/math/min"
95
6+ // apHelper stores auxiliary data used to identify articulation points in a graph.
107type apHelper struct {
11- is_ap []bool
12- visited []bool
13- child_cnt []int
14- discovery_time []int
15- earliest_discovery []int
8+ isAP []bool
9+ visited []bool
10+ childCount []int
11+ discoveryTime []int
12+ earliestDiscovery []int
1613}
1714
18- // ArticulationPoint is a function to identify articulation points in a graph.
19- // The function takes the graph as an argument and returns a boolean slice
20- // which indicates whether a vertex is an articulation point or not.
15+ // ArticulationPoint identifies articulation points in a graph. It returns a boolean slice
16+ // where each element indicates whether a vertex is an articulation point.
2117// Worst Case Time Complexity: O(|V| + |E|)
2218// Auxiliary Space: O(|V|)
23- // reference : https://en.wikipedia.org/wiki/Biconnected_component and https://cptalks.quora.com/Cut-Vertex-Articulation-point
19+ // Reference : https://en.wikipedia.org/wiki/Biconnected_component and https://cptalks.quora.com/Cut-Vertex-Articulation-point
2420func ArticulationPoint (graph * Graph ) []bool {
25- // time variable to keep track of the time of discovery_time of a vertex
21+ // Time variable to keep track of the discovery time of a vertex
2622 time := 0
2723
28- //initialize all the variables
24+ // Initialize apHelper instance with the required data structures
2925 apHelperInstance := & apHelper {
30- is_ap : make ([]bool , graph .vertices ),
31- visited : make ([]bool , graph .vertices ),
32- child_cnt : make ([]int , graph .vertices ),
33- // integer slice to store the discovery time of a vertex as we traverse
34- // the graph in a depth first manner
35- discovery_time : make ([]int , graph .vertices ),
36- // integer slice to store the earliest discovered vertex reachable from a vertex
37- earliest_discovery : make ([]int , graph .vertices ),
26+ isAP : make ([]bool , graph .vertices ),
27+ visited : make ([]bool , graph .vertices ),
28+ childCount : make ([]int , graph .vertices ),
29+ discoveryTime : make ([]int , graph .vertices ),
30+ earliestDiscovery : make ([]int , graph .vertices ),
3831 }
39- articulationPointHelper (
40- apHelperInstance ,
41- 0 ,
42- - 1 ,
43- & time ,
44- graph ,
45- )
4632
47- if apHelperInstance .child_cnt [0 ] == 1 {
48- // if the root has only one child, it is not an articulation point
49- apHelperInstance .is_ap [0 ] = false
33+ // Start traversal from the root (0)
34+ articulationPointHelper (apHelperInstance , 0 , - 1 , & time , graph )
35+
36+ // Check if the root has only one child, making it non-articulate
37+ if apHelperInstance .childCount [0 ] == 1 {
38+ apHelperInstance .isAP [0 ] = false
5039 }
5140
52- return apHelperInstance .is_ap
41+ return apHelperInstance .isAP
5342}
5443
55- // articulationPointHelper is a recursive function to traverse the graph
56- // and mark articulation points. Based on the depth first search transversal
57- // of the graph, however modified to keep track and update the
58- // `child_cnt`, `discovery_time` and `earliest_discovery` slices defined above
44+ // articulationPointHelper recursively traverses the graph using DFS and marks articulation points.
45+ // It updates `childCount`, `discoveryTime`, and `earliestDiscovery` slices for the given vertex.
5946func articulationPointHelper (
6047 apHelperInstance * apHelper ,
6148 vertex int ,
@@ -65,41 +52,38 @@ func articulationPointHelper(
6552) {
6653 apHelperInstance .visited [vertex ] = true
6754
68- // Mark the time of discovery of a vertex
69- // set the earliest discovery time to the discovered time
70- // increment the time
71- apHelperInstance .discovery_time [vertex ] = * time
72- apHelperInstance .earliest_discovery [vertex ] = apHelperInstance .discovery_time [vertex ]
55+ // Set discovery and earliest discovery times for the vertex
56+ apHelperInstance .discoveryTime [vertex ] = * time
57+ apHelperInstance .earliestDiscovery [vertex ] = * time
7358 * time ++
7459
75- for next_vertex := range graph .edges [vertex ] {
76- if next_vertex == parent {
60+ for nextVertex := range graph .edges [vertex ] {
61+ if nextVertex == parent {
7762 continue
7863 }
7964
80- if apHelperInstance .visited [next_vertex ] {
81- apHelperInstance .earliest_discovery [vertex ] = min .Int (
82- apHelperInstance .earliest_discovery [vertex ],
83- apHelperInstance .discovery_time [next_vertex ],
65+ if apHelperInstance .visited [nextVertex ] {
66+ // Update the earliest discovery time to the smallest reachable discovery time
67+ apHelperInstance .earliestDiscovery [vertex ] = min .Int (
68+ apHelperInstance .earliestDiscovery [vertex ],
69+ apHelperInstance .discoveryTime [nextVertex ],
8470 )
8571 continue
8672 }
8773
88- apHelperInstance .child_cnt [vertex ]++
89- articulationPointHelper (
90- apHelperInstance ,
91- next_vertex ,
92- vertex ,
93- time ,
94- graph ,
95- )
96- apHelperInstance .earliest_discovery [vertex ] = min .Int (
97- apHelperInstance .earliest_discovery [vertex ],
98- apHelperInstance .earliest_discovery [next_vertex ],
74+ // Increment child count and perform recursive traversal for DFS
75+ apHelperInstance .childCount [vertex ]++
76+ articulationPointHelper (apHelperInstance , nextVertex , vertex , time , graph )
77+
78+ // Update the earliest discovery time post DFS
79+ apHelperInstance .earliestDiscovery [vertex ] = min .Int (
80+ apHelperInstance .earliestDiscovery [vertex ],
81+ apHelperInstance .earliestDiscovery [nextVertex ],
9982 )
100- if apHelperInstance .earliest_discovery [next_vertex ] >= apHelperInstance .discovery_time [vertex ] {
101- apHelperInstance .is_ap [vertex ] = true
102- }
10383
84+ // Mark vertex as articulation point if condition meets
85+ if apHelperInstance .earliestDiscovery [nextVertex ] >= apHelperInstance .discoveryTime [vertex ] {
86+ apHelperInstance .isAP [vertex ] = true
87+ }
10488 }
10589}
0 commit comments