Graph Algorithms the Fun Way by Jeremy Kubica
Author:Jeremy Kubica
Language: eng
Format: epub, pdf, mobi, azw
Publisher: No Starch Press
The bridge-finding algorithm checks each subtree in the depth-first search tree by recording the lowest-order indices that neighbor any node in the subtree. The only adjacent edge we exclude is the link between the subtree root u and its parent, as this is the edge we are testing.
The Code
We can implement the bridge-finding algorithm using a single pass of depth-first search. To simplify the code, weâll use the helper data structure DFSTreeStats to track information about the order in which the depth-first search reaches various nodes, including:
parent (list)âMaps each nodeâs index to that of its parent in the depth-first search tree
next_order_index (int)âStores the next order index to assign
order (list)âMaps each nodeâs index to its order index
lowest (list)âMaps each node to the lowest order index of any nodes in its depth-first search subtree or their immediate neighbors (excluding the nodeâs parent)
The data structure DFSTreeStats provides a wrapper for this information and saves us from having to pass many parameters to the search function. We can also use the object to perform basic assignments and updates. We define DFSTreeStats in the following code:
class DFSTreeStats: def __init__(self, num_nodes: int): â¶ self.parent: list = [-1] * num_nodes self.next_order_index: int = 0 self.order: list = [-1] * num_nodes self.lowest: list = [-1] * num_nodes def set_order_index(self, node_index: int): self.order[node_index] = self.next_order_index self.next_order_index += 1 â· self.lowest[node_index] = self.order[node_index]
The constructor sets all the information to its initial values â¶. The code initializes all entries of the lists parent, order, and lowest to -1 in order to indicate that these values are unset for each node. It sets next_order_index to 0 in preparation for the first node.
The helper method set_order_index() records the current nodeâs order index and increments the next one to assign. It also initializes the lowest order index seen for this node, which is initially the order index of the node itself â·.
We use a depth-first search adapted from those in Chapter 4 to fill in the entries of DFSTreeStats and find the bridges:
def bridge_finding_dfs(g: Graph, index: int, stats: DFSTreeStats, results: list): ⶠstats.set_order_index(index) for edge in g.nodes[index].get_sorted_edge_list(): neighbor: int = edge.to_node ⷠif stats.order[neighbor] == -1: stats.parent[neighbor] = index bridge_finding_dfs(g, neighbor, stats, results) ⸠stats.lowest[index] = min(stats.lowest[index], stats.lowest[neighbor]) ⹠if stats.lowest[neighbor] >= stats.order[neighbor]: results.append(edge) elif neighbor != stats.parent[index]: ⺠stats.lowest[index] = min(stats.lowest[index], stats.order[neighbor]) def find_bridges(g: Graph) -> list: results: list = [] stats: DFSTreeStats = DFSTreeStats(g.num_nodes) for index in range(g.num_nodes): if stats.order[index] == -1: bridge_finding_dfs(g, index, stats, results) return results
The recursive helper function bridge_finding_dfs() starts by setting the current nodeâs order index and initial value for the lowest order index reachable in the subtree using the set_order_index() helper method â¶.
The code then uses a for loop to check each of the nodeâs neighbors. For consistency of ordering with other examples, we use the function get_sorted_edge_list() to traverse the neighbors in order of their index, though traversing them in sorted order is not necessary for the correctness of the algorithm. If a neighbor has not been visited (its order value is unset) â·, the code sets its parent and recursively explores it.
Download
Graph Algorithms the Fun Way by Jeremy Kubica.pdf
Graph Algorithms the Fun Way by Jeremy Kubica.mobi
Graph Algorithms the Fun Way by Jeremy Kubica.azw
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Computer Vision & Pattern Recognition | Expert Systems |
Intelligence & Semantics | Machine Theory |
Natural Language Processing | Neural Networks |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8264)
Test-Driven Development with Java by Alan Mellor(6406)
Data Augmentation with Python by Duc Haba(6307)
Principles of Data Fabric by Sonia Mezzetta(6083)
Learn Blender Simulations the Right Way by Stephen Pearson(5947)
Hadoop in Practice by Alex Holmes(5946)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(5830)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5791)
RPA Solution Architect's Handbook by Sachin Sahgal(5225)
Big Data Analysis with Python by Ivan Marin(5190)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5111)
The Infinite Retina by Robert Scoble Irena Cronin(4914)
Pretrain Vision and Large Language Models in Python by Emily Webber(4167)
Functional Programming in JavaScript by Mantyla Dan(4025)
Infrastructure as Code for Beginners by Russ McKendrick(3924)
The Age of Surveillance Capitalism by Shoshana Zuboff(3920)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3628)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3440)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3415)
