Beispiel #1
0
bool Bfs::SearchWithTrail(int from, int to, Trail* trail)
{
  // Need to remember which nodes we have already visited
  std::set<int> visited;
  Breadcrumbs breadcrumbs;

  // Breadth first uses a queue to hold nodes we will visit
  std::queue<GraphEdge> nodesToVisit;

  float cost = 0; // dummy value
  // Put dummy edge in stack to start search. TODO better way, e.g. do/while ?
  nodesToVisit.push(GraphEdge(from, from, cost));

  while (!nodesToVisit.empty()) 
  {
    GraphEdge e = nodesToVisit.front();
    nodesToVisit.pop();

    std::cout << "Trying edge from " << e.GetFrom() << " to " << e.GetTo() << "\n";

    breadcrumbs[e.GetTo()] = e.GetFrom(); // map version
    //breadcrumbs.push_back(e); // vector version
    // In addition to here....
    visited.insert(e.GetTo());

    if (e.GetTo() == to)
    {
      // We have found the finish!
      std::cout << "Successfully got to the finish!\n";
      MakeTrail(from, to, breadcrumbs, trail);
      return true;
    }
    else
    {
      // Push unvisited neighbours onto stack
      const EdgeList& edgelist = m_graph->GetEdgeList(e.GetTo());
      for (EdgeList::const_iterator it = edgelist.begin(); it != edgelist.end(); ++it)
      {
        const GraphEdge& childEdge = *it;
        if (visited.count(childEdge.GetTo()) == 0)
        {
          nodesToVisit.push(childEdge);
          // ...we mark node as visited right away here
          visited.insert(childEdge.GetTo());
        }
      }
    }
  }
  return false;
}
Beispiel #2
0
bool Dfs::SearchWithTrail(int from, int to, Trail* trail)
{
    std::set<int> visited;
    Breadcrumbs breadcrumbs;

    std::stack<GraphEdge> nodesToVisit;
    float cost = 0;
    // Put dummy edge in stack
    nodesToVisit.push(GraphEdge(from, from, cost));
    while (!nodesToVisit.empty())
    {
        GraphEdge e = nodesToVisit.top();
        nodesToVisit.pop();

        std::cout << "Trying edge from " << e.GetFrom() << " to " << e.GetTo() << "\n";

        breadcrumbs[e.GetTo()] = e.GetFrom(); // map version
        // breadcrumbs.push_back(e); // vector version
        visited.insert(e.GetTo());

        if (e.GetTo() == to)
        {
            // We have found the finish! Loop back through the breadcrumbs to create the trail.
            MakeTrail(from, to, breadcrumbs, trail);
            return true;
        }
        else
        {
            // Push unvisited neighbours onto stack
            const EdgeList& edgelist = m_graph->GetEdgeList(e.GetTo());
            for (EdgeList::const_iterator it = edgelist.begin(); it != edgelist.end(); ++it)
            {
                const GraphEdge& childEdge = *it;
                if (visited.count(childEdge.GetTo()) == 0)
                {
                    nodesToVisit.push(childEdge);
                }
            }
        }
    }
    return false;
}
//엣지를 추가한다.
void SparseGraph::AddEdge(GraphEdge edge)
{
    //엣지의 양끝이 유효한 노드인지 확인한다.
    assert((edge.GetFrom() < m_iNextNodeIndex) && (edge.GetTo() < m_iNextNodeIndex) &&
           "<SparseGraph::AddEdge>: invalid node index");
    
    //유효한 노드이면
    if ((m_Nodes[edge.GetTo()].GetIndex() != -1) &&
        (m_Nodes[edge.GetFrom()].GetIndex() != -1))
    {
        //유니크한 엣지인지 확인한다.
        if (UniqueEdge(edge.GetFrom(), edge.GetTo()))
        {
            m_Edges[edge.GetFrom()].push_back(edge);
        }
        
        
        //거꾸로 방향도 확인한다.
        if (UniqueEdge(edge.GetTo(), edge.GetFrom()))
        {
            GraphEdge NewEdge = edge;
            
            NewEdge.SetTo(edge.GetFrom());
            NewEdge.SetFrom(edge.GetTo());
            
            m_Edges[edge.GetTo()].push_back(NewEdge);
        }
        
    }
}
void DrawEdge(const GraphEdge& edge, const Graph& g)
{
  glDisable(GL_LIGHTING);
  glBegin(GL_LINES);
  const GraphNode& from = g.GetNode(edge.GetFrom());
  const GraphNode& to = g.GetNode(edge.GetTo());
  const Vec2f& u = from.GetPos();
  const Vec2f& v = to.GetPos();
  glVertex3f(u.x, 0, u.y);
  glVertex3f(v.x, 0, v.y);
  glEnd();
  glEnable(GL_LIGHTING);
}
Beispiel #5
0
bool Dfs::SearchNoTrail(int from, int to)
{
    // Need to remember which nodes we have already visited
    std::set<int> visited;

    // Depth first uses a stack to hold nodes we will visit
    std::stack<GraphEdge> nodesToVisit;

    float cost = 0; // dummy value
    // Put dummy edge in stack to start search. TODO better way, e.g. do/while ?
    nodesToVisit.push(GraphEdge(from, from, cost));

    while (!nodesToVisit.empty())
    {
        GraphEdge e = nodesToVisit.top();
        nodesToVisit.pop();

        std::cout << "Trying edge from " << e.GetFrom() << " to " << e.GetTo() << "\n";

        visited.insert(e.GetTo());

        if (e.GetTo() == to)
        {
            // We have found the finish!
            return true;
        }
        else
        {
            // Push unvisited neighbours onto stack
            const EdgeList& edgelist = m_graph->GetEdgeList(e.GetTo());
            for (EdgeList::const_iterator it = edgelist.begin(); it != edgelist.end(); ++it)
            {
                const GraphEdge& childEdge = *it;
                if (visited.count(childEdge.GetTo()) == 0)
                {
                    nodesToVisit.push(childEdge);
                }
            }
        }
    }
    return false;
}