forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_edges.py
More file actions
42 lines (31 loc) · 728 Bytes
/
Copy pathcount_edges.py
File metadata and controls
42 lines (31 loc) · 728 Bytes
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
# Use handshaking lemma
# deg(v) = 2|E|
# Time - O(V)
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [[] for i in range(vertices)]
def add_edge(self, u, v):
self.graph[u].append(v)
self.graph[v].append(u)
def count_edges(self):
s = 0
for i in range(self.V):
s += len(self.graph[i])
return s // 2
g = Graph(9)
g.add_edge(0, 1 )
g.add_edge(0, 7 )
g.add_edge(1, 2 )
g.add_edge(1, 7 )
g.add_edge(2, 3 )
g.add_edge(2, 8 )
g.add_edge(2, 5 )
g.add_edge(3, 4 )
g.add_edge(3, 5 )
g.add_edge(4, 5 )
g.add_edge(5, 6 )
g.add_edge(6, 7 )
g.add_edge(6, 8 )
g.add_edge(7, 8 )
print(g.count_edges())