You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code was excellent, except for printing out paths where there was more than one
intermediate vertex as in the case:
v0 v4 v1 v
v4 v1 v2 v3
Your code has:
public static void path(int q, int r)
{
if (P[q, r] != -1)
{
Console.Write("\nPath [" + q + ", " + r + "]\t");
path(q, P[q, r]);
Console.Write("v" + P[q, r] + " ");
path(P[q, r], r);
}
}
whereas the code should be:
public static void path(int q, int r, int[][] P)
{
if (P[q][r] != -1)
{
path(q, P[q][r], P);
Console.Write(" v" + P[q][r] + " ");
path(P[q][r], r, P);
}
}
Adding even a line of code in a recursive function can cause different outputs from what expected.
Below is my code:
for (i = 0; i < numVertex; i++) // Output all shortest paths
for (j = 0; j < numVertex; j++)
{
if (P[i][j] != -1)
/* there is a path with intermediate vertices. Does not print if there is
just an edge between v[i] and v[j] */
{
Console.WriteLine("The shortest path between vertex " + i +
" and vertex " + j + " is:");
Console.Write("v" + i);
path(i, j, P);
Console.Write("v" + j);
Console.WriteLine();
}
if (i != j && P[i][j] == -1 && D[i][j] != INFINITY)
{
Console.WriteLine("The shortest path between vertex " + i +
" and vertex " + j + " is:");
Console.WriteLine("v" + i + " v" + j);
}
}
The text was updated successfully, but these errors were encountered:
The code was excellent, except for printing out paths where there was more than one
intermediate vertex as in the case:
v0 v4 v1 v
v4 v1 v2 v3
Your code has:
public static void path(int q, int r)
{
if (P[q, r] != -1)
{
Console.Write("\nPath [" + q + ", " + r + "]\t");
path(q, P[q, r]);
Console.Write("v" + P[q, r] + " ");
path(P[q, r], r);
}
whereas the code should be:
public static void path(int q, int r, int[][] P)
{
if (P[q][r] != -1)
{
path(q, P[q][r], P);
Console.Write(" v" + P[q][r] + " ");
path(P[q][r], r, P);
}
}
Adding even a line of code in a recursive function can cause different outputs from what expected.
Below is my code:
for (i = 0; i < numVertex; i++) // Output all shortest paths
for (j = 0; j < numVertex; j++)
{
if (P[i][j] != -1)
/* there is a path with intermediate vertices. Does not print if there is
just an edge between v[i] and v[j] */
{
Console.WriteLine("The shortest path between vertex " + i +
" and vertex " + j + " is:");
Console.Write("v" + i);
path(i, j, P);
Console.Write("v" + j);
Console.WriteLine();
}
The text was updated successfully, but these errors were encountered: