mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2026-04-16 11:33:34 +00:00
reorganise directories
upd chapter_01/binary.py
This commit is contained in:
9
chapter_06/README.md
Normal file
9
chapter_06/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Breadth-First Search
|
||||
|
||||
Can tell you if there's a path between A and B and will find the shortest.
|
||||
|
||||
In these examples, 1st degree Mango sellers are found before 2nd degree, 2nd before 3rd and so on.
|
||||
|
||||
Visted nodes should be stored in a set to ensure no infinite loops.
|
||||
|
||||
Running time for BFS on a directed graph: `O(V + E`) where V = vertices, E = edges.
|
||||
BIN
chapter_06/bfs1.png
Normal file
BIN
chapter_06/bfs1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
BIN
chapter_06/bfs2.png
Normal file
BIN
chapter_06/bfs2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
48
chapter_06/ex6.1.py
Normal file
48
chapter_06/ex6.1.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import logging
|
||||
from collections import deque
|
||||
from dataclasses import dataclass
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
name: str
|
||||
distance: int
|
||||
|
||||
|
||||
graph = {
|
||||
"s": [Node("a", 1), Node("b", 1)],
|
||||
"a": [Node("c", 0), Node("f", 0)],
|
||||
"b": [Node("c", 0), Node("d", 0)],
|
||||
"c": [],
|
||||
"d": [Node("f", 0)],
|
||||
"f": [],
|
||||
}
|
||||
|
||||
visited = set()
|
||||
|
||||
|
||||
def bfs():
|
||||
queue = deque()
|
||||
queue += graph["s"]
|
||||
|
||||
while queue:
|
||||
logger.debug(queue)
|
||||
current_node = queue.popleft()
|
||||
if current_node.name == "f":
|
||||
return current_node.distance
|
||||
|
||||
if current_node.name in visited:
|
||||
continue
|
||||
visited.add(current_node.name)
|
||||
|
||||
next_nodes = graph[current_node.name]
|
||||
for node in next_nodes:
|
||||
node.distance = current_node.distance + 1
|
||||
queue += next_nodes
|
||||
|
||||
|
||||
distance = bfs()
|
||||
print(f"shortest distance from s to f: {distance}")
|
||||
48
chapter_06/ex6.2.py
Normal file
48
chapter_06/ex6.2.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import logging
|
||||
from collections import deque
|
||||
from dataclasses import dataclass
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
name: str
|
||||
distance: int
|
||||
|
||||
|
||||
graph = {
|
||||
"cab": [Node("cat", 1), Node("car", 1)],
|
||||
"cat": [Node("mat", 0), Node("bat", 0)],
|
||||
"car": [Node("cat", 0), Node("bar", 0)],
|
||||
"mat": [Node("bat", 0)],
|
||||
"bar": [Node("bat", 0)],
|
||||
"bat": [],
|
||||
}
|
||||
|
||||
visited = set()
|
||||
|
||||
|
||||
def bfs():
|
||||
queue = deque()
|
||||
queue += graph["cab"]
|
||||
|
||||
while queue:
|
||||
logger.debug(queue)
|
||||
current_node = queue.popleft()
|
||||
if current_node.name == "bat":
|
||||
return current_node.distance
|
||||
|
||||
if current_node.name in visited:
|
||||
continue
|
||||
visited.add(current_node.name)
|
||||
|
||||
next_nodes = graph[current_node.name]
|
||||
for node in next_nodes:
|
||||
node.distance = current_node.distance + 1
|
||||
queue += next_nodes
|
||||
|
||||
|
||||
distance = bfs()
|
||||
print(f"shortest distance from cab to bat: {distance}")
|
||||
68
chapter_06/mango.py
Normal file
68
chapter_06/mango.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import logging
|
||||
from collections import deque
|
||||
from dataclasses import dataclass
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Person:
|
||||
name: str
|
||||
distance: int
|
||||
|
||||
@property
|
||||
def is_seller(self):
|
||||
return self.name.endswith("m")
|
||||
|
||||
|
||||
graph = {
|
||||
"you": [
|
||||
Person("bob", 1),
|
||||
Person("claire", 1),
|
||||
Person("alice", 1),
|
||||
],
|
||||
"claire": [
|
||||
Person("thom", 0),
|
||||
Person("jonny", 0),
|
||||
],
|
||||
"alice": [
|
||||
Person("peggy", 0),
|
||||
],
|
||||
"bob": [
|
||||
Person("peggy", 0),
|
||||
Person("anuj", 0),
|
||||
],
|
||||
"thom": [],
|
||||
"jonny": [],
|
||||
"peggy": [],
|
||||
"anuj": [],
|
||||
}
|
||||
|
||||
visited = set()
|
||||
|
||||
|
||||
def bfs():
|
||||
queue = deque()
|
||||
queue += graph["you"]
|
||||
|
||||
while queue:
|
||||
logger.debug(queue)
|
||||
current_person = queue.popleft()
|
||||
if current_person.is_seller:
|
||||
return current_person
|
||||
|
||||
if current_person.name in visited:
|
||||
continue
|
||||
visited.add(current_person.name)
|
||||
|
||||
next_persons = graph[current_person.name]
|
||||
for p in next_persons:
|
||||
p.distance = current_person.distance + 1
|
||||
queue += next_persons
|
||||
|
||||
|
||||
to_word = ["", "first", "second", "third"]
|
||||
|
||||
person = bfs()
|
||||
print(f"{person.name} is a {to_word[person.distance]} degree mango seller.")
|
||||
Reference in New Issue
Block a user