简体   繁体   English

考虑到我们有一个带有节点和edges.am的图,我们如何表示边缘列表。

[英]how do we represent an edge list given that we have a graph with nodes and edges.am not talking about adjacency lists

什么是边缘列表?不是邻接列表。.如果给定一个带有节点和边缘的图,我们如何在C编程中表示边缘列表?

Here is the base structure 这是基本结构

struct Edge
{
    int id;
    int weight; // If you need one
    vector<Edge *> neighbours; // List of references on your neightbours
}

vector<Edge> graph;

But as Michael noticed it does look like a homework :) Take a look at boot graphs library. 但是正如Michael所注意到的,它看起来确实像是一项家庭作业:)看一下启动图库。

UPDATE C version UPDATE C版本

struct Edge
{
    int id;
    int weight; // If you need one
    Edge *next; // Next edge in the list
    Edge *neighbours; // List of neightbours
}

Edge *graph;

Using the definition of edge list found here , and assuming undirected edges, emulating the "human" representation would be a good first attempt: 使用此处找到的边缘列表的定义,并假设无方向的边缘,模拟“人”表示将是一个不错的尝试:

typedef struct UndirectedEdge {
    int ends[2];
};

Where your vertices are all numbered within the range of int . 您的所有顶点都在int范围内编号的地方。 If they're directed: 如果有指示:

typedef struct DirectedEdge {
    int from;
    int to;
}

Add other properties as required, with type appropriate to your problem: 根据需要添加其他属性,并使用适合您问题的类型:

typedef struct WeightedEdge {
    size_t from;
    size_t to;
    double weight;
}

Note that a list of vertices isn't required, unless to map integer vertex indices to human-readable labels if they exist in your initial problem. 请注意,不需要顶点列表,除非将整数顶点索引映射到人类可读标签(如果它们存在于初始问题中)。 Furthermore, you should define a suitable comparison function for your edge list to ensure uniqueness of your edges depending on properties of your graph, such as directedness. 此外,您应该为边列表定义合适的比较函数,以确保边的唯一性取决于图形的属性,例如有向性。

typedef struct EdgeList {
    size_t edge_count;
    EdgeType *edges;
}

_Bool undirected_edge_equal(UndirectedEdge *this, UndirectedEdge *other) {
    return this->ends[0] == other->ends[0] && this->ends[1] == other->ends[1]
        || this->ends[0] == other->ends[1] && this->ends[1] == other->ends[0]
}

_Bool directed_edge_equal(DirectedEdge *this, DirectedEdge *other) {
    return this->from == other->from && this->to == other->to;
}

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

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