コード例 #1
0
ファイル: hw2.cpp プロジェクト: fwswdev/public
    // TODO: this is the only missing part... will do this tomorrow :)
    void PerformPathFinding(NodeIdxT start, NodeIdxT end)
    {
    const NodeT * nodes = mPtrGraph->GetNodes();
    NodeValT currdist, mindist;
    NodeIdxT currentNode = start;

    mPath.clear();

    while (true)
        {
        mindist = MAX_INVALID_NODE;
        int nodeConnectedSize = nodes[currentNode].nodesConnected.size();
        for (int ctr = 0; ctr < nodeConnectedSize; ctr++)
            {
            currdist = mPtrGraph->GetDistanceDirect(*mPtrGraph, currentNode,
                    nodes[currentNode].nodesConnected[ctr]);
            if (currdist != INVALID_NODE)
                {
                mindist = currdist;
                }
            }

        break;
        }

    this->mDistance = mindist;
    }
コード例 #2
0
std::vector<int> primPrincipal(Graph & G, float & length, int v)
{
    int size = G->GetNodes();
    std::vector<float> distances (size,inf);
    std::vector<bool> visitedNodes (size,false);
    std::vector<int> parents (size,-1);
    std::deque<QueueItem> queue;
    for(int i=0; i<size; i++)
    {
        QueueItem q;
        q.VertexID = i+1;
        q.distance = inf;
        queue.push_back(q);
    }
    distances[v-1] = 0;
    visitedNodes[v-1] = true;
    while(!queue.empty())
    {
        std::sort(queue.begin(), queue.end(), sortQueue);
        QueueItem u = queue.front();
        queue.pop_front();
        SnapEdge EI;
        if(parents[u.VertexID-1] != -1)
        {
            TNodeEDatNet<TInt, TFlt>::TEdgeI EI = G->GetEI(parents[u.VertexID-1],u.VertexID);
            length+=(float)EI.GetDat();
        }
        visitedNodes[u.VertexID-1] = true;
        SnapNode NI = G->GetNI(u.VertexID);
        for (int e = 0; e < NI.GetOutDeg(); e++)
        {
            SnapEdge EI = G->GetEI(u.VertexID,NI.GetOutNDat(e));
            int outNode = NI.GetOutNDat(e);
            float edgeWeight = (float)EI.GetDat();
            if(!visitedNodes[outNode-1] && distances[outNode-1] > edgeWeight)
            {
                parents[outNode-1] = u.VertexID;
                distances[outNode-1] = edgeWeight;
                for(int i=0; i< queue.size(); i++)
                {
                    if(queue[i].VertexID == outNode)
                        queue[i].distance = edgeWeight;
                }
            }
        }
    }
    return parents;
}
コード例 #3
0
void dijkstra(Graph & G, int v, bool bench)
{
    int size = G->GetNodes();
    std::vector<float> distances (size,inf);
    std::vector<int> parents (size,-1);
    std::priority_queue<std::pair<float, Graph::TObj::TNodeI> > queue;

    distances[v-1] = 0;
	parents[v-1] = -1;

    for(SnapNode NI = G->BegNI(); NI != G->EndNI(); NI++)
    {
		queue.push(std::make_pair(distances[NI.GetDat()-1], NI));
    }

    while(!queue.empty())
    {
        SnapNode u = queue.top().second;
        float dist = queue.top().first;
        queue.pop();

        int sourceNode = u.GetDat();
        for (int ed = 0; ed < u.GetOutDeg(); ed++)//For each adjacent vertex to node NI
        {
            int destNode = u.GetOutNDat(ed);
            SnapEdge e =  G->GetEI(sourceNode,destNode);
			float alt = dist + e.GetDat();
            if(alt < distances[destNode-1])//relax
            {
                distances[destNode-1] = alt;
                parents[destNode-1] = sourceNode;
                queue.push(std::make_pair(alt, G->GetNI(destNode)));
            }
        }
    }
    if(!bench)
    {
        int node = 1;
        for (int i = 0 ; i< parents.size(); i++)
        {
            if(parents[node-1] == -1)
                std::cout << "start -> " << node << " distance:  " << distances[node-1] << std::endl;
            else
                std::cout << parents[node-1] << " -> " << node << " distance:  " << distances[node-1] << std::endl;
            node++;
        }
    }
}