Full Code
Full Code
return True
def binary(n):
count = 1
while n > 1:
count += 1
n /= 2
return count
def binary_rec(n):
if n == 1: return 1
return binary_rec(n//2) + 1
swap(numbers, lo, s)
return s
"""
Partitions subarray by Hoare’s algorithm using first element as pivot.
Output: Partition of numbers with the split position returned as this
function’s value
"""
def hoare_partition(numbers, lo, hi):
p = numbers[lo]
i = lo+1
j = hi
while i < j:
swap(numbers, i, j)
swap(numbers, i, j)
swap(numbers, lo, j)
return j
from collections import defaultdict
import heapq
while edges:
cost, v, w = heapq.heappop(edges)
if w not in visited and w in graph:
visited.add(w)
mst[v].add(w)
for adj, cost in graph[w]:
if adj not in visited:
heapq.heappush(edges, (cost, w, adj))
return mst
graph = {
'A': {('C', 31)},
'B': {('C', 15)},
'C': {('G', 77), ('H', 40)},
'E': {('C', 17), ('I', 3)},
'G': {('B', 22), ('E', 23)},
'H': {('G', 66)},
'I': {('J', 70), ('K', 31)},
}
print(prims(graph, 'A'))
# Time-Complexity O(E log V)
from queue import PriorityQueue
pq.put((0, s))
while not pq.empty():
_, v = pq.get()
seen.add(v)
return parent_path
G = {
'A': {('C', 31)},
'B': {('C', 15), ('J', 58)},
'C': {('C', 60), ('G', 77), ('H', 40)},
'E': {('C', 17), ('E', 55), ('I', 3)},
'G': {('B', 22), ('E', 23), ('G', 31)},
'H': {('G', 66)},
'I': {('J', 70), ('K', 31)},
'J': {('H', 8), ('K', 28)},
'K': {('I', 13)}
}