Introducción

1

¿Qué es un grafo?

2

¿Qué es un árbol?

3

¿Qué es recursión?

4

Aplicaciones reales de grafos y árboles

5

Formas de representar un grafo

DFS

6

Análisis de DFS: algoritmo de búsqueda en profundidad

7

Programando DFS de forma recursiva

8

Otras formas de programar DFS

9

Recorridos y profundidad de un Árbol

10

Sum Root to Leaf Numbers: análisis del problema

11

Solución de Sum Root to Leaf Numbers

12

Playground: Sum Root to Leaf Numbers

13

Programando Sum Root to Leaf Numbers en Golang

14

Number of Islands: análisis del problema

15

Solución de Number of Islands

16

Playground: Number of Islands

17

Programando Number of Islands en Python

18

Ejercicios recomendados de DFS

19

Ejercicios resueltos de DFS

BFS

20

Análisis de BFS: algoritmo de búsqueda en anchura

21

Programando BFS con Python

22

Minimum Knights Moves (movimientos de caballo en ajedrez): análisis del problema

23

Solución de Minimum Knights Moves

24

Playground: Minimum Knights Moves

25

Programando Minimum Knights Moves con Python

26

Rotting Oranges: análisis del problema

27

Solución de Rotting Oranges

28

Playground: Rotting Oranges

29

Rotting Oranges con Java

30

Shortest Bridge Between Islands: análisis del problema

31

Solución de Shortest Bridge Between Islands

32

Playground: Shortest Bridge Between Islands

33

Programando Shortest Bridge Between Islands con Python

34

Ejercicios recomendados de BFS

35

Ejercicios resueltos de BFS

Backtrack

36

Algoritmo de Backtrack

37

Letter Combinations of a Phone Number: análisis del problema

38

Solución de Letter Combinations of a Phone Number

39

Programando Letter Combinations of a Phone Number con C++

40

Playground: Letter Combinations of a Phone Number

41

Restore IP Addresses: análisis del problema

42

Programando Restore IP Addresses con C++

43

Playground: Restore IP Addresses

44

Word Search: análisis del problema

45

Solución de Word Search

46

Playgrund: Word Search

47

Programando Word Search JavaScript

48

Reto: N Queens Puzzle

49

Ejercicios recomendados de Backtrack

50

Ejercicios resueltos de Backtrack

Próximos pasos

51

¿Qué otros algoritmos y tipos de grafos puedes aprender?

52

¿Quieres más cursos avanzados de algoritmos?

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
9 Hrs
17 Min
23 Seg

Shortest Bridge Between Islands: análisis del problema

30/52
Resources

How to solve the shortest bridge problem between islands?

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.

What is an island?

  • An island is made up of adjacent cells of ones ('1') that form a block of land.
  • They must be surrounded by cells of zeros ('0'), symbolizing water.
  • They can have any shape, which makes it complex to find the optimal point for the bridge.

How to find the shortest distance to build the bridge?

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.

Practical example

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:

  1. Equal distances: when the islands are equidistant at all points, the result would be 1, since any bridge will have the same minimum length.
  2. Irregular shapes: You might be faced with unusually shaped islands, where you need to identify the optimal points for the bridge, often involving more computational complexity.

How do you reuse parts of the code from the previous problem?

Moving forward, consider how you used search algorithms in the 'number of islands' problem. You could:

  • Employ Depth First Search (DFS) or Breadth First Search (BFS) to identify islands.
  • Modify these methods to search the shortest possible distance between island edges.

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.

Motivate yourself to solve the problem

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

Sort by:

Want to see more contributions, questions and answers from the community?