ENع
→Components and flood fill
الدرس 23 · Graph algorithms

Connected components and grid flood fill

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة8 خطوات5 أسئلةIntermediate
عن هذا الدرس

How many islands are on this map? Treat the map as a graph, with land squares as nodes and shared sides as edges, and the question becomes: how many connected components does the graph have? Scan the land in reading order, and start a breadth-first flood from every square no earlier flood reached. Each flood collects exactly one component. The Software lessons "Graphs" and "Graph search: BFS and DFS" cover the search itself, and the Maths lesson "Graph theory: vertices, edges and breadth-first search" defines paths and connectivity.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 23 · تفاعلي
01 / 08

A map is a graph

Each square of this 4 × 5 map is land or water. Make every land square a node, and join two land squares with an edge when they share a side: up, down, left or right. That gives 10 nodes and 8 edges.

  row 0  # # . . .
  row 1  # # . # #
  row 2  . . # . #
  row 3  . # # . .

# = land (10 nodes), . = water
edges (shared sides): 8
  (0, 0) – (0, 1)
  (0, 0) – (1, 0)
  (0, 1) – (1, 1)
  (1, 0) – (1, 1)
  (1, 3) – (1, 4)
  (1, 4) – (2, 4)
  (2, 2) – (3, 2)
  (3, 1) – (3, 2)
a square is (row, column), counted from 0
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A map is a graph

    Each square of this 4 × 5 map is land or water. Make every land square a node, and join two land squares with an edge when they share a side: up, down, left or right. That gives 10 nodes and 8 edges.

      row 0  # # . . .
      row 1  # # . # #
      row 2  . . # . #
      row 3  . # # . .
    
    # = land (10 nodes), . = water
    edges (shared sides): 8
      (0, 0) – (0, 1)
      (0, 0) – (1, 0)
      (0, 1) – (1, 1)
      (1, 0) – (1, 1)
      (1, 3) – (1, 4)
      (1, 4) – (2, 4)
      (2, 2) – (3, 2)
      (3, 1) – (3, 2)
    a square is (row, column), counted from 0
  2. Scan for unseen land

    A connected component is a biggest group of nodes joined by paths. To find them all, the code walks the land squares in reading order. todo holds every square not yet in an island; a square still in todo starts a new one.

    land, in reading order:
      (0, 0) (0, 1) (1, 0) (1, 1) (1, 3) (1, 4) (2, 2) (2, 4) (3, 1) (3, 2)
    
    todo = set(land)   every square, none labelled yet
    parts = []         one list per island found
    
    line 4: skip a square that left todo already
  3. Flood the first island

    (0, 0) is in todo, so it starts a part. Breadth-first search spreads from it: pop a square from the queue; each neighbour still in todo is removed, added to the part and queued. 4 squares: island 1.

    part 1:
      scan (0, 0): in todo → part 1, q = [(0, 0)]
        pop (0, 0)
          add (1, 0), q = [(1, 0)]
          add (0, 1), q = [(1, 0), (0, 1)]
        pop (1, 0)
          add (1, 1), q = [(0, 1), (1, 1)]
        pop (0, 1)
        pop (1, 1)
      parts.append(part)  → len(parts) = 1
    
    island 1 = (0, 0) (1, 0) (0, 1) (1, 1)
  4. Skip, then flood again

    The scan moves on. (0, 1), (1, 0) and (1, 1) left todo during the flood, so line 4 skips them. (1, 3) is new: flood it. The queue reaches (1, 4), then (2, 4). Island 2 has 3 squares.

    part 2:
      scan (0, 1): not in todo, skip
      scan (1, 0): not in todo, skip
      scan (1, 1): not in todo, skip
      scan (1, 3): in todo → part 2, q = [(1, 3)]
        pop (1, 3)
          add (1, 4), q = [(1, 4)]
        pop (1, 4)
          add (2, 4), q = [(2, 4)]
        pop (2, 4)
      parts.append(part)  → len(parts) = 2
    
    island 2 = (1, 3) (1, 4) (2, 4)
  5. Corners do not connect

    (1, 4) is skipped. (2, 2) touches island 1 only at a corner, and a corner is not an edge, so no flood reached it. It starts island 3: (2, 2), then (3, 2), then (3, 1).

    part 3:
      scan (1, 4): not in todo, skip
      scan (2, 2): in todo → part 3, q = [(2, 2)]
        pop (2, 2)
          add (3, 2), q = [(3, 2)]
        pop (3, 2)
          add (3, 1), q = [(3, 1)]
        pop (3, 1)
      parts.append(part)  → len(parts) = 3
    
    island 3 = (2, 2) (3, 2) (3, 1)
  6. 3 connected components

    The scan skips the last three squares, all filled already. parts now holds 3 lists, one per island, so len(parts) is 3. Every land square sits in exactly one part: 4 + 3 + 3 = 10.

    end of the scan:
      scan (2, 4): not in todo, skip
      scan (3, 1): not in todo, skip
      scan (3, 2): not in todo, skip
    
    return parts:
      1: (0, 0) (1, 0) (0, 1) (1, 1)
      2: (1, 3) (1, 4) (2, 4)
      3: (2, 2) (3, 2) (3, 1)
    len(parts) = 3
  7. Each square once

    Each land square leaves todo once, so it is queued once and popped once, checking 4 neighbours: here 10 pops and 40 checks. That is O(V + E) for any graph, and O(R × C) for a whole grid of R rows and C columns.

    pops:   10  (one per land square)
    checks: 40  (4 per pop)
    adds:   7  (+ 3 starts = 10)
    
    set: x in s, s.remove(x)   O(1) average
    deque: append, popleft     O(1)
    total: O(V + E); a grid has E ≤ 2V
    building land from the grid: O(R × C)
  8. Diagonals change the answer

    Whether corners count is part of the problem. With 8 neighbours, diagonals are edges too: (1, 1)–(2, 2) and (2, 2)–(1, 3) link all three islands, and the count drops to 1. Only near() would change.

    8 neighbours: add (r±1, c±1) to near()
      (1, 1) – (2, 2)  joins two islands
      (2, 2) – (1, 3)  joins two islands
    
    4 neighbours: 3 islands
    8 neighbours: 1 island of 10 squares

اختبار يتاح بعد الخطوة الأخيرة.