Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
graph_generator.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef UTIL_GRAPH_GRAPH_GENERATOR_H_
15#define UTIL_GRAPH_GRAPH_GENERATOR_H_
16
17// Generates a complete undirected graph with `num_nodes` nodes.
18//
19// A complete graph is a graph in which all pairs of distinct nodes are
20// connected by an edge.
21// The graph is represented using the provided `Graph` template.
22// If the chosen graph type requires a call to `Build()`, the user is expected
23// to perform this call, possibly after tweaking the graph.
24//
25// Consider using `CompleteGraph` (util/graph/graph.h) instead of this function
26// in production code, as it uses constant memory to store the graph. Instead,
27// this function explicitly creates the graph using the template type, which is
28// mostly useful for tests or when you have to tweak the graph after creation
29// (i.e. a complete graph is just the core of your final graph).
30//
31// Args:
32// num_nodes: The number of nodes in the graph.
33//
34// Returns:
35// A complete undirected graph.
36template <typename Graph>
38 const typename Graph::NodeIndex num_nodes) {
39 Graph graph;
40 graph.Reserve(num_nodes, num_nodes * (num_nodes - 1));
41 graph.AddNode(num_nodes - 1); // Only for degenerate cases with no arcs.
42 for (typename Graph::NodeIndex src = 0; src < num_nodes; ++src) {
43 for (typename Graph::NodeIndex dst = src + 1; dst < num_nodes; ++dst) {
44 graph.AddArc(src, dst);
45 graph.AddArc(dst, src);
46 }
47 }
48 return graph;
49}
50
51// Generates a complete undirected bipartite graph with `num_nodes_1` and
52// `num_nodes_2` nodes in each part.
53//
54// A complete bipartite graph is a graph in which all pairs of distinct nodes,
55// one in each part, are connected by an edge.
56// The graph is represented using the provided `Graph` template.
57// If the chosen graph type requires a call to `Build()`, the user is expected
58// to perform this call, possibly after tweaking the graph.
59//
60// Args:
61// num_nodes_1: The number of nodes in the first part of the graph.
62// num_nodes_2: The number of nodes in the second part of the graph.
63//
64// Returns:
65// A complete undirected bipartite graph.
66template <typename Graph>
68 const typename Graph::NodeIndex num_nodes_1,
69 const typename Graph::NodeIndex num_nodes_2) {
70 Graph graph;
71 graph.Reserve(num_nodes_1 + num_nodes_2, 2 * num_nodes_1 * num_nodes_2);
72 graph.AddNode(num_nodes_1 + num_nodes_2 - 1); // Only for degenerate cases.
73 for (typename Graph::NodeIndex src = 0; src < num_nodes_1; ++src) {
74 for (typename Graph::NodeIndex dst = 0; dst < num_nodes_2; ++dst) {
75 graph.AddArc(src, num_nodes_1 + dst);
76 graph.AddArc(num_nodes_1 + dst, src);
77 }
78 }
79 return graph;
80}
81
82// Generates a complete directed bipartite graph with `num_nodes_1` and
83// `num_nodes_2` nodes in each part.
84//
85// A complete bipartite graph is a graph in which all pairs of distinct nodes,
86// one in each part, are connected by an edge. Edges are directed from the first
87// part towards the second part.
88// The graph is represented using the provided `Graph` template.
89// If the chosen graph type requires a call to `Build()`, the user is expected
90// to perform this call, possibly after tweaking the graph.
91//
92// Consider using `CompleteBipartiteGraph` (util/graph/graph.h) instead of this
93// function in production code, as it uses constant memory to store the graph.
94// Instead, this function explicitly creates the graph using the template type,
95// which is mostly useful for tests or when you have to tweak the graph after
96// creation (i.e. a complete graph is just the core of your final graph).
97//
98// Args:
99// num_nodes_1: The number of nodes in the first part of the graph.
100// num_nodes_2: The number of nodes in the second part of the graph.
101//
102// Returns:
103// A complete directed bipartite graph.
104template <typename Graph>
106 const typename Graph::NodeIndex num_nodes_1,
107 const typename Graph::NodeIndex num_nodes_2) {
108 Graph graph;
109 graph.Reserve(num_nodes_1 + num_nodes_2, num_nodes_1 * num_nodes_2);
110 graph.AddNode(num_nodes_1 + num_nodes_2 - 1); // Only for degenerate cases.
111 for (typename Graph::NodeIndex src = 0; src < num_nodes_1; ++src) {
112 for (typename Graph::NodeIndex dst = 0; dst < num_nodes_2; ++dst) {
113 graph.AddArc(src, num_nodes_1 + dst);
114 }
115 }
116 return graph;
117}
118
119#endif // UTIL_GRAPH_GRAPH_GENERATOR_H_
void Reserve(NodeIndexType node_capacity, ArcIndexType arc_capacity)
Definition graph.h:278
Graph GenerateCompleteUndirectedGraph(const typename Graph::NodeIndex num_nodes)
Graph GenerateCompleteUndirectedBipartiteGraph(const typename Graph::NodeIndex num_nodes_1, const typename Graph::NodeIndex num_nodes_2)
Graph GenerateCompleteDirectedBipartiteGraph(const typename Graph::NodeIndex num_nodes_1, const typename Graph::NodeIndex num_nodes_2)