from dataclasses import dataclass
from typing import ClassVar
@dataclass
class AdjNode:
vertex: int
next: ClassVar = None
@dataclass
class Graph:
vertices: int
graph: ClassVar = []
def __post_init__(self):
self.graph = [None] * vertices
def add_edge(self, src, dest):
'''Добавление ребра в неориентированный граф'''
node = AdjNode(dest)
node.next = self.graph[src]
self.graph[src] = node
# Добавление исходного узла к целевому
node = AdjNode(src)
node.next = self.graph[dest]
self.graph[dest] = node
def print_graph(self):
'''Вывод графа'''
for i in range(self.vertices):
print(f'Список смежности вершины {i}\n', end='')
temp = self.graph[i]
while temp:
print(" -> {}".format(temp.vertex), end="")
temp = temp.next
print(" \n")
if __name__ == "__main__":
vertices = 5
graph = Graph(vertices)
graph.add_edge(0, 1)
graph.add_edge(0, 4)
graph.add_edge(1, 2)
graph.add_edge(1, 3)
graph.add_edge(1, 4)
graph.add_edge(2, 3)
graph.add_edge(3, 4)
graph.print_graph()
from collections import defaultdict
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Graph:
'''Класс ориентированного графа'''
graph: ClassVar
def __post_init__(self):
self.graph = defaultdict(list)
def addEdge(self, start, finish):
'''Добавления ребра к графу'''
self.graph[start].append(finish)
def BFS(self, vertex):
'''Функция вывода обхода'''
# Логический массив посещений
visited = [False] * (max(self.graph) + 1)
# очередь обхода BFS
queue = []
# добавление первого узла обхода
queue.append(vertex)
visited[vertex] = True
while queue:
vertex = queue.pop(0)
print(vertex, end=' ')
# Проход по соседним вершинам
for i in self.graph[vertex]:
if not visited[i]:
queue.append(i)
visited[i] = True
if __name__ == '__main__':
graph = Graph()
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(1, 2)
graph.addEdge(2, 0)
graph.addEdge(2, 3)
graph.addEdge(3, 3)
vertex: int = 3
print(f"Обход в ширину для стартового узла {vertex}")
graph.BFS(vertex)
from collections import defaultdict
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class graphraph:
'''Класс ориентированного графа'''
graphraph: ClassVar
def __post_init__(self):
self.graphraph = defaultdict(list)
def addEdgraphe(self, start, finish):
'''Добавления ребра к графу'''
self.graphraph[start].append(finish)
def DFSUtil(self, v, visited):
'''Функция вывода обхода'''
visited.add(v)
print(v, end=' ')
for neigraphhbour in self.graphraph[v]:
if neigraphhbour not in visited:
self.DFSUtil(neigraphhbour, visited)
def DFS(self, vertex):
'''Функция рекурсии обхода'''
visited = set()
self.DFSUtil(vertex, visited)
if __name__ == '__main__':
graph = graphraph()
graph.addEdgraphe(0, 1)
graph.addEdgraphe(0, 2)
graph.addEdgraphe(1, 2)
graph.addEdgraphe(2, 0)
graph.addEdgraphe(2, 3)
graph.addEdgraphe(3, 3)
vertex: int = 2
print(f"Обход в ширину для стартового узла {vertex}")
graph.DFS(vertex)
import sys
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Graph():
vertices: int
graph: ClassVar
def __post_init__(self):
self.graph = [[0 for column in range(self.vertices)]
for row in range(self.vertices)]
def printSolution(self, dist):
'''Вывод вершин'''
print("Вершина \tРастояние до источника")
for node in range(self.vertices):
print(node+1, "\t", dist[node])
def minDistance(self, dist, sptSet):
'''Вычисление минимального расстояния'''
min = sys.maxsize
for u in range(self.vertices):
if dist[u] < min and not(sptSet[u]):
min = dist[u]
min_index = u
return min_index
def dijkstra(self, src):
'''Вычисление минимальны= маршрутов'''
dist = [sys.maxsize] * self.vertices
dist[src] = 0
sptSet = [False] * self.vertices
for cout in range(self.vertices):
x = self.minDistance(dist, sptSet)
sptSet[x] = True
for y in range(self.vertices):
if self.graph[x][y] > 0 and\
not(sptSet[y]) and \
dist[y] > dist[x] + self.graph[x][y]:
dist[y] = dist[x] + self.graph[x][y]
self.printSolution(dist)
if __name__ == '__main__':
graph = Graph(6)
graph.graph = [[0, 3, 1, 3, 0, 0],
[3, 0, 4, 0, 0, 0],
[1, 4, 0, 0, 7, 5],
[3, 0, 0, 0, 0, 2],
[0, 0, 7, 0, 0, 4],
[0, 0, 5, 2, 4, 0]]
graph.dijkstra(0)
from collections import defaultdict
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Graph():
'''Класс ориентированного графа'''
vertices: int
graph: ClassVar
def __post_init__(self):
self.graph = defaultdict(list)
def addEdge(self, start, finish):
'''Добавления ребра к графу'''
self.graph[start].append(finish)
def isCyclicUtil(self, v, visited, recStack):
'''Поиск цикла'''
visited[v] = True
recStack[v] = True
for neighbour in self.graph[v]:
if not visited[neighbour]:
if self.isCyclicUtil(neighbour, visited, recStack):
return True
elif recStack[neighbour]:
return True
recStack[v] = False
return False
def isCyclic(self):
'''Проверка всех путей на цикличность'''
visited = [False] * (self.vertices + 1)
recStack = [False] * (self.vertices + 1)
for node in range(self.vertices):
if not visited[node]:
if self.isCyclicUtil(node, visited, recStack):
return True
return False
if __name__ == '__main__':
graph = Graph(4)
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(1, 2)
graph.addEdge(2, 0)
graph.addEdge(2, 3)
graph.addEdge(3, 3)
if graph.isCyclic():
print('График имеет цикл')
else:
print('График не имеет циклов')
from collections import defaultdict
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Graph():
'''Класс ориентированного графа'''
vertices: int
graph: ClassVar
def __post_init__(self):
self.graph = defaultdict(list)
def addEdge(self, start, finish):
'''Добавления двух ребер к графу'''
self.graph[start].append(finish)
self.graph[finish].append(start)
def isCyclicUtil(self, v, visited, parent):
'''Поиск цикла'''
visited[v] = True
for i in self.graph[v]:
if not visited[i]:
if(self.isCyclicUtil(i, visited, v)):
return True
elif parent != i:
return True
return False
def isCyclic(self):
'''Проверка всех путей на цикличность'''
visited = [False]*(self.vertices)
for i in range(self.vertices):
if not visited[i]:
if(self.isCyclicUtil
(i, visited, -1)):
return True
return False
if __name__ == "__main__":
graph = Graph(5)
graph.addEdge(1, 0)
graph.addEdge(1, 2)
graph.addEdge(2, 0)
graph.addEdge(0, 3)
graph.addEdge(3, 4)
if graph.isCyclic():
print("График имеет цикл")
else:
print("График не имеет циклов")
from collections import defaultdict
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Graph():
'''Класс ориентированного графа'''
vertices: int
graph: ClassVar
def __post_init__(self):
self.graph = defaultdict(list)
def addEdge(self, start, finish):
'''Добавления ребра к графу'''
self.graph[start].append(finish)
def topological_sort_util(self, v, visited, stack):
visited[v] = True
for i in self.graph[v]:
if not visited[i]:
self.topological_sort_util(i, visited, stack)
stack.append(v)
def topologicalSort(self):
'''Сортировка вершин'''
visited = [False]*self.vertices
stack = []
for i in range(self.vertices):
if not visited[i]:
self.topological_sort_util(i, visited, stack)
print(stack[::-1])
if __name__ == '__main__':
graph = Graph(6)
graph.addEdge(5, 2)
graph.addEdge(5, 0)
graph.addEdge(4, 0)
graph.addEdge(4, 1)
graph.addEdge(2, 3)
graph.addEdge(3, 1)
graph.topologicalSort()