graphs.graph_list

Attributes

T

Classes

GraphAdjacencyList

Adjacency List type Graph Data Structure that accounts for directed and undirected

Module Contents

class graphs.graph_list.GraphAdjacencyList(directed: bool = True)

Bases: Generic[T]

Adjacency List type Graph Data Structure that accounts for directed and undirected Graphs. Initialize graph object indicating whether it’s directed or undirected.

Directed graph example: >>> d_graph = GraphAdjacencyList() >>> print(d_graph) {} >>> d_graph.add_edge(0, 1) {0: [1], 1: []} >>> d_graph.add_edge(1, 2).add_edge(1, 4).add_edge(1, 5) {0: [1], 1: [2, 4, 5], 2: [], 4: [], 5: []} >>> d_graph.add_edge(2, 0).add_edge(2, 6).add_edge(2, 7) {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} >>> d_graph {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} >>> print(repr(d_graph)) {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []}

Undirected graph example: >>> u_graph = GraphAdjacencyList(directed=False) >>> u_graph.add_edge(0, 1) {0: [1], 1: [0]} >>> u_graph.add_edge(1, 2).add_edge(1, 4).add_edge(1, 5) {0: [1], 1: [0, 2, 4, 5], 2: [1], 4: [1], 5: [1]} >>> u_graph.add_edge(2, 0).add_edge(2, 6).add_edge(2, 7) {0: [1, 2], 1: [0, 2, 4, 5], 2: [1, 0, 6, 7], 4: [1], 5: [1], 6: [2], 7: [2]} >>> u_graph.add_edge(4, 5) {0: [1, 2],

1: [0, 2, 4, 5], 2: [1, 0, 6, 7], 4: [1, 5], 5: [1, 4], 6: [2], 7: [2]}

>>> print(u_graph)
{0: [1, 2],
 1: [0, 2, 4, 5],
 2: [1, 0, 6, 7],
 4: [1, 5],
 5: [1, 4],
 6: [2],
 7: [2]}
>>> print(repr(u_graph))
{0: [1, 2],
 1: [0, 2, 4, 5],
 2: [1, 0, 6, 7],
 4: [1, 5],
 5: [1, 4],
 6: [2],
 7: [2]}
 >>> char_graph = GraphAdjacencyList(directed=False)
 >>> char_graph.add_edge('a', 'b')
 {'a': ['b'], 'b': ['a']}
 >>> char_graph.add_edge('b', 'c').add_edge('b', 'e').add_edge('b', 'f')
 {'a': ['b'], 'b': ['a', 'c', 'e', 'f'], 'c': ['b'], 'e': ['b'], 'f': ['b']}
 >>> char_graph
 {'a': ['b'], 'b': ['a', 'c', 'e', 'f'], 'c': ['b'], 'e': ['b'], 'f': ['b']}
__repr__() str
add_edge(source_vertex: T, destination_vertex: T) GraphAdjacencyList[T]

Connects vertices together. Creates and Edge from source vertex to destination vertex. Vertices will be created if not found in graph

adj_list: dict[T, list[T]]
directed = True
graphs.graph_list.T