def npin(self): """ calculates the cardinality of each edge of the passed HyGraph instance. Input Arguments: self: a HyGraph instance Output Argument: ret: a ParVec instance with each element containing the cardinality of the corresponding edge. SEE ALSO: rank, antirank """ if self.nedge() == 0: return ParVec.zeros(self.nedge()) ret = self._spm.Reduce(pcb.pySpParMat.Row(),pcb.plus(), pcb.ifthenelse(pcb.bind2nd(pcb.not_equal_to(), 0), pcb.set(1), pcb.set(0))) return ParVec.toParVec(ret)
def k2validate(G, root, parents): ret = 1; # assume valid nrowG = G.getnrow(); # calculate level in the tree for each vertex; root is at level 0 # about the same calculation as bfsTree, but tracks levels too parents2 = pcb.pyDenseParVec(nrowG, -1); fringe = pcb.pySpParVec(nrowG); parents2[root] = root; fringe[root] = root; levels = pcb.pyDenseParVec(nrowG, -1); levels[root] = 0; level = 1; while fringe.getnee() > 0: fringe.setNumToInd(); G.SpMV_SelMax_inplace(fringe); pcb.EWiseMult_inplacefirst(fringe, parents2, True, -1); #fringe.printall(); parents2.ApplyMasked(pcb.set(0), fringe); parents2.add(fringe); levels.ApplyMasked(pcb.set(level), fringe); level += 1; # spec test #1 # Not implemented # spec test #2 # tree edges should be between verts whose levels differ by 1 #print "starting spec test#2" # root = element of parents that points to itself ##tmp1 = parents.copy() ##tmp1 -= pcb.pyDenseParVec.range(nrowG,0) ##root = tmp1.FindInds_NotEqual(0); #treeEdges = ((parents <> -1) & (parents <> root); tmp1 = parents.copy(); tmp1[root] = -1; treeEdges = tmp1.FindInds(pcb.bind2nd(pcb.not_equal_to(), -1)); #treeI = parents[treeEdges] treeI = parents.SubsRef(treeEdges); #treeJ = 1..nrowG[treeEdges] treeJ = pcb.pyDenseParVec.range(nrowG,0).SubsRef(treeEdges); #if any(levels[treeI]-levels[treeJ] <> -1): tmp1 = levels.SubsRef(treeI); tmp1 -= levels.SubsRef(treeJ); tmp2 = tmp1.FindInds(pcb.bind2nd(pcb.not_equal_to(), -1)); if tmp2.getnee(): print "spec test #2 failed." ret = -1; # spec test #3 # Not implemented # spec test #4 # Not implemented # spec test #5 # Not implemented del G, parents, parents2, fringe, levels, tmp1, tmp2, treeEdges, treeI, treeJ return ret
def degree(self): """ calculates the degree of each vertex of the passed HyGraph instance. Input Arguments: self: a HyGraph instance Output Argument: ret: a ParVec instance with each element containing the degree of the corresponding vertex. SEE ALSO: sum """ if self.nedge() == 0: return ParVec.zeros(self.nvert()) ret = self._spm.Reduce(pcb.pySpParMat.Column(),pcb.plus(), pcb.ifthenelse(pcb.bind2nd(pcb.not_equal_to(), 0), pcb.set(1), pcb.set(0))) return ParVec.toParVec(ret)