コード例 #1
0
def main():
    edges = []
    vertices = []
    heap = []
    with open('dijkstraData.txt') as data:
        for line in data:
            a = line.strip().split()
            for _ in range(1, len(a), 1):
                placeholder = [
                    int(a[_].split(',')[1]),
                    int(a[_].split(',')[0]),
                    int(a[_].split(',')[1])
                ]
                edges.append(placeholder)
                if int(a[0]) not in vertices:
                    vertices.append(int(a[0]))
    print(edges)
    print(vertices)
    for x in range(len(edges)):
        heap_insert(heap, edges[x])

    print(heap)
    #X = set of vertices processed, A = dictionary of shortest path distances for vertex V, B = dictionary of the computed shortest path for V
    X = [1]
    A = {1: 0}
    B = {1: None}
コード例 #2
0
ファイル: test_heap.py プロジェクト: saikoa0709/neu-cs5800
 def testMaxHeap(self):
     A = [1,3,2,6,4,5]
     heap.heap_make(A)
     self.assertEqual(A[0], 6)
     heap.heap_delete(A, len(A))
     self.assertEqual(A[0], 5)
     self.assertEqual(A[-1], 6)
     heap.heap_insert(A, len(A)-1, 7)
     self.assertEqual(A[0], 7)
コード例 #3
0
ファイル: test_heap.py プロジェクト: songy23/neu-cs5800
 def testMaxHeap(self):
     A = [1, 3, 2, 6, 4, 5]
     heap.heap_make(A)
     self.assertEqual(A[0], 6)
     heap.heap_delete(A, len(A))
     self.assertEqual(A[0], 5)
     self.assertEqual(A[-1], 6)
     heap.heap_insert(A, len(A) - 1, 7)
     self.assertEqual(A[0], 7)
コード例 #4
0
ファイル: test_heap.py プロジェクト: saikoa0709/neu-cs5800
 def testMinHeap(self):
     cmp = lambda a,b: a > b
     A = [1,3,2,6,4,5]
     heap.heap_make(A, cmp)
     self.assertEqual(A[0], 1)
     heap.heap_delete(A, len(A), cmp)
     self.assertEqual(A[0], 2)
     self.assertEqual(A[-1], 1)
     heap.heap_insert(A, len(A)-1, 0, cmp)
     self.assertEqual(A[0], 0)
コード例 #5
0
ファイル: test_heap.py プロジェクト: songy23/neu-cs5800
 def testMinHeap(self):
     cmp = lambda a, b: a > b
     A = [1, 3, 2, 6, 4, 5]
     heap.heap_make(A, cmp)
     self.assertEqual(A[0], 1)
     heap.heap_delete(A, len(A), cmp)
     self.assertEqual(A[0], 2)
     self.assertEqual(A[-1], 1)
     heap.heap_insert(A, len(A) - 1, 0, cmp)
     self.assertEqual(A[0], 0)
コード例 #6
0
ファイル: maintain_median.py プロジェクト: jgerardsimcock/ADA
def maintain_median(stream):
  """Returns medians for the sequence of 
  already popped elements whenever the stream pops
  """
  out = [stream.pop(0)]
  heap_lo, heap_hi = [out[0]], []
  while stream:
    num = stream.pop(0)
    if num >= heap_lo[0]:
        heap.heap_insert(heap_hi, num, 'min')
    
    else:
        heap.heap_insert(heap_lo, num, 'max')

    if len(heap_lo) > len(heap_hi) + 1:
        heap.heap_insert(heap_hi, heap.heap_extract(heap_lo, 'max'), 'min')
    
    if len(heap_hi) > len(heap_lo) + 1:
        heap.heap_insert(heap_lo, heap.heap_extract(heap_hi, 'min'), 'max')

    if len(heap_lo) >= len(heap_hi):
        out.append(heap_lo[0])
    else:
        out.append(heap_hi[0])

  return out
def maintainMedian(stream):
    out = [stream.pop(0)]
    heap_lo, heap_hi = [out[0]], []
    while stream:
        num = stream.pop(0)
        if num >= heap_lo[0]:
            heap.heap_insert(heap_hi, num, 'min')
        else:
            heap.heap_insert(heap_lo, num, 'max')
        if len(heap_lo) > len(heap_hi) + 1:
            heap.heap_insert(heap_hi, heap.heap_extract(heap_lo, 'max'), 'min')
        if len(heap_hi) > len(heap_lo) + 1:
            heap.heap_insert(heap_lo, heap.heap_extract(heap_hi, 'min'), 'max')
        if len(heap_lo) >= len(heap_hi):
            out.append(heap_lo[0])
        else:
            out.append(heap_hi[0])
    return out
コード例 #8
0
ファイル: median.py プロジェクト: alanhtam/algorithms
def maintain_median(stream):
    """Returns the medians for the sequence of already popped elements every
    time when stream pops."""

    out = [stream.pop(0)]
    heap_lo, heap_hi = [out[0]], []
    while stream:
        num = stream.pop(0)
        if num >= heap_lo[0]:
            heap.heap_insert(heap_hi, num, 'min')
        else:
            heap.heap_insert(heap_lo, num, 'max')
        if len(heap_lo) > len(heap_hi) + 1:
            heap.heap_insert(heap_hi, heap.heap_extract(heap_lo, 'max'), 'min')
        if len(heap_hi) > len(heap_lo) + 1:
            heap.heap_insert(heap_lo, heap.heap_extract(heap_hi, 'min'), 'max')
        if len(heap_lo) >= len(heap_hi):
            out.append(heap_lo[0])
        else:            out.append(heap_hi[0])
    return out