community[patch]: Added more functions in NetworkxEntityGraph class (#17624)

- **Description:** 
1. Added add_node(), remove_node(), has_node(), remove_edge(),
has_edge() and get_neighbors() functions in
       NetworkxEntityGraph class.

2. Added the above functions in graph_networkx_qa.ipynb documentation.
This commit is contained in:
Raunak
2024-02-22 06:32:56 +05:30
committed by GitHub
parent 42f158c128
commit 1ec8199c8e
2 changed files with 88 additions and 0 deletions

View File

@@ -139,6 +139,34 @@ class NetworkxEntityGraph:
"""Clear the graph edges."""
self._graph.clear_edges()
def add_node(self, node: str) -> None:
"""Add node in the graph."""
self._graph.add_node(node)
def remove_node(self, node: str) -> None:
"""Remove node from the graph."""
if self._graph.has_node(node):
self._graph.remove_node(node)
def has_node(self, node: str) -> bool:
"""Return if graph has the given node."""
return self._graph.has_node(node)
def remove_edge(self, source_node: str, destination_node: str) -> None:
"""Remove edge from the graph."""
self._graph.remove_edge(source_node, destination_node)
def has_edge(self, source_node: str, destination_node: str) -> bool:
"""Return if graph has an edge between the given nodes."""
if self._graph.has_node(source_node) and self._graph.has_node(destination_node):
return self._graph.has_edge(source_node, destination_node)
else:
return False
def get_neighbors(self, node: str) -> List[str]:
"""Return the neighbor nodes of the given node."""
return self._graph.neighbors(node)
def get_number_of_nodes(self) -> int:
"""Get number of nodes in the graph."""
return self._graph.number_of_nodes()