def BellmanFordOpti(G, src):
    dist = [inf] * G.order
    dist[src] = 0
    p = [-1] * G.order
    n = G.order
    q = queue.Queue()
    q.enqueue(src)
    inQueue = [False] * G.order
    inQueue[src] = True
    q2 = queue.Queue()
    while n > 0 and not q.isempty():
        x = q.dequeue()
        inQueue[x] = False
        for y in G.adjlists[x]:
            if dist[x] + G.costs[(x, y)] < dist[y]:
                dist[y] = dist[x] + G.costs[(x, y)]
                p[y] = x
                if not inQueue[y]:
                    q2.enqueue(y)
                    inQueue[y] = True
        if q.isempty():  # level change
            q = q2
            q2 = queue.Queue()
            n -= 1
    return (not q.isempty(), dist, p)
Example #2
0
def bfsasbin(B):
    q = queue.Queue()
    q2 = queue.Queue()
    q.enqueue(B)
    while not q.isempty():
        B = q.dequeue()
        print(B.key)
        child = B.child
        while child:
            q2.enqueue(child)
            child = child.sibling
        if q.isempty():
            print()
            (q, q2) = (q2, q)
Example #3
0
def is_perfectBFS(B):  # complet!
    """
    BFS: tests if each level has twice as many nodes 
    as the previous level
    """
    if B == None:
        return True
    else:
        q = queue.Queue()
        q.enqueue(B)
        q.enqueue(None)
        ok = True
        (current, expected) = (0, 1)
        while not q.isempty() and ok:
            B = q.dequeue()
            if B == None:
                ok = (current == expected)
                if not q.isempty():
                    q.enqueue(None)
                    (expected, current) = (2 * current, 0)
            else:
                current += 1
                if B.left != None:
                    q.enqueue(B.left)
                if B.right != None:
                    q.enqueue(B.right)
        return ok
Example #4
0
def __colorize_graph(G, result, precolors, vec, cur):
    maxcol = 0
    q = queue.Queue()

    q.enqueue(cur)
    vec[cur] = 1

    while not q.isempty():
        cur = q.dequeue()
        col = result[cur]
        maxcol = max(maxcol, col)

        for child in G.adjlists[cur]:
            if not vec[child]:
                q.enqueue(child)
                vec[child] = 1

            childcol = result[child]

            if col == childcol:
                __add_color(col, precolors[child])
                result[child] = __fix_color(col, precolors[child])
            elif col > childcol:
                __add_color(col, precolors[child])

    return maxcol
Example #5
0
def levels(B):
    q = queue.Queue()
    q.enqueue(B)
    q2 = queue.Queue()
    Levels = []
    L = []
    while not q.isempty():
        B = q.dequeue()
        L.append(B.key)
        C = B.child
        while C:
            q2.enqueue(C)
            C = C.sibling
        if q.isempty():
            (q, q2) = (q2, q)
            Levels.append(L)
            L = []
    return Levels
Example #6
0
def __BFS_forest(G, s, p):
    q = queue.Queue()
    q.enqueue(s)
    p[s] = -1  # root
    while not q.isempty():
        x = q.dequeue()
        for y in G.adjlists[x]:
            if p[y] == None:
                q.enqueue(y)
                p[y] = x
Example #7
0
def width2(B):
    w_max = 0
    if B != None:
        q = queue.Queue()  #current
        q.enqueue(B)
        q2 = queue.Queue()  #next level
        w = 0
        while not q.isempty():
            B = q.dequeue()
            (w, w_max) = (0, 0)
            if B.left != None:
                q2.enqueue(B.left)
            if B.right != None:
                q2.enqueue(B.right)
            if q.isempty():
                w_max = max(w, w_max)
                w = 0
                (q, q2) = (q2, q)
    return w_max
Example #8
0
def __BFS(G, s, p):
    q = queue.Queue()
    q.enqueue(s)
    p[s] = -1
    while not q.isempty():
        s = q.dequeue()
        print(s)
        for adj in G.adjlists[s]:
            if p[adj] == None:
                q.enqueue(adj)
                p[adj] = s
Example #9
0
def __componentsBFS(G, s, cc, nocc):

    q = queue.Queue()
    q.enqueue(s)
    cc[s] = nocc
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if cc[adj] is None:
                q.enqueue(adj)
                cc[adj] = nocc
Example #10
0
def __bfs(G, s, p):
    q = queue.Queue()
    q.enqueue(s)
    p[s] = -1    # M[s] = True
    while not q.isempty():
        s = q.dequeue()
        print(s, end = ' ')
        for adj in G.adjlists[s]:
            if p[adj] is None: # not M[adj]       
                p[adj] = s
                q.enqueue(adj)
Example #11
0
def bfs(B):  #breadth first search
    if B != None:
        q = queue.Queue()
        q.enqueue(B)
        while not q.isempty():
            B = q.dequeue()
            print(B.key, end=' ')
            if B.left != None:
                q.enqueue(B.left)
            if B.right != None:
                q.enqueue(B.right)
Example #12
0
def excentricity(G, src):
    dist = [-1] * G.order
    q = queue.Queue()
    q = q.enqueue(src)
    dist[src] = 0
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if dist[adj] == -1:
                dist[adj] = dist[s] + 1
                q.enqueue(adj)
    return dist[s]
Example #13
0
def __BFS(Gmat, s, M):
    q = queue.Queue()
    q.enqueue(s)
    M[s] = True
    while not q.isempty():
        x = q.dequeue()
        print(x)
        for y in range(Gmat.order):
            if Gmat.adj[x][y]:  # x is adjacent to y
                if not M[y]:  # y is not marked
                    M[y] = True
                    q.enqueue(y)
Example #14
0
def __distances2(G, s, M, dmin, dmax):
    q = queue.Queue()
    q.enqueue(s)
    M[s] = True
    q2 = queue.Queue()
    depth = 0
    while not q.isempty():
        s = q.dequeue()
        if depth >= dmin:
            print(s, end=' ')
        if depth < dmax:
            for adj in range(G.order):
                if G.adj[s][adj]:
                    if not M[adj]:
                        q2.enqueue(adj)
                        M[adj] = True
        if q.isempty():
            depth += 1
            if depth > dmin:
                print()
            (q, q2) = (q2, q)
Example #15
0
def bfs_h(T):
    if len(T) > 1 and T[1] != None:
        l = len(T)
        q = queue.Queue()
        q.enqueue(1)
        while not q.isempty():
            no = q.dequeue()
            print(T[no])
            if no < l and T[no] != None:   # left child
                q.enqueue(2 * no)
            if no < l and T[no] != None:   # right child
                q.enqueue(2 * no + 1)
Example #16
0
def __bfsMat(G, s, p):
    q = queue.Queue()
    q.enqueue(s)
    p[s] = -1    # M[s] = True
    while not q.isempty():
        s = q.dequeue()
        print(s, end = ' ')
        for adj in range(G.order):
            if G.adj[s][adj]:   #adj is a successor
                if p[adj] is None: # not M[adj]       
                    p[adj] = s
                    q.enqueue(adj)
Example #17
0
def width(B):
    """
    computes the width of a bintree
    """
    w_max = 0
    if B != None:
        q = queue.Queue()  #current
        q.enqueue(B)
        q_next = queue.Queue()  #next level
        w = 0
        while not q.isempty():
            B = q.dequeue()
            w = w + 1
            if B.left != None:
                q_next.enqueue(B.left)
            if B.right != None:
                q_next.enqueue(B.right)
            if q.isempty():
                w_max = max(w, w_max)
                w = 0
                (q, q_next) = (q_next, q)
    return w_max
Example #18
0
def __distances(G, s):
    dist = [None] * G.order
    dist[s] = 0
    q = queue.Queue()
    q.enqueue(s)
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if dist[adj] == None:
                dist[adj] = dist[s] + 1
                q.enqueue(adj)

    return (s, dist[s])
def __bfs(G, s, dst, p):
    q = queue.Queue()
    q.enqueue(s)
    p[s] = -1    
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if p[adj] is None: 
                p[adj] = s
                if adj == dst:
                    return True
                q.enqueue(adj)
    return False       
Example #20
0
def __pathBFS(G, src, dst, p):
    """
    return True if dst reachable from src
    """
    q = queue.Queue()
    q.enqueue(src)
    p[s] = -1
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if p[adj] == None:
                q.enqueue(adj)
                p[adj] = s
Example #21
0
def __pathBFS(G, src, dst, p):
    q = queue.Queue()
    q.enqueue(src)
    p[src] = -1
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if p[adj] == None:
                p[adj] = s
                if adj == dst:
                    return True
                q.enqueue(adj)

    return False
Example #22
0
def __bipartiteBFS(G, s, Set):
    q = queue.Queue()
    q.enqueue(s)
    Set[s] = 1
    while not q.isempty():
        s = q.dequeue()
        for adj in G.adjlists[s]:
            if Set[adj] == 0:
                Set[adj] = -Set[s]
                q.enqueue(adj)
            else:
                if Set[s] == Set[adj]:
                    return False
    return True
Example #23
0
def treeToOccList(B):
    '''
    Builds the list of node occurrences
    '''
    q = queue.Queue()
    q.enqueue((B, ""))
    L = []
    while not q.isempty():
        (T, occ) = q.dequeue()
        L.append(occ)
        if T.left != None:
            q.enqueue((T.left, occ + '0'))
        if T.right != None:
            q.enqueue((T.right, occ + '1'))
    return L
Example #24
0
def __pathBFSmat(G, src, dst, p):
    q = queue.Queue()
    q.enqueue(src)
    p[src] = -1
    while not q.isempty():
        s = q.dequeue()
        for adj in range(G.order):
            if G.adj[s][adj]:
                if p[adj] == None:
                    p[adj] = s
                    if adj == dst:
                        return True
                    q.enqueue(adj)

    return False
Example #25
0
def occurrences(B):
    L = []
    if B != None:
        q = queue.Queue()
        q.enqueue((B, ""))
        while not q.isempty():
            (B, occ) = q.dequeue()
            L.append(occ)
            if B.left != None:
                q.enqueue((B.left, occ + '0'))
            if B.right != None:
                q.enqueue((B.right, occ + '1'))
        L[0] = chr(949)  # 'ε'

    return L
Example #26
0
def treeToOccList(B):
    '''
    Builds the list of node occurrences
    '''
    L = []
    if B:
        q = queue.Queue()
        q.enqueue((B, ""))
        while not q.isempty():
            (B, occ) = q.dequeue()
            L.append(occ)
            if B.left:
                q.enqueue((B.left, occ + '0') )
            if B.right:
                q.enqueue((B.right, occ + '1'))
    return L
def Bellman2(G, src):
    dist = [inf] * G.order
    dist[src] = 0
    p = [-1] * G.order
    din = indegreesSubGraph(G, src)
    q = queue.Queue()
    q.enqueue(src)
    while not q.isempty():
        x = q.dequeue()
        for y in G.adjlists[x]:
            din[y] -= 1
            if din[y] == 0:
                q.enqueue(y)
            if dist[x] + G.costs[(x, y)] < dist[y]:
                dist[y] = dist[x] + G.costs[(x, y)]
                p[y] = x
    return (dist, p)
Example #28
0
def bfs_levels(B):
    if B:
        q = queue.Queue()
        q.enqueue(B)
        q.enqueue(None)
        while not q.isempty():
            B = q.dequeue()
            if B == None:
                print()
                if not q.isempty():
                    q.enqueue(None)
            else:
                print(B.key, end=' ')
                if B.left:
                    q.enqueue(B.left)
                if B.right:
                    q.enqueue(B.right)
Example #29
0
def listCodes(B):
    '''
    Builds the list of (letter, code)
    B is not None 
    B is full (localement complet)
    '''
    q = queue.Queue()
    q.enqueue((B, ""))
    L = []
    while not q.isempty():
        (T, occ) = q.dequeue()
        if T.left == T.right:
            L.append((T.key, occ))
        else:
            q.enqueue((T.left, occ + '0'))
            q.enqueue((T.right, occ + '1'))
    return L
Example #30
0
def levels(T):
    q = queue.Queue()
    q.enqueue(T)
    q.enqueue(None)
    List = []
    level = []
    while not q.isempty():
        T = q.dequeue()
        if T == None:
            List.append(level)
            level = []
            if not q.isempty():
                q.enqueue(None)
        else:
            level.append(T.key)
            for child in T.children:
                q.enqueue(child)
    return List