Sunday, February 1, 2009

The Dijkstra Algorithm (Shortest Path) in Java

Hello Folks,

SearchGraphAlgorithms is a class that I've made that only has a single method search(type::TypeSearchEnum, v:int, cost:Graph):SearchInfo

Let's have a discussing about the O.O of this class:
This method was designed for diferent kinds of search. The enumeration TypeSearchEnum has only one type "DIJKSTRA", because is the unique search that I've implemented. After, we can insert other kinds as "BFS", "DFS","KRUSKAL",...

If "TypeSearchEnum" is "DIJKSTRA" the private method is called:
-dijkstra(v:int, cost:Graph<Double,Double>):SearchInfo

Obviously, this previous method searchs using Dijkstra algorithm in a graph defined by the interface Graph<K,V> (K = V = Double). Why an interface?
I could have created a signature for each type of graph e.g: "double[][] costs",
"{LinkedList<LinkedList<Double>> costs", "ArrayList<ArrayList<Double>> custos", "ArrayList<LinkedList<Double>> costs"...

It's possible think about a huge amount of signatures, that causes code multiplication. But, if a graph class implements the interface "Graph<K,V>" that contains appropriated access methods to the data structure, doesn't mind the implementation because this class is a "Graph<E,V>". So with a single signature we can solve this problem! (Polimorfism provided by the interface)

The method returns an object SearchInfo (I'll explain later) that contains the results of the search. Which are these results?

Each vertex is represented by an integer. The search result from a vertex "v" are the shortest paths from it to all other. A shortest path is not the number of the vertices but is the lowest distance. So these are the information that a SearchInfo object keeps. This class contains two public methods:

getPathFrom(vertex:int):LinkedList<Integer>

It returns the shortest path from a vertex "v" to an vertex "vertex" of the graph. A LinkedList is used because of the diferent number of vertices in a path and due the elements are added by addFirst(e:E) (O(1) execution)

getPathFrom(vertex:int):double
It returns the lowest distance from a vertex "v" to another "vertex" of the graph.

SearchInfo is an inner class of SearchGraphAlgorithms, public with private constructor. It was thought by the following way:
This class should contain a method that would build the paths from a vector of previous vertices generated by dijkstra method. But the class users must not have access to this method, so, it should be private. However, the class SearchGraphAlgorithms should be able to use the method to build the paths, so, the class SearchInfo should be inner of SearchGraphAlgorithms.

SearchInfo should be public to extern code, however, the constructor is created from an array of lowest distances that is created by "dijsktra" method, so, the constructor should be private.

The interface Graph contains only two methods:
+ get(i:int, j:int):V
+ size():int

As an application, I've created the class MatGraph that builds an adjacency matrix from a formated file
Ex:
8
0 * * * * * * *
30 0 * * * * * *
100 80 0 * * * * *
* * 120 0 * * * *
* * * 150 0 25 * *
* * * 100 * 0 90 140
* * * * * * 0 100
170 * * * * * * 0

The first line must contains the number of vertices. The other lines contain the distances of the adjacencies. The symbol "*" is inserted when there is no conexion between two vertices.

Main is a class that runs all the codes as an application.

The vertices ordering start from the zero.

I hope that you've enjoyed!

p.s:
1- Be correct, if you copy these codes on your college work/project don't forget to write that I am the author. Remember yourself that this post is really easy to find and has an unique Objects Orientation, don't be fool.
2-If there is any kind of english error, I really would appreciate to know.

1 comment:

  1. you have a nice site. thanks for sharing this site. you can download lots of ebook from here

    http://feboook.blogspot.com

    ReplyDelete