Skip to content

Instantly share code, notes, and snippets.

@JustinStitt
Created November 8, 2022 07:29
Show Gist options
  • Save JustinStitt/f7647f5e42860dc7778bc910a5147f1c to your computer and use it in GitHub Desktop.
Save JustinStitt/f7647f5e42860dc7778bc910a5147f1c to your computer and use it in GitHub Desktop.
Quick Graph Demo in Python
class Vertex:
def __init__(self, data):
self.data = data
def __repr__(self):
return f"{self.data}"
class Graph:
def __init__(self):
self.matrix: dict[Vertex, set[Vertex]] = dict()
def addVertex(self, data):
v = Vertex(data)
self.matrix[v] = set()
return v
def addEdge(self, u, v):
self.matrix[u].add(v)
self.matrix[v].add(u)
g = Graph()
v1 = g.addVertex(4)
v2 = g.addVertex(7)
v3 = g.addVertex(1)
v4 = g.addVertex(3)
g.addEdge(v1, v2)
g.addEdge(v1, v4)
print(g.matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment