1

The implementation for my graph is the following hash table:

public class DiGraphHash{

private int numNodos, numArcos;
private TheList<Nodo> nodos[];
private TheList<Arco> arcos[];
private TheList<Arco> preds[];

}

where TheList, is a list I made myself.

For Dijkstra's algorithm I need to map The cost of each node and the path to reach that node. I have the following two arrays:

   int[] cost = new cost[num_nodes];
   Nodo[] path = new Nodo[num_nodes];

Another important detail, is that my nodes are going to be the letters A, B, C, D.

So when I map my nodes, for example lets say I have to assign the cost the the Node A, how do I find the position in the array?

I was thinking of using hashcode % array.length but I am not sure if I will get collisions (take into consideration that it will be only 1 char letters)

I am not asking about the code, and need the idea.

1 Answer 1

1

Two ideas:

  1. Add a cost field to your Nodo class. This way, you'll only have to manage an array of nodes, rather than a separate array of costs as well. You can assign the costs when you instantiate the nodes, and look them up easily later.

  2. A better data structure than two arrays might be a map, where the keys are the nodes themselves and the values are the nodes' costs. Here's an example:

HashMap<Nodo, Integer> nodesToCosts = new HashMap<Nodo, Integer>();

nodes.put(nodeA, new Integer(5));
nodes.put(nodeB, new Integer(20));
nodes.put(nodeC, new Integer(10));
nodes.put(nodeD, new Integer(5));
Sign up to request clarification or add additional context in comments.

2 Comments

ok I like the idea! thanks. and how about this: to save the shortest "path" to each node I am going to make an array of List, where the List has Arco (Arcs in English) and this way I can use Hashcode to find the Arc of a node, and the source of that Arc is going to be the "shortest predecesor. dont know if i explained myself well.
That sounds promising. You might also check out Java implementations from others: en.literateprograms.org/Dijkstra%27s_algorithm_%28Java%29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.