forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigraph.py
More file actions
107 lines (82 loc) · 2.54 KB
/
Copy pathdigraph.py
File metadata and controls
107 lines (82 loc) · 2.54 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
96
97
98
99
100
101
102
103
104
105
106
107
"""
Directed Graph
--------------
The Digraph class represents a directed graph of vertices
which can be any hashable value. Parallel edges and self-loops are permitted.
Pseudo Code: http://algs4.cs.princeton.edu/42directed/Digraph.java.html
"""
class Digraph():
def __init__(self):
self.__adj = {}
self.__v_count = 0
self.__e_count = 0
def vertex_count(self):
"""
Returns the number of vertices in the graph.
Worst Case Complexity: O(1)
"""
return self.__v_count
def edge_count(self):
"""
Returns the number of edges in the graph.
Worst Case Complexity: O(1)
"""
return self.__e_count
def add_edge(self, src, dest):
"""
Adds an undirected edge 'src'-'dest' to the graph.
Worst Case Complexity O(1)
"""
if src in self.__adj:
self.__adj[src].append(dest)
else:
self.__adj[src] = [dest]
self.__v_count += 1
if dest in self.__adj:
pass
else:
self.__adj[dest] = []
self.__v_count += 1
self.__e_count += 1
def adj(self, src):
"""
Returns the vertices adjacent to vertex 'src'.
Worst Case Complexity: O(1)
"""
return self.__adj[src]
def outdegree(self, src):
"""
Returns the degree of the vertex 'src'
Worst Case Complexity: O(1)
"""
if src in self.__adj:
return len(self.__adj[src])
else:
raise LookupError("This vertex is not in the graph.")
def vertices(self):
"""
Returns an iterable of all the vertices in the graph.
Worst Case Complexity: O(V)
"""
return self.__adj.keys()
def reverse(self):
"""
Returns the reverse of this digraph
Worst Case Complexity: O(V+E)
"""
digraph_reversed = Digraph()
old_vertices = self.vertices()
for src in old_vertices:
for dest in self.adj(src):
digraph_reversed.add_edge(dest, src)
return digraph_reversed
def __str__(self):
s = []
s.append("{0} vertices and {1} edges \n".format(self.__v_count,
self.__e_count))
for key in self.vertices():
s.append("{0}: ".format(key))
for val in self.adj(key):
s.append("{0} ".format(val))
s.append("\n")
return "".join(s)