图邻接表的实现

发布于 2024-11-03 08:44:09 字数 1328 浏览 4 评论 0原文

我正在尝试实现非加权图的邻接列表和一些问题/疑虑。我意识到我需要一个链表来存储边和一个数组来存储顶点。目前我有一个(基本)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2024-11-10 08:44:09

关于代码的一些注释:

  1. 您使用固定大小的数组来存储节点。切换到在添加新节点时自动增长的数组列表。

  2. 我是否正确理解,您可能只有一条边离开节点(下一个)?您还应该在此处使用列表。

  3. 只要您的图不是有向的,请注意从 A 到 B 的边也会从 B 到 A,因此您必须将其添加到节点 A 和节点 B 的边列表中。

A few notes on your code:

  1. You use fixed size array to store your nodes. Switch to an arraylist which grows automatically while adding new nodes.

  2. Do I understand correctly that you may only have a single edge leaving your node (next)? You should also use a list here.

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文