简体   繁体   English

用C清除数组数据(Prim算法)

[英]Clear array data in C (Prim’s Algorithm)

I write a code about implement Prim's Algorithm and build the minimum spanning tree starting from vertex 1. 我编写了有关实现Prim's Algorithm的代码,并从顶点1开始构建最小生成树。

Here's the input format: 这是输入格式:

The first line will be the number of cases. 第一行将是案例数。

Each case begins with the number of vertices. 每种情况都以顶点数开始。

Suppose i input like this: 假设我这样输入:

2
4
0,1,0,4
1,0,3,2
0,3,0,5
4,2,5,0
6
0,7,9,0,0,6
7,0,0,2,0,0
9,0,0,0,5,0
0,2,0,0,0,3
0,0,5,0,0,4
6,0,0,3,4,0

This means i have 2 test case, and the '4' means have 4 lines of vertices. 这意味着我有2个测试用例,而“ 4”意味着有4条顶点。

Example: 例:

0,1,0,4 0,1,0,4

1,0,3,2 1,0,3,2

0,3,0,5 0,3,0,5

4,2,5,0 4,2,5,0

Will be like this 会像这样

  |0||1||2||3| 
0| 0  1  0  4
1| 1  0  3  2 
2| 0  3  0  5
3| 4  2  5  0 

0 to 4 need 4 distances, 2-2 need 3 distances, etc 0至4需要4个距离,2-2需要3个距离,依此类推

The first output was correct, which is 第一个输出是正确的,这是

0-1 1
1-2 3
1-3 2

But the second output also same with the first output, which means wrong 但是第二个输出也与第一个输出相同,这意味着错误

0-1 1
1-2 3
1-3 2

Here's my code 这是我的代码

#include <stdio.h>
#include <limits.h>
#include<stdbool.h>
// Number of vertices in the graph
#define V 100

// A utility function to find the vertex with
// minimum key value, from the set of vertices
// not yet included in MST
int minKey(int key[], bool mstSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
int v;
for (v = 0; v < V; v++)
    if (mstSet[v] == false && key[v] < min)
        min = key[v], min_index = v;

return min_index;
}

// A utility function to print the
// constructed MST stored in parent[]
int printMST(int parent[], int n, int graph[V][V])
{
int i;
char front1;
char back2;

//printf("Edge \tWeight\n");
for (i = 1; i < V; i++)
    if(graph[i][parent[i]]!=0)
    {
        front1 = parent[i];
        front1 += 16;
        back2 = i;
        back2 +=16;
        printf("%d-%d %d \n", parent[i], i, graph[i][parent[i]]);
    }

    else
        break;
}

// Function to construct and print MST for
// a graph represented using adjacency
// matrix representation
void primMST(int graph[V][V])
{
int i,count,v;
// Array to store constructed MST
int parent[V];
// Key values used to pick minimum weight edge in cut
int key[V];
// To represent set of vertices not yet included in MST
bool mstSet[V];

// Initialize all keys as INFINITE
for (i = 0; i < V; i++)
    key[i] = INT_MAX, mstSet[i] = false;

// Always include first 1st vertex in MST.
// Make key 0 so that this vertex is picked as first vertex.
key[0] = 0;
parent[0] = -1; // First node is always root of MST

// The MST will have V vertices
for (count = 0; count < V-1; count++)
{
    // Pick the minimum key vertex from the
    // set of vertices not yet included in MST
    int u = minKey(key, mstSet);

    // Add the picked vertex to the MST Set
    mstSet[u] = true;

    // Update key value and parent index of
    // the adjacent vertices of the picked vertex.
    // Consider only those vertices which are not
    // yet included in MST
    for (v = 0; v < V; v++)

        // graph[u][v] is non zero only for adjacent vertices of m
        // mstSet[v] is false for vertices not yet included in MST
        // Update the key only if graph[u][v] is smaller than key[v]
        if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
            parent[v] = u, key[v] = graph[u][v];
}

// print the constructed MST
printMST(parent, V, graph);
}

int main()
{

printf("%c",one1);
int i,j,limit1,limit2;
int column_a=0;
int row_a=0;
char datawithco[1000];
char *pch;
int graph[V][V];

scanf("%d",&limit1);

for(i=0; i<limit1; i++)
{
    scanf("%d",&limit2);
    for(j=0; j<limit2; j++)
    {
        scanf("%s",datawithco);
        pch = strtok (datawithco,",");
        while (pch != NULL)
        {
            //printf ("%s\n",pch);
            graph[row_a][column_a] = atoi(pch);
            column_a++;
            pch = strtok (NULL, ",");
        }
        column_a=0;
        row_a++;
    }

    primMST(graph);
    graph[row_a][column_a] = '\0';
}

return 0;
}

The second output should be 第二个输出应该是

0-5 6
5-3 3
3-1 2
5-4 4
4-2 5

You don't reset row_a variable after completing MST for each case. 完成每种情况的MST后,您无需重置row_a变量。

for(i=0; i<limit1; i++)
{
    scanf("%d",&limit2);
    for(j=0; j<limit2; j++)
    {
        scanf("%s",datawithco);
        pch = strtok (datawithco,",");
        while (pch != NULL)
        {
            //printf ("%s\n",pch);
            graph[row_a][column_a] = atoi(pch);
            column_a++;
            pch = strtok (NULL, ",");
        }
        column_a=0;
        row_a++;
    }

    primMST(graph);
    graph[row_a][column_a] = '\0';  
    row_a=0; //<---added
}

Besides you never reset your graph after each case. 此外,在每种情况下您都不会重置graph For these particular cases that you've provided, it is fine, because the number of vertices in the 2nd case is larger than the number of vertices in the 1st case, so the cells for the 2nd case will overwrite the cells for the 1st case, otherwise the cells left from the previous case will be regarded as valid ones which is obviously wrong. 对于您提供的这些特殊情况,这很好,因为第二种情况下的顶点数大于第一种情况下的顶点数,因此第二种情况下的像元将覆盖第一种情况下的像元,否则前一种情况留下的单元格将被视为有效单元格,这显然是错误的。 So you should either add the graph reset into primMST() function or add it at the start of every case 因此,您应该将图重置添加到primMST()函数中,或者在每种情况的开始处添加它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM