Skip to content

Commit dfae587

Browse files
committed
some small updates
1 parent 8b245bb commit dfae587

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

implementations/Data Structure - Graphs.ipynb

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,55 @@
6565
" print(node.value, [child.value for child in node.children])"
6666
]
6767
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Example: [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)\n",
73+
"\n",
74+
"A graph is a tree with no cycles; for this problem, we need to find cycles from a list of edges. We can do this with a regular BFS, quitting if we visit the same node twice. "
75+
]
76+
},
6877
{
6978
"cell_type": "code",
70-
"execution_count": null,
79+
"execution_count": 18,
7180
"metadata": {},
7281
"outputs": [],
73-
"source": []
82+
"source": [
83+
"from collections import deque\n",
84+
"\n",
85+
"class Solution:\n",
86+
" def validTree(self, n: int, edges: List[List[int]]) -> bool:\n",
87+
" if not edges:\n",
88+
" return n < 2\n",
89+
" adj_list = {i:set() for i in range(0, n)}\n",
90+
" for n1,n2 in edges:\n",
91+
" adj_list[n1].add(n2)\n",
92+
" adj_list[n2].add(n1)\n",
93+
" \n",
94+
" visited = set()\n",
95+
" q = deque([(edges[0][0], None)])\n",
96+
" while q:\n",
97+
" curr, parent = q.popleft()\n",
98+
" visited.add(curr)\n",
99+
" for child in adj_list[curr]:\n",
100+
" if child == parent:\n",
101+
" continue\n",
102+
" if child in visited:\n",
103+
" return False\n",
104+
" q.append((child,curr))\n",
105+
" return len(visited) == n\n",
106+
"\n",
107+
"s = Solution()\n",
108+
"cases = [\n",
109+
" (5, [[0,1], [0,2], [0,3], [1,4]], True),\n",
110+
" (5, [[0,1], [1,2], [2,3], [1,3], [1,4]], False),\n",
111+
"]\n",
112+
"for n, edges, expected in cases:\n",
113+
" actual = s.validTree(n, edges)\n",
114+
" assert actual == expected, f\"{n, edges}\"\n",
115+
" "
116+
]
74117
}
75118
],
76119
"metadata": {

implementations/Technique - Topological Sorting.ipynb

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,6 @@
276276
" print(s.findOrder(n_courses, prereqs))"
277277
]
278278
},
279-
{
280-
"cell_type": "markdown",
281-
"metadata": {},
282-
"source": [
283-
"## Example: [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)"
284-
]
285-
},
286-
{
287-
"cell_type": "code",
288-
"execution_count": null,
289-
"metadata": {},
290-
"outputs": [],
291-
"source": []
292-
},
293279
{
294280
"cell_type": "markdown",
295281
"metadata": {},
@@ -324,7 +310,7 @@
324310
},
325311
{
326312
"cell_type": "code",
327-
"execution_count": 5,
313+
"execution_count": 6,
328314
"metadata": {},
329315
"outputs": [],
330316
"source": [
@@ -375,11 +361,6 @@
375361
" only return top ordering if we visit every \n",
376362
" node without hitting a cycle.\n",
377363
" \"\"\"\n",
378-
" # Instead of taking the complement\n",
379-
" # of the union of all successors and the\n",
380-
" # set of all nodes, we cumulative take\n",
381-
" # the complement of each successor with\n",
382-
" # the set of all letters.\n",
383364
" dag = create_letter_dag(words)\n",
384365
" roots = set(dag.keys())\n",
385366
" for successors in dag.values():\n",
@@ -407,13 +388,6 @@
407388
" actual = s.alienOrder(word_list)\n",
408389
" assert actual == expected, f\"{word_list}: {expected} != {actual}\""
409390
]
410-
},
411-
{
412-
"cell_type": "code",
413-
"execution_count": null,
414-
"metadata": {},
415-
"outputs": [],
416-
"source": []
417391
}
418392
],
419393
"metadata": {

0 commit comments

Comments
 (0)