#codingquestion
List of stations and distances between them are given and find all pairs shortest distance.
Solution:
A straightforward implementation could be Floyd Warshall algorithm. This algorithm computes the shortest path weights in a bottom-up manner. It exploits the relationship between a pair of intermediary vertices and the shortest paths that pass through them. If there is no intermediary vertex, then such a path has at most one edge and the weight of the edge is the minimum. Otherwise, the minimum weight is the minimum of the path from I to j or the path from I to k and k to j. Thus this algorithm iterates for each of the intermediary vertices for each of the given input of an N*N matrix to compute the shortest path weight.
Algorithm Warshall(A[1..n, 1..n])
{
R-0 is initialized with the adjacency matrix ; // R-0 is the node to itself.
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
R-k[i, j] = R-(k-1) [i, j] OR
(R-(k-1) [i, k] AND R-(k-1) [k,j])
return R-(n)
}
Another method could be to use the Bellman Ford algorithm to find the shortest pair distance between two vertices and also to find all paths.
public static bool GetShortestPath(int[,] graph, int numVertex, int start, ref List<int> distances, ref List<int> parents)
{
// initialize Single Source
for (int i = 0; i < numVertex; i++)
{
distances[i] = int.MaxValue;
parents[i] = -1;
}
distances[start] = 0;
var allEdges = GetAllEdges(graph, numVertex);
for (int k = 0; k < numVertex - 1; k++)
{
for (int i = 0; i < allEdges.Count; i++)
{
// relax
int sum = distances[allEdges[i].Item1] == int.MaxValue ?
distances[allEdges[i].Item1] :
distances[allEdges[i].Item1] + allEdges[i].Item3;
if (distances[allEdges[i].Item2] > sum)
{
distances[allEdges[i].Item2] = distances[allEdges[i].Item1] + allEdges[i].Item3;
parents[allEdges[i].Item2] = allEdges[i].Item1;
}
}
}
for (int i = 0; i < allEdges.Count; i++)
{
if (distances[allEdges[i].Item2] > distances[allEdges[i].Item1] + allEdges[i].Item3)
{
return false; // cycle exists;
}
}
return true;
}
public static void GetAllPaths(int[,] graph, int numVertex, int source, int destination, int threshold, ref List<int> candidatePath, ref List<int> candidateDist, ref List<List<int>> pathList, ref List<List<int>> distanceList)
{
if (candidatePath.Count > threshold) return;
var path = new List<int>();
var distances = new List<int>();
GetOutboundEdges(graph, numVertex, source, ref path, ref distances);
if (path.Contains(destination) && (candidatePath.Count == 0 || (candidatePath.Count > 0 && candidatePath.Last() != destination)))
{
candidatePath.Add(destination);
candidateDist.Add(distances[path.IndexOf(destination)]);
if (pathList.Contains(candidatePath) == false)
pathList.Add(new List<int>(candidatePath));
if (distanceList.Contains(candidateDist) == false)
distanceList.Add(new List<int>(candidateDist));
candidatePath.RemoveAt(candidatePath.Count - 1);
candidateDist.RemoveAt(candidateDist.Count - 1);
}
for (int i = 1; i < path.Count; i++)
{
// if (i != source)
{
candidatePath.Add(path[i]);
candidateDist.Add(distances[i]);
GetAllPaths(graph, numVertex, path[i], destination, threshold, ref candidatePath, ref candidateDist, ref pathList, ref distanceList);
candidatePath.RemoveAt(candidatePath.Count - 1);
candidateDist.RemoveAt(candidateDist.Count - 1);
}
}
}
List of stations and distances between them are given and find all pairs shortest distance.
Solution:
A straightforward implementation could be Floyd Warshall algorithm. This algorithm computes the shortest path weights in a bottom-up manner. It exploits the relationship between a pair of intermediary vertices and the shortest paths that pass through them. If there is no intermediary vertex, then such a path has at most one edge and the weight of the edge is the minimum. Otherwise, the minimum weight is the minimum of the path from I to j or the path from I to k and k to j. Thus this algorithm iterates for each of the intermediary vertices for each of the given input of an N*N matrix to compute the shortest path weight.
Algorithm Warshall(A[1..n, 1..n])
{
R-0 is initialized with the adjacency matrix ; // R-0 is the node to itself.
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
R-k[i, j] = R-(k-1) [i, j] OR
(R-(k-1) [i, k] AND R-(k-1) [k,j])
return R-(n)
}
Another method could be to use the Bellman Ford algorithm to find the shortest pair distance between two vertices and also to find all paths.
public static bool GetShortestPath(int[,] graph, int numVertex, int start, ref List<int> distances, ref List<int> parents)
{
// initialize Single Source
for (int i = 0; i < numVertex; i++)
{
distances[i] = int.MaxValue;
parents[i] = -1;
}
distances[start] = 0;
var allEdges = GetAllEdges(graph, numVertex);
for (int k = 0; k < numVertex - 1; k++)
{
for (int i = 0; i < allEdges.Count; i++)
{
// relax
int sum = distances[allEdges[i].Item1] == int.MaxValue ?
distances[allEdges[i].Item1] :
distances[allEdges[i].Item1] + allEdges[i].Item3;
if (distances[allEdges[i].Item2] > sum)
{
distances[allEdges[i].Item2] = distances[allEdges[i].Item1] + allEdges[i].Item3;
parents[allEdges[i].Item2] = allEdges[i].Item1;
}
}
}
for (int i = 0; i < allEdges.Count; i++)
{
if (distances[allEdges[i].Item2] > distances[allEdges[i].Item1] + allEdges[i].Item3)
{
return false; // cycle exists;
}
}
return true;
}
public static void GetAllPaths(int[,] graph, int numVertex, int source, int destination, int threshold, ref List<int> candidatePath, ref List<int> candidateDist, ref List<List<int>> pathList, ref List<List<int>> distanceList)
{
if (candidatePath.Count > threshold) return;
var path = new List<int>();
var distances = new List<int>();
GetOutboundEdges(graph, numVertex, source, ref path, ref distances);
if (path.Contains(destination) && (candidatePath.Count == 0 || (candidatePath.Count > 0 && candidatePath.Last() != destination)))
{
candidatePath.Add(destination);
candidateDist.Add(distances[path.IndexOf(destination)]);
if (pathList.Contains(candidatePath) == false)
pathList.Add(new List<int>(candidatePath));
if (distanceList.Contains(candidateDist) == false)
distanceList.Add(new List<int>(candidateDist));
candidatePath.RemoveAt(candidatePath.Count - 1);
candidateDist.RemoveAt(candidateDist.Count - 1);
}
for (int i = 1; i < path.Count; i++)
{
// if (i != source)
{
candidatePath.Add(path[i]);
candidateDist.Add(distances[i]);
GetAllPaths(graph, numVertex, path[i], destination, threshold, ref candidatePath, ref candidateDist, ref pathList, ref distanceList);
candidatePath.RemoveAt(candidatePath.Count - 1);
candidateDist.RemoveAt(candidateDist.Count - 1);
}
}
}
No comments:
Post a Comment