How can we implement the other graph representations?


#1

Question

In the context of this lesson, how can we implement the other graph representations?

Answer

This lesson covered the object representation of graphs using a Vertex and Graph class. The other two methods of representing a graph are as follows,

Adjacency Matrix
This is usually implemented using a list of lists.

graph = [
  [0, 1, 1],
  [1, 0, 1],
  [1, 1, 0]
]

Adjacency List
This can be implemented in Python using a dictionary, which maps the vertices to a list of their adjacent vertices.

graph = {
  0: [1, 2],
  1: [0, 2],
  2: [0, 1]
}