这是一项家庭作业,所以解释和指针优先于解决方案。我们之前的任务是使用邻接矩阵创建Graph模板。这项作业分配是使用邻接列表修改前一作业。我将之前基于矩阵的代码注释掉以供参考。
import java.util.*;
public class Graph {
public Node rootNode;
public List<Node> nodes = new ArrayList<Node>();
Map<Integer, List<Integer>> adjList;
//public int[][] adjMatrix;
int size;
public void setRootNode(Node n) {
rootNode = n;
}
public Node getRootNode() {
return rootNode;
}
public void addNode(Node n) {
nodes.add(n);
}
// This method will be called to make connect two nodes
public void connectNode(Node src, Node dst) {
// if (adjMatrix == null)
// {
// size = nodes.size();
// adjMatrix = new int[size][size];
// }
int srcIndex = nodes.indexOf(src);
int dstIndex = nodes.indexOf(dst);
// adjMatrix[srcIndex][dstIndex] = 1;
// adjMatrix[dstIndex][srcIndex] = 1;
if (!adjList.containsKey(srcIndex))
adjList.put(srcIndex, new ArrayList<Integer>());
adjList.get(srcIndex).add(dstIndex);
if (!adjList.containsKey(dstIndex))
adjList.put(dstIndex, new ArrayList<Integer>());
adjList.get(dstIndex).add(srcIndex);
}
private Node getUnvisitedChildNode(Node n) {
int index = nodes.indexOf(n);
for (int j = 0; j < size; j++)
//if (adjMatrix[index][j] == 1 && ((Node) nodes.get(j)).visited == false)
if (adjList.containsKey(index) && ((Node) nodes.get(j)).visited == false)
return nodes.get(j);
return null;
}
// BFS traversal: iterative version
public void bfs() {
// BFS uses a Queue data structure
Queue<Node> q = new LinkedList<Node>();
q.add(rootNode);
printNode(rootNode);
rootNode.visited = true;
while (!q.isEmpty()) {
Node n = q.remove();
Node child = null;
while ((child = getUnvisitedChildNode(n)) != null) {
child.visited = true;
printNode(child);
q.add(child);
}
}
}
// DFS traversal: iterative version
public void dfs() {
// DFS uses a Stack data structure
Stack<Node> s = new Stack<Node>();
s.push(rootNode);
rootNode.visited = true;
printNode(rootNode);
while (!s.isEmpty()) {
Node n = s.peek();
Node child = getUnvisitedChildNode(n);
if (child != null) {
child.visited = true;
printNode(child);
s.push(child);
} else
s.pop();
}
}
// Utility methods for clearing visited property of node
private void reset() {
for (Node n : nodes)
n.visited = false;
}
// Utility methods for printing the node's label
private void printNode(Node n) {
System.out.print(n.label + " ");
}
// get all neighboring nodes of node n.
public List<Node> getNeighbors(Node n) {
int srcIndex = nodes.indexOf(n);
List<Node> neighbors = new ArrayList<Node>();
// for (int j = 0; j < adjMatrix[srcIndex].length; j++)
// if (adjMatrix[srcIndex][j] == 1)
for (int j = 0; j < adjList.get(srcIndex).size(); j++)
if (adjList.get(srcIndex).contains(j))
neighbors.add(nodes.get(j));
return neighbors;
}
// implement recursive version of dfs
public void dfs(Node n) {
printNode(n); // visit the node
n.visited = true;
for (Node node : getNeighbors(n))
if (!node.visited)
dfs(node);
}
static class Node {
public char label;
public boolean visited = false;
public Node(char label) {
this.label = label;
}
}
public static void main(String[] args) {
// Lets create nodes as given as an example in the article
Node n0 = new Node('0');
Node n1 = new Node('1');
Node n2 = new Node('2');
Node n3 = new Node('3');
Node n4 = new Node('4');
Node n5 = new Node('5');
Node n6 = new Node('6');
Node n7 = new Node('7');
Node n8 = new Node('8');
// Create the graph, add nodes, create edges between nodes
Graph g = new Graph();
g.addNode(n0);
g.addNode(n1);
g.addNode(n2);
g.addNode(n3);
g.addNode(n4);
g.addNode(n5);
g.addNode(n6);
g.addNode(n7);
g.addNode(n8);
g.setRootNode(n8);
g.connectNode(n0, n1);
g.connectNode(n0, n3);
g.connectNode(n0, n8);
g.connectNode(n1, n7);
g.connectNode(n3, n2);
g.connectNode(n3, n4);
g.connectNode(n3, n4);
g.connectNode(n4, n8);
g.connectNode(n2, n7);
g.connectNode(n2, n5);
g.connectNode(n5, n6);
// Perform the DFS and BFS traversal of the graph
// System.out.print("DFS Traversal (Iterative): ");
// g.dfs();
// g.reset();
System.out.print("\nDFS Traversal (Recursive): ");
g.dfs(g.getRootNode());
g.reset();
System.out.print("\nBFS Traversal (Iterative): ");
g.bfs();
g.reset();
}
}
我在connectNode方法中获取了一个Null指针,我在其中引入了ArrayList。再次请记住这是功课。提前感谢您的帮助。
编辑:如何在stackoverflow中显示行号?
答案 0 :(得分:0)
if (!adjList.containsKey(srcIndex))
你的adjList为空。您永远不会在对象中设置此列表。