📄 graph.cs
字号:
using System;
namespace MapService
{
/// <summary>
/// The Graph class represents a graph, which is composed of a collection of nodes and edges. This Graph class
/// maintains its collection of nodes using the NodeList class, which is a collection of Node objects.
/// It delegates the edge maintenance to the Node class. The Node class maintains the edge information using
/// the adjacency list technique.
/// </summary>
public class Graph
{
#region Private Member Variables
// private member variables
private NodeList nodes;
#endregion
#region Constructor
/// <summary>
/// Default constructor. Creates a new Graph class instance.
/// </summary>
public Graph()
{
this.nodes = new NodeList();
}
/// <summary>
/// Creates a new graph class instance based on a list of nodes.
/// </summary>
/// <param name="nodes">The list of nodes to populate the newly created Graph class with.</param>
public Graph(NodeList nodes)
{
this.nodes = nodes;
}
#endregion
public virtual void Clear()
{
nodes.Clear();
}
#region Adding Node Methods
/// <summary>
/// Adds a new node to the graph.
/// </summary>
/// <param name="key">The key value of the node to add.</param>
/// <param name="data">The data of the node to add.</param>
/// <returns>A reference to the Node that was created and added to the graph.</returns>
/// <remarks>If there already exists a node in the graph with the same <b>key</b> value then an
/// <b>ArgumentException</b> exception will be thrown.</remarks>
public virtual Node AddNode(string key, object data)
{
// Make sure the key is unique
if (!nodes.ContainsKey(key))
{
Node n = new Node(key, data);
nodes.Add(n);
return n;
}
else
throw new ArgumentException("There already exists a node in the graph with key " + key);
}
/// <summary>
/// Adds a new node to the graph.
/// </summary>
/// <param name="n">A node instance to add to the graph</param>
/// <remarks>If there already exists a node in the graph with the same <b>key</b> value as <b>n</b> then an
/// <b>ArgumentException</b> exception will be thrown.</remarks>
public virtual void AddNode(Node n)
{
// Make sure this node is unique
if (!nodes.ContainsKey(n.Key))
nodes.Add(n);
else
throw new ArgumentException("There already exists a node in the graph with key " + n.Key);
}
public void Remove(Node n)
{
if(nodes.ContainsKey(n.Key))
{
nodes.Remove(n);
}
}
#endregion
public virtual void AddEdge(Node u, Node v, bool twoway)
{
if (nodes.ContainsKey(u.Key) && nodes.ContainsKey(v.Key))
{
if(twoway)
{
u.AddEdge(v, true);//u->v
v.AddEdge(u, true);//v->u
}
else
u.AddEdge(v, false);//u-v
}
}
public virtual bool Contains(Node n)
{
return Contains(n.Key);
}
public virtual bool Contains(string key)
{
return nodes.ContainsKey(key);
}
#region Public Properties
/// <summary>
/// Returns the number of nodes in the graph.
/// </summary>
public virtual int Count
{
get
{
return nodes.Count;
}
}
/// <summary>
/// Returns a reference to the set of nodes in the graph.
/// </summary>
public virtual NodeList Nodes
{
get
{
return this.nodes;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -