问题描述
我无法理解 Donald Johnson 发表的关于在图中找到循环(电路)的论文的某个部分.
更具体地说,我无法理解伪代码的以下行中提到的矩阵 Ak 是什么:
Ak:=强分量K的邻接结构由 {s,s+1,....n} 诱导的 G 子图中的顶点;
为了让事情变得更糟,在没有声明 Vk 是什么的情况下,在for i in Vk do"之后的一些行...
据我所知,我们有以下几点:1)一般来说,一个强组件是一个图的子图,其中对于这个子图的每个节点都有一条通往子图任何节点的路径(换句话说,你可以访问该子图的任何节点)来自子图的任何其他节点的子图)
2) 由节点列表诱导的子图是包含所有这些节点以及连接这些节点的所有边的图.在论文中,数学定义是如果 W 是 V 的子集,并且 F = (W,{u,y)|u,y in W and (u,y) in E)})其中 u,y 是边,E 是图中所有边的集合,W 是节点的集合.
3) 在代码实现中,节点以整数 1 ... n 命名.
4) 我怀疑 Vk 是强分量 K 的节点集.
现在来回答问题.假设我们有一个图 G= (V,E),其中 V = {1,2,3,4,5,6,7,8,9} 它可以分为 3 个强分量 SC1 = {1,4,7,8} SC2= {2,3,9} SC3 = {5,6}(和它们的边)
谁能给我举个例子,s = 1, s= 2, s= 5 如果根据代码变成 Vk 和 Ak 会怎样?
伪代码在我之前的问题中理解唐纳德中的伪代码B. Johnson 算法
先谢谢你
它有效!在 早期迭代中="nofollow noreferrer">Johnson 算法,我原以为 A
是一个 邻接矩阵.相反,它似乎代表一个邻接列表.在下面实现的那个例子中,顶点 {a, b, c} 被编号为 {0, 1, 2},产生以下电路.
附录:如本提议的编辑和有用的answer,算法指定 unblock()
应该删除具有 value w
的元素>,不是具有 index w
的元素.
list.remove(Integer.valueOf(w));
示例输出:
0 1 00 1 2 00 2 00 2 1 01 0 11 0 2 11 2 0 11 2 12 0 1 22 0 22 1 0 22 1 2默认情况下,程序以s = 0
开始;实现 s := V 中的最少顶点
作为优化仍然存在.此处显示了仅产生唯一循环的变体.
import java.util.ArrayList;导入 java.util.Arrays;导入 java.util.List;导入 java.util.Stack;/*** @see http://dutta.csc.ncsu.edu/csc791_spring07/wrap/circuits_johnson.pdf* @see https://stackoverflow.com/questions/2908575* @see https://stackoverflow.com/questions/2939877* @see http://en.wikipedia.org/wiki/Adjacency_matrix* @see http://en.wikipedia.org/wiki/Adjacency_list*/公共最终类 CircuitFinding {最终堆栈stack = new Stack();最终列表<列表>一个;最终列表<列表>乙;final boolean[] 阻塞;最终int n;整数 s;公共静态无效主(字符串 [] args){列表<列表>a = new ArrayList();a.add(new ArrayList(Arrays.asList(1, 2)));a.add(new ArrayList(Arrays.asList(0, 2)));a.add(new ArrayList(Arrays.asList(0, 1)));CircuitFinding cf = new CircuitFinding(a);cf.find();}/*** @param 强分量 K 的邻接结构* 由 {s, s + 1, n} 诱导的 G 子图中的最小顶点;*/公共电路查找(列表<列表> a){this.a = a;n = a.size();阻塞 = 新布尔 [n];b = new ArrayList();for (int i = 0; i < n; i++) {b.add(new ArrayList());}}私有无效解锁(int u){阻塞[u] = 假;列表列表 = b.get(u);for (int w : list) {//从B(u)中删除w;list.remove(Integer.valueOf(w));如果(阻塞[w]){解锁(w);}}}私有布尔电路(int v){布尔 f = 假;堆栈推(v);阻塞 [v] = 真;L1:for (int w : a.get(v)) {如果(w == s){//由堆栈后跟s组成的输出电路;for (int i: 堆栈) {System.out.print(i + " ");}System.out.println(s);f = 真;} else if (!blocked[w]) {如果(电路(w)){f = 真;}}}L2:如果 (f) {解除封锁(v);} 别的 {for (int w : a.get(v)) {//如果(v∉B(w))把v放在B(w)上;如果 (!b.get(w).contains(v)) {b.get(w).add(v);}}}v = stack.pop();返回 f;}公共无效查找(){而 (s < n) {如果(一个!=空){//s := V 中的最小顶点;L3:电路;s++;} 别的 {s = n;}}}}
i cannot understand a certain part of the paper published by Donald Johnson about finding cycles (Circuits) in a graph.
More specific i cannot understand what is the matrix Ak which is mentioned in the following line of the pseudo code :
Ak:=adjacency structure of strong component K with least vertex in subgraph of G induced by {s,s+1,....n};
to make things worse some lines after is mentins " for i in Vk do " without declaring what the Vk is...
As far i have understand we have the following:1) in general, a strong component is a sub-graph of a graph, in which for every node of this sub-graph there is a path to any node of the sub-graph (in other words you can access any node of the sub-graph from any other node of the sub-graph)
2) a sub-graph induced by a list of nodes is a graph containing all these nodes plus all the edges connecting these nodes. in paper the mathematical definition is " F is a subgraph of G induced by W if W is subset of V and F = (W,{u,y)|u,y in W and (u,y) in E)}) where u,y are edges , E is the set of all the edges in the graph, W is a set of nodes.
3)in the code implementation the nodes are named by integer numbers 1 ... n.
4) I suspect that the Vk is the set of nodes of the strong component K.
now to the question. Lets say we have a graph G= (V,E) with V = {1,2,3,4,5,6,7,8,9} which it can be divided into 3 strong components the SC1 = {1,4,7,8} SC2= {2,3,9} SC3 = {5,6} (and their edges)
Can anybody give me an example for s =1, s= 2, s= 5 what if going to be the Vk and Ak according to the code?
The pseudo code is in my previous question inUnderstanding the pseudocode in the Donald B. Johnson's algorithm
and the paper can be found atUnderstanding the pseudocode in the Donald B. Johnson's algorithm
thank you in advance
It works! In an earlier iteration of the Johnson algorithm, I had supposed that A
was an adjacency matrix. Instead, it appears to represent an adjacency list. In that example, implemented below, the vertices {a, b, c} are numbered {0, 1, 2}, yielding the following circuits.
Addendum: As noted in this proposed edit and helpful answer, the algorithm specifies that unblock()
should remove the element having the value w
, not the element having the index w
.
list.remove(Integer.valueOf(w));
Sample output:
0 1 0 0 1 2 0 0 2 0 0 2 1 0 1 0 1 1 0 2 1 1 2 0 1 1 2 1 2 0 1 2 2 0 2 2 1 0 2 2 1 2
By default, the program starts with s = 0
; implementing s := least vertex in V
as an optimization remains. A variation that produces only unique cycles is shown here.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
/**
* @see http://dutta.csc.ncsu.edu/csc791_spring07/wrap/circuits_johnson.pdf
* @see https://stackoverflow.com/questions/2908575
* @see https://stackoverflow.com/questions/2939877
* @see http://en.wikipedia.org/wiki/Adjacency_matrix
* @see http://en.wikipedia.org/wiki/Adjacency_list
*/
public final class CircuitFinding {
final Stack<Integer> stack = new Stack<Integer>();
final List<List<Integer>> a;
final List<List<Integer>> b;
final boolean[] blocked;
final int n;
int s;
public static void main(String[] args) {
List<List<Integer>> a = new ArrayList<List<Integer>>();
a.add(new ArrayList<Integer>(Arrays.asList(1, 2)));
a.add(new ArrayList<Integer>(Arrays.asList(0, 2)));
a.add(new ArrayList<Integer>(Arrays.asList(0, 1)));
CircuitFinding cf = new CircuitFinding(a);
cf.find();
}
/**
* @param a adjacency structure of strong component K with
* least vertex in subgraph of G induced by {s, s + 1, n};
*/
public CircuitFinding(List<List<Integer>> a) {
this.a = a;
n = a.size();
blocked = new boolean[n];
b = new ArrayList<List<Integer>>();
for (int i = 0; i < n; i++) {
b.add(new ArrayList<Integer>());
}
}
private void unblock(int u) {
blocked[u] = false;
List<Integer> list = b.get(u);
for (int w : list) {
//delete w from B(u);
list.remove(Integer.valueOf(w));
if (blocked[w]) {
unblock(w);
}
}
}
private boolean circuit(int v) {
boolean f = false;
stack.push(v);
blocked[v] = true;
L1:
for (int w : a.get(v)) {
if (w == s) {
//output circuit composed of stack followed by s;
for (int i : stack) {
System.out.print(i + " ");
}
System.out.println(s);
f = true;
} else if (!blocked[w]) {
if (circuit(w)) {
f = true;
}
}
}
L2:
if (f) {
unblock(v);
} else {
for (int w : a.get(v)) {
//if (v∉B(w)) put v on B(w);
if (!b.get(w).contains(v)) {
b.get(w).add(v);
}
}
}
v = stack.pop();
return f;
}
public void find() {
while (s < n) {
if (a != null) {
//s := least vertex in V;
L3:
circuit(s);
s++;
} else {
s = n;
}
}
}
}
这篇关于帮助 Donalds B. Johnson 的算法,我无法理解伪代码(第二部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!