Last active
April 13, 2017 18:56
-
-
Save dotapetro/a267cfc5ee1bd30a17171aae0f18e199 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, name): | |
self.name = name | |
self.neighbours = [] | |
def add_neighbour_to_self(self, neighbour): | |
self.neighbours.append(neighbour) | |
def find_neighbour_index(self, name): | |
for i in range(len(self.neighbours)): | |
if self.neighbours[i].name == name: | |
return i | |
def remove_neighbour(self, removable_neighbour): | |
neighbour = self.find_neighbour_index(removable_neighbour) | |
if not neighbour == None or neighbour == 0: | |
self.neighbours.pop(neighbour) | |
def add_neighbour(self, AnotherNode): | |
self.add_neighbour_to_self(AnotherNode) | |
def show_neighbours(self): | |
return [i.name for i in self.neighbours] | |
def __repr__(self): | |
return "NODE object named '{0}', with neighbours {1}".format(self.name, [i.name for i in self.neighbours]) | |
class Graph: | |
def __init__(self): | |
self.Nodes = [] | |
self.node_count = 1 | |
def add_Node(self, node): | |
node = Node(str(node)) | |
self.Nodes.append(node) | |
self.node_count += 1 | |
def add_unnamed_Node(self): | |
node = Node(str(self.node_count)) | |
self.Nodes.append(node) | |
self.node_count += 1 | |
def add_nude(self, node): | |
self.Nodes.append(node) | |
self.node_count += 1 | |
def find_Node(self, name): | |
for i in self.Nodes: | |
if i.name == name: | |
return i | |
def add_edge(self, name_1, name_2): | |
try: | |
Node_1 = self.find_Node(name_1) | |
Node_2 = self.find_Node(name_2) | |
Node_1.add_neighbour(Node_2) | |
Node_2.add_neighbour(Node_1) | |
except AttributeError: | |
print("INCORRECT") | |
def remove_edge(self, name_1, name_2): | |
try: | |
Node_1 = self.find_Node(name_1) | |
Node_2 = self.find_Node(name_2) | |
Node_1.remove_neighbour(Node_2.name) | |
Node_2.remove_neighbour(Node_1.name) | |
except AttributeError: | |
raise AttributeError("Incorrect: there in none one of Nodes, fix it and try again") | |
def has_edge(self, name_1, name_2): | |
try: | |
Node_1 = self.find_Node(name_1) | |
Node_2 = self.find_Node(name_2) | |
if Node_1 in Node_2.neighbours and Node_2 in Node_1.neighbours: | |
return True | |
return False | |
except AttributeError: | |
return False | |
def get_neighbours(self, name): | |
Node_1 = self.find_Node(name) | |
return Node_1.neighbours | |
def show_Nodes(self): | |
return self.Nodes | |
def main(): | |
graph = Graph() # Создает граф | |
graph.add_Node("vsause") # Добавляет ноду vsause | |
graph.add_Node("michael") # Добавляет ноду michael | |
graph.add_unnamed_Node() # Добавляет безымянную ноду на 3 слот массива нод графа | |
graph.add_edge("vsause", "michael") # Создает грань между vsause и michael | |
print(graph.show_Nodes()) # Показывает нашы текущие ноды | |
graph.remove_edge("vsause", "michael") # Убирает грань между vsause и michael | |
print(graph.show_Nodes()) # -- | |
graph.add_Node("hello") # Добавляет ноду hello | |
graph.add_Node("here") # Добавляет ноду here | |
graph.add_edge("hello", "vsause") # Создвет грань между hello и vsause | |
graph.add_edge("michael", "here") # Создвет грань между michael и here | |
print(graph.show_Nodes()) | |
print(graph.has_edge("hello", "vsause")) # Проверяет есть ли грань между hello и vsause, если есть возвращает True | |
print(graph.get_neighbours("michael")) # Возвращает объекты-соседи michael (hello) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment