December 2023
Intermediate to advanced
504 pages
11h 43m
English
I demonstrated how a hash table can be used to implement a graph, but going forward, we’ll work with an object-oriented approach.
Here’s the beginning of an object-oriented graph implementation, using Python:
| | class Vertex: |
| | def __init__(self, value): |
| | self.value = value |
| | self.adjacent_vertices = [] |
| | |
| | def add_adjacent_vertex(self, vertex): |
| | self.adjacent_vertices.append(vertex) |
The Vertex class has two primary attributes, the value and an array of adjacent_vertices. In our social network example, each vertex represents a person, and the value might be a string containing the person’s name. With a more complex application, we’d probably want to store multiple pieces of data inside a vertex, ...
Read now
Unlock full access