Graph 并行边缘检测
我正在研究一个问题(来自Sedgewick的算法,第4.1节,问题),以帮助我理解,我不知道如何继续 并行边缘检测。设计一个线性时间算法来计算(多)图中的并行边缘。 提示:维护顶点邻域的布尔数组,只需根据需要重新初始化条目即可重用此数组。” 如果两条边连接同一对顶点,则认为它们是平行的Graph 并行边缘检测,graph,adjacency-list,Graph,Adjacency List,我正在研究一个问题(来自Sedgewick的算法,第4.1节,问题),以帮助我理解,我不知道如何继续 并行边缘检测。设计一个线性时间算法来计算(多)图中的并行边缘。 提示:维护顶点邻域的布尔数组,只需根据需要重新初始化条目即可重用此数组。” 如果两条边连接同一对顶点,则认为它们是平行的 知道怎么做吗?假设图形中的顶点是整数0|V | 如果图形是定向的,则图形中的边表示为(i,j) 这允许您生成任意边到整数(散列函数)的唯一映射,该整数可以在O(1)中找到 您可以在摊销O(1)时间内在哈希表中插入
知道怎么做吗?假设图形中的顶点是整数
0|V |
如果图形是定向的,则图形中的边表示为(i,j)
这允许您生成任意边到整数(散列函数)的唯一映射,该整数可以在O(1)中找到
您可以在摊销O(1)时间内在哈希表中插入/查找元组(i,j)
。对于邻接列表中的| E |
边,这意味着总运行时间将是O(| E |)或邻接列表中边数的线性
这方面的python实现可能如下所示:
def identify_parallel_edges(adj_list):
# O(n) list of edges to counts
# The Python implementation of tuple hashing implements a more sophisticated
# version of the approach described above, but is still O(1)
edges = {}
for edge in adj_list:
if edge not in edges:
edges[edge] = 0
edges[edge] += 1
# O(n) filter non-parallel edges
res = []
for edge, count in edges.iteritems():
if count > 1:
res.append(edge)
return res
edges = [(1,0),(2,1),(1,0),(3,4)]
print identify_parallel_edges(edges)
我想我们可以用BFS来做这个 其主要思想是能够判断两个节点之间是否存在两条或多条路径,因此,我们可以使用一个集合,查看与节点的相邻列表相对应的相邻节点是否已经在集合中 这使用了O(n)个额外空间,但具有O(n)个时间复杂性
boolean bfs(int start){
Queue<Integer> q = new Queue<Integer>(); // get a Queue
boolean[] mark = new boolean[num_of_vertices];
mark[start] = true; // put 1st node into Queue
q.add(start);
while(!q.isEmpty()){
int current = q.remove();
HashSet<Integer> set = new HashSet<Integer>(); /* use a hashset for
storing nodes of current adj. list*/
ArrayList<Integer> adjacentlist= graph.get(current); // get adj. list
for(int x : adjacentlist){
if(set.contains(x){ // if it already had a edge current-->x
return true; // then we have our parallel edge
}
else set.add(x); // if not then we have a new edge
if(!marked[x]){ // normal bfs routine
mark[x]=true;
q.add(x);
}
}
}
}// assumed graph has ArrayList<ArrayList<Integer>> representation
// undirected
boolean bfs(int start){
Queue q=new Queue();//获取一个队列
布尔[]标记=新布尔[num_of_顶点];
标记[start]=true;//将第一个节点放入队列
q、 添加(开始);
而(!q.isEmpty()){
int current=q.remove();
HashSet set=new HashSet();/*将HashSet用于
存储当前调整列表的节点*/
ArrayList adjacentlist=graph.get(当前);//get adj.list
用于(int x:邻接列表){
if(set.contains(x){//如果它已经有一个当前边缘-->x
return true;//那么我们就有了平行边
}
else set.add(x);//如果不是,则我们有一个新的边
如果(!标记为[x]){//正常bfs例程
标记[x]=真;
q、 加(x);
}
}
}
}//假定图具有ArrayList表示
//无向
您尝试过什么?不要担心得到O(n)现在,试着数一数平行边。你会怎么做?我唯一能想到的是显而易见的方法。假设我有一个邻接列表,我循环到列表中的每个元素并计算重复次数,我也跳过了双计数,而没有更多关于你对一个没有人会去的图的定义的信息o be给你一个很好的答案。一个图是一组顶点和边G=(V,E),但没有任何与之相关的几何概念。这个图是嵌入格中的吗?如果是,是什么类型(正方形、三角形、其他?)。这个图是作为邻接列表给出的。意思是,这是一个2d数组,其中每个(i,J)表示两个顶点之间是否有一条边。同时假设图是无向的,simpleI不确定(i,j)=(min(i,j),max(i,j))的意思。我处理的图是无向的,因此我处理的数据结构是数组(邻接列表或矩阵)
boolean bfs(int start){
Queue<Integer> q = new Queue<Integer>(); // get a Queue
boolean[] mark = new boolean[num_of_vertices];
mark[start] = true; // put 1st node into Queue
q.add(start);
while(!q.isEmpty()){
int current = q.remove();
HashSet<Integer> set = new HashSet<Integer>(); /* use a hashset for
storing nodes of current adj. list*/
ArrayList<Integer> adjacentlist= graph.get(current); // get adj. list
for(int x : adjacentlist){
if(set.contains(x){ // if it already had a edge current-->x
return true; // then we have our parallel edge
}
else set.add(x); // if not then we have a new edge
if(!marked[x]){ // normal bfs routine
mark[x]=true;
q.add(x);
}
}
}
}// assumed graph has ArrayList<ArrayList<Integer>> representation
// undirected