图形邻接列表C#

时间:2015-02-26 15:06:06

标签: c# generics graph

我对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.
    }
}

非常感谢

1 个答案:

答案 0 :(得分:1)

几点说明:

  • 您的一些方法采用节点&#34; ID&#34;而不是节点本身。使用节点会不会更容易?
  • 对大多数这些项目使用LinkedList而不是List有什么好的理由吗?有关此here的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)),我不会发现您的方法存在问题。