Querying the adjacency of a Graph: neighbors, incident edges, and edge endpoints.
Overview
Using the four-vertex / five-edge graph from Tutorial_108_Graph, this console tutorial prints the three basic graph adjacency relationships:
- the incident vertices (neighbors) of each vertex
- the incident edges of each vertex
- the two end points of each edge As in Tutorial_105_SurfaceMesh_Connectivity, each traversal is written both as a range-based for loop and as an explicit circulator, chosen with the USE_FOR_LOOP macro.
The technique
A Graph has no faces or halfedges, so its connectivity is simpler than a surface mesh's: it is essentially an adjacency list. Each vertex remembers the edges touching it; following an edge to its other endpoint gives a neighbor. vertices(v) walks those neighbors, edges(v) walks the incident edges, and vertex(e, 0/1) returns an edge's two endpoints. A circulator iterates a vertex's incident edges in order and wraps back to the start (it has no natural end), which is why the explicit form saves the start handle and loops until it returns there; the for-loop wrapper hides that detail. These are the building blocks for graph algorithms such as traversal, shortest paths, and connected components.
Easy3D API
- Graph::vertices(v): the neighboring vertices of v.
- Graph::edges(v): the edges incident to v.
- Graph::vertex(e, i): the i-th endpoint (0 or 1) of edge e.
- Graph::vertices()/edges(): ranges over all vertices / all edges.
- VertexAroundVertexCirculator / EdgeAroundVertexCirculator: the explicit traversal forms.
Running it
Console-only. It prints a titled section for each query, listing element handles (e.g. "v0", "e1") for every vertex and edge. The graph is heap-allocated and deleted at the end.
27#include <easy3d/core/graph.h>
28#include <easy3d/util/initializer.h>
82Graph *old_graph_from_previous_example() {
84 auto graph =
new Graph;
93 graph->add_edge(v0, v1);
94 graph->add_edge(v1, v2);
95 graph->add_edge(v2, v3);
96 graph->add_edge(v3, v0);
97 graph->add_edge(v1, v3);
102int main(
int argc,
char **argv) {
106 Graph *graph = old_graph_from_previous_example();
108 std::cout <<
"----------------------------------------\n";
109 std::cout <<
"The incident vertices of each vertex" << std::endl;
110 std::cout <<
"----------------------------------------\n";
113 for (
auto v : graph->vertices()) {
114 std::cout <<
"incident vertices of vertex " << v <<
": ";
117 for (
auto vv : graph->vertices(v))
118 std::cout << vv <<
" ";
126 std::cout << vv <<
" ";
128 }
while (cir != end);
130 std::cout << std::endl;
133 std::cout <<
"\n--------------------------------------\n";
134 std::cout <<
"The incident edges of each vertex" << std::endl;
135 std::cout <<
"----------------------------------------\n";
138 for (
auto v : graph->vertices()) {
139 std::cout <<
"incident edges of vertex " << v <<
": ";
142 for (
auto h : graph->edges(v))
143 std::cout << h <<
" ";
149 std::cout << h <<
" ";
151 }
while (cir != end);
153 std::cout << std::endl;
156 std::cout <<
"\n--------------------------------------\n";
157 std::cout <<
"The two end points of each edge" << std::endl;
158 std::cout <<
"----------------------------------------\n";
161 for (
auto e : graph->edges()) {
162 std::cout <<
"the two end points of edge " << e <<
": ";
164 std::cout << vs <<
" ";
166 std::cout << vt <<
" " << std::endl;
This class circulates through all edges connected with a vertex. It also acts as a container-concept ...
Definition graph.h:508
This class circulates through all one-ring neighbors of a vertex. It also acts as a container-concept...
Definition graph.h:616
A Graph data structure with easy property management.
Definition graph.h:51
EdgeContainer edges() const
Returns the edge container for range-based for-loops.
Definition graph.h:1205
VertexContainer vertices() const
Returns the vertex container for range-based for-loops.
Definition graph.h:1178
Vertex vertex(Edge e, unsigned int i) const
Returns the i-th vertex of an edge.
Definition graph.h:904
Definition collider.cpp:182
This type represents an edge (internally it is basically an index).
Definition graph.h:152
This type represents a vertex (internally it is basically an index).
Definition graph.h:133
- See also
- Graph, Tutorial_108_Graph, Tutorial_105_SurfaceMesh_Connectivity, Tutorial_113_PolyMesh_Connectivity