简体   繁体   English

提取图中奇数度顶点的边

[英]Extracting the edges of odd degree vertices in a graph

I have a graph as follows, represented by an adjacency matrix MyCustomVector<MyCustomVector<int>> graph :我有一个如下图,由邻接矩阵MyCustomVector<MyCustomVector<int>> graph

0       1       2       3       4       5       6       7       8       9       10      11      12      13      14      15

1       0       1       1       INF     INF     INF     INF     INF     INF     INF     INF     INF     INF     INF     INF
2       1       0       1       1       1       INF     INF     INF     INF     INF     INF     INF     INF     INF     INF
3       1       1       0       INF     INF     1       INF     INF     INF     INF     INF     INF     INF     INF     INF
4       INF     1       INF     0       1       INF     INF     INF     1       INF     INF     INF     INF     INF     INF
5       INF     1       INF     1       0       1       INF     INF     INF     1       1       INF     INF     INF     INF
6       INF     INF     1       INF     1       0       1       INF     1       1       INF     1       INF     INF     INF
7       INF     INF     INF     INF     INF     1       0       1       INF     INF     INF     INF     1       1       1
8       INF     INF     INF     INF     INF     INF     1       0       INF     INF     INF     INF     INF     INF     1
9       INF     INF     INF     1       INF     1       INF     INF     0       INF     INF     INF     INF     INF     INF
10      INF     INF     INF     INF     1       1       INF     INF     INF     0       INF     INF     INF     INF     INF
11      INF     INF     INF     INF     1       INF     INF     INF     INF     INF     0       1       INF     INF     INF
12      INF     INF     INF     INF     INF     1       INF     INF     INF     INF     1       0       INF     INF     INF
13      INF     INF     INF     INF     INF     INF     1       INF     INF     INF     INF     INF     0       1       INF
14      INF     INF     INF     INF     INF     INF     1       INF     INF     INF     INF     INF     1       0       1
15      INF     INF     INF     INF     INF     INF     1       1       INF     INF     INF     INF     INF     1       0

The leftmost and topmost column and row just represent the # node.最左边和最上面的列和行只代表# 节点。 I thought it was easier to see.我认为它更容易看到。 All edges are undirected and have a weight of one.所有边都是无向的,并且权重为 1。 INF indicates that there node A and node B do not share a singular edge. INF表示节点 A 和节点 B 不共享一条奇异边。

I can calculate all of the odd degree vertices, which are { 3 4 5 7 14 15 } and they are contained in a vector that I have MyCustomVector<int> oddVertices .我可以计算所有奇数度顶点,它们是{ 3 4 5 7 14 15 }并且它们包含在我拥有的向量中MyCustomVector<int> oddVertices What I want to do is build a subgraph of this graph with only these vertices and their edges, so something like this.我想要做的是建立这个图的子图,只有这些顶点和它们的边,所以像这样。

0       3       4       5       7       14       15   
3       0       INF     INF     INF     INF      INF
4       INF     0       1       INF     INF      INF
5       INF     1       0       INF     INF      INF
7       INF     INF     INF     0       1        1  
14      INF     INF     INF     1       0        1                                             
15      INF     INF     INF     1       1        0

So that I can run the Floyd Warshall Algorithm on this graph and get这样我就可以在这张图上运行 Floyd Warshall 算法并得到

0       3       4       5       7       14       15   
3       0       2       2       2       3        3
4       2       0       1       3       4        4
5       2       1       0       2       1        1
7       2       3       2       0       1        1  
14      3       4       3       1       0        1                                             
15      3       4       3       1       1        0

I am having trouble managing the adjacency matrix and grabbing the columns that I need.我在管理邻接矩阵和获取我需要的列时遇到了麻烦。 Essentially what I want to do is基本上我想做的是

  1. Iterate through the graph遍历图
  2. Notice that the node we are currently on is contained in oddVertices请注意,我们当前所在的节点包含在oddVertices
  3. Add the other nodes that are contained in oddVertices .添加包含在oddVertices中的其他节点。

I cannot use any other data structure than MyCustomVector , and it only has basic functionality like [] , size() , pop_back() and push_back() .我不能使用除MyCustomVector之外的任何其他数据结构,它只有基本功能,如[]size()pop_back()push_back()

Simply extract the rows and columns corresponding to the subset of indices:只需提取对应于索引子集的行和列:

using std::vector;

/*
** input:
**   adjacency list                adj
**   subset of node indices        v
** output:
**   adj list of induced subgraph  subadj
*/
vector<vector<int>> get_subgraph(vector<vector<int>> const &adj, vector<int> const &v)
{
    vector<vector<int>> subadj(v.length(), vector<int>(v.length()));
    for (unsigned int i = 0; i < v.size(); ++i)
        for (unsigned int j = 0; j < v.size(); ++j)
        {
            subadj[i][j] = adj[v[i]][v[j]];
        }
    return subadj;
}

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

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