如何找到事件的边缘列表到特定顶点

发布于 2025-01-27 02:27:03 字数 633 浏览 3 评论 0原文

我尝试了以下内容,但我不确定它是否正确。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2025-02-03 02:27:03

方法1:查询邻接矩阵,

由于您已经将边缘存储在邻接矩阵中,因此您可以简单地查询它。将您的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;
}

方法2:生成邻接列表

,如果您可以控制输入(即制作邻接矩阵之前),则想要的是相邻列表:其中每个list [start]指向arraylist&lt; integer&gt;(表示连接的角度)。我会避免使用arrayList,因为顶点的数量已经知道。因此,我将使用arrayList&lt; integer&gt; [] list。这绝对可以使编码更容易。

这是从邻接矩阵生成邻接列表的示例

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];
}

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.

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

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 an ArrayList<Integer> (representing the connected vertices). I would avoid the use of ArrayList since the number of vertices is already known. So I would instead use ArrayList<Integer>[] list instead. 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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文