简体   繁体   English

加权单向图的顶点表示

[英]Vertex representation of a weighted unidirectional graph

I am using adjacency matrix to represent all the vertex of my weighted unidirectional large graph.我正在使用邻接矩阵来表示我的加权单向大图的所有顶点。 In this graph no edge connects a vertex to itself.在此图中,没有边将顶点连接到自身。 This makes all the diagonal elements of my adjacency matrix null .这使得我的邻接矩阵的所有对角线元素为null As my graph is large so in adjacency matrix i need not to save any elements in left triangle.由于我的图很大,所以在邻接矩阵中我不需要在左三角形中保存任何元素。 Below is a small sample graph with adjacency matrix.下面是一个带有邻接矩阵的小样本图。这是带有邻接矩阵的样本小图

In an unidirectional graph left triangle is just mirror image of right triangle.在单向图中,左三角形只是直角三角形的镜像。 ie adjacency_matrix[i][j] , adjacency_matrix[j][i] are same.adjacency_matrix[i][j] , adjacency_matrix[j][i]是一样的。 so why to store the left triangle.那么为什么要存储左三角形。 for a large graph this trick can save so much memory. At the same time diagonal elements are also zero since no edge connects a vertex to itself.对于一个大图,这个技巧可以节省这么多 memory。同时对角线元素也为零,因为没有边将顶点连接到自身。 ie adjacency_matrix[i][i] are zero.adjacency_matrix[i][i]为零。 But how can i implement this?但是我该如何实现呢? can 2D array be used here?这里可以使用二维数组吗?

Java doesn't really have 2D arrays, although there is syntatic sugar for allocating an array of arrays. Java 并没有真正的 2D arrays,尽管有用于分配 arrays 数组的语法糖。

You probably just want:你可能只想:

int[][] weight = new int[N][];
for (int i = 0; i < N; i++) weight[i] = new int[N-1-i];

That will allocate the triangle you want.这将分配你想要的三角形。 Then just index row r, col c at weight[r][cr-1].然后只索引行 r,col c at weight[r][cr-1]。

The other option is just to use a single array with另一种选择是只使用一个数组

int[] weight = new int[N*(N-1)/2];

The indexing can be a bit more complicated to compute, but less allocation and pointer overhead.索引计算起来可能有点复杂,但分配和指针开销更少。

You can use a jagged array.您可以使用锯齿状数组。

int[][] matrix = new int[N][];
for (int i = 1; i <= N; i++) {
   matrix[i] = new int[N - i + 1];
   for (int j = 1; j <= N - i + 1; j++)
       matrix[i][j] = edgeValue;
}

Basically you allocate for each row as much as you need.基本上,您根据需要为每一行分配。

PS Maybe I messed up some boundaries here, but you should still get the main point:) PS 也许我在这里搞砸了一些界限,但你仍然应该明白要点:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM