How to represent a graph on a computer?

  • Thread starter Thread starter Pithikos
  • Start date Start date
  • Tags Tags
    Computer Graph
Click For Summary

Discussion Overview

The discussion centers around the representation of a graph on a computer, specifically focusing on the use of an adjacency matrix versus an array to represent vertices (cities) and edges (distances between cities). The conversation explores the implications of each method for space efficiency and ease of access, particularly in the context of finding the shortest path in a graph.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant proposes using an array to represent connections between cities, noting that it saves space compared to an adjacency matrix but raises concerns about efficiency in finding the shortest path.
  • Another participant argues that an adjacency matrix is easier to work with, despite its larger space requirement, and explains how it can be utilized to find distances between cities.
  • A follow-up response questions the space-saving claim of the array representation, suggesting that it may not be more efficient than the matrix due to the need to store city names and the number of edges.
  • Further clarification is provided about the total number of edges in a fully connected graph, emphasizing that even with bidirectional edges, the representation may not yield significant space savings.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency and practicality of using an array versus an adjacency matrix for graph representation. No consensus is reached regarding which method is superior, as both sides present valid points about space and access efficiency.

Contextual Notes

Participants discuss the implications of bidirectional edges and the potential redundancy in storing distances, but do not resolve the mathematical details or assumptions about graph connectivity.

Pithikos
Messages
55
Reaction score
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:

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?
 
Technology news on Phys.org
An adjacency matrix would be easier to work with than an array like the one you show. The matrix would need to be v X v, but you need entries in only half of the elements. (The distance from city vi to city vj is the same as the distance from city vj to vi.)
This matrix would need to hold .5v2 ints. To find the distance from vi to vj, get the value at D[j], where D is the name of the matrix.

Your array, as you show it doesn't save any space, since you will still need v*(v - 1) elements, plus you are storing the names of all the cities (it appears).
 
Mark44 said:
An adjacency matrix would be easier to work with than an array like the one you show. The matrix would need to be v X v, but you need entries in only half of the elements. (The distance from city vi to city vj is the same as the distance from city vj to vi.)
This matrix would need to hold .5v2 ints. To find the distance from vi to vj, get the value at D[j], where D is the name of the matrix.

Your array, as you show it doesn't save any space, since you will still need v*(v - 1) elements, plus you are storing the names of all the cities (it appears).


Why v*(v-1)? The list would have the same amount of elements as many edges there are. The graph is bidirectional so Athens->Stockholm is the same as Stockholm->Athens. As about the names I was thinking to make a list with the city names so that each city corresponds to an integer number.
 
In a graph with v nodes, each node can have an edge to the other v - 1 nodes. That makes v(v - 1) edges in all, counting all of them twice (i.e. the edge or path from, say Stockholm to Athens and the edge from Athens to Stockholm). If you got rid of duplicates, you would still have (1/2)v(v - 1) edges, assuming that each node is connected to each other node.

You really don't save anything with a one-dimensional array of distances as compared to a two-dimensional adjacency array.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K