Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given a m by n square grids. Suppose that each square is either land of water. An example is a follows, which shows a 4

Given a m by n square grids. Suppose that each square is either land of water. An example is a follows, which shows a 4 by 5 grid with land squares filled with green and water squares filled with blue.

image text in transcribed

Two land squares are considered in the same island if there are adjacent. In the above example, the grids has 3 different islands. Note that diagonal pairs of lands are not considered adjacent. Describe an algorithm to output the number of islands of a given m by n square grid. The running time of your algorithm should be O(mn). You may assume that the input is an m by n array. A[i,j] = 1 means that square [i,j] is land, and A[i,j] = 0 means that square [i,j] is water. Thus the above grid is given as:

image text in transcribed

Hint: Consider a graph with every land square as a vertex, and there is an edge connects every pair of adjacent lands. What are islands in this graph?

1-0-0-0 0 1-0-0-0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the business opportunity in the creation of virtual humans?

Answered: 1 week ago