Esempio n. 1
0
static int _dfs(struct _internal_node * node,
     int (*leafcb)(LEAFTYPE),
     uint32_t (*inodecb)(struct _internal_node *),
     uint32_t direction,
     int32_t pre_post) {
    uint32_t cur = direction;

    if ((pre_post == -1) && (inodecb != NULL) && (inodecb(node) == 0)) return 0;
    if (IS_LEAF(node, cur)) {
        if (leafcb(node->child[cur].leaf) == 0)
            return 0;
    } else {
        if (_dfs(node->child[cur].node, leafcb, inodecb, direction, pre_post) == 0)
            return 0;
    }

    if ((pre_post ==  0) && (inodecb != NULL) && (inodecb(node) == 0)) return 0;
    cur = 1 - cur; /* now the other child */
    if (IS_LEAF(node, cur)) {
        if (leafcb(node->child[cur].leaf) == 0)
            return 0;
    } else {
        if (_dfs(node->child[cur].node, leafcb, inodecb, direction, pre_post) == 0)
            return 0;
    }
    if ((pre_post ==  1) && (inodecb != NULL) && (inodecb(node) == 0)) return 0;

    return 1;
}
Esempio n. 2
0
  bool _dfs(int i, const string& s, int j, const string& p) {
    //printf("i = %d, j = %d\n", i, j);
    int ns = (int)s.size(), np = (int)p.size();

    if (j >= np) {
      return i == ns;
    }

    if (j == np - 1) {
      return (i == ns - 1) && (s[i] == p[j] || p[j] == '.');
    } else if (j + 1 < np) {
      if (p[j + 1] == '*') {
        while (i < ns && (s[i] == p[j] || p[j] == '.')) {
          if (_dfs(i, s, j + 2, p)) {
            return true;
          }
          ++i;
        }
        return _dfs(i, s, j + 2, p);
      } else {
        return (s[i] == p[j] || p[j] == '.') && _dfs(i + 1, s, j + 1, p);
      }
    }

    return false;
  }
Esempio n. 3
0
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
void RB_Tree::_dfs(RB_Node * v)
{
	// DO WHAT U WANNA eg printf data->name or sth
	// i ve been using boost::function (callback handling)	but it sux (long for compile, and slower)	 
	// so i am put result to vector
	
	result[rs]=v->data;
	rs++;
	if(v->left!=NULLNODE)
		_dfs(v->left);
	if(v->right!=NULLNODE)
		_dfs(v->right);	
}
Esempio n. 4
0
void _dfs(graph_t * g, list_t * L,
          int v, std::vector<int> & low,
          std::vector<int> & dfn,
          std::stack<int> & stk,
          std::vector<int> & in_stack)
{
    low[v] = dfn[v] = ++_count;
    in_stack[v] = 1;
    stk.push(v);
    _visited[v] = 1;

    for (arc_t * p = L->_heads[v]; p; p = p->next) {
        if (_processed[p->adj]) {
            continue;
        }
        if (!in_stack[p->adj]) {
            _dfs(g,L,p->adj,low,dfn,stk,in_stack);
            low[v] = std::min(low[v], low[p->adj]);
        } else {
            low[v] = std::min(low[v], dfn[p->adj]);
        }
    }

    if (low[v] == dfn[v]) {
        int t;
        do {
            t = stk.top();
            in_stack[t] = 0;
            stk.pop();
            std::cout << g->vex(t) << ' ';
            _processed[t] = 1;
        } while (t != v);
        std::cout << '\n';
    }
}
	void run() {
		for(std::map<SmartObject *, Parametres>::iterator i = objects.begin(); i != objects.end(); ++i) {
			if(i->second.local) {
				DFS<GCollector> _dfs(this->instance());
				_dfs.start(i->first);
				std::vector<SmartObject *> tmp = _dfs.return_marked();
				for(std::vector<SmartObject *>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
					std::map<SmartObject *, Parametres>::iterator fnd = objects.find(*i);
					if(fnd == objects.end())
						throw std::runtime_error("Incorrect work of GCollector. Some object is't register.");
					fnd->second.marked = true;
				}
			}
		}
		for(std::map<SmartObject *, Parametres>::iterator i = objects.begin(); i != objects.end();) {
			if(!(*i).second.marked) {
				delete i++->first;
				//std::cout << "SmartObject in heap is deleted!\n";
			} else {
				i->second.marked = false;
				++i;
			}
		}
		//std::cout << "Mr. Proper finished!\n";
	}
Esempio n. 6
0
void dfs(int a[][6],int n, int s) { 
  //color: 0 never visited 1 being visited 2 finished visiting
  int c[n];
  init_array(c,n,0);
  _dfs(a,n,c,s);
  print_array_w_index(c,n,1);
}
Esempio n. 7
0
void _dfs (BSTREE *t, BSNODE *n, runkey_declare(pre), runkey_declare(in), runkey_declare(pos), va_list args) {
	int i;

	for (i = 0; i < n->count; ++i)
	{
		if (pre)
		{
			va_list copy;
			va_copy (copy, args);
			pre (n->pairs[i]->key, n->pairs[i]->recoffset, copy);
			va_end (copy);
		}

		if (n->children != NULL)
		{
			BSNODE *child = _bsnode_read (t, n->children[i]);
			_dfs (t, child, pre, in, pos, args);
			_bsnode_free (t, child);
		}

		if (in)
		{
			va_list copy;
			va_copy (copy, args);
			in (n->pairs[i]->key, n->pairs[i]->recoffset, copy);
			va_end (copy);
		}
	}

	if (n->children != NULL)
	{
		BSNODE *child = _bsnode_read (t, n->children[i]);
		_dfs (t, child, pre, in, pos, args);
		_bsnode_free (t, child);
	}

	if (pos)
	{
		for (i = n->count - 1; i >= 0; --i)
		{
			va_list copy;
			va_copy (copy, args);
			pos (n->pairs[i]->key, n->pairs[i]->recoffset, copy);
			va_end (copy);
		}
	}
}
Esempio n. 8
0
void __bstree_debug_dfs (BSTREE *t, runkey_declare(pre), runkey_declare(in), runkey_declare(pos), ...) {
	va_list args;
	BSNODE *n;

	va_start (args, pos);
	n = t->root;
	_dfs (t, n, pre, in, pos, args);
	va_end (args);
}
Esempio n. 9
0
void
radix_rev(struct ROOTSTRUCT * tree, int (*cb)(LEAFTYPE)) {
    if (tree->leafcount == 0) return;
    if (tree->leafcount == 1) {
        cb(tree->root.leaf);
        return;
    } /* else */
    _dfs(tree->root.node, cb, NULL, 1, 0);
}
Esempio n. 10
0
bool contains_loop(PWdgraph g) {
    int num = g->vertex_num;
    Color *c = (Color *)malloc(sizeof(Color) * num);
    memset(c, 0, sizeof(Color) * num);
    for(int i=0;i<num;i++)
        if(c[i] == WHITE)
            if(_dfs(g, i, c))
                return true;
    return false;
}
Esempio n. 11
0
void
radix_end(struct ROOTSTRUCT * tree, int (*cb)(LEAFTYPE)) {
    if (! tree->leafcount == 0) {
        if (tree->leafcount == 1) {
            cb(tree->root.leaf);
        } else {
            _dfs(tree->root.node, cb, _dealloc_internal_node, 0, 1);
        }
    }
    DEALLOC(tree);
}
Esempio n. 12
0
void _dfs(int a[][6],int n,  int *c, int s) {
  int i;
  if(c[s]!=0) {printf("found a backedge: %d\n",s+1);return;}
  c[s]=1;
  printf("visiting node %d\n", s+1);
  for(i=0;i<n;++i) { 
    if(s==i)continue;
    if(a[s][i]!=-1)
      _dfs(a,n,c,i);
  }
  c[s]=2;
}
Esempio n. 13
0
bool ids::_run(Model& m,std::vector<Node* >& solution){    
    int rs;
    int limit = 0;
    std::set<Node*> explored;

    for(;;){
        explored.clear();
        rs = _dfs(m,solution,limit,m.start_tile,explored);
        if( rs == 2){limit += 1;}        
        if( rs == 1){return false;}
        if( rs == 0){return true;}        
    }
    return false;    
}
Esempio n. 14
0
// 0 is good
// 1 is failure
// 2 is failure by cutoff
int ids::_dfs(Model& m, std::vector<Node*>& solution,
    int limit,Node* currentNode, std::set<Node*>& explored)
{
    numNodesVisited += 1;

    if(currentNode == m.goal_tile){
        // the goal tile has been found
        // add to the solutoin chain and return success
        solution.insert(solution.begin(),currentNode);
        return 0;
    }else if( limit == 0){
        // we have reach a cut-off depth
        return 2;
    }else{
        bool cutoff_occured = false;
        int rs;
        Node* cand;

        explored.insert(currentNode);
        
        // recursively explore each of the neighbours
        for(int i = 0;i < 6; ++i){
            cand = (*currentNode)[i];
            if(currentNode->canMove(i) == false){continue;}            
            if( explored.find(cand) != explored.end()){continue;}
            numGenNodes++;

            rs = _dfs(m,solution,limit-1,cand,explored);
            if( rs == 2 ){
                // cutoff failure occured
                cutoff_occured = true;
            }else if( rs == 0){                
                // we have found a solution, add to the solution chain
                // and return success
                solution.insert(solution.begin(),currentNode);
                return 0;
            }            
        }


        if( cutoff_occured){
            // a cutoff occured, and we stil haven't found a solution.
            return 2;
        }else{
            // a normal failure
            return 1;
        }
    }
    return 1;
}
Esempio n. 15
0
bool _dfs(struct wdgraph *g, int v, Color *c) {
    c[v] = GREY;
    PEdge edge = g->adjs[v];
    while(edge != NULL) {
        size_t w = edge->to;
        if(c[w] == GREY)
            return true;
        else if(c[w] == WHITE)
            if(_dfs(g, w, c))
                return true;
        edge = edge->next;
    }
    c[v] = BLACK;
    return false;
}
Esempio n. 16
0
int find_num_of_path(graph_t * g, char i, char j, int k)
{
    if (!g || g->id(i) < 0 || g->id(j) < 0 || k <= 0) {
        std::cout << "param error\n";
        return 0;
    }

    K = k;
    int C = g->vex_count();
    mx_t & mx = *g->get_structure();

    std::vector<int> visited(C, 0), path;
    int start = g->id(i), end = g->id(j);
    _dfs(g, mx, start, end, visited, path);

    return num_path;
}
Esempio n. 17
0
ConnectedComponents::ConnectedComponents(const Graph& graph)
: _count(0)
, _ids(graph.number_of_vertices())
{
	bitset_t visited(graph.number_of_vertices(), false);
	
	for (vertex_t vertex = 0; vertex < graph.number_of_vertices(); ++vertex)
	{
		if (! visited[vertex])
		{
			component_t component;
			
			_dfs(graph, vertex, visited, component, _count++);
			
			_components.push_back(component);
		}
	}
}
Esempio n. 18
0
void get_scc(graph_t * g)
{
    if (!g || !g->vex_count()) {
        return;
    }

    int C = g->vex_count();
    list_t * L = g->get_structure();
    _processed.assign(C, 0);
    _visited.assign(C, 0);

    std::stack<int> stk;
    std::vector<int> in_stack(C, 0), low(C, 0), dfn(C, 0);

    for (int i = C - 1; i >= 0; --i) {
        if (!_visited[i]) {
            _dfs(g, L, i, low, dfn, stk, in_stack);
        }
    }
}
Esempio n. 19
0
void ConnectedComponents::_dfs(const Graph& graph,
							   vertex_t vertex,
							   bitset_t& visited,
							   component_t& component,
							   id_t id)
{
	visited[vertex] = true;
	
	_ids[vertex] = id;
	
	component.push_back(vertex);
	
	for (auto adjacent : graph.adjacent(vertex))
	{
		if (! visited[adjacent.first])
		{
			 _dfs(graph, adjacent.first, visited, component, id);
		}
	}
}
Esempio n. 20
0
 // can not handle the case when there is continuous *
 bool isMatch1(string s, string p) {
   return _dfs(0, s, 0, p);
 }
Esempio n. 21
0
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
void RB_Tree::dfs()
{
	rs=0;
	_dfs(root);
	
}