Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wanderingstan/4b8ae84dd77eadf716b012f2a5e74790 to your computer and use it in GitHub Desktop.
Save wanderingstan/4b8ae84dd77eadf716b012f2a5e74790 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Depth-First Search and Breadth-First Search in Python\n",
"\n",
"By Edd Mann\n",
"\n",
"Source: https://eddmann.com/posts/depth-first-search-and-breadth-first-search-in-python/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Graph theory and in particular the graph ADT (abstract data-type) is widely explored and implemented in the field of Computer Science and Mathematics. Consisting of vertices (nodes) and the edges (optionally directed/weighted) that connect them, the data-structure is effectively able to represent and solve many problem domains. One of the most popular areas of algorithm design within this space is the problem of checking for the existence or (shortest) path between two or more vertices in the graph. Properties such as edge weighting and direction are two such factors that the algorithm designer can take into consideration. In this post I will be exploring two of the simpler available algorithms, Depth-First and Breath-First search to achieve the goals highlighted below:\n",
"\n",
"- Find all vertices in a subject vertices connected component.\n",
"- Return all available paths between two vertices.\n",
"- And in the case of BFS, return the shortest path (length measured by number of path edges).\n",
"\n",
"# The Graph\n",
"\n",
"So as to clearly discuss each algorithm I have crafted a connected graph with six vertices and six incident edges. The resulting graph is undirected with no assigned edge weightings, as length will be evaluated based on the number of path edges traversed. There are two popular options for representing a graph, the first being an adjacency matrix (effective with dense graphs) and second an adjacency list (effective with sparse graphs). I have opted to implement an adjacency list which stores each node in a dictionary along with a set containing their adjacent nodes. As the graph is undirected each edge is stored in both incident nodes adjacent sets.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"graph = {'A': set(['B', 'C']),\n",
" 'B': set(['A', 'D', 'E']),\n",
" 'C': set(['A', 'F']),\n",
" 'D': set(['B']),\n",
" 'E': set(['B', 'F']),\n",
" 'F': set(['C', 'E'])}"
]
},
{
"attachments": {
"image.png": {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XbqY1H6WN6gNhIuAkkW4iFl9/IEDB+SOO+4wxYGdqSdi9RVefv7AiqJz586OlkW4vGf6SSwElCxigeGal1TjwhKA0nPw4MGu6VekO3Lu3Dnp1auXWVGQwPj8eQeKk0T6oqL3fEoWbh3b2DcSJfx2797t1q4mqV8bN240K6g0adJENSEmCRx3/kjJwp3j8l+vxowZYyqKU2X9/fff/+8Lj77Ca/XFF1+Ua6+9VnLmzKmu7t4ZRyULL4wVCX6bNGliFJ8VKlTwZGVxXLZR2hIYxmoCn5JffvnFC/BrHy8goGThpZmAS3i5cuXMHp+yfl4JsFq1apUhO9y369WrJwsWLPAS7NpXJQtvzoHjx4/LoEGD5MorrzQu0VhOiJ9wW76Ho0ePyvTp06VOnTqmTCNBYePGjVOzqDenHb3WlYVXxw6F51NPPWXyYfDEphr7kCFDHF9tULsVL0z0EfTrf//7n9FROJc+0Ksj7Lp+K1m4bkjC7NCJEydkzpw5JpQ7RYoUkiNHDvM0hzh++OEHs+KgXKIVgh7ijz/+kNWrVxuLBvqU9OnTm1UPGa5Y8Wj1cyuQd+ScShaOwG5Ro/v27TNP9fLlyxvSSJUqlXHu4iZ+8sknTRj8mjVrhCS4POm52RPzbcCEy3EUcN61a5esXLnSbCcgg9KlSxv39HTp0pncE5UrV5bRo0c7XuzZInj9floli2idARs2bJDJkyfLwIED5Z577jEV29kW4OiFRYKEOzVq1DB/OIC1adNGUJr269dPOnbsaN43a9ZMqlSpIjVr1pRSpUqZc0BAnKdw4cLmGEowYuX46aefohVKva4LCChZ+GUmsJJgZQCBvPDCC4LHJHEoEELJkiUlb968pqQiugbqsPKelcOdd95pLBkURMKbdPbs2WZlwvZDxVcIKFn4arhDuNiDBw+GcJQe4kMElCx8OOh6yYpAUhBQskgKavobRcCHCJyOwdlC/xQDnQM6BxKZA4f+D7SFrJ4oz8fcAAAAAElFTkSuQmCC"
}
},
"cell_type": "markdown",
"metadata": {},
"source": [
"Looking at the graph depiction below you will also notice the inclusion of a cycle, by the adjacent connections between ‘F’ and ‘C/E’. This has been purposely included to provide the algorithms with the option to return multiple paths between two desired nodes.\n",
"\n",
"![image.png](attachment:image.png)\n",
"\n",
"# Depth-First Search\n",
"\n",
"The first algorithm I will be discussing is Depth-First search which as the name hints at, explores possible vertices (from a supplied root) down each branch before backtracking. This property allows the algorithm to be implemented succinctly in both iterative and recursive forms. Below is a listing of the actions performed upon each visit to a node.\n",
"\n",
"- Mark the current vertex as being visited.\n",
"- Explore each adjacent vertex that is not included in the visited set."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Connected Component\n",
"\n",
"The implementation below uses the stack data-structure to build-up and return a set of vertices that are accessible within the subjects connected component. Using Python’s overloading of the subtraction operator to remove items from a set, we are able to add only the unvisited adjacent vertices."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'A', 'B', 'C', 'D', 'E', 'F'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def dfs(graph, start):\n",
" visited, stack = set(), [start] # ❌ Because set() is not ordered, the order of node visits is lost.\n",
" while stack:\n",
" vertex = stack.pop()\n",
" if vertex not in visited:\n",
" visited.add(vertex)\n",
" stack.extend(graph[vertex] - visited)\n",
" return visited\n",
"\n",
"dfs(graph, 'A') # {'E', 'D', 'F', 'A', 'C', 'B'} ❌ Not code result, or correct for algorithm\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A', 'B', 'E', 'F', 'C', 'D']"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ✅ Fixed?\n",
"\n",
"def dfs(graph, start):\n",
" visited, stack = [], [start] # ✅ Use list\n",
" while stack:\n",
" vertex = stack.pop()\n",
" if vertex not in visited:\n",
" visited.append(vertex)\n",
" stack.extend(graph[vertex] - set(visited)) # ✅ Convert to set()\n",
" return visited\n",
"\n",
"dfs(graph, 'A') # ['A', 'B', 'E', 'F', 'C', 'D'] # Correct output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The second implementation provides the same functionality as the first, however, this time we are using the more succinct recursive form. Due to a common Python gotcha with default parameter values being created only once, we are required to create a new visited set on each user invocation. Another Python language detail is that function variables are passed by reference, resulting in the visited mutable set not having to reassigned upon each recursive call.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'A', 'B', 'C', 'D', 'E', 'F'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def dfs(graph, start, visited=None):\n",
" if visited is None:\n",
" visited = set() # ❌ Because set() is not ordered, the order of node visits is lost.\n",
" visited.add(start)\n",
" for next in graph[start] - visited:\n",
" dfs(graph, next, visited)\n",
" return visited\n",
"\n",
"dfs(graph, 'C') # {'E', 'D', 'F', 'A', 'C', 'B'} ❌ Does not match output, and output incorrect\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['C', 'F', 'E', 'B', 'A', 'D', 'A']"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ✅ Fixed?\n",
"\n",
"def dfs(graph, start, visited=[]): # ✅ Use list\n",
" visited.append(start)\n",
" for next in graph[start] - set(visited): # ✅ Convert to set()\n",
" dfs(graph, next, visited)\n",
" return visited\n",
"\n",
"dfs(graph, 'C') # ['C', 'F', 'E', 'B', 'A', 'D', 'A']\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Paths\n",
"\n",
"We are able to tweak both of the previous implementations to return all possible paths between a start and goal vertex. The implementation below uses the stack data-structure again to iteratively solve the problem, yielding each possible path when we locate the goal. Using a generator allows the user to only compute the desired amount of alternative paths.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['A', 'B', 'E', 'F'], ['A', 'C', 'F']]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def dfs_paths(graph, start, goal):\n",
" stack = [(start, [start])]\n",
" while stack:\n",
" (vertex, path) = stack.pop()\n",
" for next in graph[vertex] - set(path):\n",
" if next == goal:\n",
" yield path + [next]\n",
" else:\n",
" stack.append((next, path + [next]))\n",
"\n",
"list(dfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']] ⚠️ Correct, but order is different\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The implementation below uses the recursive approach calling the ‘yield from’ PEP380 addition to return the invoked located paths. Unfortunately the version of Pygments installed on the server at this time does not include the updated keyword combination."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['C', 'F'], ['C', 'A', 'B', 'E', 'F']]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def dfs_paths(graph, start, goal, path=None):\n",
" if path is None:\n",
" path = [start]\n",
" if start == goal:\n",
" yield path\n",
" for next in graph[start] - set(path):\n",
" yield from dfs_paths(graph, next, goal, path + [next])\n",
"\n",
"list(dfs_paths(graph, 'C', 'F')) # [['C', 'F'], ['C', 'A', 'B', 'E', 'F']]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Breath-First Search\n",
"\n",
"An alternative algorithm called Breath-First search provides us with the ability to return the same results as DFS but with the added guarantee to return the shortest-path first. This algorithm is a little more tricky to implement in a recursive manner instead using the queue data-structure, as such I will only being documenting the iterative approach. The actions performed per each explored vertex are the same as the depth-first implementation, however, replacing the stack with a queue will instead explore the breadth of a vertex depth before moving on. This behavior guarantees that the first path located is one of the shortest-paths present, based on number of edges being the cost factor.\n",
"\n",
"Connected Component\n",
"Similar to the iterative DFS implementation the only alteration required is to remove the next item from the beginning of the list structure instead of the stacks last.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'A', 'B', 'C', 'D', 'E', 'F'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def bfs(graph, start):\n",
" visited, queue = set(), [start]\n",
" while queue:\n",
" vertex = queue.pop(0)\n",
" if vertex not in visited:\n",
" visited.add(vertex)\n",
" queue.extend(graph[vertex] - visited)\n",
" return visited\n",
"\n",
"bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'} ❌❌❌"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A', 'C', 'B', 'F', 'D', 'E']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ✅ Fixed?\n",
"\n",
"def bfs(graph, start):\n",
" visited, queue = [], [start] # ✅ Use list\n",
" while queue:\n",
" vertex = queue.pop(0)\n",
" if vertex not in visited:\n",
" visited.append(vertex)\n",
" queue.extend(graph[vertex] - set(visited)) # ✅ Convert list to set()\n",
" return visited\n",
"\n",
"bfs(graph, 'A') # ['A', 'C', 'B', 'F', 'D', 'E'] # ✅"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Paths\n",
"\n",
"This implementation can again be altered slightly to instead return all possible paths between two vertices, the first of which being one of the shortest such path.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['A', 'C', 'F'], ['A', 'B', 'E', 'F']]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def bfs_paths(graph, start, goal):\n",
" queue = [(start, [start])]\n",
" while queue:\n",
" (vertex, path) = queue.pop(0)\n",
" for next in graph[vertex] - set(path):\n",
" if next == goal:\n",
" yield path + [next]\n",
" else:\n",
" queue.append((next, path + [next]))\n",
"\n",
"list(bfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Knowing that the shortest path will be returned first from the BFS path generator method we can create a useful method which simply returns the shortest path found or ‘None’ if no path exists. As we are using a generator this in theory should provide similar performance results as just breaking out and returning the first matching path in the BFS implementation.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A', 'C', 'F']"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def shortest_path(graph, start, goal):\n",
" try:\n",
" return next(bfs_paths(graph, start, goal))\n",
" except StopIteration:\n",
" return None\n",
"\n",
"shortest_path(graph, 'A', 'F') # ['A', 'C', 'F']\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@BoManev
Copy link

BoManev commented Jan 6, 2024

The set has the following signature for the __sub__ method:

  • def __sub__(self, __value: AbstractSet[_T | None]) -> set[_T]:
    • This creates an intermediate set.

We can instead use an iterator directly from the visited list

  • difference(*s: Iterable[Any]) -> set[str]
  • stack.extend(graph[vertex].difference(visited))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment