Skip to content

Instantly share code, notes, and snippets.

@pupba
Created January 11, 2023 11:25
Show Gist options
  • Save pupba/843dbb06b187240f5e405699b6f4689d to your computer and use it in GitHub Desktop.
Save pupba/843dbb06b187240f5e405699b6f4689d to your computer and use it in GitHub Desktop.
Direct, Undirect Graph
# Direct Graph
class DGraph:
def __init__(self):
self.__nodes = {}
def add(self,name,edgelist):
# name , edgelist
self.__nodes[name] = edgelist
def remove(self,name):
del self.__nodes[name]
def printAll(self):
for data in self.__nodes.keys():
print(f'{data} -> {self.__nodes[data]}')
# Undirect Graph
class UGraph:
def __init__(self):
self.__nodes = {}
def add(self,name,edgelist):
# [('A','B'),('A','C')]
self.__nodes[name] = edgelist
def remove(self,name):
del self.__nodes[name]
def printAll(self):
for data in self.__nodes.keys():
print(f'{data}의 인접 리스트 : {[i[1] for i in self.__nodes[data]]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment