-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.py
112 lines (97 loc) · 2.97 KB
/
cluster.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
from __future__ import annotations
import json
import math
from pydoc import cli
from typing import List
import numpy as np
class Graph():
def __init__(self,
nodecount : None):
self.nodecount = nodecount
# IMPORTANT!!!
# Replace the next line so the Laplacian is a nodecount x nodecount array of zeros.
# You will need to do this in order for the code to run!
self.laplacian = np.zeros((nodecount, nodecount), dtype = float)
# Add an edge to the Laplacian matrix.
# An edge is a pair [x,y].
def addedge(self,edge):
self.laplacian[edge[0]][edge[1]] = -1
self.laplacian[edge[1]][edge[0]] = -1
self.laplacian[edge[0]][edge[0]] += 1
self.laplacian[edge[1]][edge[1]] += 1
# Nothing to return.
# Don't change this - no need.
def laplacianmatrix(self) -> np.array:
return self.laplacian
# Calculate the Fiedler vector and return it.
# You can use the default one from np.linalg.eig
# but make sure the first entry is positive.
# If not, negate the whole thing.
def fiedlervector(self) -> np.array:
# Replace this next line with your code.
eigenValues, eigenVector = np.linalg.eig(self.laplacian)
value = 0
#print(eigenValues)
eigenV = np.argsort(eigenValues)
value = eigenV[1]
fvec = eigenVector[ : , value]
if fvec[0] < 0:
fvec = -1 * fvec
# Return
return fvec
# Cluster the nodes.
# You should return a list of two lists.
# The first list contains all the indices with nonnegative (positive and 0) Fiedler vector entry.
# The second list contains all the indices with negative Fiedler vector entry.
def clustersign(self):
# Replace the next two lines with your code.
pind = []
nind = []
list = self.fiedlervector()
for i in range(len(list)):
if list[i] <= 0:
nind.append(i)
else:
pind.append(i)
# Return
return([pind,nind])
'''
cluster = Graph(7)
cluster.addedge([0,5])
cluster.addedge([0,2])
cluster.addedge([2,5])
cluster.addedge([2,1])
cluster.addedge([1,4])
cluster.addedge([4,6])
cluster.addedge([6,3])
cluster.addedge([3,1])
print(cluster.laplacianmatrix())
print(cluster.fiedlervector())
print("-------")
print(cluster.clustersign())
cluster = Graph(10)
cluster.addedge([0,1])
cluster.addedge([0,3])
cluster.addedge([0,5])
cluster.addedge([0,6])
cluster.addedge([0,8])
cluster.addedge([1,2])
cluster.addedge([1,3])
cluster.addedge([1,8])
cluster.addedge([2,4])
cluster.addedge([2,5])
cluster.addedge([2,7])
cluster.addedge([2,8])
cluster.addedge([2,9])
cluster.addedge([3,5])
cluster.addedge([3,6])
cluster.addedge([3,8])
cluster.addedge([4,5])
cluster.addedge([4,7])
cluster.addedge([4,9])
cluster.addedge([5,7])
cluster.addedge([5,9])
cluster.addedge([6,8])
cluster.addedge([7,9])
print(cluster.fiedlervector())
'''