图邻接表的实现
我正在尝试实现非加权图的邻接列表和一些问题/疑虑。我意识到我需要一个链表来存储边和一个数组来存储顶点。目前我有一个(基本)Node 类和一个 Graph 类,它负责将边添加到特定顶点。然而,这并没有明确定义边的链接列表。我想做 DFS 和 BFS,想知道我应该怎么做?我是否需要更改已经包含这些方法的代码,或者现在是否需要更改。帮助将不胜感激。
// Inside the graph class
public boolean insertNode(NodeRecord n) {
int j;
if (isFull()) return false;
for (j=0; j<arraySize; j++)
if (node[j]==null)
break;
node[j] = new Node(n);
graphSize++;
return true;
}
public boolean insertEdge(int nodeID, EdgeRecord e) {
int j;
for (j=0; j<arraySize; j++)
if (nodeID==((NodeRecord) node[j].item).getID())
break;
if (j>=arraySize) return false;
node[j].next = new Node(e, node[j].next);
return true;
}
// inside the node class
class Node<E> {
E item;
Node<E> next;
Node(E e) {
item = e;
next = null;
}
Node(E e, Node<E> newNext) {
item = e;
next = newNext;
}
Node(Node<E> n) { // copy constructor
item = n.item;
next = n.next;
}
}
public static void depthFirst(){
for(int i=0;i<mygraph.arraySize;i++){
Node counter =mygraph.node[i];
while(counter!=null){
System.out.println(" " +counter.item);
counter= counter.next;
}
}
}
I am trying to implement the adjacency list for a non-weighted graph and a few questions/concerns. i realize I need a linked list to store the edges and an array to store the vertices.Currently I have a (basic)Node class and a Graph class, which takes care of the addition of the edges to a particular vertex. This however does not explicitly define a linked list for the edges. I want to do a DFS and BFS and was wondering how should I go about it? Do I need to changed the code that I already have to incorporate these methods or now. Help will be appreciated.
// Inside the graph class
public boolean insertNode(NodeRecord n) {
int j;
if (isFull()) return false;
for (j=0; j<arraySize; j++)
if (node[j]==null)
break;
node[j] = new Node(n);
graphSize++;
return true;
}
public boolean insertEdge(int nodeID, EdgeRecord e) {
int j;
for (j=0; j<arraySize; j++)
if (nodeID==((NodeRecord) node[j].item).getID())
break;
if (j>=arraySize) return false;
node[j].next = new Node(e, node[j].next);
return true;
}
// inside the node class
class Node<E> {
E item;
Node<E> next;
Node(E e) {
item = e;
next = null;
}
Node(E e, Node<E> newNext) {
item = e;
next = newNext;
}
Node(Node<E> n) { // copy constructor
item = n.item;
next = n.next;
}
}
public static void depthFirst(){
for(int i=0;i<mygraph.arraySize;i++){
Node counter =mygraph.node[i];
while(counter!=null){
System.out.println(" " +counter.item);
counter= counter.next;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于代码的一些注释:
您使用固定大小的数组来存储节点。切换到在添加新节点时自动增长的数组列表。
我是否正确理解,您可能只有一条边离开节点(
下一个
)?您还应该在此处使用列表。只要您的图不是有向的,请注意从 A 到 B 的边也会从 B 到 A,因此您必须将其添加到节点 A 和节点 B 的边列表中。
A few notes on your code:
You use fixed size array to store your nodes. Switch to an arraylist which grows automatically while adding new nodes.
Do I understand correctly that you may only have a single edge leaving your node (
next
)? You should also use a list here.As long as your graph is not directed take care that an edge running from A to B also goes from B to A and thus you have to add it to edge-lists of node A and node B.