how do we represent an edge list given that we have a graph with nodes and edges.am not talking about adjacency lists
what is an edge list?not an adjacency list.. and how do we represent an edge list in C programming provided that we are given a graph with nodes and 开发者_运维知识库edges?
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;
}
精彩评论