예제 #1
0
void uniform_cost_search(graph <string> & _graph, string _start, string _target, int _cost_type)
{
	int _start_num = _graph.find_node(_start);
	if(_start_num == -1)
		return;
	if(_start == _target)
	{
		cout << "Find " << _target << " in UCS:" << endl;
		return;
	}
	// using stack to clean memory
	stack <fcost *> _clean = stack <fcost *> ();
	fcost * _fcost = new fcost(_start, 0);
	_clean.push(_fcost);
	// using open list and closed list to implement Uniform-Cost Search
	list <fcost *> _open = list <fcost *> ();
	_open.push_back(_fcost);
	list <fcost *> _closed = list <fcost *> ();
	while(!_open.empty())
	{
		fcost * _currnode = _open.front();
		_open.pop_front();
		if(_currnode -> _name == _target)
			cout << "Find " << _currnode -> _name << " in UCS:(" << (_cost_type == 0 ? "Time" : "Risk") << ")" << endl;
		int _currnode_num = _graph.find_node(_currnode -> _name);
		int _currnode_cost = _currnode -> _cost;
		graph_arc <string> * _arc_child = _graph.first_arc(_currnode_num);
		queue <graph_arc <string> *> _children = queue <graph_arc <string> *> ();
		while(_arc_child)
		{
			_children.push(_arc_child);
			_arc_child = _graph.next_arc(_arc_child);
		}
		while(!_children.empty())
		{
			_arc_child = _children.front();
			_children.pop();
			bool _in_open = exist_list(_open, _arc_child -> _data);
			bool _in_closed = exist_list(_closed, _arc_child -> _data);
			int _cost_child = _currnode_cost + cost(_arc_child, _cost_type);
			if(!_in_open && !_in_closed)
				sort_insert_list(_open, _arc_child -> _data, _cost_child, _clean);
			else if(_in_open)
				replace_insert_list(_open, _arc_child -> _data, _cost_child, _clean);
			else if(_in_closed) // really need this step?
				replace_insert_list(_open, _closed, _arc_child -> _data, _cost_child, _clean);
		}
		_closed.push_back(_currnode);
		if(debug)
		{
			print_list(_open, "Open");
			print_list(_closed, "Closed");
		}
	}
	print_cost_path(_graph, _closed, _target, _cost_type);
	clean(_clean);
}
예제 #2
0
void depth_first_search(graph <string> & _graph, string _start, string _target)
{
	int _start_num = _graph.find_node(_start);
	if(_start_num == -1)
		return;
	// set the visited table, avoid visiting repeat
	map <string, int> _visited;
	initial_visited(_graph, _visited);
	_visited[_start]++;
	if(_start == _target)
	{
		cout << "Find " << _target << " in DFS:" << endl;
		return;
	}
	// using stack to clean memory
	stack <fpath *> _clean = stack <fpath *> ();
	// using fpath to trace the path through friend
	fpath * _fpath = new fpath(_start);
	_clean.push(_fpath);
	// using stack to implement DFS
	stack <fpath *> _stack = stack <fpath *> ();
	_stack.push(_fpath);
	while(!_stack.empty())
	{
		fpath * _fcur = _stack.top();
		_stack.pop();
		string _cur = _fcur -> _name;
		int _cur_num = _graph.find_node(_cur);
		graph_arc <string> * _arc = _graph._nodes[_cur_num]._first_arc;
		while(_arc)
		{
			string _cur_sub = _arc -> _data;
			if(_visited[_cur_sub] == 0)
			{
				_visited[_cur_sub]++;
				fpath * _fpath_sub = new fpath(_cur_sub);
				_stack.push(_fpath_sub);
				_fpath_sub -> _last = _fcur;
				if(_cur_sub == _target)
				{
					cout << "Find " << _cur_sub << " in DFS:" << endl;
					print_path(_fpath_sub, DFS);
					return;
				}
				_stack.push(_fpath_sub);
				if(debug)
					print_visited(_graph, _visited);
			}
			_arc = _arc -> _next_arc;
		}
	}
	clean(_clean);
	error("Cannot find " + _target);
}
예제 #3
0
void parse_string(const string& s, graph& ret)
{
  vector<string> v = split_string(s);

  if (ret.find_node(v[0]) == ret.nodes.end())
    ret.nodes.emplace_front(node{ v[0] });

  for (size_t i = 2; i < v.size(); i++)
  {
    if (ret.find_node(v[i]) == ret.nodes.end())
      ret.nodes.emplace_front(node{ v[i] });

    auto p = ret.find_node(v[0]);
    p->output_edges.emplace_back(edge{"", ret.find_node(v[i]), false});
  }

}