Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_109_Graph_Connectivity

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 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

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.

1
27#include <easy3d/core/graph.h>
28#include <easy3d/util/initializer.h>
29
74using namespace easy3d;
75
76// There are two ways to traverse the incident entities of an element.
77// - use a "for" loop (cleaner code);
78// - use a circulator.
79#define USE_FOR_LOOP
80
81// the graph created in the previous tutorial (so you can skip it)
82Graph *old_graph_from_previous_example() {
83 // Create a graph
84 auto graph = new Graph;
85
86 // Add 4 vertices
87 Graph::Vertex v0 = graph->add_vertex(vec3(0, 0, 0));
88 Graph::Vertex v1 = graph->add_vertex(vec3(1, 0, 0));
89 Graph::Vertex v2 = graph->add_vertex(vec3(0, 1, 0));
90 Graph::Vertex v3 = graph->add_vertex(vec3(0, 0, 1));
91
92 // Add some edges
93 graph->add_edge(v0, v1); // e0
94 graph->add_edge(v1, v2); // e1
95 graph->add_edge(v2, v3); // e2
96 graph->add_edge(v3, v0); // e3
97 graph->add_edge(v1, v3); // e4
98
99 return graph;
100}
101
102int main(int argc, char **argv) {
103 // initialize Easy3D.
104 initialize();
105
106 Graph *graph = old_graph_from_previous_example();
107
108 std::cout << "----------------------------------------\n";
109 std::cout << "The incident vertices of each vertex" << std::endl;
110 std::cout << "----------------------------------------\n";
111
112 // loop over all vertices
113 for (auto v : graph->vertices()) {
114 std::cout << "incident vertices of vertex " << v << ": ";
115#ifdef USE_FOR_LOOP
116 // loop over all incident vertices
117 for (auto vv : graph->vertices(v))
118 std::cout << vv << " ";
119#else // use circulator
120 // A circulator wraps around the incident edges and has no natural end, so we save the
121 // start position and stop once advancing brings us back to it.
124 do {
125 Graph::Vertex vv = *cir;
126 std::cout << vv << " ";
127 ++cir;
128 } while (cir != end);
129#endif
130 std::cout << std::endl;
131 }
132
133 std::cout << "\n--------------------------------------\n";
134 std::cout << "The incident edges of each vertex" << std::endl;
135 std::cout << "----------------------------------------\n";
136
137 // loop over all vertices
138 for (auto v : graph->vertices()) {
139 std::cout << "incident edges of vertex " << v << ": ";
140#ifdef USE_FOR_LOOP
141 // loop over all incident outgoing/ingoing edges
142 for (auto h : graph->edges(v))
143 std::cout << h << " ";
144#else // use circulator
147 do {
148 Graph::Edge h = *cir;
149 std::cout << h << " ";
150 ++cir;
151 } while (cir != end);
152#endif
153 std::cout << std::endl;
154 }
155
156 std::cout << "\n--------------------------------------\n";
157 std::cout << "The two end points of each edge" << std::endl;
158 std::cout << "----------------------------------------\n";
159
160 // loop over all edges
161 for (auto e : graph->edges()) {
162 std::cout << "the two end points of edge " << e << ": ";
163 Graph::Vertex vs = graph->vertex(e, 0);
164 std::cout << vs << " ";
165 Graph::Vertex vt = graph->vertex(e, 1);
166 std::cout << vt << " " << std::endl;
167 }
168
169 // Delete the graph (i.e., release memory)
170 delete graph;
171
172 return EXIT_SUCCESS;
173}
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