-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_is_tree.py
executable file
·58 lines (44 loc) · 1.07 KB
/
graph_is_tree.py
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
class Graph:
def __init__(self, V, adj = {}):
self.V = V
self.adj = adj
def add_edge(self, i, j):
if i in self.adj.keys():
self.adj[i].add(j)
else:
self.adj[i] = set([j])
def is_cyclic(self, current, visited, parent):
"""
@param current: current node number
@visited: list determining whether node x has been visited
@parent: parent node of current
"""
visited[current] = True
for i in self.adj[current]:
if not visited[i] and self.is_cyclic(i, visited, current):
return True
elif visited[i] and i != parent:
return True
return False
def is_tree(self):
visited = [False]*self.V
if self.is_cyclic(0, visited, -1):
return False
for i in range(self.V):
if not visited[i]:
return False
return True
if __name__ == '__main__':
g1 = Graph(5)
g1.add_edge(1, 0)
g1.add_edge(0, 2)
g1.add_edge(0, 3)
g1.add_edge(3, 4)
print "Is g1 a tree --> %s" % g1.is_tree()
g2 = Graph(5)
g2.add_edge(1, 0);
g2.add_edge(0, 2);
g2.add_edge(2, 1);
g2.add_edge(0, 3);
g2.add_edge(3, 4);
print "Is g2 a tree --> %s" % g2.is_tree()