Graphs are one of the most expressive data structures in computer science. A graph is a collection of vertices (also called nodes) connected by edges. Graphs can be directed — where each edge flows from one vertex to another in a single direction — or undirected, where connections are bidirectional. Edges can carry a weight, a numeric value that models cost, distance, or priority. Real-world applications include GPS navigation (road networks), social networks (friend connections), airline routing, and dependency resolution in build systems.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/ed/llms.txt
Use this file to discover all available pages before exploring further.
Key Concepts
| Term | Definition |
|---|---|
| Vertex | A node in the graph, identified by a unique id. |
| Edge | A directed connection from one vertex (from) to another (to). |
| Weight | A numeric value assigned to an edge, representing cost or distance. Defaults to 0. |
| Directed Graph | A graph where every edge has an explicit direction (from → to). |
| Adjacency List | A dictionary-based structure where each vertex stores its neighbors and edge weights. |
The Vertex Class
Each node in the graph is represented by aVertex object. It stores its own identifier and a dictionary mapping every outgoing neighbor to the corresponding edge weight.
self.id— the unique label for this vertex (e.g.,'A','B').self.adjacent— a Python dictionary that acts as the adjacency list. Keys are neighbor IDs; values are edge weights.add_neighbor(neighbor, weight)— registers a directed edge from this vertex toneighborwith the givenweight.get_connections()— returns the full adjacency dictionary.get_id()— returns this vertex’s identifier.
The Graph Class
TheGraph class is the container for all vertices. It maintains a master dictionary (vertex_list) that maps each vertex ID to its Vertex object, and a counter (num_vertex) that tracks how many vertices exist.
self.vertex_list— a dictionary keyed by vertex ID, holding everyVertexobject in the graph.self.num_vertex— an integer counter incremented each time a new vertex is added.add_vertex(id)— creates a newVertex, inserts it intovertex_list, and returns it.get_vertex(id)— looks up and returns theVertexfor a given ID, orNoneif it does not exist.add_edge(from_id, to_id, weight)— registers a directed, weighted edge. If either endpoint does not yet exist, it is created automatically.get_vertices()— returns all vertex IDs currently in the graph.
Quick Start Example
The following snippet builds a small directed weighted graph and prints all vertex IDs:add_edge automatically creates any vertices that do not already exist. You never need to call add_vertex manually before calling add_edge.