
检查图表中是否存在满足给定条件的长度为 3 的循环,准备重复遍历每个顶点并查看其相邻顶点。如果一个顶点有两个过于关联的邻居,则存在长度为 3 的环。此条件保证两个邻居之间有一条边,从而形成一个三角形。通过过滤所有顶点及其相邻顶点,我们将识别这样的循环是否存在。如果我们发现一个顶点有两个相关的邻居,我们就可以得出结论,图表中显示了满足给定条件的长度为 3 的循环。
使用的方法
邻接矩阵方法
邻接表方法
邻接法
为了检查图表中是否存在满足给定条件的长度为 3 的循环,我们可以利用传染性方法。在这种方法中,我们重复图表中的每个顶点并检查其相邻的顶点。对于每个顶点,我们检查其任何两个相邻顶点是否过于紧密关联。如果找到这样的匹配,我们将检查是否满足该匹配的条件。如果满足条件,则表明接近满足给定条件的长度为 3 的循环。通过查看图表中的所有顶点,我们可以确定是否存在这样的循环。
算法
将名为“cycleExists”的布尔变量初始化为 false。
迭代图中的每个顶点:
对于每个顶点,重复其相邻的顶点。
对于每个相邻顶点,强调其相邻顶点(当前顶点除外)。
如果任意两个相邻顶点关联,请继续下一步。
检查步骤 2c 中找到的关联顶点的组合是否满足条件。
如果满足条件,则将“cycleExists”设置为 true 并跳出循环。
完成循环后,检查“cycleExists”的值。
如果“cycleExists”为真,则图中存在满足给定条件的长度为 3 的循环。
如果“cycleExists”错误,则不存在这样的循环。
输出结果。
此计算会重复图表的顶点,分析其相邻顶点,并检查相邻顶点的任何匹配是否形成满足给定条件的长度为 3 的循环。
李>
示例
#include #include using namespace std;bool checkCycle(vector<vector>& graph, int v, vector& visited, int parent, int condition) { visited[v] = true; for (int u : graph[v]) { if (!visited[u]) { visited[u] = true; for (int w : graph[u]) { if (visited[w] && w != parent && condition == graph[v][u] + graph[u][w]) { return true; } } visited[u] = false; } } return false;}bool hasCycleOfLength3(vector<vector>& graph, int condition) { int numVertices = graph.size(); vector visited(numVertices, false); for (int v = 0; v < numVertices; v++) { visited[v] = true; for (int u : graph[v]) { if (checkCycle(graph, u, visited, v, condition)) { return true; } } visited[v] = false; } return false;}int main() { int numVertices, numEdges; cout <> numVertices >> numEdges; vector<vector> graph(numVertices); cout << "Enter the connections between vertices (u, v) and their corresponding weights: " << endl; for (int i = 0; i > u >> v >> weight; graph[u].push_back(v); graph[v].push_back(u); // Store the weight/condition between u and v graph[u][v] = weight; graph[v][u] = weight; } int condition; cout <> condition; if (hasCycleOfLength3(graph, condition)) { cout << "Cycle of length 3 satisfying the condition exists." << endl; } else { cout << "Cycle of length 3 satisfying the condition does not exist." << endl; } return 0;}
输出
Enter the number of vertices and edges:
邻接表方法
相邻的列表方法可以是用于与图表对话的信息结构。在这种方法中,图表的每个顶点都与包含其所有相邻顶点的列表相关。为了检查图表中是否存在满足给定条件的长度为 3 的循环,我们将迭代每个顶点及其相邻顶点。对于每个相邻顶点,我们检查它是否包含与当前顶点共同的相邻顶点。如果存在这样的公共顶点,则找到长度为 3 的环。这种方法可以通过存放有关传染性列表中几乎所有顶点及其关联的基本数据来保证对图表的有效调查。
算法
制作一个与图表对话的传染性列表,其中每个顶点都包含其相邻顶点的列表。
迭代图中的每个顶点。
对于每个顶点,重复其相邻的顶点。
对于每个相邻顶点,强调其相邻顶点(当前顶点除外)。
检查当前顶点和相邻顶点的相邻顶点之间是否存在公共顶点。
如果找到公共顶点,则存在长度为 3 的环。返回 true。
如果没有找到长度为 3 的环,则返回 false。
示例
#include #include #include using namespace std;bool hasCycleOfLength3(vector<vector>& graph) { int n = graph.size(); for (int u = 0; u < n; ++u) { unordered_set adjSet(graph[u].begin(), graph[u].end()); for (int v : graph[u]) { for (int w : graph[v]) { if (w != u && adjSet.count(w) > 0) { return true; // Cycle of length 3 found } } } } return false; // No cycle of length 3 found}int main() { // Create the graph as an adjacency list vector<vector> graph = { {1, 2}, {0, 2}, {0, 1, 3}, {2, 4}, {3} }; // Check if a cycle of length 3 exists bool cycleExists = hasCycleOfLength3(graph); // Print the result if (cycleExists) { cout << "A cycle of length 3 exists in the graph." << endl; } else { cout << "No cycle of length 3 exists in the graph." << endl; } return 0;}
输出
A cycle of length 3 exists in the graph.
结论
本文研究了检查图表中是否存在满足给定条件的长度为 3 的循环的方法。它阐明了两种方法,特别是传染性框架方法和传染性列表方法。本文跟踪了计算过程并给出了这两种方法的 C 代码位。传染性网络方法包括强调每个顶点及其相邻顶点来识别满足条件的长度为 3 的循环。传染性列表方法利用与图表对话的信息结构,并检查相邻顶点之间的公共顶点来确定循环的接近程度。
以上就是检查图中是否存在满足给定条件的长度为3的循环的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444504.html
微信扫一扫
支付宝扫一扫