def partition(partitionSize,G,k): # print(nx.adjacency_matrix(G,nodelist = ['c','e']).todense()) A = nx.adjacency_matrix(G,nodelist=k) # Alpah cut M = Handler.alphaCut(A, 1) # eigen calculatio eigenvalues, eigenvectors = np.linalg.eig(M) # tempEigenValues = np.absolute(eigenvalues) tempEigenValues = eigenvalues idx = tempEigenValues.argsort()[:partitionSize][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:, idx] z = eigenVectors # normalize the matrix for i in range(0, eigenVectors.shape[0]): total = 0 for j in range(0, eigenVectors.shape[1]): total += abs(eigenVectors.item((i, j))) ** 2 if (total > 0): z[i] = z[i] / (total ** (1 / 2)) # z = np.matrix.transpose(z) # find k means paritions kmeans = KMeans(n_clusters=partitionSize, random_state=0).fit(z) lables = kmeans.labels_ array = Handler.indexArray(G.nodes(), partitionSize, lables) return array
def partition(partitionSize, G): # print(nx.adjacency_matrix(G,nodelist = ['c','e']).todense()) partitionNodeArray = [] for a in G.nodes(): partitionNodeArray.append(str(a).split("+")) H = nx.read_graphml("../Osmnx_large_trips_graph_75000.graphml") print("Done partition array") adjMat, pickMat = Handler.adjecencyMatrixes(partitionNodeArray,H,'trips') with open('matrix_alpha.csv', 'w') as csvfile: writer = csv.writer(csvfile) [writer.writerow(r) for r in adjMat] with open('pickMatrix_alpha.csv', 'w') as csvfile: writer = csv.writer(csvfile) [writer.writerow(r) for r in pickMat] # with open('matrix_alpha.csv', 'r') as csvfile: # reader = csv.reader(csvfile) # adjecencyMatrix = [[float(e) for e in r] for r in reader] A = np.array(pickMat) # Alpah cut M = Handler.alphaCut(A) # eigen calculatio eigenvalues, eigenvectors = np.linalg.eig(M) # tempEigenValues = np.absolute(eigenvalues) tempEigenValues = eigenvalues idx = tempEigenValues.argsort()[:partitionSize][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:, idx] z = eigenVectors # normalize the matrix for i in range(0, eigenVectors.shape[0]): total = 0 for j in range(0, eigenVectors.shape[1]): total += abs(eigenVectors.item((i, j))) ** 2 if (total > 0): z[i] = z[i] / (total ** (1 / 2)) # z = np.matrix.transpose(z) # find k means paritions kmeans = KMeans(n_clusters=partitionSize, random_state=0).fit(z) lables = kmeans.labels_ array = Handler.indexArray(G.nodes(), partitionSize, lables) return array
def partitioning(G, k): A = nx.adjacency_matrix(G) # Alpah cut M = Handler.alphaCut(A, 1) # eigen calculation eigenvalues, eigenvectors = np.linalg.eig(M) # define K partitionSize = k # tempEigenValues = np.absolute(eigenvalues) tempEigenValues = eigenvalues idx = tempEigenValues.argsort()[:partitionSize][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:, idx] z = eigenVectors # normalize the matrix for i in range(0, eigenVectors.shape[0]): total = 0 for j in range(0, eigenVectors.shape[1]): total += abs(eigenVectors.item((i, j)))**2 if (total > 0): z[i] = z[i] / (total**(1 / 2)) # z = np.matrix.transpose(z) # find k means paritions kmeans = KMeans(n_clusters=partitionSize, random_state=0).fit(z) lables = kmeans.labels_ array = Handler.indexArray(G.nodes(), partitionSize, lables) np.savetxt('test_1.txt', array, fmt='%r') print(lables) # New partition array partitionArray = [] # get each laplacian matrix for k in array: # sort = tempEigenvalues if (len(k) > 1): print(k) H = G.subgraph(k) r = nx.connected_components(H) for i in r: partitionArray.append(i) else: partitionArray.append(k) matrix, edgecut1 = Handler.conectivityMatrix(partitionArray, G) print(edgecut1) edgecut2 = 0 q = queue.Queue() partitionQueue = queue.Queue() partitionQueue.put(partitionArray) q.put(matrix) alpha = Handler.alphaCut(matrix, 0) partitionCount = 1 part = [] part.append(partitionArray) while (partitionCount != partitionSize): if (q.empty() is False): matrix = q.get() if (matrix.shape[0] > 1): alpha = Handler.alphaCut(matrix, 0) eigenvalues, eigenvectors = np.linalg.eig(alpha) tempEigenValues = np.absolute(eigenvalues) idx = tempEigenValues.argsort()[:2][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:, idx] z = eigenVectors # normalize the matrix for i in range(0, eigenVectors.shape[0]): total = 0 for j in range(0, eigenVectors.shape[1]): total += abs(eigenVectors.item((i, j)))**2 if (total > 0): z[i] = z[i] / (total**(1 / 2)) # norm = np.linalg.norm(z,axis=1,ord=2) # print(norm) # z = z.astype(np.float)/norm[:,None] # print(z) # find k means paritions kmeans = KMeans(n_clusters=2, random_state=0).fit(z) w = 0 p1, p2 = [], [] partition = partitionQueue.get() for p in kmeans.labels_: if (p == 0): p1.append(partition[w]) else: p2.append(partition[w]) w += 1 put1, tempedge1 = Handler.conectivityMatrix(p1, G) put2, tempedge2 = Handler.conectivityMatrix(p2, G) edgecut2 += tempedge1 + tempedge2 part.pop(0) if (len(p1) >= len(p2)): partitionQueue.put(p2) partitionQueue.put(p1) q.put(put2) q.put(put1) part.append(p2) part.append(p1) else: partitionQueue.put(p1) partitionQueue.put(p2) q.put(put1) q.put(put2) part.append(p1) part.append(p2) partitionCount += 1 partition = [] for p in part: partTemp = [] for par in p: for part in par: partTemp.append(part) partition.append(partTemp) return partition
G.add_edge('c','f',weight=0.2) G.add_edge('f','d',weight=0.7) G.add_edge('e','f',weight=0.8) G.add_edge('e','d',weight=0.6) G.add_edge('f','g',weight=1) G.add_edge('g','h',weight=0.1) G.add_edge('e','h',weight=1) """ #print(nx.adjacency_matrix(G,nodelist = ['c','e']).todense()) A = nx.adjacency_matrix(G) #Alpah cut M = Handler.alphaCut(A,1) #eigen calculation eigenvalues, eigenvectors = np.linalg.eig(M) #define K partitionSize=4 tempEigenValues = np.absolute(eigenvalues) idx = tempEigenValues.argsort()[:partitionSize][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:,idx] z = eigenVectors #normalize the matrix