#!/usr/bin/env python3
class UnionSet:
    def __init__(self, n):
        self.arr = []
        for i in range(n):
            self.arr.append(i)

    def query(self, i):
        while self.arr[i] != self.arr[self.arr[i]]:
            self.arr[i] = self.arr[self.arr[i]]
        return self.arr[i]

    def join(self, i, j):
        self.arr[self.query(i)] = self.query(j)

    def same(self, i, j):
        return self.query(i) == self.query(j)


def make_graph(text):
    graph = {}
    edges = []
    for line in text.split('\n'):
        val = -1
        v0 = v1 = None
        for i in range(len(line)):
            char = line[i]
            if char == ' ' or char == '-':
                continue

            if ord('0') <= ord(char) <= ord('9'):
                val = int(line[i:])
                break

            if ord('A') <= ord(char) <= ord('Z'):
                if v0 is None:
                    v0 = ord(char) - ord('A')
                elif v1 is None:
                    v1 = ord(char) - ord('A')

        if val != -1 and v0 is not None and v1 is not None:
            if v0 not in graph:
                graph[v0] = dict()
            if v1 not in graph:
                graph[v1] = dict()
            graph[v0][v1] = graph[v1][v0] = val
            edges.append((val, v0, v1))

    return graph, edges


def kruskal(edges):
    edges.sort(key=lambda x: x[0])
    us = UnionSet(30)
    for edge in edges:
        if us.same(edge[1], edge[2]):
            continue
        us.join(edge[1], edge[2])
        print(edge[0], end=' ')
    print()


def prim(graph, start):
    import queue
    todo = queue.PriorityQueue()
    done = set([start])

    for k, v in graph[start].items():
        todo.put((v, k))

    while not todo.empty():
        elem = todo.get()
        v = elem[0]
        k = elem[1]
        if k in done:
            continue
        done.add(k)
        print(v, end=' ')

        for k, v in graph[k].items():
            if k in done:
                continue
            todo.put((v, k))
    print()

q1 = '''
    A-B    17
    A-F    15
    F-B    11
    G-B     7
    C-B     5
    B-H     4
    C-H     6
    D-C     2
    I-C     1
    E-D    16
    D-J    12
    I-D     8
    J-E    13
    F-G     9
    H-G    14
    H-I     3
    I-J    10'''

q2 = '''
    A-F      16
    A-B      13
    B-G      12
    H-B       9
    F-B       6
    C-B       4
    I-C      15
    C-D       7
    H-C       1
    D-J      17
    E-D      10
    D-I       3
    E-J       8
    F-G       5
    H-G      11
    I-H      14
    J-I       2'''

q2_start = 'F'

kruskal(make_graph(q1)[1])
prim(make_graph(q2)[0], ord(q2_start) - ord('A'))

@@ -2057,59 +2057,59 @@
     
-A-B
+F-A
     1
-7
+5
 %0A    A-
-F
+B
     
-1
+ 
 5%0A    
-F-B
+A-G 
     
-11
+3
 %0A    
-G-B
+B-G
      
-7
+6
 %0A   
@@ -2121,65 +2121,65 @@
     
-5
+4
 %0A    
-B-H 
+D-C
     
-4
+17
 %0A    
-C-H
+I-C
     
- 6
+10
 %0A    
-D-C
+C-H
      
-2
+7
 %0A    
-I
+G
 -C     
-1
+2
 %0A   
@@ -2191,37 +2191,37 @@
    1
-6
+2
 %0A    D-
-J
+I
     1
-2
+1
 %0A    
-I-D
+D-J
      
-8
+9
 %0A   
@@ -2233,37 +2233,37 @@
    1
-3
+4
 %0A    
-F-G 
+G-F
     
-9
+16
 %0A    
-H-G
+G-H
     1
-4
+3
 %0A   
@@ -2267,25 +2267,25 @@
     H-I     
-3
+1
 %0A    I-J    
@@ -2284,18 +2284,19 @@
  I-J    
-10
+ 8%0A
 '''%0A%0Aq2 
@@ -2309,11 +2309,11 @@
     
-A-F
+B-A
     
@@ -2319,25 +2319,25 @@
    16%0A    A-
-B
+F
       13%0A   
@@ -2333,27 +2333,27 @@
      13%0A    
-B-G
+C-B
       12%0A   
@@ -2357,33 +2357,33 @@
     
-H-B
+B-G
       
- 9
+11
 %0A    
-F
+H
 -B      
  6%0A 
@@ -2374,32 +2374,32 @@
    H-B      
- 6
+10
 %0A    
-C
+F
 -B       4%0A 
@@ -2399,47 +2399,47 @@
     
-4
+3
 %0A    
-I-C
+C-D
       
-15
+ 6
 %0A    C-
-D
+H
        
-7
+5
 %0A    
-H
+I
 -C  
@@ -2447,105 +2447,105 @@
     
-1
+2
 %0A    D-
-J
+I
       1
-7
+5
 %0A    
-E-D
+D-E
       
-10
+ 8
 %0A    
-D-I
+E-J
        
-3
+9
 %0A    
-E-J
+I-E
        
-8
+1
 %0A    
-F-G 
+G-F
       
-5
+17
 %0A    
-H-G
+G-H
       
-11
+ 7
 %0A   
@@ -2558,26 +2558,27 @@
     
-1
+ 
 4%0A    
-J-I
+I-J
       
- 2
+14%0A
 '''%0A
@@ -2590,17 +2590,17 @@
 tart = '
-F
+D
 '%0A%0Akrusk

Improve coding style.