Esempio n. 1
0
	def bfsTree(self, root):
		"""
		calculates a breadth-first search tree from the edges in the
		passed HyGraph, starting from the root vertex.  "Breadth-first"
		in the sense that all vertices reachable in step i are added
		to the tree before any of the newly-reachable vertices' reachable
		vertices are explored.  The returned tree (implied in the parent
		relationships) consists of simple edges, not the original 
		hyperedges of the HyGraph instance.

		Input Arguments:
			root:  an integer denoting the root vertex for the tree

		Input Arguments:
			parents:  a ParVec instance of length equal to the number
			    of vertices in the HyGraph, with each element denoting 
			    the vertex number of that vertex's parent in the tree.
			    The root is its own parent.  Unreachable vertices
			    have a parent of -1. 

		SEE ALSO: isBfsTree 
		"""
		parents = pcb.pyDenseParVec(self.nvert(), -1)
		fringeV = pcb.pySpParVec(self.nvert())
		parents[root] = root
		fringeV[root] = root
		while fringeV.getnee() > 0:
			fringeV.setNumToInd()
			fringeE = self._spm.SpMV_SelMax(fringeV)
			fringeV = self._spmT.SpMV_SelMax(fringeE)
			pcb.EWiseMult_inplacefirst(fringeV, parents, True, -1)
			parents[fringeV] = 0
			parents += fringeV
		return ParVec.toParVec(parents)
Esempio n. 2
0
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
Esempio n. 3
0
	c = Cands[i]
	#try:
	#	print "c=%d"%(c)
	#except TypeError:
	#	print "------------------------------ TYPE ERROR on c! setting c=0"
	#	c = 0
	#	print "i=%d, len(Cands)=%d"%(i, len(Cands))
	#	Cands.printall()
	#	Cands.sparse().printall()
	
	# start the clock
	tstart = time.time()
	#------------------------- TIMED --------------------------------------------
	
	parents = pcb.pyDenseParVec(n, -1);
	fringe = pcb.pySpParVec(n)
	fringe[c] = c;
	parents[c] = c;
	
	if debugprint:
		if (pcb.root()):
			print "start fringe:" 
		fringe.printall()
	
	niter = 0
	while (fringe.getnee() > 0):
		if debugprint:
			print "----- on iteration %d"%(niter+1) 
		fringe.setNumToInd()
		if debugprint:
			print "fringe at start of iteration"