Skip to content

Commit b4d2c91

Browse files
committed
feat(algs): add articulation point finding alg (brute force)
1 parent d6fd306 commit b4d2c91

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

algorithms/articulation_points.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Articulation Points
3+
- vertices of graphs which, when removed, will increase the number of 'islands' in the graph.
4+
- i.e. vulnerable points of a network
5+
6+
Based on tutorial on HackerEarth: https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/tutorial/
7+
8+
Jessica Yung
9+
Jan 2018
10+
"""
11+
import numpy as np
12+
from graph import Graph
13+
14+
class find_articulation_points(Graph):
15+
16+
def __init__(self, adjacency_matrix, num_vertices):
17+
Graph.__init__(self, adjacency_matrix, num_vertices)
18+
self.saved_adj_mat = self.adj_mat
19+
20+
def brute_force(self):
21+
"""Alternative brute force approach: find islands using a DFS for each vertex."""
22+
self.articulation_points = []
23+
original_islands = self.find_num_islands()
24+
print("Original number of islands: ", original_islands)
25+
for vertex in range(self.v):
26+
self.remove_vertex(vertex)
27+
if self.find_num_islands() > original_islands:
28+
print(self.num_islands, "islands with vertex", vertex, "removed.")
29+
self.articulation_points.append(vertex)
30+
# Restore vertex
31+
self.v += 1
32+
self.adj_mat = self.saved_adj_mat
33+
return self.articulation_points
34+
35+
adjmat = np.zeros((6,6))
36+
edges = [[0,1],[0,5],[1,2],[1,3],[2,3],[2,4],[3,4]]
37+
test1 = find_articulation_points(adjmat, 6)
38+
test1.add_edges(edges)
39+
test1.brute_force()
40+
print(test1.articulation_points)
41+

0 commit comments

Comments
 (0)