1

My project is to implement minimum spanning tree using java. I aim to use Prim's algorithm to do the task.

The definition of the graph is G = (V, E) where V is the set of pins, E is the set of possible interconnections between pairs of pins, and for each edge (u,v) in E we have a weigh w(u,v) specifying the cost to connect u and v.

My idea is to use two hashmaps. First would have pin as a key and a list of neighbours as value. The second hashmap would take list of edge (u,v) as a key and the value would be its weight.

What do you think is the best way to store the graph?

3 Answers 3

2

Graphs are generally (without regard to the algorithm used with those graphs) stored as either :

  • adjacency lists,
  • adjacency matrices,
  • incidence lists,
  • incidence matrices.

All have their advantages and disadvantages concerning memory usage and time required to traverse them. Wikipedia has a great write-up on graph representations in computers.

Sign up to request clarification or add additional context in comments.

2 Comments

So let's assume I go with the adjacency list, it would still be viable to use the second hashmap to contain the weight of each pair?
Sure. :) As long as you don't forget to update that extra structure, you'll be fine.
0

Look on wikipedia. You have different data structures available as well as the inherited complexities

Comments

0

For both Kruskal’s and Prim’s minimum spanning tree algorithms adjacency-list representation of the graph is enough. It's more efficient in memory usage than matrice's approach. So it's a good choise in your case.

Comments

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.