Home > OS >  Wrong cost adjacency matrix
Wrong cost adjacency matrix

Time:11-01

I try to obtain the adjacency matrix of weights, and then use it in the calculation of the minimum weight path. There is a problem, when I try to display it, I get a wrong result : By logic, the diagonal must have only 0, and in the places where the vertices are adjacent, must be the weight of the edge enter image description here

//set the source and destination of each edge
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[0]->weight = 9;

g->edge[1]->src = 0;
g->edge[1]->dest = 10;
g->edge[1]->weight = 6;

g->edge[2]->src = 1;
g->edge[2]->dest = 2;
g->edge[2]->weight = 3;

g->edge[3]->src = 1;
g->edge[3]->dest = 10;
g->edge[3]->weight = 2;

g->edge[4]->src = 2;
g->edge[4]->dest = 3;
g->edge[4]->weight = 2;

g->edge[5]->src = 2;
g->edge[5]->dest = 6;
g->edge[5]->weight = 3;

g->edge[6]->src = 2;
g->edge[6]->dest = 5;
g->edge[6]->weight = 3;

g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[7]->weight = 5;

g->edge[8]->src = 4;
g->edge[8]->dest = 5;
g->edge[8]->weight = 4;

g->edge[9]->src = 6;
g->edge[9]->dest = 10;
g->edge[9]->weight = 2;

g->edge[10]->src = 6;
g->edge[10]->dest = 7;
g->edge[10]->weight = 9;

g->edge[11]->src = 7;
g->edge[11]->dest = 8;
g->edge[11]->weight = 7;

g->edge[12]->src = 7;
g->edge[12]->dest = 9;
g->edge[12]->weight = 2;

g->edge[13]->src = 8;
g->edge[13]->dest = 9;
g->edge[13]->weight = 7;

g->edge[14]->src = 9;
g->edge[14]->dest = 10;
g->edge[14]->weight = 5;

My code :

  for (i = 0; i < numberOfVertices; i  )
    {
        adjacency_matrix[i][i] = 0;
        for (j = i   1; j < numberOfVertices; j  )
        {
                adjacency_matrix[i][j] = g->edge[i]->weight;
                adjacency_matrix[j][i] = g->edge[i]->weight;
        }
    }
    
  

What's wrong?

CodePudding user response:

  for (i = 0; i < numberOfVertices; i  )
    {
        adjacency_matrix[i][i] = 0;
        for (j = i   1; j < numberOfVertices; j  )
        {
                adjacency_matrix[i][j] = g->edge[i]->weight;
                adjacency_matrix[j][i] = g->edge[i]->weight;
        }
    }

In this code you are setting every edge from vertex i to every other vertex to the same weight. I do not think this is what you want.

( Note: It is hard to know what you do want. When reporting a problem you need to include a description of both what happens AND what you wanted to happen. "It's wrong!" is almost useless as a bug report. )

  • Related