Introducción
¿Qué es un grafo?
¿Qué es un árbol?
¿Qué es recursión?
Aplicaciones reales de grafos y árboles
Formas de representar un grafo
DFS
Análisis de DFS: algoritmo de búsqueda en profundidad
Programando DFS de forma recursiva
Otras formas de programar DFS
Recorridos y profundidad de un Árbol
Sum Root to Leaf Numbers: análisis del problema
Solución de Sum Root to Leaf Numbers
Playground: Sum Root to Leaf Numbers
Programando Sum Root to Leaf Numbers en Golang
Number of Islands: análisis del problema
Solución de Number of Islands
Playground: Number of Islands
Programando Number of Islands en Python
Ejercicios recomendados de DFS
Ejercicios resueltos de DFS
BFS
Análisis de BFS: algoritmo de búsqueda en anchura
Programando BFS con Python
Minimum Knights Moves (movimientos de caballo en ajedrez): análisis del problema
Solución de Minimum Knights Moves
Playground: Minimum Knights Moves
Programando Minimum Knights Moves con Python
Rotting Oranges: análisis del problema
Solución de Rotting Oranges
Playground: Rotting Oranges
Rotting Oranges con Java
Shortest Bridge Between Islands: análisis del problema
Solución de Shortest Bridge Between Islands
Playground: Shortest Bridge Between Islands
Programando Shortest Bridge Between Islands con Python
Ejercicios recomendados de BFS
Ejercicios resueltos de BFS
Backtrack
Algoritmo de Backtrack
Letter Combinations of a Phone Number: análisis del problema
Solución de Letter Combinations of a Phone Number
Programando Letter Combinations of a Phone Number con C++
Playground: Letter Combinations of a Phone Number
Restore IP Addresses: análisis del problema
Programando Restore IP Addresses con C++
Playground: Restore IP Addresses
Word Search: análisis del problema
Solución de Word Search
Playgrund: Word Search
Programando Word Search JavaScript
Reto: N Queens Puzzle
Ejercicios recomendados de Backtrack
Ejercicios resueltos de Backtrack
Próximos pasos
¿Qué otros algoritmos y tipos de grafos puedes aprender?
¿Quieres más cursos avanzados de algoritmos?
You don't have access to this class
Keep learning! Join and start boosting your career
The problem we are talking about is to find the most efficient way to create a bridge between two islands in a binary matrix. You are faced with a new challenge: finding the minimum distance needed to connect two islands, represented by groups of adjacent cells of ones, surrounded by cells of zeros representing water. Let's break down how you might approach this problem with your programming skills.
The goal is to find the ends of the islands that are closest to each other, so as to minimize the "bridge" of cells of zeros needed to connect the islands. I invite you to think about how you can use previous solutions. In particular, you could reuse elements of the code used to identify the number of islands and adapt it to this new context.
Imagine a ring formed by an island surrounding another central island. Here, since each boundary point of the outer island is equidistant from the inner one, several solutions can be equally valid. We can consider these situations:
Moving forward, consider how you used search algorithms in the 'number of islands' problem. You could:
In implementing these concepts, I share a code snippet you might consider:
# Example for BFS in Pythonfrom collections import deque
def bfs_shortest_bridge(grid): # Initialize queue for BFS and apply initial search for an island queue = deque() for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1:
dfs_mark_island(grid, i, j, queue) break if queue: break
# Expand the bridge using BFS steps = 0 directions = [(-1, 0), (1, 0), (0,-1), (0, 1)] while queue: size = len(queue) for _ in range(size): x, y = queue.popleft() for dx, dy in directions: nx, ny = x + dx, y + dy if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]): if grid[nx][ny] == 1: return steps elif grid[nx][ny] == 0: grid[nx][ny] =-1 # Mark the expanded water queue.append((nx, ny)) steps += 1 return-1
def dfs_mark_island(grid, i, j, queue): if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1: return grid[i][j] =-1 # Mark as visited queue.append((i, j)) for di, dj in [(-1, 0), (1, 0), (0,-1), (0, 1)]: dfs_mark_island(grid, i + di, j + dj, queue)
This snippet illustrates how to identify an island and then use BFS to find the minimum bridge distance. By running the code, you find the shortest path between islands, improving the efficiency of materials needed to build it.
If you already have an idea, I encourage you to try to solve it yourself. Use what you have learned above, adapt the code to your needs and share your experiences and solutions in the comments section - keep learning and improving your skills!
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?