如何找到事件的边缘列表到特定顶点
我尝试了以下内容,但我不确定它是否正确。
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
public static ArrayList<ArrayList<Integer>> incidentEdges(int v) {
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
if(a[v][j] == 1) {
list.get(v).get(new Edge(start, destination));
list.get(j).get(new Edge(start, destination);
}
}
}
return list;
}
阵列 a 是邻接矩阵,参数 v 是无向图的顶点。如果顶点 v 和 j 之间有边缘,则我们将边缘入射添加到顶点 v 。
I tried the following but I'm not sure that it is correct.
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
public static ArrayList<ArrayList<Integer>> incidentEdges(int v) {
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
if(a[v][j] == 1) {
list.get(v).get(new Edge(start, destination));
list.get(j).get(new Edge(start, destination);
}
}
}
return list;
}
The array a is an adjacency matrix and the parameter v is the vertex of an undirected graph. If there is an edge between vertex v and j then we add the edge incident to vertex v.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法1:查询邻接矩阵,
由于您已经将边缘存储在邻接矩阵中,因此您可以简单地查询它。将您的
i
设置为V(因为您甚至首先都不使用i
),然后检查所有连接的顶点。方法2:生成邻接列表
,如果您可以控制输入(即制作邻接矩阵之前),则想要的是相邻列表:其中每个
list [start]
指向arraylist&lt; integer&gt;
(表示连接的角度)。我会避免使用arrayList
,因为顶点的数量已经知道。因此,我将使用arrayList&lt; integer&gt; [] list
。这绝对可以使编码更容易。这是从邻接矩阵生成邻接列表的示例
Method 1: Query Adjacency Matrix
Since you have already stored the edges in an adjacency matrix, you can simply query it. Set your
i
to v (since you are not even usingi
in the first place), and then check all vertices that are connected.Method 2: Generating an Adjacency List
If you get control of the input (i.e. before making the adjacency matrix), what you want is an adjacency list: Where each
list[start]
points to anArrayList<Integer>
(representing the connected vertices). I would avoid the use ofArrayList
since the number of vertices is already known. So I would instead useArrayList<Integer>[] list
instead. This definitely makes it easier to code.Here is an example of generating the adjacency list from an adjacency matrix