-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathminimally-connected-graph.py
139 lines (122 loc) · 3.3 KB
/
minimally-connected-graph.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
# 182
Facebook
A graph is minimally-connected if it is connected and there is no edge that can be removed while still leaving the graph connected.
For example, any binary tree is minimally-connected.
Given an undirected graph, check if the graph is minimally-connected.
You can choose to represent the graph as either an adjacency matrix or adjacency list.
"""
def hasCycleHelper(adjacencyMatrix, visitedNodes):
# print(visitedNodes)
for nodeIndex in range(len(adjacencyMatrix)):
if adjacencyMatrix[visitedNodes[-1]][nodeIndex] == 1:
if nodeIndex in visitedNodes[:-2]:
# print(visitedNodes, nodeIndex)
return True
if len(visitedNodes) > 1 and nodeIndex == visitedNodes[-2]:
continue
if hasCycleHelper(adjacencyMatrix, visitedNodes+[nodeIndex]):
# print(visitedNodes, nodeIndex)
return True
return False
def hasCycle(adjacencyMatrix):
return hasCycleHelper(adjacencyMatrix, [0])
def isMinimallyConnected(adjacencyMatrix):
"""
Checking for isolated vertices, parallel edges and self loops
Max(row) == 0 => Isolated Vertex
Max(row) > 1 => Parallel Edge
Max([row[i][i]]) != 0 => Self Loop
"""
if min([max(row) for row in adjacencyMatrix]) != 1 or max([max(row) for row in adjacencyMatrix]) != 1 or max([adjacencyMatrix[i][i] for i in range(len(adjacencyMatrix))]) != 0:
return False
# Checking for cycles
return not hasCycle(adjacencyMatrix)
def main():
"""
Minimally connected graph
0
/ \
/ \
1 2
/ \
/ \
3 4
"""
adjacencyMatrix = [
# 0 1 2 3 4
[0, 1, 1, 0, 0], #0
[0, 0, 0, 1, 1], #1
[1, 0, 0, 0, 0], #2
[0, 1, 0, 0, 0], #3
[0, 1, 0, 0, 0], #4
]
print(isMinimallyConnected(adjacencyMatrix)) # True
"""
Graph with cycle
0
/ \
/ \
/ \
1 — — — 2
"""
adjacencyMatrix = [
# 0 1 2
[0, 1, 1], #0
[1, 0, 1], #1
[1, 1, 0], #2
]
print(isMinimallyConnected(adjacencyMatrix)) # False
"""
Graph with parallel edge
— — 0
| / \
| / \
| / \
1 2
"""
adjacencyMatrix = [
# 0 1 2
[0, 2, 1], #0
[2, 0, 0], #1
[1, 0, 0], #2
]
print(isMinimallyConnected(adjacencyMatrix)) # False
"""
Graph with self loop
0
/ \
/ \
/ \
1 2 — —
| |
| |
— — —
"""
adjacencyMatrix = [
# 0 1 2
[0, 1, 1], #0
[1, 0, 0], #1
[1, 0, 1], #2
]
print(isMinimallyConnected(adjacencyMatrix)) # False
"""
Graph with isolated vertex
0
/
/
1 3
\
\
2
"""
adjacencyMatrix = [
# 0 1 2 3
[0, 1, 0, 0], #0
[1, 0, 1, 0], #1
[0, 1, 0, 0], #2
[0, 0, 0, 0], #3
]
print(isMinimallyConnected(adjacencyMatrix)) # False
if __name__ == "__main__":
main()