I was wondering how to create an adjacency list class
Here is what I have so far:
class AdjNode:def __init__(self, value):self.vertex = valueself.next = Noneclass Graph:def __init__(self):# Add edgesdef add_edge(self, u, v):node = AdjNode(v)node.next = self.graph[u]self.graph[u] = node
Here is a very verbose example; not exactly what you want, but I feel it's a start, and as mentioned in the comments, uses a standard list.
I think you should look into classes further and attempt to understand OOP; I think you'd be doing yourself an injustice by not understanding what is being asked but rather asking what is being asked. - JUST MY OPINION
class Graph:def __init__(self):self.graph = {}def addEdge(self, u, v):if u in self.graph:self.graph[u].append(v)else:self.graph[u] = [v]def deleteEdge(self, u, v):if u in self.graph:if v in self.graph[u]:self.graph[u].remove(v)else:print("Edge doesn't exist")else:print("Edge doesn't exist")def getNeighbors(self, u):return self.graph[u]def isAdjacent(self, u, v):if u in self.graph:if v in self.graph[u]:return Trueelse:return Falseelse:return Falsedef printGraph(self):print(self.graph)g = Graph()
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(2, 3)
g.addEdge(2, 4)
g.addEdge(3, 4)
g.addEdge(3, 5)
g.addEdge(4, 5)
g.addEdge(5, 6)
g.addEdge(5, 7)
g.addEdge(6, 7)
g.printGraph()
print(g.getNeighbors(1))
print(g.isAdjacent(1, 2))
print(g.isAdjacent(1, 5))
print(g.isAdjacent(5, 6))
g.deleteEdge(1, 2)
g.deleteEdge(1, 5)
g.printGraph()
Output:
{1: [2, 3], 2: [3, 4], 3: [4, 5], 4: [5], 5: [6, 7], 6: [7]}
[2, 3]
True
False
True
Edge doesn't exist
{1: [3], 2: [3, 4], 3: [4, 5], 4: [5], 5: [6, 7], 6: [7]}