Пример #1
0
int getLargestNode(Graph & G)
{
    int largest = G->BegNI().GetDat();
    for (SnapNode NI = G->BegNI(); NI != G->EndNI(); NI++)
    {
        if(NI.GetDat() > largest)
            largest = NI.GetDat();
    }
    return largest;
}
Пример #2
0
void kruskalCluster(Graph & G)
{
    std::vector<Cluster> clusters;
    for (SnapNode NI = G->BegNI(); NI < G->EndNI(); NI++)
    {
        Cluster c;
        c.empty = false;
        c.clusterElements.push_back(NI.GetDat());
        clusters.push_back(c);
    }

    std::vector <SnapEdge> result;
    std::deque <SnapEdge> Q;

    for (SnapEdge NI = G->BegEI(); NI < G->EndEI(); NI++)
    {
        Q.push_back(NI);
    }

    while(!Q.empty())
    {
        std::sort(Q.begin(), Q.end(), sortClusters);
        SnapEdge edge = Q.front();
        Q.pop_front();
        if(!compareCluster((int)edge.GetSrcNDat(), (int)edge.GetDstNDat(), clusters))
        {
            result.push_back(edge);
        }
    }

    for(int i = 0; i<result.size(); i++)
    {
        std::cout << "(" << result[i].GetSrcNDat() << ", " << result[i].GetDstNDat() << ", " << result[i].GetDat() << ")" << std::endl;
    }
}
Пример #3
0
void printDetail(Graph & G)
{
    for (SnapNode NI = G->BegNI(); NI != G->EndNI(); NI++)
    {
        std::cout << "Id: " << NI.GetId() << " Out Degree: " << NI.GetOutDeg() << " In Degree: " << NI.GetInDeg() << std::endl;
    }

    for (SnapEdge NI = G->BegEI(); NI < G->EndEI(); NI++)
    {
        std::cout << "Edge from " << (int)NI.GetSrcNDat() << " --> " << (int)NI.GetDstNDat() << " with weight " << (int)NI.GetDstNDat() << std::endl;
    }
}
Пример #4
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++;
        }
    }
}
Пример #5
0
void kruskal(Graph & G, bool bench)
{
    int largestNode = getLargestNode(G);
    std::vector<disjointTreeNode*> disjointNodes(largestNode);
    std::vector<SnapEdge> result;
    std::deque<SnapEdge> Q;

    for(SnapNode NI = G->BegNI(); NI < G->EndNI(); NI++)
    {
        disjointTreeNode* x =  new disjointTreeNode();
        x->id = NI.GetDat();
        makeSet(x);
        disjointNodes[x->id-1] = x;
    }


    for (SnapEdge NI = G->BegEI(); NI < G->EndEI(); NI++)
    {
        Q.push_back(NI);
    }

    while (! Q.empty())
    {
        std::sort(Q.begin(), Q.end(), sortClusters);
        SnapEdge e = Q.front();
        Q.pop_front();
        disjointTreeNode* u = Find(disjointNodes[e.GetSrcNDat()-1]);
        disjointTreeNode* v = Find(disjointNodes[e.GetDstNDat()()-1]);
        if ( u != v )
        {
            result.push_back(e);
            Union(disjointNodes[e.GetSrcNDat()-1], disjointNodes[e.GetDstNDat()-1]);
        }

    }
    if(!bench)
    {
        for(int i = 0; i<result.size(); i++)
        {
            std::cout << result[i].GetSrcNDat() << " --> " << result[i].GetDstNDat() << " peso: " << result[i].GetDat() << std::endl;
        }
    }
}