Skip to content

Commit 017e889

Browse files
committed
Add test code
1 parent 4069208 commit 017e889

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

source_code/intersection_trees/intersection_tree.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,31 @@ def create_queries(size: int = 1_000, max_end: int = 1_000_000) -> Queries:
226226

227227

228228
def populate_db(db: Node | None, intervals: typing.Sequence[Interval]) -> Node:
229+
'''Populate an existing database with additional intervals or create a new one.
230+
231+
Parameters
232+
----------
233+
db: Node | None
234+
existing database to populate, if None a new database is created
235+
intervals: typing.Sequence[Interval]
236+
intervals to insert into the database
237+
238+
Returns
239+
-------
240+
Node
241+
root of the populated intersection tree
242+
243+
Raises
244+
------
245+
ValueError
246+
if intervals is empty
247+
'''
229248
if len(intervals) == 0:
230249
raise ValueError('At least 1 interval is required')
250+
start_idx = 0
231251
if db is None:
232-
db = Node(intervals.pop(0))
233-
for interval in intervals:
252+
db = Node(intervals[0])
253+
start_idx = 1
254+
for interval in intervals[start_idx:]:
234255
db.insert(interval)
235-
return db
256+
return db

source_code/intersection_trees/intersection_trees.ipynb

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@
459459
},
460460
{
461461
"cell_type": "code",
462-
"execution_count": 9,
462+
"execution_count": 2,
463463
"id": "efe82236-da69-4654-8f26-23f2a5da6419",
464464
"metadata": {},
465465
"outputs": [],
@@ -803,7 +803,7 @@
803803
},
804804
{
805805
"cell_type": "code",
806-
"execution_count": 15,
806+
"execution_count": 3,
807807
"id": "153b9a8d-c90a-43ad-bb9b-8bd03b062397",
808808
"metadata": {},
809809
"outputs": [],
@@ -999,6 +999,59 @@
999999
"source": [
10001000
"It is clear that the intersection tree implementation is more efficient than the naive approaches. The expected time complexity is $\\bigO(N \\log N)$ for $|Q| = |D| = N$. Likely, the actual time complexity will be worse since the vinary tree is unlikely to be balanced."
10011001
]
1002+
},
1003+
{
1004+
"cell_type": "markdown",
1005+
"id": "eff509fe-8612-4ccf-bd91-d6fb7535f551",
1006+
"metadata": {},
1007+
"source": [
1008+
"## Testing"
1009+
]
1010+
},
1011+
{
1012+
"cell_type": "markdown",
1013+
"id": "6d763a5d-3bc2-47d0-831d-585521e20b63",
1014+
"metadata": {},
1015+
"source": [
1016+
"Since the naive appreach is pretty straightforward, and the intersection tree is a bit more sophisticated, it might be wise to compare the results by way of test."
1017+
]
1018+
},
1019+
{
1020+
"cell_type": "code",
1021+
"execution_count": 9,
1022+
"id": "88c24d03-83ef-4f3e-9332-846cba39b52c",
1023+
"metadata": {},
1024+
"outputs": [],
1025+
"source": [
1026+
"import intersection_tree\n",
1027+
"import naive_intersectionic_queries\n",
1028+
"\n",
1029+
"random.seed(1234)\n",
1030+
"max_end = 1_000_000\n",
1031+
"nr_intervals = 1_000\n",
1032+
"nr_queries = 1_000\n",
1033+
"db_intervals = [\n",
1034+
" intersection_tree.generate_interval(max_end=max_end) for _ in range(nr_intervals)\n",
1035+
"]\n",
1036+
"queries = intersection_tree.create_queries(size=nr_queries, max_end=max_end)\n",
1037+
"\n",
1038+
"db = intersection_tree.populate_db(None, db_intervals)\n",
1039+
"db_results = intersection_tree.execute_queries(queries, db)\n",
1040+
"\n",
1041+
"naive_db = db_intervals\n",
1042+
"naive_db_result = naive_intersectionic_queries.execute_queries(queries, naive_db)\n",
1043+
"\n",
1044+
"assert len(db_results) == len(naive_db_result)\n",
1045+
"assert set(db_results) == set(naive_db_result)"
1046+
]
1047+
},
1048+
{
1049+
"cell_type": "markdown",
1050+
"id": "eb4b24f3-f503-4ecf-9c10-0c371b3119b2",
1051+
"metadata": {},
1052+
"source": [
1053+
"Both algorithms yield identical results."
1054+
]
10021055
}
10031056
],
10041057
"metadata": {

0 commit comments

Comments
 (0)