Performance 有向图的顶点和边反转

Performance 有向图的顶点和边反转,performance,algorithm,graph,directed-graph,Performance,Algorithm,Graph,Directed Graph,所以我正在研究这个问题: 这是我到目前为止所拥有的 a) Create a new graph with the same size 2-dimensional array, B. Iterate through the original graph's 2-dimensional array, A, which is of size VxV, where V is the number of vertices in the graph. If A[i][j] is true, then a

所以我正在研究这个问题:

这是我到目前为止所拥有的

a) Create a new graph with the same size 2-dimensional array, B. Iterate 
through the original graph's 2-dimensional array, A, which is of size VxV, 
where V is the number of vertices in the graph. If A[i][j] is true, then an 
edge exists there. Set B[j][i] true in the new reversed graph's matrix. 
The algorithm will be of complexity V^2 since it needs to iterate through 
all of the 2-dimensional array.

b) Create a new graph with an empty array, B, of size V, where V is the 
number of vertices. Iterate through the array of lists in the original 
graph, A. if there is a list  at A[i] then iterate through each one and i to
the corresponding B[x] where x is the integer in the list. The algorithm 
will be complexity of V + E since it needs to iterate through an array of 
size V, and then a total list elements of size E.
首先,我想再核对一下我的答案。我不太熟悉有向图,也不太擅长发现算法的效率/复杂性。我想我做得对,但是如果我需要的话,我想得到一些帮助。我也在寻找任何想法,使它更有效。这些是我脑海中第一个出现的算法,所以我觉得可能有更好的方法


谢谢你的a是正确的。在矩阵语言中,您正在寻找矩阵A的转置。需要考虑的一个小细节是,是否需要将B的非边项设置为false。这取决于语言,尽管我认为大多数语言在初始化内存时都会将B的所有条目设置为false或0。其复杂性实际上是OV^2

你的b字写得不好。但我认为你有这个想法。你必须在B[x]的列表中加入一些东西。更好地描述x。复杂性确实是OV+E

你没办法让他们更有效率。在第一种情况下,必须处理每一对顶点,在第二种情况下必须处理每一条边。在邻接矩阵的情况下,可以在查找之前交换顶点的顺序,因此,如果询问是否有从x,y开始的边,则返回y,x的答案,但如果这样做的次数超过V^2次,则不会领先。与邻接列表相同