在图形

时间:2016-02-13 07:44:55

标签: c++ graph

我想打印每个节点的级别。我给每个节点输出但是最后一个。 那是10的等级是0 它应该是4。 我的图表:Ghaphical view

输入:
10 14
1 2
1 4
1 3
2 6
4 7 3 7
3 8
6 10
7 9
8 5
8 7
9 10
5 10

我的输出: 节点1连接到:2 4 3
节点2连接到:6
节点3连接到:7 8
节点4连接到:7
节点5连接到:10
节点6连接到:10
节点7连接到:9
节点8连接到:5 7
节点9连接到:10
节点10连接到:
等级1是:0
等级2是:1
等级3是:1
等级4是:1
等级5是:3
等级6是:2
等级7是:2
等级8是:2
等级9是:3
等级10是:0

我的代码: http://pastebin.com/F7QXafXD

 #include<stdlib.h>
using namespace std;
#define MAX 100000
vector<int>edges[MAX];
vector<int>vis[MAX];

int main()
{
int N,E;
scanf("%d%d",&N,&E);
int level[N];
for(int i=0; i<=N; i++)
{
    level[i]=-1;
}
for(int i=1; i<=E; i++)
{
    int x,y;
    scanf("%d%d",&x,&y);
    edges[x].push_back(y);
}
int source = 1;
level[source] = 0;
for(int i=source; i<=N; i++)
{
    int l = edges[i].size();


    printf("Node %d is connected to: ",i);
    for(int j=0; j<l; j++)
    {
        printf("%d ",edges[i][j]);
        if(level[edges[i][j]]==-1)
        {
            level[edges[i][j]]=level[i]+1;
        }

    }
    printf("\n");


}
int size = sizeof(level)/sizeof(int);
for(int i=1; i<=size; i++)
{
    printf("Level of %d is :%d\n",i,level[i]);
}
return 0;

}

1 个答案:

答案 0 :(得分:1)

#include <stdlib.h>
#include <vector>
using namespace std;

#define MAX 100000

vector<int> edges[MAX];
vector<int> vis[MAX];

int main() {

    int numNodes, numEdges; // number of edges

    // reading number of nodes
    printf("Please input number of nodes: ");
    scanf("%d", &numNodes);
    // reading number of edges
    printf("Please input number of edges: ");
    scanf("%d", &numEdges);
    int level[numNodes];

    for (int i = 0; i < numNodes; i++) {
        level[i] = -1;
    }

    // reading edges
    printf("Please input the %d edges in the form of \"NODE1 NODE2\":\n", numEdges);
    for (int i = 0; i < numEdges; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        edges[x-1].push_back(y);
        edges[y-1].push_back(x);
        printf("\nEdge #%d stored! Please input %d more edge(s).\n", i+1, 13-i-1);
    }

    level[0] = 0;

    // printing edges for all nodes
    for (int i = 0; i < numNodes; i++) {
        int l = edges[i].size();

        printf("Node %d is connected to: ", i+1);

        if (l == 0) {
            printf("nothing!\n", i+1);
            break;
        }
        for (int j = 0; j < l; j++)
        {
            printf("%d ", edges[i][j]);

        // update nodes' levels
        /* if the adjacent node's level is lower than the current node's level and the adjacent node node is the larger node,
        OR if the adjacent node's level is -1 (means it had not been updated yet),
        then the adjacent node's level should be changed to the current node's level + 1.
        This is possible because it implicitly appears that higher numbered nodes are given higher levels,
        e.g. when 1 is connected by an edge to 2, it is assumed that 2 is at level 1 and 2 is at level 0,
        rather than the other way around.*/
        if (((level[edges[i][j]-1] < level[i]) && (edges[i][j] > i+1)) || (level[edges[i][j]-1] == -1)) {
            level[edges[i][j]-1] = level[i] + 1;
            }
        }
        printf("\n");
    }

    // printing level of all nodes
    for(int i = 0; i < numNodes; i++) {
        printf("Level of node %d is: %d\n", i+1, level[i]);
    }

    return 0;
}

我做了一些改变:

  1. 重构代码以包含更好的缩进,变量名称和轻微的注释。

  2. 修复了用于各种循环的索引。当你输入时,你的代码会要求扫描13个边缘。

  3. 使边缘双向存储,以便打印每个节点的所有相邻边缘。

  4. 修正了节点的更新过程&#39;水平。

  5. 删除了不必要的变量。

  6. 添加了其他控制台消息。