我正在制作无向友谊图,其中包括在特定学校成为朋友的学生。我想使用dfs获取派系(图中所有连接的子图)。但由于某种原因,我的dfs无法正常工作..对算法或代码的任何建议表示赞赏
这是手动创建的示例图表。
import java.util.LinkedHashMap;
public class DFS {
/**
* @param args
*/
class Node {
String personName, schoolName;
Node next;
public Node(String personName, String schoolName, Node next) {
this.personName = personName;
this.schoolName = schoolName;
this.next = next;
}
public String toString() {
return this.personName + " " + this.schoolName;
}
}
public Node[] build() {
Node[] graph = new Node[6];
for (int i = 0; i < graph.length; i++) {
Node temp = new Node(Integer.toString(i + 1), "MIT", null);
graph[i] = temp;
}
graph[0].next = new Node("2", "MIT", null);
graph[1].next = new Node("1", "MIT", null);
graph[1].next.next = new Node("3", "MIT", null);
graph[1].next.next.next = new Node("4", "MIT", null);
graph[2].next = new Node("2", "MIT", null);
graph[2].next.next = new Node("4", "MIT", null);
graph[3].next = new Node("3", "MIT", null);
graph[3].next.next = new Node("2", "MIT", null);
graph[4].next = new Node("6", "MIT", null);
graph[5].next = new Node("5", "MIT", null);
printGraph(graph);
return graph;
}
public void dfsDriver() {
Node[] graph = build();
LinkedHashMap<String, Integer> names = new LinkedHashMap<String, Integer>();
int count = 0;
for (int i = 0; i < graph.length; i++) {
if (graph[i] != null) {
names.put(graph[i].personName, count);
count++;
}
}
boolean[] visited = new boolean[graph.length];
for (int v = 0; v < visited.length; v++) {
visited[v] = false;
}
for (int i = 0; i < graph.length; i++) {
if (graph[i] != null) {
if (!visited[i]) {
System.out.println("Starting at " + graph[i].personName);
dfs(i, visited, names, graph);
}
}
}
}
private void dfs(int i, boolean[] visited, LinkedHashMap<String, Integer> names, Node[] subGraph) {
visited[i] = true;
for (Node e = subGraph[i].next; e != null; e = e.next) {
System.out.println("visiting " + e.personName);
int index = names.get(e.personName);
if (!visited[index]) {
dfs(index, visited, names, subGraph);
}
}
}
public void printGraph(Node[] list) {
System.out.println();
for (int i = 0; i < list.length; i++) {
if (list[i] != null) {
System.out.print(list[i]);
for (Node a = list[i].next; a != null; a = a.next) {
System.out.print(" " + a);
}
System.out.println();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DFS a = new DFS();
a.dfsDriver();
}
}
答案 0 :(得分:-1)
#1:图表创建效率低下。 请在代码中查看此方法:
public Node[] build() {
您需要6个节点,并查看您调用“new Node
”的次数。它的6 + 10倍。尝试修改您的数据结构,使它们适合输入。
目前的DS是:
class Node {
String personName, schoolName;
Node next;
考虑修改它,以便每个节点都可以“指向”多个其他节点,而无需每次都为其内存创建新对象。
#2在dfs方法()中混淆print语句
应该是这样的:
private void dfs(int i, boolean[] visited,
LinkedHashMap<String, Integer> names, Node[] subGraph) {
visited[i] = true;
for (Node e = subGraph[i].next; e != null; e = e.next) {
int index = names.get(e.personName);
if (!visited[index]) {
System.out.println("visiting " + e.personName);
dfs(index, visited, names, subGraph);
}
}
}
#3:没有存储最终结果的机制
您想要主图中的所有连接子图。但是,我没有看到任何存储/标记图表的规定。您可以修改for loop
内部public void dfsDriver()
方法,以便在每次迭代后从新访问的节点创建一个新图形。