05-图3. 六度空间 (30)

本文介绍了一种使用下三角矩阵表示社交网络的方法,并通过宽度优先搜索(BFS)算法来计算社交网络中个体与其六度分隔内的联系人数量。详细介绍了如何创建和操作下三角矩阵图,实现节点之间的连接判断与建立,以及如何通过BFS算法遍历图并计算紧密度。

1 1500ms时间能够慷慨挥霍

2 内存一般也能够挥霍,可是要记得释放用完的内存,否则可能累积到内存超限

3 BFS分层注意细节,下三角矩阵找邻接节点也要注意细节


#include <iostream>
#include <malloc.h>
#include <queue>

using namespace std;

bool* CreateMatrixGraph(const int& N)
{
    int arraySize = N * (N + 1) / 2;
    bool* graph = (bool*) malloc(sizeof(bool) * arraySize);
    for (int i = 0; i < arraySize; i++)
    {
        graph[i] = 0;
    }
    return graph;
}

bool IsMatrixConnected(const int& a, const int& b, bool* graph, const int& N)
{
    if (a == b)
    {
        return false;
    }
    if (a > b)
    {
        return (graph[a * (a + 1) / 2 + b]);
    }
    else
    {
        return (graph[b * (b + 1) / 2 + a]);
    }
}

void MatrixConnect(const int& a, const int& b, bool* graph, const int& N)
{
    if (a >= N || b >= N)
    {
        printf("ERROR : NODE OUT OF RANGE\n");
        //return;
    }
    if (IsMatrixConnected(a, b, graph, N))
    {
        printf("ERROR : %d AND %d ALREADY CONNECTED\n", a, b);
        //return;
    }
    if (a == b)
    {
        printf("ERROR : THE SAME VERTICE\n");
        //return;
    }
    if (a > b)
    {
        graph[a * (a + 1) / 2 + b] = 1;
    }
    else
    {
        graph[b * (b + 1) / 2 + a] = 1;
    }
}

void GetAdjoinVertice(const int& vertice, bool* graph, int* adjoinVertice, int N)
{
    int currentIndex = 0;
    // 横行计算
    const int VERTICALPRIMEINDEX = (vertice*vertice + vertice) / 2;
    for (int j = 0; j <= vertice; j++)  // 共遍历了(vertice+1)个元素
    {
        if (graph[VERTICALPRIMEINDEX + j] == 1)
        {
            adjoinVertice[currentIndex++] = j;
        }
    }
    // 竖向计算初始位置。该位置是横向计算的最后一个位置
    const int COLUMNPRIMEINDEX = (vertice*vertice + vertice) / 2 + vertice;
    for (int j = 0; j < N - vertice - 1; j++)
    {
        if (graph[COLUMNPRIMEINDEX + (j+1) * (vertice+1) + (j*j + j) / 2] == 1)
        {
            adjoinVertice[currentIndex++] = vertice + j + 1;
        }
    }
}

int BFS(bool* graph, int vertice, bool* isVisited, int N)
{
    // 找vertice的朋友
    queue<int> t;
    t.push(vertice);
    isVisited[vertice] = true;
    // 近处的朋友包含自己我也是醉的不行
    int closeFriendship = 1;
    int lastLevelFriend = 0;

    int currentLevel = 0;
    int currentLevelFriend = 0;

    while (!t.empty())
    {
        int currentVertice = t.front();
        t.pop();
        //printf("%d ", currentVertice);

        int* adjoinVertice = (int*) malloc(sizeof(int) * N);
        for (int i = 0; i < N; i++)
        {
            adjoinVertice[i] = -1;
        }
        GetAdjoinVertice(currentVertice, graph, adjoinVertice, N);
        int i = 0;

        // 假设上层朋友数已经为0,即当前层遍历已经结束,将当前层朋友数设为上层朋友数。然后当前层朋友数置0
        if (lastLevelFriend == 0)
        {
            currentLevel++;
            lastLevelFriend = currentLevelFriend;
            currentLevelFriend = 0;
        }
        if (currentLevel > 6)
        {
            return closeFriendship;
        }
        // 遍历当前层的朋友
        while (adjoinVertice[i] != -1)
        {
            if (!isVisited[adjoinVertice[i]])
            {
                t.push(adjoinVertice[i]);
                isVisited[adjoinVertice[i]] = true;

                closeFriendship++;
                currentLevelFriend++;
            }
            i++;
        }
        if (lastLevelFriend > 0)
        {
            lastLevelFriend--;
        }
        free(adjoinVertice);
    }

    return closeFriendship;
}

int main()
{
    int N;
    int M;
    scanf("%d %d", &N, &M);
    N++;
    bool* graph = CreateMatrixGraph(N);
    for (int i = 0; i < M; i++)
    {
        int node1;s
        int node2;
        scanf("%d %d", &node1, &node2);
        MatrixConnect(node1, node2, graph, N);
    }
    bool* isVisited = (bool*) malloc(sizeof(bool) * N);
    // 输出
    for (int m = 1; m <= N - 1; m++)
    {
        for (int i = 0; i < N; i++)
        {
            isVisited[i] = false;
        }
        float closeFriend = BFS(graph, m, isVisited, N);
        float percent = closeFriend / (N - 1) * 100;
        printf("%d: %.2f%%\n", m, percent);
    }

    return 0;
}


现在我有15组眼在手外的标定数据,其中机械手末端的数据为 [1286.47, 992.29, 1067.27, 89.66, 0, 0] [1045.63, 1407.02, 931.32, 80.12, 0, 0] [1157.23, 1526.1, 1120.47, 60.82, 0, 0] [1532.22, 985.97, 1291.14, 122.24, 0, 0] [947.23, 1150.56, 1431.14, 58.16, 0, 0] [1166.81, 1390.16, 1309.7, 61.27, 0, 0] [840.58, 1380.18, 1269.01, 64, 0, 0] [1219.26, 899.35, 1314.17, 98.05, 0, 0] [1623, 668.53, 1174.82, 117.87, 0, 0] [1460.38, 1175.27, 988.82, 134.94, 0, 0] [1914.95, 1424.83, 835.48, 116.75, 0, 0] [1368.61, 1130.75, 637.48, 131.9, 0, 0] [882.77, 1597.43, 471.62, 55.14, 0, 0] [1428.61, 1050.84, 676.15, 67.15, 0, 0] [888.92, 1142.59, 386.51, 66.91, 0, 0] 其中每个数据从第一个到第六个的数据代表的意思为X,Y,Z,AZ,AY,AX 其中对应的15组标定板位姿为 标定板第0个齐次矩阵: [0.999705, 0.0224542, 0.0092758, -250.813, ] -0.0223456, 0.999682, -0.0116544, -42.0793, ] -0.00953454, 0.0114437, 0.999889, 1676, ] 0, 0, 0, 1, ] 标定板第1个齐次矩阵: [0.989419, -0.144129, 0.0166493, 173.263, ] 0.144379, 0.98941, -0.0149427, -226.558, ] -0.0143193, 0.0171884, 0.99975, 1804, ] 0, 0, 0, 1, ] 标定板第2个齐次矩阵: [0.886099, -0.463019, 0.0209981, 281.908, ] 0.463273, 0.886167, -0.00922135, -2.05317, ] -0.0143382, 0.0178989, 0.999737, 1617, ] 0, 0, 0, 1, ] 标定板第3个齐次矩阵: [0.830136, 0.557529, 0.00590481, -354.616, ] -0.557377, 0.830088, -0.0168506, 44.3669, ] -0.0142962, 0.0106971, 0.999841, 1455, ] 0, 0, 0, 1, ] 标定板第4个齐次矩阵: [0.863896, -0.503404, 0.0163511, -99.4686, ] 0.503647, 0.863709, -0.0185876, -195.379, ] -0.00476549, 0.0242929, 0.999694, 1312, ] 0, 0, 0, 1, ] 标定板第5个齐次矩阵: [0.889565, -0.456268, 0.0222202, 145.468, ] 0.456585, 0.8896, -0.0119302, 5.90197, ] -0.0143237, 0.020758, 0.999682, 1431, ] 0, 0, 0, 1, ] 标定板第6个齐次矩阵: [0.91053, -0.413031, 0.0184551, 139.055, ] 0.413333, 0.91041, -0.0175768, -334.7, ] -0.00954195, 0.0236323, 0.999675, 1469, ] 0, 0, 0, 1, ] 标定板第7个齐次矩阵: [0.985617, 0.168866, 0.00662917, -360.717, ] -0.168727, 0.985502, -0.0177851, -153.672, ] -0.00953637, 0.0164108, 0.99982, 1431, ] 0, 0, 0, 1, ] 标定板第8个齐次矩阵: [0.870638, 0.49183, 0.00961815, -652.722, ] -0.491716, 0.870673, -0.0120196, 151.006, ] -0.0142859, 0.00573532, 0.999882, 1575, ] 0, 0, 0, 1, ] 标定板第9个齐次矩阵: [0.688114, 0.725454, 0.0146813, -222.971, ] -0.725352, 0.688268, -0.0123463, -73.054, ] -0.0190613, -0.00215344, 0.999816, 1754, ] 0, 0, 0, 1, ] 标定板第10个齐次矩阵: [0.878853, 0.476796, 0.0168539, 109.5, ] -0.476498, 0.878972, -0.0189315, 447.98, ] -0.0238406, 0.00860717, 0.999679, 1905, ] 0, 0, 0, 1, ] 标定板第11个齐次矩阵: [0.725374, 0.68831, 0.00791644, -249.45, ] -0.688207, 0.725408, -0.0124338, -157.912, ] -0.0143009, 0.00357099, 0.999891, 2101, ] 0, 0, 0, 1, ] 标定板第12个齐次矩阵: [0.83742, -0.546487, 0.00889322, 348.432, ] 0.546539, 0.837136, -0.0223483, -247.731, ] 0.00476824, 0.0235754, 0.999711, 2256, ] 0, 0, 0, 1, ] 标定板第13个齐次矩阵: [0.932307, -0.361463, 0.0122011, -181.678, ] 0.361637, 0.93214, -0.0182758, 227.119, ] -0.0047671, 0.0214511, 0.999759, 2064, ] 0, 0, 0, 1, ] 标定板第14个齐次矩阵: [0.930247, -0.366353, 0.0206302, -88.0605, ] 0.366655, 0.930261, -0.0133773, -311.066, ] -0.0142907, 0.0200084, 0.999698, 2348, ] 0, 0, 0, 1, ] 请给出完整的标定结果,要给出代码片段或者示例
最新发布
08-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值