Пример #1
0
bool contains(EdgeList& backedges, const BasicBlock* src, const BasicBlock* tgt) {
  for (EdgeList::iterator E = backedges.begin(), End = backedges.end(); E != End; ++E) {
    if (E->first == src && E->second == tgt)
      return true;
  }
  return false;
}
Пример #2
0
int GraphUtil::BFSDist(Graph& g, int sid, int tid) {
	if (sid == tid) return 0;
	
	int u;
	EdgeList el;
	EdgeList::iterator eit;
	int gsize = g.num_vertices();
	vector<bool> visit = vector<bool>(gsize,false);
	vector<bool> in = vector<bool>(gsize,false);
	vector<int> sdist = vector<int>(gsize,0);
	deque<int> que;
	que.push_back(sid);
	in[sid] = true;
	sdist[sid] = 0;
	while (!que.empty()) {
		u = que.front();
		que.pop_front();
		if (!visit[u]) {
			visit[u] = true;
			el = g.out_edges(u);
			for (eit = el.begin(); eit != el.end(); eit++) {
				if (!visit[*eit] && !in[*eit]) {
					sdist[*eit] = sdist[u]+1;
					if (*eit == tid) return sdist[*eit];	
					que.push_back(*eit);
					in[*eit] = true;
				}
			}
		}
	}
	return -1; //unreachable
}
Пример #3
0
GPUGraph readDimacs(char* filename){
  FILE * fd = fopen(filename,"r");
  printf("reading file %s ...\n",filename);
  char buf[1024];
  uint64_t edges, nodes;
  EdgeList v;
  std::vector<int> weights;
  while(fgets(buf,sizeof(buf),fd)){
    if(buf[0]=='c') continue;
    if(buf[0]=='p'){
      //read nodes and edges
      char s;
      //p sp nodes edges
      sscanf(buf,"%c %c%c %lu %lu\n", &s,&s,&s, &nodes, &edges);
      printf("edges:%lu, nodes:%lu\n",edges,nodes);
    }
    if(buf[0]=='a'){
      char s;
      uint64_t src,dst,wgt;
      sscanf(buf,"%c %lu %lu %lu\n", &s,&src, &dst, &wgt);
      v.push_back(Edge(src-1,dst-1));
      weights.push_back((int)wgt);
    }
  }
  fclose(fd);
  return GPUGraph(v,weights);

}
Пример #4
0
Feature octogon(int side, int)
{
    // total number of rows is side * 3
    // top segment has side - 1 sections that increase by 2 each time
    // segment 2 has identitical sides offset 0 length side + 2*(side-1)
    // segment 3 has side -1 seconds, inverse of section 1

    EdgeList edges;
    side /= 3;
    if (side < 2) side = 2;

    for (auto i = 0; i < side - 1; i++)
    {
        edges.push_back({side-1-i,side+i*2});
    }

    for (auto i = 0; i < side; i++)
    {
        edges.push_back({0,side+2*(side-1)});
    }

    for (auto i = side - 2; i >= 0; i--)
    {
        edges.push_back({side-1-i,side+i*2});
    }
    return Feature(edges);
}
Пример #5
0
int GraphUtil::BFSTree(Graph& g, int v, ReducedGraph& tree) {
	int u;
	EdgeList el;
	EdgeList::iterator eit;
	int gsize = g.num_vertices();
	vector<bool> visit = vector<bool>(gsize,false);
	vector<bool> in = vector<bool>(gsize,false);
	deque<int> que;
	que.push_back(v);
	in[v] = true;
	tree.insert(make_pair(v,vector<int>()));
	while (!que.empty()) {
		u = que.front();
		que.pop_front();
		if (!visit[u]) {
			visit[u] = true;
			el = g.out_edges(u);
			for (eit = el.begin(); eit != el.end(); eit++) {
				if (!visit[*eit] && !in[*eit]) {
					que.push_back(*eit);
					in[*eit] = true;
					tree[u].push_back(*eit);
				}
			}
		}
	}
	
	int tcsize = 0;
	ReducedGraph::const_iterator rit;
	for (rit = tree.begin(); rit != tree.end(); rit++)
		tcsize += rit->second.size();
		
	return tcsize;
}
Пример #6
0
void Graph::linkTo(Graph::node_ptr a, Graph::node_ptr b)
{
	Edge edge = {a, b, 0};

	edges.push_back(edge);

	a->edges.push_back(&edges.back());
}
Пример #7
0
void DrawEdgeList(const EdgeList& e, const Graph& g)
{
  for (EdgeList::const_iterator it = e.begin(); it != e.end(); it++)
  {
    const GraphEdge& edge = *it;
    DrawEdge(edge, g);
  }
}
Пример #8
0
void Reader::ProcessSymbol( int i, int j, RowType& v, EdgeList& m ) const
{
   if( '*' != mInput[i][j] )
      return;

   auto p = m.insert( EdgeList::value_type( std::make_pair( i+1, j ), m.size() ) );
   v.push_back(p.first->second);
}
Пример #9
0
Feature trapezium(int top, int height)
{
    EdgeList edges;
    for (auto i = 0; i < height; i++)
    {
        edges.push_back({top-1-i,top+i*2});
    }
    return Feature(edges);
}
Пример #10
0
Feature rectangle(int width, int height)
{
    EdgeList edges;
    for (auto i = 0; i < height; i++)
    {
        edges.push_back({0,width});
    }
    return Feature(edges);
}
Пример #11
0
Feature triangle(int rows, int)
{
    EdgeList edges;
    for (auto i = 0; i < rows; i++)
    {
        edges.push_back({rows-i-1,(i+1)*2-1});
    }
    return Feature(edges);
}
Пример #12
0
static void FindSuperFamEdges(FamList &Fams, EdgeList &Edges)
	{
	const int FamCount = (int) Fams.size();

// Allocate triangular array Related[i][j], value is true
// iff families i and j are related (i.e., there is a local
// alignment between some member of i and some member of j).
	const int TriangleSize = (FamCount*(FamCount - 1))/2;
	bool *Related = all(bool, TriangleSize);
	zero(Related, bool, TriangleSize);
	for (PtrFamList p = Fams.begin(); p != Fams.end(); ++p)
		{
		FamData *Fam = *p;
		for (PtrFamData q = Fam->begin(); q != Fam->end(); ++q)
			{
			FamMemberData &FamMember = *q;
			int PileIndex = FamMember.PileIndex;
			const PileData &Pile = Piles[PileIndex];
			const int FamIndex = Pile.FamIndex;
			if (-1 == FamIndex)
				continue;
			const int ImageCount = Pile.ImageCount;
			for (int ImageIndex = 0; ImageIndex < ImageCount; ++ImageIndex)
				{
				const PileImageData &Image = Pile.Images[ImageIndex];
				const int PartnerPileIndex = Image.SIPile;
				if (PartnerPileIndex == PileIndex)
					continue;
				const PileData &PartnerPile = Piles[PartnerPileIndex];
				const int PartnerFamIndex = PartnerPile.FamIndex;
				if (-1 == PartnerFamIndex || PartnerFamIndex == FamIndex)
					continue;
				const int Index = TriangleSubscript(FamCount, FamIndex, PartnerFamIndex);
				assert(Index >- 0 && Index < TriangleSize);
				Related[Index] = true;
				}
			}
		}

	Edges.clear();
	for (int i = 0; i < FamCount; ++i)
		for (int j = i + 1; j < FamCount; ++j)
			{
			const int Index = TriangleSubscript(FamCount, i, j);
			if (Related[Index])
				{
//				Log("R %d %d\n", i, j);
				EdgeData Edge;
				Edge.Node1 = i;
				Edge.Node2 = j;
				Edge.Rev = false;
				Edges.push_back(Edge);
				}
			}
	}
Пример #13
0
void DFSCode::fromGraph(Graph& g) {
    clear();
    EdgeList edges;
    for(UINT from=0; from<g.size(); ++from)
    {
        if(!get_forward_root(g,g[from],edges))
            continue;
        for(EdgeList::iterator it = edges.begin(); it!=edges.end(); ++it)
            push(from,(*it)->to,g[(*it)->from].label,(*it)->elabel,g[(*it)->to].label);
    }
}
Пример #14
0
void OrientedGraph::permute(const Permutation& a)

/*
  This function permutes the graph according to the permutation a, according
  to the usual rule : the edges of a(x) should be the image under a of the
  edge set of x.

  As usual, permuting values is easy : it is enough to apply a to the
  elements in the various edgelists. Permuting ranges is trickier, because
  it involves a^-1.

  It is assumed of course that a holds a permutation of size size().
*/

{
  static BitMap b(0);
  static EdgeList e_buf(0);

  /* permute values */

  for (SetElt x = 0; x < size(); ++x) {
    EdgeList& e = d_edge[x];
    for (Ulong j = 0; j < e.size(); ++j) {
      e[j] = a[e[j]];
    }
  }

  /* permute ranges */

  b.setSize(size());
  b.reset();

  for (SetElt x = 0; x < size(); ++x) {
    if (b.getBit(x))
      continue;
    if (a[x] == x) { /* fixed point */
      b.setBit(x);
      continue;
    }
    for (SetElt y = a[x]; y != x; y = a[y]) {
      /* back up values for y */
      e_buf.shallowCopy(d_edge[y]);
      /* put values for x in y */
      d_edge[y].shallowCopy(d_edge[x]);
      /* store backup values in x */
      d_edge[x].shallowCopy(e_buf);
      /* set bit */
      b.setBit(y);
    }
    b.setBit(x);
  }
}
Пример #15
0
void PolyMesh::findEdgeList( Edge* e, EdgeList& elist0, EdgeList& elist1 )
{
    osg::Vec3 v0=(*e)[0], v1=(*e)[1];
    for ( EdgeMap::iterator itr=_edges.begin(); itr!=_edges.end(); ++itr )
    {
        if ( e==itr->second ) continue;

        if ( equivalent(itr->first.first,v0) || equivalent(itr->first.second,v0) )
            elist0.push_back( itr->second );
        else if ( equivalent(itr->first.first,v1) || equivalent(itr->first.second,v1) )
            elist1.push_back( itr->second );
    }
}
Пример #16
0
void GraphUtil::computePartialSD(Graph& g, vector<int>& rank, int start_ind, 
				int& end_ind, SparseVec& pdist, long tc_limit) {
	int u;
	deque<int> que;
	EdgeList el;
	EdgeList::iterator eit;
	map<int,int>::iterator mit;

	pdist.clear();
	int gsize = g.num_vertices();
	vector<bool> in = vector<bool>(gsize,false);
	vector<bool> visit = vector<bool>(gsize,false);
	int inc_count = 0;
	int sid = start_ind;
	int i = 0, j = rank[sid];
	while (true) {
		pdist.push_back(map<int,int>());
		pdist[i][j] = 0;
		que.clear();
		que.push_back(j);
		in[j] = true;
		while (!que.empty()) {
			u = que.front();
			que.pop_front();
			if (!visit[u]) {
				visit[u] = true;
				el = g.out_edges(u);
				for (eit = el.begin(); eit != el.end(); eit++) {
					if (!visit[*eit] && !in[*eit]) {
						que.push_back(*eit);
						in[*eit] = true;
						pdist[i][*eit] = pdist[i][u]+1;	
					}
				}
			}
		}
		for (mit = pdist[i].begin(); mit != pdist[i].end(); mit++) {
			in[mit->first] = false;
			visit[mit->first] = false;
		}
		i++;
		sid++;
		if (sid < rank.size()) 
			j = rank[sid];
		inc_count += pdist[i].size();
		
		if (inc_count >= tc_limit)
			break;
	}
	end_ind = sid;
}
void AspEventHandler::dfs(DFSStackElement start_elt) {
	DFSStack stack;
	push(stack, start_elt);
	while(stack.size() > 0) {
		DFSStackElement current_elt = peek(stack);
		Transaction* current_tx = current_elt.tx_;
		if(current_elt.begin_ == current_elt.end_) {
			// no out-going edges; done with current transaction; pop it
			current_tx->set_dfs_state(BLACK);
			pop(stack);

			// add tx to the topologically sorted txlist
			txlist_sorted_.push_front(current_tx);

			if(stack.size() > 0) {
				// iterate the new top to next edge
				current_elt = peek(stack);
				current_elt.begin_++;
				pop_push(stack, current_elt);
			}
			continue;
		}

		Edge e = *(current_elt.begin_);
		assert(current_tx == e.first->tx());
		Transaction* next_tx = e.second->tx();
		assert(current_tx != next_tx);

		dfs_state_t next_state = next_tx->dfs_state();
		if(next_state == WHITE) {
			next_tx->set_dfs_state(GRAY);

			EdgeList* edges = next_tx->conflict_edges();
			push(stack, DFSStackElement(next_tx, edges->begin(), edges->end()));
		} else {
			assert(next_state == BLACK || next_state == GRAY);
			// check cycle
			if(next_state == GRAY) {
				// Cycle; extract it from the current dfs stack and save it
				Cycle* cycle = extract_cycle(stack, next_tx);
				// extract_cycle may skip reporting a cycle, so check if cycle is non-null
				if(cycle != NULL) {
					cycles_.push_back(cycle);
				}
			}
			// iterate top to next edge
			current_elt.begin_++;
			pop_push(stack, current_elt);
		}
	}
}
void AspEventHandler::check_serializability() {
	assert(cycles_.empty());
	assert(txlist_sorted_.empty());

	txset_in_cycle.clear();

	for(TransactionPtrList::iterator itr = txlist_.begin(); itr != txlist_.end(); ++itr) {
		Transaction* tx = (*itr);
		if(tx->dfs_state() == WHITE) {
			EdgeList* edges = tx->conflict_edges();
			dfs(DFSStackElement(tx, edges->begin(), edges->end()));
		}
	}
}
Пример #19
0
// depth first search given a start node
void GraphUtil::dfs(Graph& g, int vid, vector<int>& preorder, vector<int>& postorder, vector<bool>& visited) {
	visited[vid] = true;
	preorder.push_back(vid);
	EdgeList el = g.out_edges(vid);
	EdgeList::iterator eit;
	int nextid = -1;
	// check whether all child nodes are visited
	for (eit = el.begin(); eit != el.end(); eit++) {
		if (!visited[*eit]) {
			nextid = *eit;
			dfs(g, nextid, preorder, postorder, visited);
		}
	}
	postorder.push_back(vid);
}
Пример #20
0
// traverse tree to label node with pre and post order by giving a start node
// using GRIPP's labeling method
void GraphUtil::traverse(Graph& tree, int vid, int& pre_post, vector<bool>& visited) {
	visited[vid] = true;
	EdgeList el = tree.out_edges(vid);
	EdgeList::iterator eit;
	int pre_order;
	for (eit = el.begin(); eit != el.end(); eit++) {
		pre_order = pre_post;
		pre_post++;
		if (!visited[*eit])
			traverse(tree, *eit, pre_post, visited);
		tree[*eit].pre_order = pre_order;
		tree[*eit].post_order = pre_post;
		pre_post++;
	}
}
Пример #21
0
static void FindEdges(const HitData &Hit, const GLIX &HitGlix, const IIX &IntervalIndex)
{
    int *QueryMatches;
    int *TargetMatches;
    const int QueryMatchCount = IntervalIndex.LookupGlobal(Hit.QueryFrom, Hit.QueryTo, &QueryMatches);
    const int TargetMatchCount = IntervalIndex.LookupGlobal(Hit.TargetFrom, Hit.TargetTo, &TargetMatches);
    const int MatchCount = QueryMatchCount + TargetMatchCount;
    int *Matches = all(int, MatchCount);
    memcpy(Matches, QueryMatches, QueryMatchCount*sizeof(int));
    memcpy(Matches + QueryMatchCount, TargetMatches, TargetMatchCount*sizeof(int));

    for (int i = 0; i < MatchCount; ++i)
    {
        const int m = Matches[i];
        if (m < 0 || m > CandCount)
            Quit("m=%d count=%d", m, CandCount);
    }

    for (int i = 0; i < MatchCount; ++i)
    {
        const int CandIndex1 = Matches[i];
        for (int j = 0; j < i; ++j)
        {
            const int CandIndex2 = Matches[j];
            if (IsEdge(HitGlix, Hit, CandIndex1, CandIndex2))
            {
                EdgeData Edge;
                Edge.Node1 = CandIndex1;
                Edge.Node2 = CandIndex2;
                Edge.Rev = Hit.Rev;
                Edges.push_back(Edge);
            }
        }
    }
}
Пример #22
0
/**
 * toEdgeList() - converts graph (adjacency list) to edge list
 * @param g - edge list for this graph 
 */
void AdjList::toEdgeList(EdgeList &g)
{
    g.maxVertexNum = maxVertexNum;
    g.infinity = infinity;
    g.directed = directed;
    for (unsigned i = 0; i < G.size(); ++i)
    {
        for (int j = 0; j < G[i].size(); ++j)
        {
            if (!directed && i < G[i][j].second)
                g.addEdge(Edge(i, G[i][j].second, G[i][j].first), false);
            else if (directed)
                g.addEdge(Edge(i, G[i][j].second, G[i][j].first), false);
        }
    }
}
Пример #23
0
void EdgeCollector::getBoundaryEdgeList(EdgeList & el)
{
    for (EdgeSet::iterator it = _edgeSet.begin(), end = _edgeSet.end(); it != end; ++it)
    {
        if ((*it)->isBoundaryEdge()) el.push_back(*it);
    }
}
Пример #24
0
bool ActiveEdges::Compute(const EdgeList& edges, const IndexedTriangle* faces, const PxVec3* verts, float epsilon)
{
	// Checkings
	if(!faces || !verts)	return SetIceError("ActiveEdges::ComputeConvexEdges: NULL parameter!");

	PxU32 NbEdges = edges.GetNbEdges();
	if(!NbEdges)			return SetIceError("ActiveEdges::ComputeConvexEdges: no edges in edge list!");

	const Edge* Edges = edges.GetEdges();
	if(!Edges)				return SetIceError("ActiveEdges::ComputeConvexEdges: no edge data in edge list!");

	const EdgeDesc* ED = edges.GetEdgeToTriangles();
	if(!ED)					return SetIceError("ActiveEdges::ComputeConvexEdges: no edge-to-triangle in edge list!");

	const PxU32* FBE = edges.GetFacesByEdges();
	if(!FBE)				return SetIceError("ActiveEdges::ComputeConvexEdges: no faces-by-edges in edge list!");

	PX_FREE_AND_RESET(mActiveEdges);
	mActiveEdges = (bool*)ICE_ALLOC_MEM(sizeof(bool)*NbEdges,EdgeList_ActiveEdges);

	// Loop through edges and look for convex ones
	bool* CurrentMark = mActiveEdges;
	while(NbEdges--)
	{
		// Get number of triangles sharing current edge
		PxU32 Count = ED->Count;
		// Boundary edges are active => keep them (actually they're silhouette edges directly)
		// Internal edges can be active => test them
		// Singular edges ? => discard them
		bool Active = false;
		if(Count==1)
		{
			Active = true;
		}
		else if(Count==2)
		{
			PxU32 Op = faces[FBE[ED->Offset+0]].OppositeVertex(Edges->mRef0, Edges->mRef1);
			Plane PL1 = faces[FBE[ED->Offset+1]].PlaneEquation(verts);
			if(PL1.Distance(verts[Op])<-epsilon)	Active = true;
		}

		*CurrentMark++ = Active;
		ED++;
		Edges++;
	}
	return true;
}
Пример #25
0
void PolyMesh::findEdgeList( osg::Vec3 p, EdgeList& elist )
{
    for ( EdgeMap::iterator itr=_edges.begin(); itr!=_edges.end(); ++itr )
    {
        if ( equivalent(itr->first.first,p) || equivalent(itr->first.second,p) )
            elist.push_back( itr->second );
    }
}
Пример #26
0
int GraphUtil::topo_level(Graph& g, int vid){
	if(g[vid].top_level != -1){
		return g[vid].top_level;
	}
	int min = g.num_vertices();	
	int max = -1;
	g[vid].top_level = 0;
	EdgeList el = g.in_edges(vid);
	EdgeList::iterator eit;
	for(eit = el.begin(); eit != el.end(); eit++){
		max = max > topo_level(g,*eit) ? max : g[*eit].top_level;
		min = min < g[*eit].top_level ? min : g[*eit].top_level;
	}
	g[vid].top_level = max + 1;
	g[vid].min_parent_level = (min == g.num_vertices() ? -1 : min );
	return g[vid].top_level;
}
Пример #27
0
void WGraph::print(FILE* file, const Interface& I) const

/*
  Prints the graph on a file in ascii format.
*/

{
  const OrientedGraph& Y = *d_graph;

  int d = digits(size()-1,10);

  /* count number of edges */

  Ulong count = 0;

  for (Vertex x = 0; x < size(); ++x) {
    const EdgeList& e = Y.edge(x);
    count += e.size();
  }

  // find alignement

  String str(0);
  LFlags f = leqmask[I.rank()-1];
  interface::append(str,f,I);
  Ulong descent_maxwidth = str.length();

  fprintf(file,"%lu vertices, %lu edges\n\n",size(),count);

  for (Vertex x = 0; x < size(); ++x) {
    fprintf(file,"%*lu : ",d,x);
    io::reset(str);
    interface::append(str,descent(x),I);
    pad(str,descent_maxwidth );
    io::print(file,str);
    fprintf(file," ");
    const EdgeList e = Y.edge(x);
    const CoeffList c = coeffList(x);
    for (Ulong j = 0; j < e.size(); ++j) {
      fprintf(file,"%lu(%lu)",e[j],static_cast<Ulong>(c[j]));
      if (j+1 < e.size()) /* there is more to come */
	fprintf(file,",");
    }
    fprintf(file,"\n");
  }
}
Пример #28
0
void OrientedGraph::levelPartition(Partition& pi) const

/*
  Assuming the graph has no oriented cycles, this function writes in pi the 
  partition of the vertices according to their level, where sinks have level
  0, then sinks in the remaining poset have level one, etc.

  NOTE : the implementation is simple-minded : we traverse the graph as many
  times as there are levels.
*/

{
  static BitMap b(0);
  static BitMap b1(0);

  b.setSize(size());
  b.reset();
  b1.setSize(size());
  b1.reset();
  pi.setSize(size());
  Ulong count = 0;
  Ulong current_level = 0;

  while (count < size()) {
    for (SetElt x = 0; x < size(); ++x) {
      if (b.getBit(x))
	continue;
      const EdgeList e = d_edge[x];
      for (Ulong j = 0; j < e.size(); ++j) {
	if (!b.getBit(e[j])) /* next x */
	  goto nextx;
      }
      /* i we get here, x is the next element in the permutation */
      pi[x] = current_level;
      b1.setBit(x);
      ++count;
    nextx:
      continue;
    }
    b.assign(b1);
    current_level++;
  }

  pi.setClassCount(current_level);
  return;
}
Пример #29
0
Feature diamond(int rows, int)
{
    // the offset starts at rows
    // the rows are (i+1)*2 -1 wide
    EdgeList edges;
    for (auto i = 0; i < rows / 2; i++)
    {
        edges.push_back({rows/2-i-1,(i+1)*2-1});
    }

    // bottom half of diamond
    for (auto i = rows / 2 - 1; i >= 0; i--)
    {
        edges.push_back({rows / 2 - i - 1,(i+1)*2-1});
    }
    return Feature(edges);
}
Пример #30
0
void PolyMesh::findEdgeList( Face* f, EdgeList& elist )
{
    unsigned int size = f->_pts.size();
    for ( unsigned int i=0; i<size; ++i )
    {
        Edge* edge = getEdge( (*f)[i%size], (*f)[(i+1)%size], _edges );
        if ( edge ) elist.push_back( edge );
    }
}