簡體   English   中英

圖鄰接表C#

[英]Graph Adjacency List C#

我對使用C#編程相當陌生,並且目前正在嘗試編寫下面包括的通用類Graph和GraphNode。 我了解IsAdjacent和GetNodeByID方法背后的知識,但是我不確定如何在C#中正確編寫這些代碼,因此我在這些方法中包括了少量偽代碼。 但是,AddEdge方法不是這種情況。 如果可能,您可以為我提供這三種方法的解決方案。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Graph
{
    public class GraphNode<T>
    {
        private T id; //data stored in graph
        private LinkedList<T> adjList; //adjacency list
        //constructor
        public GraphNode(T id)
        {
            this.id = id;
            adjList = new LinkedList<T>();
        }
        //add an edge from this node : add to to the adjacency list
        public void AddEdge(GraphNode<T> to)
        {
            adjList.AddFirst(to.ID);
        }
        //set and get for ID – data stored in graph
        public T ID
        {
            set { id = value; }
            get { return id; }
        }
        //returns adjacency list – useful for traversal methods
        public LinkedList<T> GetAdjList()
        {
            return adjList;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Graph
{
    public class Graph<T> where T : IComparable
    {
        //list of GraphNodes in this graph
        private LinkedList<GraphNode<T>> nodes;
        //constructor - set nodes to new empty list
        public Graph()
        {
            nodes = new LinkedList<GraphNode<T>>();
        }
        //only return true if the graph’s list of nodes is empty
        public bool IsEmptyGraph()
        {
            return nodes.Count == 0;
        }
        //Search through list of nodes for node
        //Node will be a new graphnode with the
        // containing the ID to be search for
        public bool ContainsGraph(GraphNode<T> node)
        {
            //search based on ID 
            foreach (GraphNode<T> n in nodes)
            {
                if (n.ID.CompareTo(node.ID) == 0)
                    return true;
            }
            return false;
        }
        //find from in list of nodes and search its adjList for to
        public bool IsAdjacent(GraphNode<T> from, GraphNode<T> to)
        {
            foreach(GraphNode<T> n in nodes)
            {
                if (n.ID  same as from.ID)
                    {   if (from.AdjList contains to.ID)
                    return true;  
                     }
                return false;
            }   
        }


        //add a new graphNode to list of nodes
        public void AddNode(T id)
        {
            GraphNode<T> n = new GraphNode<T>(id);
            nodes.AddFirst(n);
       }


        //Search through list of nodes for node with this ID
        public GraphNode<T> GetNodeByID(T id)
       {
        foreach( GraphNode<T> n in nodes )
         {
          if (id = n.ID)
            {
              return n;
            }
         }     
        return null;
       }

        //find from in list of nodes (look at other methods)
        //and call graphNode method to add an edge to to
        //think about validation here
        public void AddEdge(T from, T to)
        {
        }
        //perform a DFS traversal starting at startID, leaving a list
        //of visitied ID’s in the visited list.
    }
}

非常感謝

一些注意事項:

  • 您的一些方法使用節點“ ID”,而不是節點本身。 僅使用節點難道不是很容易嗎?
  • 對於這些項目中的大多數,是否有理由使用LinkedList而不是List? 這里有一個SO討論關於這個在這里 ,這不是明顯的帶給您的實現什么LinkedList的。

對於鄰接列表,您的AddEdge函數需要接受兩個輸入:您的源節點和目標節點,並將它們添加到彼此的鄰接列表中。 您的Node類中已經有一個AddEdge函數,可將一個頂點添加到其鄰接表中。 因此,您的代碼將如下所示:

public void AddEdge(GraphNode source, GraphNode destination)
{
    source.AddEdge(destination);
    destination.AddEdge(source);
}

對於isAdjacent ,我不清楚您為什么需要搜索整個節點列表。 您只需要檢查一個節點是否在另一個節點的鄰接表中(假設其編碼正確,就應該相反):

public bool isAdjacent(GraphNode source, GraphNode destination)
{
    if (source.AdjList.Contains(destination))
    {
        return true;
    }
    return false;
}

由於上述原因,我尚未回答您有關GetNodeByID的問題-我不確定為什么它是通過ID而不是節點本身完成的。 但是,如果您真的想使用ID(盡管應該是if (id = n.ID)而不是if (id = n.ID) ),則您的方法沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM