Coder Social home page Coder Social logo

shehneelkhan / two-dimensional-binary-indexed-tree Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 8 KB

It is a data structure used to update and query a 2D matrix in a better way because of its good time and space complexities.

Python 100.00%
fenwick-tree data-structures python prefix-sum 2d-fenwick-tree

two-dimensional-binary-indexed-tree's Issues

fix some bug and improve some code

'''python
class FenwickTree:

def __init__(self, matrix):
    self.m = len(matrix)
    self.n = len(matrix[0])
    self.ft = [[0 for i in range(self.n + 1)] for i in range(self.m + 1)] # (m+1)*(n+1) matrix full of 0
    for i in range(self.m):
        for j in range(self.n):
            self.update(i+1, j+1, matrix[i][j]) # ft[1,m][1,n]

def LSB(self, x):
    return x & (-x) # toggling of the last set 1 bit in the binary representation of i

def update(self, x, y, val):
    while x <= self.m:
        y1 = y
        while y1 <= self.n:
            self.ft[x][y1] += val
            y1 += self.LSB(y1)
        x += self.LSB(x)

def summation(self, x, y):
    if x > self.m or y > self.n or x < 1 or y < 1:
        raise IndexError(f"0 < x < {self.m +1}, 0 < y < {self.n + 1}")
    s = 0
    x1 = x
    while x1 > 0:
        y1 = y
        while y1 > 0:
            s += self.ft[x1][y1]
            y1 -= self.LSB(y1)
        x1 -= self.LSB(x1)
    return s

def Summation(self, x1, y1, x2, y2):
    return (self.summation(x2, y2) - self.summation(x1-1, y2) - self.summation(x2, y1-1) + self.summation(x1-1,x2-1))

if name == "main":

matrix = [[1, 2, 3, 4],
          [5, 3, 8, 1],
          [4, 6, 7, 5]]

f = FenwickTree(matrix)

print(f.summation(2,3)) # 1+2+3+5+3+8
print(f.summation(1, 1)) # 1
print(f.Summation(1,2,2,3)) # 2+3+3+8

'''

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.