|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import sys |
5 | | -from collections import defaultdict |
| 5 | +from data_structures.graph_no_vertice_object import Graph |
| 6 | +from data_structures.partition import Partition |
6 | 7 |
|
7 | | -class Partition: |
8 | | - class Position: |
9 | | - __slots__ = '_container', '_element', '_size', '_parent' |
10 | | - |
11 | | - def __init__(self, container, e): |
12 | | - self._container = container # reference to Partition instance |
13 | | - self._element = e |
14 | | - self._size = 1 |
15 | | - self._parent = self # convention for a group leader |
16 | | - |
17 | | - def element(self): |
18 | | - return self._element |
19 | | - |
20 | | - def _validate(self, p): |
21 | | - if not isinstance(p, self.Position): |
22 | | - raise TypeError('p must be proper Position type') |
23 | | - if p._container is not self: |
24 | | - raise ValueError('p does not belong to this container') |
25 | | - |
26 | | - def make_group(self, e): |
27 | | - return self.Position(self, e) |
28 | | - |
29 | | - def find(self, p): |
30 | | - self._validate(p) |
31 | | - if p._parent != p: |
32 | | - p._parent = self.find(p._parent) # overwrite p._parent after recursion |
33 | | - return p._parent |
34 | | - |
35 | | - def union(self, p, q): |
36 | | - a = self.find(p) |
37 | | - b = self.find(q) |
38 | | - if a is not b: # only merge if different groups |
39 | | - if a._size > b._size: |
40 | | - b._parent = a |
41 | | - a._size += b._size |
42 | | - else: |
43 | | - a._parent = b |
44 | | - b._size += a._size |
45 | | - |
46 | | -class Graph: |
47 | | - class Edge: |
48 | | - __slots__ = '_origin', '_destination' |
49 | | - |
50 | | - def __init__(self, u, v): |
51 | | - self._origin = u |
52 | | - self._destination = v |
53 | | - |
54 | | - def endpoints(self): |
55 | | - return self._origin, self._destination |
56 | | - |
57 | | - def opposite(self, v): |
58 | | - return self._destination if v == self._origin else self._origin |
59 | | - |
60 | | - def __hash__(self): # will allow edge to be a map/set key |
61 | | - return hash((self._origin, self._destination)) |
62 | | - |
63 | | - def __str__(self): |
64 | | - return '({0},{1})'.format(self._origin, self._destination) |
65 | | - |
66 | | - def __init__(self): |
67 | | - self._outgoing = defaultdict(dict) |
68 | | - |
69 | | - def vertices(self): |
70 | | - return self._outgoing.keys() |
71 | | - |
72 | | - def get_edge(self, u, v): |
73 | | - return self._outgoing[u].get(v) # returns None if v not adjacent |
74 | | - |
75 | | - def incident_edges(self, v): |
76 | | - adj = self._outgoing |
77 | | - for edge in adj[v].values(): |
78 | | - yield edge |
79 | | - |
80 | | - def insert_edge(self, u, v): |
81 | | - if self.get_edge(u, v) is not None: # includes error checking |
82 | | - raise ValueError('u and v are already adjacent') |
83 | | - e = self.Edge(u, v) |
84 | | - self._outgoing[u][v] = e |
85 | | - self._outgoing[v][u] = e |
86 | | -# |
87 | | -# Complete the componentsInGraph function below. |
88 | | -# |
89 | 8 | def componentsInGraph(gb): |
90 | 9 | forest = Partition() |
91 | 10 | p = {} |
|
0 commit comments