寻找一个图形的所有母点
- 最后更新 : 2021年8月6日
给定一个图G,任务是找到该图中所有的母顶点。
母顶点。 图G = (V, E)中的母顶点是指一个顶点v,即G中的所有其他顶点都可以通过一条路径从v到达。
例子。
**输入。**下面给出的图形
**输出。**0 1 4
**解释。**在给定的图中,母顶点是0、1和4,因为从这些顶点到每个顶点都存在一条路径。
天真的方法:
一个微不足道的方法是对所有顶点进行DFS或BFS,并找出我们是否能从该顶点到达所有顶点。
时间复杂度:O(V(E+V))
高效的方法。
如果一个图形存在一个母顶点,那么所有的母顶点都是包含母顶点的强连接部分的顶点,因为如果v是一个母顶点,并且存在一条从u->v的路径,那么u一定也是一个母顶点。
下面是上述方法的实现。
C++
// C++ program to find all the mother vertices#include <bits/stdc++.h>using namespace std;// This function does DFS traversal// from given node u, and marks the// visited nodes in the visited arrayvoid dfs_helper(int u,vector<vector<int> >& adj,bool visited[]){if (visited[u])return;visited[u] =true;for (auto v : adj[u]) {if (!visited[v])dfs_helper(v, adj, visited);}}// Function that stores the adjacency// list of the transpose graph of the// given graph in the trans_adj vectorvoid getTransposeGraph(vector<vector<int> >& adj,vector<vector<int> >& trans_adj){for (int u = 0; u < adj.size(); u++) {for (auto v : adj[u]) {trans_adj[v].push_back(u);}}}// Initializes all elements of visited// array with value falsevoid initialize_visited(bool visited[],int n){for (int u = 0; u < n; u++)visited[u] =false;}// Returns the list of mother// vertices. If the mother vertex// does not exists returns -1vector<int> findAllMotherVertices(vector<vector<int> >& adj){int n = adj.size();bool visited[n];// Find any mother vertex// in given graph, Ginitialize_visited(visited, n);int last_dfs_called_on = -1;for (int u = 0; u < n; u++) {if (!visited[u]) {dfs_helper(u, adj, visited);last_dfs_called_on = u;}}// Check if we can reach// all vertices from// last_dfs_called_on nodeinitialize_visited(visited, n);dfs_helper(last_dfs_called_on, adj, visited);for (int u = 0; u < n; u++) {// Check of the mother vertex// exist else return -1if (!visited[u]) {vector<int> emptyVector;emptyVector.push_back(-1);return emptyVector;}}// Now in G_transpose, do DFS// from that mother vertex,// and we will only reach the// other mother vertices of Gint motherVertex = last_dfs_called_on;// trans_adj is the transpose// of the given Graphvector<vector<int> > trans_adj(n);// Function call to get// the transpose graphgetTransposeGraph(adj, trans_adj);// DFS from that mother vertex// in the transpose graph and the// visited nodes are all the// mother vertices of the given// graph Ginitialize_visited(visited, n);dfs_helper(motherVertex, trans_adj, visited);// Vector to store the list// of mother verticesvector<int> ans;for (int u = 0; u < n; u++) {if (visited[u])ans.push_back(u);}// Return the required listreturn ans;}// Driver Codeint main(){// No. of nodesint V = 8;vector<vector<int> > adj(V);adj[0].push_back(1);adj[1].push_back(2);adj[1].push_back(4);adj[1].push_back(5);adj[2].push_back(3);adj[2].push_back(6);adj[3].push_back(2);adj[3].push_back(7);adj[4].push_back(0);adj[4].push_back(5);adj[5].push_back(6);adj[6].push_back(5);adj[6].push_back(7);// Function call to find the mother vertcesvector<int> motherVertices= findAllMotherVertices(adj);// Print answerif (motherVertices[0] == -1)cout <<"No mother vertex exists";else {for (int v : motherVertices)cout << v <<" ";}return 0;} |
输出。
0 1 4
时间复杂度。 O(V+E)
空间复杂度。 O(V+E)
读者请注意!现在不要停止学习。掌握所有重要的DSA概念。 DSA自学课程以适合学生的价格获得所有重要的DSA概念,并成为行业的准备者。 要完成从学习语言到DS Algo以及更多的准备,请参考 完整的面试准备课程.
如果你想参加专家的现场课程 ,请参考 面向在职人士的DSA现场课程 和 面向学生的竞争性编程直播课程.
我的个人笔记 箭头_下降_上升
保存