Example #1
0
void ArrayGraph::pushBackEdge(__uint32 a, __uint32 b, float desiredEdgeLength)
{
    // get the index of a free element
    __uint32 e_index = m_numEdges++;

    // get the pair entry
    EdgeAdjInfo& e = edgeInfo(e_index);

    // (a,b) is the pair we are adding
    e.a = a;
    e.b = b;

    m_desiredEdgeLength[e_index] = desiredEdgeLength;
    m_desiredAvgEdgeLength += (double)desiredEdgeLength;
    // get the node info
    NodeAdjInfo& aInfo = nodeInfo(a);
    NodeAdjInfo& bInfo = nodeInfo(b);

    // if a is part of at least one edge
    if (aInfo.degree)
    {
        // adjust the links
        EdgeAdjInfo& a_e = edgeInfo(aInfo.lastEntry);
        // check which one is a
        if (a==a_e.a)
            a_e.a_next = e_index;
        else
            a_e.b_next = e_index;
    } else
    {
        // this edge is the first for a => set the firstEntry link
        aInfo.firstEntry = e_index;
    };

    // same for b: if b is part of at least one edge
    if (bInfo.degree)
    {
        // adjust the links
        EdgeAdjInfo& b_e = edgeInfo(bInfo.lastEntry);
        // check which one is b
        if (b==b_e.a)
            b_e.a_next = e_index;
        else
            b_e.b_next = e_index;
    } else
    {
        // this edge is the first for b => set the firstEntry link
        bInfo.firstEntry = e_index;
    };
    // and the lastEntry link
    aInfo.lastEntry = e_index;
    bInfo.lastEntry = e_index;
    // one more edge for each node
    aInfo.degree++;
    bInfo.degree++;
};
Example #2
0
void ReadGraphData(char * graphStream[5000], Graph & graph, EdgeInfoDict & edgeInfoDict) {
    for(int i = 0; graphStream[i] != 0x0 && i < 5000; ++i) {
        int j = 0;
        int edgeNo = ReadANumberFromStr(graphStream[i], j);
        int edgeFrom = ReadANumberFromStr(graphStream[i], j);
        int edgeTo = ReadANumberFromStr(graphStream[i], j);
        int edgeCost = ReadANumberFromStr(graphStream[i], j);

        graph[edgeFrom].insert(edgeTo);

        Edge edge(edgeFrom, edgeTo);
        EdgeInfo edgeInfo(edgeNo, edgeCost);

        // 如果边信息字典中已经有了这条边且当前权大于字典中的权, 则不更新字典
        // 否则就要更新字典(可能是插入新边, 也可能是更新旧边)
        if(!(edgeInfoDict.count(edge) && edgeCost > edgeInfoDict[edge].second)) {
            edgeInfoDict[edge] = edgeInfo;
        }
    }
}