chapter_graph/graph_operations/ #305
Replies: 58 comments 66 replies
-
大佬,在GraphAdjMat 中
这里的edges 能否对应图给个具体的值,这里想不明白。谢谢! |
Beta Was this translation helpful? Give feedback.
-
Hello, Baby K! 有一个微不足道的想法,邻接矩阵 |
Beta Was this translation helpful? Give feedback.
-
自定义Vertex对象,将该节点的邻接点列表包含其中,可以省一点空间?不知是否会影响效率 public class Vertex {
int id;
int value;
List<Vertex> neighbors; // 节点邻居列表
...
} |
Beta Was this translation helpful? Give feedback.
-
在使用邻接表添加边这里有问题, 如果边已经存在那么他还是会添加进去
我做的修改是
|
Beta Was this translation helpful? Give feedback.
-
邻接表删除顶点的复杂度为什么是 O(n + m) 呢? |
Beta Was this translation helpful? Give feedback.
-
Hello,K神. 请问一下/添加顶点/里面的emplace_back(n, 0)如何理解?不太懂
|
Beta Was this translation helpful? Give feedback.
-
hii, 不太理解用hashtable实现的删除边、删除节点的时间复杂度为什么分别是O(1), O(n)?
|
Beta Was this translation helpful? Give feedback.
-
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 |
Beta Was this translation helpful? Give feedback.
-
不好意思,初学者想问一下,那个java实现的邻接表里,关于添加节点的代码 // 遍历其他顶点的链表,删除所有包含 vet 的边
for (List<Vertex> list : adjList.values()) {
list.remove(vet);
} remove只会删除list里第一次出现的vet,要是假如出现了重边,即添加了两次addEdge(n6, n7); // 遍历其他顶点的链表,删除所有包含 vet 的边
for (List<Vertex> list : graph.values())
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(vet))
{ list.remove(i);//使用list.remove(i)删除节点
i--;//并将索引i递减以保持正确的循环遍历
}
} 这样也行吧,感觉这两种存储方式都没考虑到重边 |
Beta Was this translation helpful? Give feedback.
-
hi,想问一下在用链表实现的邻接表中,添加边的复杂度不应该是O(n)吗,因为要遍历找到要添加边的顶点,初学者/(ㄒoㄒ)/~~ |
Beta Was this translation helpful? Give feedback.
-
对不起我比较笨, 我想请问一下java的vertex类是怎么实现的呢? |
Beta Was this translation helpful? Give feedback.
-
是不是释放二级指针p总是需要free(p[0]);free(p);这样两个语句?我看C语言的邻接矩阵实现中添加节点操作用到这种操作,但是后面,邻接列表的添加顶点操作只用了free(t->verticesList); 来释放内存,这两者之间有什么差别吗? |
Beta Was this translation helpful? Give feedback.
-
self.adj_list = dictVertex, list[Vertex],这段代码不是建立了一个字典吗?不应该是"{}"吗? |
Beta Was this translation helpful? Give feedback.
-
”添加顶点:在邻接表中添加一个链表,并将新增顶点作为链表头节点“。我并没有找到有关链表的实现,请问是哪一步? |
Beta Was this translation helpful? Give feedback.
-
graph_adjacency_list.c有一处注释错误,位于删除顶点函数中
这个注释应该是 |
Beta Was this translation helpful? Give feedback.
-
请问 邻接表(哈希表)是相当于用一个哈希表存储n个顶点,然后对应n个哈希表存m条边吗?然后邻接表(哈希表)删除顶点是O(n)是不是因为这考虑的是最坏情况,哈希冲突退化成链表,O(n)呀 |
Beta Was this translation helpful? Give feedback.
-
原文有误,在邻接矩阵中删除顶点的时间复杂度应该为 O(n),而不是O(n^2)。 |
Beta Was this translation helpful? Give feedback.
-
/* 删除顶点 */
void removeVertex(GraphAdjList *graph, Vertex *vet) {
AdjListNode *node = findNode(graph, vet);
assert(node != NULL);
// 在邻接表中删除顶点 vet 对应的链表
AdjListNode *cur = node, *pre = NULL;
while (cur) {
pre = cur;
cur = cur->next;
free(pre);
}
// 遍历其他顶点的链表,删除所有包含 vet 的边
for (int i = 0; i < graph->size; i++) {
cur = graph->heads[i];
pre = NULL;
// 修改的部分:在进入while循环遍历链表之前,添加条件语句确保不要遍历到先前已被释放的顶点所在的链表
if (cur != node) {
// 在符合if条件后,进入对某一合法链表的遍历
while (cur) {
pre = cur;
cur = cur->next;
if (cur && cur->vertex == vet) {
pre->next = cur->next;
free(cur);
break;
}
}
}
}
// 将该顶点之后的顶点向前移动,以填补空缺
...
} |
Beta Was this translation helpful? Give feedback.
-
9.2.2 JS和TS代码片段中的删除边函数好像少了个判断条件 如果传入的两个顶点不为邻接点,那么在adjList两顶点对应的value中会删除最后一个顶点即splice(-1, 1) 如果没错的话是否可以改成这样:(如果我理解错了还请指正) removeEdge(vet1: Vertex, vet2: Vertex): void {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2 ||
this.adjList.get(vet1).indexOf(vet2) !== -1 // 判断这两个顶点是否邻接(无向图)
) {
throw new Error('Illegal Argument Exception');
}
// 删除边 vet1 - vet2
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);
this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);
} |
Beta Was this translation helpful? Give feedback.
-
邻接矩阵:初始化矩阵时怎么推算的?就是每个格子里的 0 或 1 |
Beta Was this translation helpful? Give feedback.
-
#include class AmGraph public: AmGraph(const vector& vex1, const vector<vector>& arc1) } int sumDu(char val)
} //获取顶点数 //添加顶点
} // 删除顶点
} //获取顶点位置 //添加边 //删除边 void printvex() int main()
} |
Beta Was this translation helpful? Give feedback.
-
c++初始化直接调用拷贝构造即可 |
Beta Was this translation helpful? Give feedback.
-
请问为什么利用邻接表(哈希表)实现时删除边的时间复杂度是1呢? 找到对应的边确定key需要1, 在这个顶点的链表(动态数组)中找到这条边为什么也是1呢? |
Beta Was this translation helpful? Give feedback.
-
C环境下基于邻接表的实现的代码中,那个自定义Vertex类型具体是什么组成呢?代码里没有具体说明。 |
Beta Was this translation helpful? Give feedback.
-
是否在最后一行末尾多添加了一个0呢?添加行的时候最后一行已有n个0了 |
Beta Was this translation helpful? Give feedback.
-
看了半天才知道这里的list是hashSet,所以判断顶点是否邻接的效率是O(1) |
Beta Was this translation helpful? Give feedback.
-
在邻接矩阵的删除顶点中,还有种方法就是将该顶点对应的行和列都置为0,然后增加一个变量用来表示这个顶点是不是为空即可,可以降低时间复杂度 |
Beta Was this translation helpful? Give feedback.
-
邻接矩阵和邻接表操作图示tab切换好像会互相影响 |
Beta Was this translation helpful? Give feedback.
-
有点看不懂了,┭┮﹏┭┮ |
Beta Was this translation helpful? Give feedback.
-
根据我老师和Wikipedia的说法,邻接矩阵中添加顶点的时间复杂度是O(n^2) |
Beta Was this translation helpful? Give feedback.
-
chapter_graph/graph_operations/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_graph/graph_operations/
Beta Was this translation helpful? Give feedback.
All reactions