Skip to main content
bisect
Source Link
shreevatsa
  • 661
  • 1
  • 10
  • 14

It's not so hard to implement, andIt doesn't require linked lists, just arrays that can grow.

There's a Java applet online that implements it.

I'm sure there are other implementations online, but since I couldn't find any, as a start, here's a simple Python implementation. [Though it feels odd giving a programming answer here, and althoughI'm sure several people here can write it much better, here's a Python implementation that takes O(n2) per element inserted. Binary search can improve it to O(n log n).!]

'''Thefrom bumpingbisect algorithmimport forbisect
def RSK(p):
    '''Given a permutation p, spit out a pair of Young tableaux'''
 
    P = []
[]; Q = []
    def insert(m, n=0):
        '''Insert m into P, then place n in Q at the same place'''
        for r in range(len(P)):
        l = len(P[r])
        if m > P[r][lP[r][-1]:
                P[r].append(m)
           ; Q[r].append(n)
            return
        for c in range(l):return
            ifc P[r][c]= >bisect(P[r], m:)
                P[r][c],m = m,P[r][c]
                break
    P.append([m])
        Q.append([n])

s = '1364752'
  for i in range(len(sp)):
        insert(int(s[i]p[i]), i+1)
 
print    return (P,Q)

print QRSK('1364752')

Edit: Used binary search to improve from O(n3) to O(n2log n), which should matter only for very large permutations.

It's not so hard to implement, and doesn't require linked lists, just arrays that can grow.

There's a Java applet online that implements it.

I'm sure there are other implementations online, but since I couldn't find any, as a start, and although several people here can write it much better, here's a Python implementation that takes O(n2) per element inserted. Binary search can improve it to O(n log n).

'''The bumping algorithm for Young tableaux'''
 
P = []
Q = []
def insert(m, n=0):
    '''Insert m into P, then place n in Q at the same place'''
    for r in range(len(P)):
        l = len(P[r])
        if m > P[r][l-1]:
            P[r].append(m)
            Q[r].append(n)
            return
        for c in range(l):
            if P[r][c] > m:
                P[r][c],m = m,P[r][c]
                break
    P.append([m])
    Q.append([n])

s = '1364752'
for i in range(len(s)):
    insert(int(s[i]), i+1)
 
print P
print Q

It doesn't require linked lists, just arrays that can grow.

There's a Java applet online that implements it.

I'm sure there are other implementations online, but since I couldn't find any, as a start, here's a simple Python implementation. [Though it feels odd giving a programming answer here, and I'm sure several people here can write it much better!]

from bisect import bisect
def RSK(p):
    '''Given a permutation p, spit out a pair of Young tableaux'''
    P = []; Q = []
    def insert(m, n=0):
        '''Insert m into P, then place n in Q at the same place'''
        for r in range(len(P)):
            if m > P[r][-1]:
                P[r].append(m); Q[r].append(n)
                return
            c = bisect(P[r], m)
            P[r][c],m = m,P[r][c]
        P.append([m])
        Q.append([n])

    for i in range(len(p)):
        insert(int(p[i]), i+1)
    return (P,Q)

print RSK('1364752')

Edit: Used binary search to improve from O(n3) to O(n2log n), which should matter only for very large permutations.

Source Link
shreevatsa
  • 661
  • 1
  • 10
  • 14

It's not so hard to implement, and doesn't require linked lists, just arrays that can grow.

There's a Java applet online that implements it.

I'm sure there are other implementations online, but since I couldn't find any, as a start, and although several people here can write it much better, here's a Python implementation that takes O(n2) per element inserted. Binary search can improve it to O(n log n).

'''The bumping algorithm for Young tableaux'''

P = []
Q = []
def insert(m, n=0):
    '''Insert m into P, then place n in Q at the same place'''
    for r in range(len(P)):
        l = len(P[r])
        if m > P[r][l-1]:
            P[r].append(m)
            Q[r].append(n)
            return
        for c in range(l):
            if P[r][c] > m:
                P[r][c],m = m,P[r][c]
                break
    P.append([m])
    Q.append([n])

s = '1364752'
for i in range(len(s)):
    insert(int(s[i]), i+1)

print P
print Q