How to find area of connected islands (graph theory python)How can I represent an 'Enum' in Python?Python Graph LibraryHow do I use raw_input in Python 3Connectivity of a GraphWhy DFS and not BFS for finding cycle in graphsHow to install pip with Python 3?Graph Theory Depth First Searchhow to find the original directed graph?Finding all possible paths in a fully connected graph in pythonFinding Connectivity in a Graph

Is it right to use the ideas of non-winning designers in a design contest?

When was "Cable Select" introduced for IDE devices?

How do you say "to hell with everything" in French?

How do German speakers decide what should be on the left side of the verb?

When did computers stop checking memory on boot?

How to finish my PhD?

Do aarakocra have arms as well as wings?

Project Euler problem #112

Owner keeps cutting corners and poaching workers for his other company

Bacteria vats to generate edible biomass, require intermediary species?

Why is it that I have to play this note on the piano as A sharp?

The meaning of "offing" in "an agreement in the offing"

Why does PAUSE key have a long make code and no break code?

Extra arrow heads appearing tikz

Why are some hotels asking you to book through Booking.com instead of matching the price at the front desk?

Template default argument loses its reference type

More than three domains hosted on the same IP address

If every star in the universe except the Sun were destroyed, would we die?

Leaving the USA for 10 yrs when you have asylum

How should Thaumaturgy's "three times as loud as normal" be interpreted?

How strong is aircraft-grade spruce?

How to plot two curves with the same area under?

When does order matter in probability?

Examples where "thin + thin = nice and thick"



How to find area of connected islands (graph theory python)


How can I represent an 'Enum' in Python?Python Graph LibraryHow do I use raw_input in Python 3Connectivity of a GraphWhy DFS and not BFS for finding cycle in graphsHow to install pip with Python 3?Graph Theory Depth First Searchhow to find the original directed graph?Finding all possible paths in a fully connected graph in pythonFinding Connectivity in a Graph






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Hi I'm referring to the Graph Theory problem of finding connected islands.
https://www.geeksforgeeks.org/find-the-number-of-islands-set-2-using-disjoint-set/
This helps to find the number of connected islands.



I have a related problem: Find the area of the largest islands (1) including the water(0) that it surrounds. The code for finding connected islands is below. But I don't know how to go about writing an algorithm for finding area...



def numIslands(graph):

row = len(graph)
col = len(graph[0])
count += 1

for i in range(row):
for j in range(col):
if graph[i][j] == 1:
dfs(graph, row, col, i, j)
count += 1
return count

def dfs(graph, row, col, x, y):
if graph[x][y] == 0:
return

graph[x][y] = 0

if x != 0:
dfs(graph, row, col, x-1, y)

if x != rows - 1:
dfs(graph, row, col, x+1, y)

if y != 0:
dfs(graph, row, col, x, y-1)

if y != col-1:
dfs(graph, row, col, x, y+1)


The sample inputs and outputs are as below:



1111 
0101
11110

Area: 10

101
010
101

Area: 1

11111
10011
11111

Area: 15

1111111
1000001
1011101
1000001
1111111

Area 35

1111111
1000001
1011100
1000001
1111111

Area 19


Any suggestions on how to go about would be appreciated!










share|improve this question


























  • Each 1 or 0 (a node) represents a unit of area (be it 1 sq. mm or 1 sq. km or what ever, so the island area equals to the total number of 1 + total number of 0 it surrounds.

    – c0der
    Mar 30 at 13:39

















0















Hi I'm referring to the Graph Theory problem of finding connected islands.
https://www.geeksforgeeks.org/find-the-number-of-islands-set-2-using-disjoint-set/
This helps to find the number of connected islands.



I have a related problem: Find the area of the largest islands (1) including the water(0) that it surrounds. The code for finding connected islands is below. But I don't know how to go about writing an algorithm for finding area...



def numIslands(graph):

row = len(graph)
col = len(graph[0])
count += 1

for i in range(row):
for j in range(col):
if graph[i][j] == 1:
dfs(graph, row, col, i, j)
count += 1
return count

def dfs(graph, row, col, x, y):
if graph[x][y] == 0:
return

graph[x][y] = 0

if x != 0:
dfs(graph, row, col, x-1, y)

if x != rows - 1:
dfs(graph, row, col, x+1, y)

if y != 0:
dfs(graph, row, col, x, y-1)

if y != col-1:
dfs(graph, row, col, x, y+1)


The sample inputs and outputs are as below:



1111 
0101
11110

Area: 10

101
010
101

Area: 1

11111
10011
11111

Area: 15

1111111
1000001
1011101
1000001
1111111

Area 35

1111111
1000001
1011100
1000001
1111111

Area 19


Any suggestions on how to go about would be appreciated!










share|improve this question


























  • Each 1 or 0 (a node) represents a unit of area (be it 1 sq. mm or 1 sq. km or what ever, so the island area equals to the total number of 1 + total number of 0 it surrounds.

    – c0der
    Mar 30 at 13:39













0












0








0








Hi I'm referring to the Graph Theory problem of finding connected islands.
https://www.geeksforgeeks.org/find-the-number-of-islands-set-2-using-disjoint-set/
This helps to find the number of connected islands.



I have a related problem: Find the area of the largest islands (1) including the water(0) that it surrounds. The code for finding connected islands is below. But I don't know how to go about writing an algorithm for finding area...



def numIslands(graph):

row = len(graph)
col = len(graph[0])
count += 1

for i in range(row):
for j in range(col):
if graph[i][j] == 1:
dfs(graph, row, col, i, j)
count += 1
return count

def dfs(graph, row, col, x, y):
if graph[x][y] == 0:
return

graph[x][y] = 0

if x != 0:
dfs(graph, row, col, x-1, y)

if x != rows - 1:
dfs(graph, row, col, x+1, y)

if y != 0:
dfs(graph, row, col, x, y-1)

if y != col-1:
dfs(graph, row, col, x, y+1)


The sample inputs and outputs are as below:



1111 
0101
11110

Area: 10

101
010
101

Area: 1

11111
10011
11111

Area: 15

1111111
1000001
1011101
1000001
1111111

Area 35

1111111
1000001
1011100
1000001
1111111

Area 19


Any suggestions on how to go about would be appreciated!










share|improve this question
















Hi I'm referring to the Graph Theory problem of finding connected islands.
https://www.geeksforgeeks.org/find-the-number-of-islands-set-2-using-disjoint-set/
This helps to find the number of connected islands.



I have a related problem: Find the area of the largest islands (1) including the water(0) that it surrounds. The code for finding connected islands is below. But I don't know how to go about writing an algorithm for finding area...



def numIslands(graph):

row = len(graph)
col = len(graph[0])
count += 1

for i in range(row):
for j in range(col):
if graph[i][j] == 1:
dfs(graph, row, col, i, j)
count += 1
return count

def dfs(graph, row, col, x, y):
if graph[x][y] == 0:
return

graph[x][y] = 0

if x != 0:
dfs(graph, row, col, x-1, y)

if x != rows - 1:
dfs(graph, row, col, x+1, y)

if y != 0:
dfs(graph, row, col, x, y-1)

if y != col-1:
dfs(graph, row, col, x, y+1)


The sample inputs and outputs are as below:



1111 
0101
11110

Area: 10

101
010
101

Area: 1

11111
10011
11111

Area: 15

1111111
1000001
1011101
1000001
1111111

Area 35

1111111
1000001
1011100
1000001
1111111

Area 19


Any suggestions on how to go about would be appreciated!







python-3.x graph depth-first-search breadth-first-search






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 8:26







spidermarn

















asked Mar 28 at 6:27









spidermarnspidermarn

1481 silver badge8 bronze badges




1481 silver badge8 bronze badges















  • Each 1 or 0 (a node) represents a unit of area (be it 1 sq. mm or 1 sq. km or what ever, so the island area equals to the total number of 1 + total number of 0 it surrounds.

    – c0der
    Mar 30 at 13:39

















  • Each 1 or 0 (a node) represents a unit of area (be it 1 sq. mm or 1 sq. km or what ever, so the island area equals to the total number of 1 + total number of 0 it surrounds.

    – c0der
    Mar 30 at 13:39
















Each 1 or 0 (a node) represents a unit of area (be it 1 sq. mm or 1 sq. km or what ever, so the island area equals to the total number of 1 + total number of 0 it surrounds.

– c0der
Mar 30 at 13:39





Each 1 or 0 (a node) represents a unit of area (be it 1 sq. mm or 1 sq. km or what ever, so the island area equals to the total number of 1 + total number of 0 it surrounds.

– c0der
Mar 30 at 13:39












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391367%2fhow-to-find-area-of-connected-islands-graph-theory-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391367%2fhow-to-find-area-of-connected-islands-graph-theory-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현