简体   繁体   English

如何找到入射到特定顶点的边列表

[英]How to find the list of edges incident to a particular vertex

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.数组a是邻接矩阵,参数v是无向图的顶点。 If there is an edge between vertex v and j then we add the edge incident to vertex v .如果顶点vj之间有一条边,那么我们将边添加到顶点v

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 using i in the first place), and then check all vertices that are connected.将你的i设置为 v (因为你一开始甚至没有使用i ),然后检查所有连接的顶点。

public static ArrayList<Integer> incidentEdges(int v) {
   ArrayList<Integer> result = new ArrayList<>();
   for(int i = 0; i < a[v].length; i++) {
     if(a[v][i] == 1) {
        result.add(a[v].get(i));
     }
   }
   return result;
}

Method 2: Generating an Adjacency List方法 2:生成邻接表

If you get control of the input (ie before making the adjacency matrix), what you want is an adjacency list : Where each list[start] points to an ArrayList<Integer> (representing the connected vertices).如果您控制了输入(即在制作邻接矩阵之前),您想要的是一个邻接列表:其中每个list[start]指向一个ArrayList<Integer> (代表连接的顶点)。 I would avoid the use of ArrayList since the number of vertices is already known.我会避免使用ArrayList因为顶点的数量是已知的。 So I would instead use ArrayList<Integer>[] list instead.所以我会改用ArrayList<Integer>[] list This definitely makes it easier to code.这无疑使编码更容易。

Here is an example of generating the adjacency list from an adjacency matrix这是从邻接矩阵生成邻接列表的示例

static ArrayList<Integer>[] list;

public static ArrayList<Integer>[] generateAl(int v) {
   list = new ArrayList[a.length];
   for(int i = 0; i < a.length; i++) {
      list[i] = new ArrayList<>();
      for(int j = 0; j < a[i].length; j++) {
         if(a[i][j] == 1) {
            list[i].add(j);
         }
      }
   }
   return list;
}

public static ArrayList<Integer> incidentEdges(int v) {
   return list[v];
}

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

相关问题 如何在图中找到特定顶点的度数? - How to find the degree of a particular vertex in graph? 如何使用Java查找图的中心(顶点,该顶点与其他每个顶点相连,但边指向图的中心) - how to Find the center of graph (vertex, that is connected with every other vertex, but edges are directed to the center of graph) with java JUNG-MouseOver如何更改顶点和边的厚度 - JUNG - How can MouseOver change thicknes of Vertex and Edges 如何在单个遍历中从一个顶点创建多个边 - How to create multiple edges from one vertex in single traversal 两个顶点之间的Gremlinepipeline边缘 - Gremlinepipeline Edges between two Vertex 如何以编程方式查找继承了 Java 中特定 class 的类的列表 - How to programmatically find list of classes that have inherited a particular class in Java 如何在来自特定顶点的有向图中执行 BFS 或 DFS,某些顶点的出度为 0? - How to do a BFS or DFS in directed graph from particular vertex with some vertex having outdegree 0? 如何找到2个给定顶点之间的边缘连通性 - How to find edge connectivity between 2 given vertex 如何创建具有相同权重和相同源顶点的多个边? - How can I create multiple edges with the same weight and same source vertex? 如何从包含顶点和边的文本文件创建图形? - How can I create a graph from Text File containing the vertex and edges?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM