Build a Graph (Adjacency List)

Implement a Graph class using an adjacency list: add_vertex(vertex), add_edge(v1, v2) (undirected - record the connection on both vertices), neighbours(vertex) (returning a sorted list), and degree(vertex) (the number of edges connected to it).

Read the number of edges, then that many "V1 V2" pairs, building the graph. Then read one vertex to query, and print its sorted neighbours (comma-and-space separated) followed by its degree.

main.py
Loading editor…

Console

Run your code to see output here.