Skip to content

Commit b4da8e1

Browse files
committed
fix remove_e bugs
1 parent dab2ea1 commit b4da8e1

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,65 +159,79 @@ from hyperdb import HypergraphDB
159159
hg = HypergraphDB()
160160

161161
# Add vertices
162-
hg.add_v(1, {"name": "Alice"})
163-
hg.add_v(2, {"name": "Bob"})
164-
hg.add_v(3, {"name": "Charlie"})
162+
hg.add_v(1, {"name": "Alice", "age": 30, "city": "New York"})
163+
hg.add_v(2, {"name": "Bob", "age": 24, "city": "Los Angeles"})
164+
hg.add_v(3, {"name": "Charlie", "age": 28, "city": "Chicago"})
165+
hg.add_v(4, {"name": "David", "age": 35, "city": "Miami"})
166+
hg.add_v(5, {"name": "Eve", "age": 22, "city": "Seattle"})
167+
hg.add_v(6, {"name": "Frank", "age": 29, "city": "Houston"})
168+
hg.add_v(7, {"name": "Grace", "age": 31, "city": "Phoenix"})
169+
hg.add_v(8, {"name": "Heidi", "age": 27, "city": "San Francisco"})
170+
hg.add_v(9, {"name": "Ivan", "age": 23, "city": "Denver"})
171+
hg.add_v(10, {"name": "Judy", "age": 26, "city": "Boston"})
165172

166173
# Add hyperedges
167-
hg.add_e((1, 2), {"relation": "knows"})
168-
hg.add_e((1, 3, 2), {"relation": "collaborates"})
174+
hg.add_e((1, 2, 3), {"type": "friendship", "duration": "5 years"})
175+
hg.add_e((1, 4), {"type": "mentorship", "topic": "career advice"})
176+
hg.add_e((2, 5, 6), {"type": "collaboration", "project": "AI Research"})
177+
hg.add_e((4, 5, 7, 9), {"type": "team", "goal": "community service"})
178+
hg.add_e((3, 8), {"type": "partnership", "status": "ongoing"})
179+
hg.add_e((9, 10), {"type": "neighbors", "relationship": "friendly"})
180+
hg.add_e((1, 2, 3, 7), {"type": "collaboration", "field": "music"})
181+
hg.add_e((2, 6, 9), {"type": "classmates", "course": "Data Science"})
169182
```
170183

171184
#### **2. Query Vertices and Hyperedges**
172185

173186
```python
174187
# Get all vertices and hyperedges
175-
print(hg.all_v) # Output: {1, 2, 3}
176-
print(hg.all_e) # Output: {(1, 2), (1, 2, 3)}
188+
print(hg.all_v) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
189+
print(hg.all_e) # Output: {(4, 5, 7, 9), (9, 10), (3, 8), (1, 2, 3), (2, 6, 9), (1, 4), (1, 2, 3, 7), (2, 5, 6)}
177190

178191
# Query a specific vertex
179-
print(hg.v(1)) # Output: {'name': 'Alice'}
192+
print(hg.v(1)) # Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}
180193

181194
# Query a specific hyperedge
182-
print(hg.e((1, 2))) # Output: {'relation': 'knows'}
195+
print(hg.e((1, 2, 3))) # Output: {'type': 'friendship', 'duration': '5 years'}
183196
```
184197

185198
#### **3. Update and Remove Vertices/Hyperedges**
186199

187200
```python
188201
# Update a vertex
189-
hg.update_v(1, {"name": "Alice Smith"})
190-
print(hg.v(1)) # Output: {'name': 'Alice Smith'}
202+
hg.update_v(1, {"name": "Smith"})
203+
print(hg.v(1)) # Output: {'name': 'Smith', 'age': 30, 'city': 'New York'}
191204

192205
# Remove a vertex
193-
hg.remove_v(2)
194-
print(hg.all_v) # Output: {1, 3}
195-
print(hg.all_e) # Output: {(1, 3)}
206+
hg.remove_v(3)
207+
print(hg.all_v) # Output: {1, 2, 4, 5, 6, 7, 8, 9, 10}
208+
print(hg.all_e) # Output: {(4, 5, 7, 9), (9, 10), (1, 2, 7), (1, 2), (2, 6, 9), (1, 4), (2, 5, 6)}
196209

197210
# Remove a hyperedge
198-
hg.remove_e((1, 3))
199-
print(hg.all_e) # Output: set()
211+
hg.remove_e((1, 4))
212+
print(hg.all_e) # Output: {(4, 5, 7, 9), (9, 10), (1, 2, 7), (1, 2), (2, 6, 9), (2, 5, 6)}
200213
```
201214

202215
#### **4. Calculate Degrees**
203216

204217
```python
205218
# Get the degree of a vertex
206-
print(hg.degree_v(1)) # Output: 1
219+
print(hg.degree_v(1)) # Example Output: 2
207220

208221
# Get the degree of a hyperedge
209-
print(hg.degree_e((1, 2))) # Output: 2
222+
print(hg.degree_e((2, 5, 6))) # Example Output: 3
210223
```
211224

212225
#### **5. Neighbor Queries**
213226

214227
```python
215228
# Get neighbors of a vertex
216-
hg.add_e((1, 3, 4), {"relation": "team"})
217-
print(hg.nbr_v(1)) # Output: {3, 4}
229+
print(hg.nbr_v(1)) # Example Output: {2, 7}
230+
hg.add_e((1, 4, 6), {"relation": "team"})
231+
print(hg.nbr_v(1)) # Example Output: {2, 4, 6, 7}
218232

219233
# Get incident hyperedges of a vertex
220-
print(hg.nbr_e_of_v(1)) # Output: {(1, 3, 4)}
234+
print(hg.nbr_e_of_v(1)) # Example Output: {(1, 2, 7), (1, 2), (1, 4, 6)}
221235
```
222236

223237
#### **6. Persistence (Save and Load)**
@@ -228,8 +242,8 @@ hg.save("my_hypergraph.hgdb")
228242

229243
# Load the hypergraph from a file
230244
hg2 = HypergraphDB(storage_file="my_hypergraph.hgdb")
231-
print(hg2.all_v) # Output: {1, 3, 4}
232-
print(hg2.all_e) # Output: {(1, 3, 4)}
245+
print(hg2.all_v) # Output: {1, 2, 4, 5, 6, 7, 8, 9, 10}
246+
print(hg2.all_e) # Output: {(4, 5, 7, 9), (9, 10), (1, 2, 7), (1, 2), (2, 6, 9), (1, 4, 6), (2, 5, 6)}
233247
```
234248

235249

hyperdb/hypergraph.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,22 @@ def remove_v(self, v_id: Any):
200200
v_id in self._v_data
201201
), f"The vertex {v_id} does not exist in the hypergraph."
202202
del self._v_data[v_id]
203+
old_e_tuples, new_e_tuples = [], []
203204
for e_tuple in self._v_inci[v_id]:
204205
new_e_tuple = self.encode_e(set(e_tuple) - {v_id})
205206
if len(new_e_tuple) >= 2:
207+
# todo: maybe new e tuple existing in hg, need to merge to hyperedge information
206208
self._e_data[new_e_tuple] = deepcopy(self._e_data[e_tuple])
207209
del self._e_data[e_tuple]
210+
old_e_tuples.append(e_tuple)
211+
new_e_tuples.append(new_e_tuple)
208212
del self._v_inci[v_id]
213+
for old_e_tuple, new_e_tuple in zip(old_e_tuples, new_e_tuples):
214+
for _v_id in old_e_tuple:
215+
if _v_id != v_id:
216+
self._v_inci[_v_id].remove(old_e_tuple)
217+
if len(new_e_tuple) >= 2:
218+
self._v_inci[_v_id].add(new_e_tuple)
209219
self._clear_cache()
210220

211221
def remove_e(self, e_tuple: Union[List, Set, Tuple]):

0 commit comments

Comments
 (0)