forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_trees.py
More file actions
51 lines (36 loc) · 1.25 KB
/
Copy pathcount_trees.py
File metadata and controls
51 lines (36 loc) · 1.25 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
"""
Count the number of trees in a forest
A forest is a collection of trees. Do a DFS. If any vertex if not reachable
from any other, then it means it is not a part of that subgraph (tree). Hence
it is a different tree, so increment the count
"""
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.vertices = vertices
self.graph = defaultdict(list)
def add_edge(self, u, v):
self.graph[v].append(u)
self.graph[u].append(v)
def count_trees(self):
visited = [False] * self.vertices
count = 0
for s in range(self.vertices):
if not visited[s]:
visited[s] = True
stack = []
stack.append(s)
count += 1
while stack:
print(stack)
s = stack.pop()
for i in self.graph[s]:
if not visited[i]:
visited[i] = True
stack.append(i)
return count
g = Graph(5)
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(3, 4)
print('Count of trees - ', g.count_trees())