-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApproximation_Graph.py
More file actions
95 lines (79 loc) · 3.27 KB
/
Copy pathApproximation_Graph.py
File metadata and controls
95 lines (79 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Author: Jingze Dai
# Email Address: daij24@mcmaster.ca or david1147062956@163.com
# Github: https://github.com/daijingz
# Linkedin: https://www.linkedin.com/in/jingze-dai/
# Description: Undirected graph with approximation method (To find algorithms)
from collections import defaultdict
class Approximation_Graph:
def __init__(self):
"""!
@return a graph object with bodies and nodes
"""
self.vertices = [] # Time Complexity: O(1)
self.graph = defaultdict(list) # Time Complexity: O(1)
# Constructor Total Time Complexity: O(1) + O(1) = O(1)
def get_vertices(self):
"""! Get vertices values of an object"""
try:
return self.vertices # Time Complexity: O(1)
except:
raise Exception()
# Method Total Time Complexity: O(1)
def get_graph(self):
"""! Get graph body values of an object"""
try:
return self.graph # Time Complexity: O(1)
except:
raise Exception()
# Method Total Time Complexity: O(1)
def __str__(self):
"""! Returns the string of all vertices"""
try:
return str(self.vertices)
except:
raise Exception()
# Method Total Time Complexity: O(1)
def __len__(self):
"""! Returns the length of all vertices"""
try:
return len(self.vertices)
except:
raise Exception()
# Method Total Time Complexity: O(1)
def __eq__(self, other):
"""! Compare whether 2 graph objects are equal"""
if isinstance(other, Approximation_Graph):
if self.vertices != other.vertices:
return False
elif self.graph != other.graph:
return False
return True
return False
def addEdge(self, vertex1, vertex2):
"""! Add a new edge to a new graph object"""
try:
if self.vertices.count(vertex1) == 0: # Time Complexity: O(1)
self.vertices += [vertex1]
if self.vertices.count(vertex2) == 0: # Time Complexity: O(1)
self.vertices += [vertex2]
self.graph[vertex1].append(vertex2) # Time Complexity: O(1)
self.graph[vertex2].append(vertex1) # Time Complexity: O(1)
except:
raise Exception()
# Method Total Time Complexity: O(1) + O(1) + O(1) + O(1) = O(1)
def FindVertexCover(self):
"""! Find the smallest vertex cover of an undirected graph"""
Visited = [False] * len(self.vertices) # Initially all nodes have not been visited
Output = [] # Output shows the vertex set of vertex cover
for vertex in range(len(self.vertices)):
if not Visited[vertex]:
# Select edges that both connected vertices have not been visited
# Reachable edges are ignored
for edge in self.graph[vertex]:
if not Visited[edge]:
Visited[edge] = True
Visited[vertex] = True
# Collect vertex to output list
Output += [vertex]
return Output # Time Complexity: O(1)
# Method Total Time Complexity: O(V + E)