void AffinityTask::DFS(std::list<std::list<GraphNode>>& saveAt, const std::unordered_map<CPUID, TaskSet>& cpuToTaskList,
			const std::unordered_map<AffinityTask*, Affinity>& taskToCPUList,
			const GraphNode& start, const GraphNode& target, const std::list<GraphNode>& visited)
{
	std::list<GraphNode> currentVisited(visited);
	currentVisited.push_back(start);
	if(start.isTask() == target.isTask())
	{
		if(start.isTask())
			if(target.getTask() == start.getTask())
			{
				saveAt.push_back(currentVisited);
				return;
			}
		if(!start.isTask())
			if(target.getCPUID() == start.getCPUID())
			{
				saveAt.push_back(currentVisited);
				return;
			}
	}

	if(start.isTask())
	{
		auto iter = taskToCPUList.find(start.getTask());
		if(iter == taskToCPUList.end())
			return;
		for(CPUID adjacentCPU : iter->second) //u ← G.adjacentVertex(t,e)
		{
			bool ifVisited = false;
			for(auto item : visited)
			{
				if(!item.isTask())
				{
					if(item.getCPUID() == adjacentCPU)
					{
						ifVisited = true;
						break;
					}
				}
			}
			if(ifVisited)
				continue;
			DFS(saveAt, cpuToTaskList, taskToCPUList,
					adjacentCPU, target, currentVisited);
		}
	}
	else
	{
		auto iter = cpuToTaskList.find(start.getCPUID());
		if(iter == cpuToTaskList.end())
			return;
		for(AffinityTask* adjacentTask : iter->second) //u ← G.adjacentVertex(t,e)
		{
			bool ifVisited = false;
			for(auto item : visited)
			{
				if(item.isTask())
				{
					if(item.getTask() == adjacentTask)
					{
						ifVisited = true;
						break;
					}
				}
			}
			if(ifVisited)
				continue;
			DFS(saveAt, cpuToTaskList, taskToCPUList,
					adjacentTask, target, currentVisited);
		}
	}
}
std::list<GraphNode> AffinityTask::BFS(const TaskSet& taskSet, const GraphNode& start, const GraphNode& target, const Affinity& excludeID, const TaskSet& excludeTask)
{
	std::list<GraphNode> returnList;

	//prepare link map
	std::unordered_map<CPUID, TaskSet> cpuToTaskList;
	std::unordered_map<AffinityTask*, Affinity> taskToCPUList;
	for(auto task : taskSet)
	{
		if(excludeTask.find(task) != excludeTask.end())
			continue;
		Affinity affinityCopy(task->affinity);
		for(auto cpu : excludeID)
			affinityCopy.erase(cpu);
		taskToCPUList.insert(std::pair<AffinityTask*, Affinity>(task, affinityCopy));
		for(CPUID cpu : affinityCopy)
		{
			if(cpuToTaskList.find(cpu) == cpuToTaskList.end())
				cpuToTaskList.insert(std::pair<CPUID, TaskSet>(cpu, TaskSet()));

			cpuToTaskList.find(cpu)->second.insert(task);
		}
	}

	//procedure BFS(G,v) is
	std::unordered_map<CPUID, AffinityTask*> cpuToPrevTask;
	std::unordered_map<AffinityTask*, CPUID> taskToPrevCPU;
	bool reachable = false;

	std::queue<GraphNode> queue;//create a queue Q, true is Job, false is processor
	std::unordered_set<CPUID> visitedCPU; //create a set V
	std::unordered_set<AffinityTask*> visitedTask; //create a set V

	if(start.isTask())
		visitedTask.insert(start.getTask()); //add v to V
	else
		visitedCPU.insert(start.getCPUID());
	queue.push(start); //enqueue v onto Q

	while(!queue.empty())//while Q is not empty loop
	{
		auto currentItem = queue.front(); //t ← Q.dequeue()
		queue.pop();

		if(currentItem.isTask())
		{
			if(target.isTask())
				if(target.getTask() == currentItem.getTask()) //if t is what we are looking for then
				{
					//return t
					reachable = true;
					break;
				}
		}
		else
		{
			if(!target.isTask())
				if(target.getCPUID() == currentItem.getCPUID()) //if t is what we are looking for then
				{
					//return t
					reachable = true;
					break;
				}
		}

		//for all edges e in G.adjacentEdges(t) loop
		if(currentItem.isTask())
		{
			AffinityTask* curTask = currentItem.getTask();
			assert(curTask != nullptr);

			for(CPUID adjacentCPU : taskToCPUList.find(curTask)->second) //u ← G.adjacentVertex(t,e)
			{
				if(visitedCPU.find(adjacentCPU) == visitedCPU.end()) //if u is not in V then
				{
					visitedCPU.insert(adjacentCPU); //add u to V
					queue.push(GraphNode(adjacentCPU)); //enqueue u onto Q

					assert(cpuToPrevTask.find(adjacentCPU) == cpuToPrevTask.end());
					cpuToPrevTask.insert(std::pair<CPUID,AffinityTask*>(adjacentCPU, curTask));
				}
			}
		}
		else
		{
			CPUID curCPU = currentItem.getCPUID();
			auto iter = cpuToTaskList.find(curCPU);
			if(iter == cpuToTaskList.end())
			{
				continue;
			}
			assert(iter->second.size() > 0);
			for(AffinityTask* adjacentTask : iter->second) //u ← G.adjacentVertex(t,e)
			{
				if(visitedTask.find(adjacentTask) == visitedTask.end())
				{
					visitedTask.insert(adjacentTask);
					queue.push(GraphNode(adjacentTask));

					assert(taskToPrevCPU.find(adjacentTask) == taskToPrevCPU.end());
					taskToPrevCPU.insert(std::pair<AffinityTask*,CPUID>(adjacentTask, curCPU));
				}
			}
		}
	}

	if(reachable)
	{
		GraphNode current = target;
		while(true)
		{
			returnList.push_front(current);
			if(current.isTask())
			{
				auto cpu_iter = taskToPrevCPU.find(current.getTask());
				if(cpu_iter == taskToPrevCPU.end())
				{
					assert(start.isTask());
					assert(current.getTask() == start.getTask());
					break;
				}
				current = cpu_iter->second;
			}
			else
			{
				auto task_iter = cpuToPrevTask.find(current.getCPUID());
				if(task_iter == cpuToPrevTask.end())
				{
					assert(!start.isTask());
					assert(current.getCPUID() == start.getCPUID());
					break;
				}
				current = task_iter->second;
			}
		}
	}

	return returnList;
}