From 3ab8c84df7aedc20e12695b46c3461b905a0bdc6 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Fri, 13 Sep 2019 11:44:36 -0700 Subject: [PATCH] Update graph.md --- contents/algorithms/graph.md | 37 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/contents/algorithms/graph.md b/contents/algorithms/graph.md index a68fbf93..51defd02 100644 --- a/contents/algorithms/graph.md +++ b/contents/algorithms/graph.md @@ -33,66 +33,61 @@ A simple template for doing depth-first searches on a matrix goes like this: ```py def dfs(matrix): - # check for an empty graph + # Check for an empty graph. if not matrix: return [] + rows, cols = len(matrix), len(matrix[0]) visited = set() directions = ((0, 1), (0, -1), (1, 0), (-1, 0)) - # Uses short circuiting to check whether current position is within the - # boundary and has not been visited before checking if it is valid - def pass_all_conditions(i, j): - return i in range(rows) and j in range(cols) and (i, j) not in visited - def traverse(i, j): - if not pass_all_conditions(i, j): + if (i, j) in visited: return + visited.add((i, j)) - # Traverse neighbors + # Traverse neighbors. for direction in directions: next_i, next_j = i + direction[0], j + direction[1] - traverse(next_i, next_j) + if 0 <= next_i < rows and 0 <= next_j < cols: + # Add in your question-specific checks. + traverse(next_i, next_j) for i in range(rows): for j in range(cols): traverse(i, j) ``` -Another similar template for doing breadth first searches on the matrix goes like this: +A similar template for doing breadth-first searches on the matrix goes like this: ```py from collections import deque - def bfs(matrix): - # check for an empty graph + # Check for an empty graph. if not matrix: return [] + rows, cols = len(matrix), len(matrix[0]) visited = set() directions = ((0, 1), (0, -1), (1, 0), (-1, 0)) - # Uses short circuiting to check whether current position is within the - # boundary and has not been visited before checking if it is valid - def pass_all_conditions(i, j): - return i in range(rows) and j in range(cols) and (i, j) not in visited - def traverse(i, j): queue = deque([(i, j)]) while queue: curr_i, curr_j = queue.pop() - if pass_all_conditions(curr_i, curr_j): + if (curr_i, curr_j) not in visited: visited.add((curr_i, curr_j)) - # Traverse neighbors + # Traverse neighbors. for direction in directions: next_i, next_j = curr_i + direction[0], curr_j + direction[1] - queue.append((next_i, next_j)) + if 0 <= next_i < rows and 0 <= next_j < cols: + # Add in your question-specific checks. + queue.append((next_i, next_j)) for i in range(rows): for j in range(cols): traverse(i, j) - ``` > NOTE: While DFS is implemented using recursion in this sample, it could also be implemented iteratively similar to BFS. The key difference between the algorithms lies in the underlying data structure (BFS uses a queue while DFS uses a stack). The `deque` class in Python can function as both a stack and a queue