简体   繁体   中英

How to find the largest sequence in binary array?

I'm trying to find the largest 1's secuence inside a random array.

I'm trying to find the longest connection of numbers 1 inside a binary array. I tried to compare each value of the array with its previous values (i+-1 and j+-1), but I failed.

rows = 5
cols = 5

matrix = np.random.randint(2, size=(rows, cols))
print(matrix)

count = 0
result = 0
for i in matrix:
    for j in i:
        if j <= cols and j <= len(i):
            if j == 1:
                count += 1
                result = max(result, count)
            else:
                count = 0
print(result)

This is an example of a random array given in my code

matrix =
[[1 1 0 1 1]
 [1 0 0 0 1]
 [1 1 1 1 1]
 [0 0 1 0 0]
 [1 0 1 0 0]]

Once we have found the largest conection of numbers 1, the result will be like that:

matrix =
[[(1) (1)  0  (1) (1)]
 [(1)  0   0   0  (1)]
 [(1) (1) (1) (1) (1)]
 [ 0   0  (1)  0   0]
 [ 1   0  (1)  0   0]]

The numbers inside the parentheses, those are the largest conection and the result would be like this:

result = 13

Complete Code

import numpy as np

rows = 200
cols = 200
arr = np.random.rand(rows, cols)

for i in range(len(arr)):
    for j in range(len(arr[i])):
        if arr[i][j] > 0.4:
            arr[i][j] = 1
        else:
            arr[i][j] = 0

for ii in range(len(arr)):
    for jj in range(len(arr[ii])):
        if arr[ii][jj] == 1.0 or arr[ii][jj] == 1:
            arr[ii][jj] = 1
        else:
            arr[ii][jj] = 0

arr = arr.astype(int)
print(arr)

dimension = rows * cols
danger = eval("dimension * .0002")


def find_largest(arr):
    max_count = 0
    visited = set()  # coordinates visited by DFS
    for y in range(0, len(arr)):
        for x in range(0, len(arr[y])):
            if arr[y][x] == 1 and (y, x) not in visited:
                # Runs DFS search on (y,x) and marks cell as visited
                max_count = max(max_count, explore(arr, y, x, visited))

    probable_danger = suma(max_count)
    print("Quantity of connections: " + str(max_count) +
          " ,probability of danger: " + str(probable_danger) + " ,comparing to the total; " +
          str(danger))
    return max_count


def explore(arr, y, x, visited):
    if arr[y][x] == 1 and (y, x) not in visited:
        count = 1
        visited.add((y, x))
        for i, j in adj(arr, y, x):
            count += explore(arr, i, j, visited)
        return count
    else:
        return 0


def adj(arr, y, x):
    # Generates valid adjacent coordinates for (y, x)
    if y - 1 > 0 and arr[y - 1][x] == 1:
        yield y - 1, x
    if y + 1 < len(arr) and arr[y + 1][x] == 1:
        yield y + 1, x
    if x - 1 > 0 and arr[y][x - 1] == 1:
        yield y, x - 1
    if x + 1 < len(arr[y]) and arr[y][x + 1] == 1:
        yield y, x + 1


def suma(max_count):
    dimension = rows * cols
    danger = eval("dimension * .0002")
    total_ones = 0
    total_zeros = 0

    for i in arr:
        for j in i:
            if j % 2 != 0:
                total_ones += 1
            else:
                total_zeros += 1

    found = max_count / total_ones
    ones = total_ones/dimension
    zeros = total_zeros/dimension

    print("Total of 1's "+str(ones) +
          " , total of 0's: "+str(zeros))

    if (found >= danger):
        return found
    else:
        return 0


find_largest(arr)

You can look at groups of 1s in your matrix as strongly connected components in a graph. You can solve this problem using DFS.

def find_largest(arr):
    max_count = 0
    visited = set()  # coordinates visited by DFS
    for y in range(0, len(arr)):
        for x in range(0, len(arr[y])):
            if arr[y][x] == 1 and (y, x) not in visited:
                # Runs DFS search on (y,x) and marks cell as visited
                max_count = max(max_count, explore(arr, y, x, visited))
    return max_count

Now on DFS itself:

def explore(arr, y, x, visited):
    if arr[y][x] == 1 and (y, x) not in visited:
        count = 1
        visited.add((y, x))
        for i, j in adj(arr, y, x):
            count += explore(arr, i, j, visited)
        return count
    else:
        return 0


def adj(arr, y, x):
    # Generates valid adjacent coordinates for (y, x)
    if y - 1 > 0 and arr[y - 1][x] == 1:
        yield y - 1, x
    if y + 1 < len(arr) and arr[y + 1][x] == 1:
        yield y + 1, x
    if x - 1 > 0 and arr[y][x - 1] == 1:
        yield y, x - 1
    if x + 1 < len(arr[y]) and arr[y][x + 1] == 1:
        yield y, x + 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM