I have implemented an program that can calculate shortest path's for any graph either by using Floyd's/Dijkstra's algorithm based on user input.
Both algorithm's work fine for directed graph. The output is supposed to display 1)The actual path to be taken from the start vertex 2)The shortest distance to be travelled
But when it come's to undirected graph I run into problems.
My graph is not represented by an adjacency matrix but with an Graph & Edge classes as shown below
class Graph
{
private final int noOfVertices;
private final ArrayList<Edge> edges = new ArrayList<Edge>();
private boolean undirected = false;
Graph(int vertexCount)
{
noOfVertices = vertexCount;
edges.ensureCapacity(2 * (vertexCount - 1));
}
public int getWeight(int src,int dst)
{
int weight=90000;
for(Edge edge:edges)
{
if(edge.src==src && edge.dst==dst)
{
weight=edge.weight;
break;
}
}
return weight;
}
public int getEdgeCount(){return edges.size();}
public int getVertexCount(){return noOfVertices;}
public static class Edge
{
public int src;
public int dst;
public int weight;
Edge(int v1, int v2, int w)
{
src = v1;
dst = v2;
weight = w;
}
}
}
To implement undirected graph this is the code used below
void newEdge(int src,int dst,int weight)
{
edges.add(new Edge(src,dst,weight));
if(undirected){ edges.add(new Edge(dst,src,weight));}
}
Now Dijkstra's algorithm work's fine on this setup but when I use Floyd's algorithm I start to get incorrect path's but correct distance's
This is my Floyd's algorithm
public static Integer[] Floyd(Graph graph, int startVertex, int endVertex)
{
ArrayList<Integer> pathInfo = new ArrayList<Integer>();
int dist[][] = new int[graph.getVertexCount()][graph.getVertexCount()];
int path[][] = new int[graph.getVertexCount()][graph.getVertexCount()];
int V = graph.getVertexCount();
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (i == j)
{
dist[i][j] = 0;
}
else
{
dist[i][j] = graph.getWeight(i, j);
}/*Initialize with edge weight's between vertices i,j.If edge does not exist graph.getWeight() return's 90000 i.e simply an value somewhat close to infinite because when I use Integer.MAX_VALUE I get negative distance's when doing dist[i][k]+dist[k][j] so i avoid it*/
path[i][j] = j;
}
}
/*actual Floyd's algorithm*/
for (int k = 0; k < V; k++)
{
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = k; /*if path exist's record the intermediate vertex*/
}
}
}
}
int currentVertex=endVertex; /*Start from last vertex*/
int nextVertex=path[startVertex][endVertex];/*Find path*/
pathInfo.add(currentVertex);/*Add Current Vertex*/
while(currentVertex!=nextVertex)/*Backtrack until the vertex we are currently at and the next vertex we need to go to from there are the same which mean's we have reached our target*/
{
currentVertex=nextVertex;
pathInfo.add(0,currentVertex);
nextVertex=path[startVertex][nextVertex];
}
pathInfo.add(0,startVertex);/*Finally add the vertex we ended at*/
pathInfo.add(dist[startVertex][endVertex]);/*Come's out correct in both cases*/
return pathInfo.toArray(new Integer[]{});/*Convert info to array*/
}
So here is my undirected graph given below. The doted lines represent's an edge that goes in both direction's.
0--1--2--3
Each edge has an weight of 2
Now when I call Floyd's algorithm with start vertex=0 and end vertex=3 I get The correct output path.
0,1,2,3
But when I again call Floyd's algorithm with start Vertex=3 and end Vertex=0 The output path is
3,2,0
Vertex 1 is missing.
But with Dijkstra's algorithm I get the correct result's in both cases
Here is the path matrix calculated above.
0 1 1 2
0 1 2 2
1 1 2 3
2 2 2 3
The distance come's out correct that is 6 in both cases but the path is wrong when I reverse the vertex order only for Floyd's algorithm.
Most of the video ideas were incorporated from this link https://www.bing.com/videos/search?q=floyd%27s+algorithm&&view=detail&mid=E17F409B3AB0B2307233E17F409B3AB0B2307233&&FORM=VRDGAR
Any idea's on where I went wrong?