简体   繁体   English

O(n + m)中的DFS和BFS是否会发生变化?

[英]does DFS and BFS in O(n+m) change?

We know there is an O(n+m)‌ ‌solution (DFS ‌or BFS) for checking if there is a path from s to t in a Undirected Graph G with n vertexes and m edges... that would be implemented via an adjacency List. 我们知道有一个O(n+m)‌溶液(DFS或BFS),用于检查是否存在从路径st在无向图Gn顶点和m边缘...这将经由邻接来实现名单。

If I implement my program with Adjacency Matrix , will the runtime be affected? 如果我使用Adjacency Matrix实现程序,运行时间会受到影响吗? Is this a good or bad choice? 这是好选择还是坏选择?

Edit: I‌ Need to calculate the time complexity, in this way, any idea? 编辑:我需要计算时间复杂度,这样,有什么想法吗?

Assuming that the input to your code will be n and m ( number of nodes and the number of edges ) followed by m lines of the type ab signifying there is an edge between vertex a and vertex b . 假设您的代码输入为nm (节点数和边数),后跟m条类型为ab线,表示顶点a和顶点b之间存在一条边。 Now you take an adjacency matrix M[][] such that M[i][j]=1 if there is an edge between i and j otherwise M[i][j]=0 ( as graph is undirected the matrix will be symmetric, thus you can only store the upper/lower half matrix reducing memory by half ). 现在,采用邻接矩阵M[][] ,如果ij之间存在边,则M[i][j]=1 ,否则M[i][j]=0 (因为图是无向的,矩阵将是对称,因此您只能存储上/下半矩阵以减少一半的内存)。 Now you will have to take the matrix and initialize it to 0 ( all the cells ) and while scanning the edges mark M[a][b]=M[b][a]=1 . 现在,您必须获取矩阵并将其初始化为0(所有单元格),并且在扫描边缘标记M[a][b]=M[b][a]=1 Now the initializing part is O(n^2) . 现在初始化部分是O(n^2) Scanning and marking the edges is O(m) . 扫描和标记边缘为O(m) Now lets look at the BFS/DFS routine. 现在让我们看一下BFS / DFS例程。 When you are at a node you try to see all its unvisited vertices. 在节点上时,您尝试查看其所有未访问的顶点。 Now say we want to know the neighbors of vertex a , you will have to do for(int i=0;i<n;i++) if (M[a][i]==1) ( assuming 0 based indexing ). 现在说我们想知道顶点a的邻居, for(int i=0;i<n;i++) if (M[a][i]==1) (假设基于0的索引),则必须要做for(int i=0;i<n;i++) if (M[a][i]==1) )。 Now this has to be done for each vertex and thus the complexity of routine becomes O(n^2) even if m < (n*(n-1))/2 ( assuming simple graph with no multiple edges and loops m can at maximum be (n*(n-1))/2 ). 现在必须对每个顶点执行此操作,因此即使m < (n*(n-1))/2 (假设没有多个边和循环的简单图形m都可以在m处执行O(n^2) ,例程的复杂度也变为O(n^2)最大值为(n*(n-1))/2 )。 Thus overall your complexity becomes O(n^2) . 因此,总体而言,您的复杂度变为O(n^2) Then whats the use of adjacency matrix ? 那么,邻接矩阵的用途是什么? well the DFS/BFS might be just a part of a big algorithm, your algorithm might also require one tell if there is an edge between node a and b at which adjacency matrix takes O(1) time. 好吧,DFS / BFS可能只是大型算法的一部分,您的算法可能还需要告知节点ab之间是否存在一条边缘,邻接矩阵在该边缘处花费O(1)时间。 Thus whether to choose adjacency list or adjacency matrix really depends on your algorithm ( such as maximum memory you can take, time complexity for things like DFS/BFS routine or answering queries whether two vertices are connected etc. ) . 因此,选择邻接表还是邻接矩阵确实取决于您的算法(例如您可以占用的最大内存,DFS / BFS例程之类的时间复杂性或是否回答是否连接了两个顶点等)。 Hope I answered your query. 希望我回答了您的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM