Skip to main content

Bellman-Ford Algorithm

Definition​

The Bellman-Ford Algorithm is a dynamic programming algorithm used to find the shortest path from a single source vertex to all other vertices in a weighted graph, even if the graph contains negative weight edges (as long as there are no negative weight cycles)

Practice​

function findArticulationPoints(graph):
# Initialize variables and data structures
articulationPoints = []
visited = {}
discoveryTime = {}
low = {}
parent = {}
time = 0

# Perform DFS traversal
for each vertex in graph.vertices():
if vertex not in visited:
dfs(vertex, visited, discoveryTime, low, parent, articulationPoints, time)

return articulationPoints

function dfs(vertex, visited, discoveryTime, low, parent, articulationPoints, time):
# Mark vertex as visited and set its discovery time
visited[vertex] = True
time += 1
discoveryTime[vertex] = time
low[vertex] = time
children = 0

# Iterate over adjacent vertices
for each neighbor in graph.neighbors(vertex):
if neighbor not in visited:
# Update parent of neighbor
parent[neighbor] = vertex
children += 1
dfs(neighbor, visited, discoveryTime, low, parent, articulationPoints, time)

# Update low value of vertex
low[vertex] = min(low[vertex], low[neighbor])

# Check if vertex is an articulation point
if parent[vertex] is None and children > 1:
articulationPoints.append(vertex)
elif parent[vertex] is not None and low[neighbor] >= discoveryTime[vertex]:
articulationPoints.append(vertex)
elif neighbor != parent[vertex]:
# Update low value of vertex if neighbor is not parent
low[vertex] = min(low[vertex], discoveryTime[neighbor])