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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 88 additions and 0 deletions

View File

@ -302,6 +302,66 @@
"loaded_graph.get_number_of_nodes()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7cc06554",
"metadata": {},
"outputs": [],
"source": [
"loaded_graph.add_node(\"NewNode\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f42deb48",
"metadata": {},
"outputs": [],
"source": [
"loaded_graph.has_node(\"NewNode\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e91bc6b9",
"metadata": {},
"outputs": [],
"source": [
"loaded_graph.remove_node(\"NewNode\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1d1e745",
"metadata": {},
"outputs": [],
"source": [
"loaded_graph.get_neighbors(\"Intel\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eecea586",
"metadata": {},
"outputs": [],
"source": [
"loaded_graph.has_edge(\"Intel\", \"Silicon Valley\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1fdc612",
"metadata": {},
"outputs": [],
"source": [
"loaded_graph.remove_edge(\"Intel\", \"Silicon Valley\")"
]
},
{
"cell_type": "code",
"execution_count": null,

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()