How to represent a graph on a computer?

  • Thread starter Thread starter Pithikos
  • Start date Start date
  • Tags Tags
    Computer Graph
AI Thread Summary
The discussion focuses on the representation of a graph with vertices as cities and edges as distances between them. The original poster opted for an array structure to save space compared to an adjacency matrix, which they found to be inefficient due to its size. The array includes point1, point2, and distance for each connection, but concerns arise about the efficiency of searching for the shortest path later in the project. Critics argue that while the array may appear space-saving, it does not significantly reduce the number of elements needed, as it still requires storage for all edges and city names. They highlight that an adjacency matrix, despite its larger size, provides easier access to distance values and simplifies operations like finding shortest paths. The discussion emphasizes the trade-offs between space efficiency and ease of data manipulation in graph representations, particularly in bidirectional graphs where edges can be counted in both directions.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top