- #1
Pithikos
- 55
- 1
I want to represent a graph with v vertices and e edges. Each vertex represents a city and each edge the distance between two cities.
I am aware of adjacency matrix but I found that it took more than double space so I went with an array. Each item in the list keeps a point1, a point2 and the distance between those two.
So for example to represent these two connections:
Stockholm Athens 34km
Athens Amsterdam 23km
My array would look like this:
Now I have my doubts if I took the right decision. I surely save space but what about going through the array? Later in the project I will need to find the shortest path. Is the list going to be slower?
I am aware of adjacency matrix but I found that it took more than double space so I went with an array. Each item in the list keeps a point1, a point2 and the distance between those two.
So for example to represent these two connections:
Stockholm Athens 34km
Athens Amsterdam 23km
My array would look like this:
Code:
_______0__________________1__________
point1 | Stockholm | Athens |
point2 | Athens | Amsterdam |
distance | 34 | 23 |
---------------------------------------
Now I have my doubts if I took the right decision. I surely save space but what about going through the array? Later in the project I will need to find the shortest path. Is the list going to be slower?