示例#1
0
/*----------------------------------------------------------*/
void aStarSearch(Graph <string> & graph, int cost_type)
{
	// retrieval current nodes
	list <SocialHeuristic *> open_list;
	// store retrieved nodes
	list <SocialHeuristic *> close_list;
	// store all nodes to clean up memory
	list <SocialHeuristic *> clean_list;
	// record the cost have paid

	// cost_time_map[person_start] = 0;
	// cost_risk_map[person_start] = 0;

	SocialHeuristic * start = new SocialHeuristic(person_start, 0, 0);
	open_list.push_back(start);
	clean_list.push_back(start);
	while(!open_list.empty())
	{
		// get the first node in the list (has the smallest cost)
		SocialHeuristic * heuristic = open_list.front();
		open_list.pop_front();
		// get name and heuristic cost
		string pname = heuristic -> name;
		// push the node into close to avoid retrieval again
		close_list.push_back(heuristic);
		// push all the child node (in order)
		ArcNode * arc = graph.FirstArc(pname);
		// check whether find the target
		bool found = false;
		while(arc != 0)
		{
			string name = graph.GetValue(arc -> adjvex);
			// if not retrieved, push into open list
			if(!checkIsRetrieved(close_list, name))
			{
				int time_cost = heuristic -> time_cost + arc -> _time;
				int risk_cost = heuristic -> risk_cost + arc -> risk;
				SocialHeuristic * heuristic_child = new SocialHeuristic(name, getHeurTime(name) + time_cost, getHeurRisk(name) + risk_cost);
				heuristic_child -> time_cost = time_cost;
				heuristic_child -> risk_cost = risk_cost;
				heuristic_child -> lastNode = heuristic;
				insertHeurList(open_list, heuristic_child, cost_type);
				clean_list.push_back(heuristic_child);
				// find the target
				if(name == person_target)
				{
					found = true;
					printListPath(heuristic_child, "A-star", cost_type);
					break;
				}
			}
			arc = arc -> nextArc;
		}
		if(found)
			break;
	}
	cleanList(clean_list);
}
示例#2
0
void traversalGraph(Graph <string> & graph)
{
	printTitle("Graph");
	for(int i = 0; i < graph.vexNum; i++)
	{
		cout << graph.vertices[i].data;
		ArcNode * arc = graph.vertices[i].firstArc;
		while(arc)
		{
			cout << " -> " << graph.GetValue(arc -> adjvex);
			arc = arc -> nextArc;
		}
		cout << endl;
	}
}