0

Как должен выглядеть список смежности python, если в одном графе есть ориентированные дуги и неориентированные?

Например есть граф A—>C | B Ничего лучше чем {‘o’: (A,C)} {‘n’: (A,B)} я не придумал Есть ещё решения? Кстати в матрице смежности решение решение очевидно ..ABC A011 B100 C000 Здесь все видно, нет ничего лишнего, можно ли так сделать в списке смежности?

16th Dec 2022, 3:22 PM
Dima
2 Réponses
+ 4
Yes, it is possible to represent a graph with a mix of directed and undirected edges using an adjacency list in Python. One way to do this is to store the edges as a dictionary, where the keys are the vertex names and the values are lists of tuples representing the edges. Each tuple can contain the vertex name at the other end of the edge and a Boolean value indicating whether the edge is directed or not. For example, you could represent the graph in your question like this: graph = { 'A': [(C, True)], 'B': [(A, False), (B, False)], 'C': [] }
17th Dec 2022, 8:00 PM
Sadaam Linux
Sadaam Linux - avatar
+ 4
Alternatively, you could use a dictionary of dictionaries to represent the edges, where the keys are the vertex names and the values are dictionaries mapping the names of neighboring vertices to Booleans indicating whether the edges are directed or not. graph = { 'A': {'C': True}, 'B': {'A': False, 'B': False}, 'C': {} } Both of these representations are efficient for storing and accessing the edges of the graph, and you can use them to perform various graph algorithms.
17th Dec 2022, 8:01 PM
Sadaam Linux
Sadaam Linux - avatar