[[+content_image]]
M
M
Michael2019-11-29 15:27:00
Python
Michael, 2019-11-29 15:27:00

How to store (write) an undirected graph?

There is a task in which it is necessary to check whether a path between two graph vertices is possible. The graph is written as a two-dimensional array:
[[a1,a2], [a2, a3], [a2, a4], [a1, a4], [a4, a5], [b1, b2], [b2, b3], [ b1, b3], [a3, b1]]
Next, using the breadth-first search algorithm, we look for a path, but the problem is that it finds a path only in one direction (for example, a1 b3 True, and b3 a1 False). This leads to the question, does writing a graph as a two-dimensional array correspond to a directed graph? If yes, then how to write this graph undirected? Or is there a different algorithm...

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
tsarevfs, 2019-11-29
@tsarevfs

You just have a list of edges. Apparently, the algorithm interprets them as oriented. You can try adding edges to the list pointing in the opposite direction.
It is usually more convenient to use a slightly different representation for traversal: an adjacency list or an adjacency matrix (easily googled). Perhaps your algorithm converts the original list into one of these structures. It is possible to slip back edges at this conversion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question