Shortest Path Algorithms, Java Collections Framework and AVL Tree
Shortest Path Algorithms, Java Collections Framework and AVL Tree
AVL tree is a self-balancing binary seach tree [BST], which was introduced
in1962 in a research paper (An algorithm for the organization of
information) by Soviet mathematicians, namely:
Georgy Adelson-Velsky
Evgenii Mikhailovich Landis
Time complexity
Worst case time complexity for search, delete and insert is O (log n).
where n = the total number of nodes in the tree.
However rebalancing after insertion and deletion may take some extra time
which has not been accounted for in this time complexity analysis.
Total time taken for adjustment and rotation is O(1).
Rotation mechanism
Left rotation
instead of node 4 which has moved to the status of left child. And node 6 has
been moved to the place of right child.
Right rotation:
References
Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and
Searching, Second Edition. Addison-Wesley, 1998.
Wikipedia article: Self balancing binary search tree
http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree
Wikipedia article: AVL tree
http://en.wikipedia.org/wiki/AVL_tree
Lectures from University of Washington website
courses.cs.washington.edu/courses/cse373/04wi/slides/lecture08.ppt
Lectures from University of Wisconsin website
http://pages.cs.wisc.edu/~ealexand/cs367/NOTES/AVLTrees/index.html
List
List is an ordered set of elements. It can have similar elements more than once,
i.e. it can have or can not have unique elements. Every element has an index
number and members can be manipulated via these index numbers. Sort, Shuffle,
rotate, binarySearch, indexOfSublist are the examples of the operations that can
be performed on the lists.
Queue
This collection is very common. Most common use for this data structure is to
hold elements before they are to be entered in for processing. A queue comprises
of additional methods which are insertion, extraction etc. queues implement FIFO
(first in first out) sequence of the element entry and removal of elements, but this
not a hard and fast rule. Priority queues are the example of such queues which
defy FIFO structure. Insert, remove and examine are some of the examples of
operations that can be performed on the queues.
Dequeues
Dequeues are double ended queues, they can be operated on from both ends of
the pipeline, they implement both FIFO (Fist in first out) and LIFO (Last in first
out). addFirst, addLast, peekFirst, peekLast are some of the examples of the
operations that can be performed on Dequeues.
Map
Map is basically an object which points to values through keys. Duplicate keys
cannot be entered because that would lead to confusing pointing and retrieval of
information. Java implements maps in three basic forms i.e. Treemaps,
linkedHashMap, HashMaps.
Sorted Set
Sorted sets are different from sets in manner that its elements are arranged in
sorted way. Advantage of sorting is taken using different new operations that are
not present in the Set interface. These find application in dictionaries, roll
numbers of students and other different areas of applications.
Sorted Map
Sorted maps are different from the Maps in the way that their mappings keys are
maintained in ascending order. Otherwise they are same.
Legacy classes:
Legacy classes have been retrofitted into the collections framework, they can be
used but they are not as efficient as newer data structures. however, the list of
present classes have been mentioned below.
Hashtable: newer alternative would be Hashmap.
Enumeration: collection and iterators should be used instead, but
Collection.Enumeration library can be used if needed.
Vectors: Arraylist can be used to replace this data structure.
Stack: linkedlists can be used in place of stacks.
Bitset: Arraylist of Boolean type variables can be used to replace the BitSet Data
structure, but obvious disadvantage would be wastage of space, because one bit
would take 8 bits to represent an array.
References
Object Oriented Software Construction (Lecture notes) University of Auckland
https://www.cs.auckland.ac.nz/~hongyul/tut/3_Collections
Chapter no. 22 Java collections framework
Liang, Introduction to Java programming, Seventh edition, Pearson
education
Dijkstra's algorithm
BellmanFord algorithm
A* search algorithm
FloydWarshall algorithm
Johnson's algorithm
Viterbi algorithm
Algorithm:
1. it compares all possible paths through the graph between each paired node.
2. It does this with the help of (|V |3) comparisons in a graph.
3. there may be up to (|V |2) edges in the graph, and every combination of edges is
tested.
4. It does so by step by step improving the estimate on the shortest path between two
vertices.
5. until the estimate is optimum the loop is continued.
A source node is given in the graph in which the shortest path is to be found, the algorithm
finds the path with lowest edge cost which obviously is the shortest path from that node to
every other node in the graph (single source - multiple destination problem). It can also be
used to find the shortest path in single source single destination problems by stopping
the algorithm once the algorithm has reached the destination and shortest path is achieved.
Same road map analogy can be applied here. For example, if we want to find the shortest
distance from our current location to all other destinations or road intersections in the city,
then Dijkstras algorithm can be used, or in the second case as we have discussed, we will
have to stop midway in order to find the shortest path from current location to a specific
location. But the algorithm will also find the shortest paths for all the destinations in
between of that specific path.
Algorithm:
Source node is called initial node. Let the distance of node Y be the distance from the initial
node to Y. Following steps will be followed to find the shortest path from initial node to
node Y.
1. An initial value is set to all nodes. This is 0 for initial node and infinity for all other
nodes.
2. All nodes are marked unvisited. Initial node is set as current. A set called unvisited
set is created comprising of all the unvisited nodes.
3. Consider all of its unvisited neighbors of the current node and calculate their
tentative distances. Compare the newly calculated distance to the current assigned
value and assign the smaller one, if the previous value was greater.
4. When the distance is measured for all the neighbors from the current node, set the
current node as visited and eliminate it from the unvisited set.
5. If the destination node is set to visited (in single source single destination
problem) or if the smallest tentative distance from current node to the nodes in
unvisited set is infinity (in single source multiple destination proble, when the
unvisited nodes are disjointed from the current node) then the algorithm has
finished.
6. Select the unvisited node that is marked with the smallest tentative distance, and set
it as the new "current node" then go back to step 3.
References:
Schaums outlines: Discrete mathematics.
3rd edition by Seymor Lipschutz & Marc Lipson
Shortest path problem (Wikipedia article).
http://en.wikipedia.org/wiki/Shortest_path_problem
Princeton University, article on the introduction of Dijkstras algorithm.
http://www .cs.princeton.edu/introalgsds/55dijkstra
Louisiana State University, article on Floyd-Warshalls algorithm.
http://www.csc.lsu.edu/~kundu/dstr/3-floyd.pdf
Floyd-Warshalls shortest path algorithm (Wikipedia Article)
http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
Dijkstras Algorithm (Wikipedia article)
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm