简体   繁体   中英

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.

UPDATE C version

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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