Skip to content

Commit

Permalink
community[patch]: Added more functions in NetworkxEntityGraph class (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
raunakshrivastava7 authored Feb 22, 2024
1 parent 42f158c commit 1ec8199
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/docs/use_cases/graph/graph_networkx_qa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions libs/community/langchain_community/graphs/networkx_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,34 @@ def clear_edges(self) -> None:
"""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()
Expand Down

0 comments on commit 1ec8199

Please sign in to comment.