Data structure learning records - graph application examples - six-dimensional space (topic description, algorithm ideas, pseudo-code and interpretation, diagrams)

Table of contents

topic description

Algorithm idea

pseudocode

overall algorithm

BFS algorithm

Pseudocode Interpretation

BFS algorithm

diagram


topic description

The core point of the six-dimensional space theory is that any two people in a human social network only need to go through no more than six intermediaries (that is, six social relationships) to establish a connection on average. In other words, through someone you know, through someone else they know, and so on, you can eventually connect with any stranger in the world.

Now assuming that a social network graph is given, please calculate the percentage of nodes conforming to the "six-dimensional space" theory to the total number of nodes for each node.

Algorithm idea

For each node, perform a breadth-first search;

The number of nodes visited during the search;

The number of "layers" needs to be recorded, and only the number of nodes within 6 layers is counted. 

pseudocode

overall algorithm

void SDS()
{
    for( each V in G )
    {
        count = BFS(V);
        Output(count/N);
    }
}

BFS algorithm

int BFS(Vertex V)
{
    visited[V] = true;
    count = 1;
    level = 0;
    last = V;
    Enqueue(V, Q);

    while (!IsEmpty(Q))
    {
        V = Dequeue(Q);
        for (每个邻接点W of V)
        {
            if (!visited[W])
            {
                visited[W] = true;
                Enqueue(W, Q);
                count++;
                tail = W;
            }
        }

        if (V == last)
        {
            level++;
            last = tail;
        }

        if (level == 6)
            break;
    }

    return count;
}

Pseudocode Interpretation

BFS algorithm

visited[V] = true; count = 1;

Mark the starting node V as visited and initialize the counter count to 1.

level = 0; last = V;

The initial level level is 0, and the last node last is the starting node V.

Enqueue(V, Q);

Enqueue the start node V into the queue, and Q is the queue for storing the nodes to be visited.

Then enter the while loop when the queue is not empty

Take a node V from the queue.

Then start traversing every adjacent point W of node V, that is, the nodes connected with node V.

If the adjacent point W has not been visited, do the following:

  • Mark adjacency W as visited.
  • Enqueue the adjacency point W.

 

Increase the counter count, indicating the number of nodes visited, and update the position of the last node to W. 

 

 

If the current node V is equal to the last node last, it means that the traversal of the current layer is completed.

So just increase the level and update the last node to be the last node of the current layer.

If the level reaches 6, that is, the node in the six-dimensional space has been searched, and the loop is jumped out.

Finally, the number of nodes passed along the path from the starting node is returned. 

 

For the role of tail and last:

diagram

Initially, last is equal to V:

Then dequeue node 1 and store it in V, enqueue all adjacent points of node 1, and assign the last adjacent point of node 1 to tail:

 

Earlier we saw that node 1 has been out of the queue and is stored in V. It is judged that V == last. If they are equal, it means that one layer has been traversed:

level++, assign tail to last:

Then start a new cycle until it comes to node 7, and the last adjacent point of node 7 is assigned to tail, that is, node 19; at this time, last points to node 7; in this way, it is judged by last and tail to enter the next layer. 

 


end


Learning from: MOOC data structure - Chen Yue, He Qinming

Guess you like

Origin blog.csdn.net/li13437542099/article/details/130813948