Các đối tượng xây dựng cấu trúc đồ thị

Một phần của tài liệu LUẬN VĂN:MÔ PHỎNG MỘT SỐ THUẬT TOÁN TRÊN ĐỒ THỊ ppt (Trang 65 - 68)

5. Giới thiệu chương trình

5.1.1.Các đối tượng xây dựng cấu trúc đồ thị

 Entity: chứa 3 thuộc tính:

+ Key: tên của đỉnh trong đồ thị

+ Value: Trọng số của cạnh (trong trường hợp đồ thị là vô hướng thì trọng số = 1 nghĩa là 2 đỉnh có cạnh nối, = 0 nghĩa là không có cạnh nối)

+ IsDirection: đánh dấu đồ thị ban đầu là có hướng hay vô hướng. public class Entity

{

public virtual string Key { get; set; } public virtual int Value { get; set; }

public static bool IsDirection {get; set;}

 Đối tượng Graph: gồm các thuộc tính: + Tập các đỉnh Vertexs

+ Tập các cạnh. public class Graph {

public Graph() {

Vertexs = new HashSet<Vertex>(); }

public HashSet<Vertex> Vertexs { get; set; }

public int GetEdgeValue(string fromKey, string toKey) {

Vertex from = GetVertex(fromKey); if (from.ConnectTo(toKey))

{

return from.GetEdge(fromKey, toKey).Value; }

return int.MaxValue; }

public Vertex GetVertex(string key) {

foreach (Vertex v in Vertexs) {

if (v.Key == key) return v; }

throw new Exception("Key is not exist."); }

}

 Vertex: Chứa các thuộc tính về đỉnh + Kế thừa các thuộc tính của lớp Entity + Phương thức:

ConnectTo: xác định đỉnh kề với đỉnh đang xét

NextToVertex: Xác định tập các đỉnh kề với đỉnh đang xét. AddEdge: Thêm 1 cạnh (cung) có 1 đầu mút là đỉnh đang xét.

RemoveEdge: Loại bỏ 1 cạnh (cung) có 1 đầu mút là đỉnh đang xét. public class Vertex : Entity

{

private Dictionary<string, Edge> _edges = new Dictionary<string, Edge>();

private IList<string> _nextToVertexs = new List<string>(); public bool ConnectTo(string keyTo)

{

string edgeKey = this.Key + ">" + keyTo; (adsbygoogle = window.adsbygoogle || []).push({});

if (_edges.ContainsKey(edgeKey)) return true;

if (Entity.IsDirection == false) {

edgeKey = keyTo + ">" + this.Key; if (_edges.ContainsKey(edgeKey)) return true;

}

return false; }

public void AddEdge(Edge edge) { _edges.Add(edge.Key, edge); if (edge.FromVertexKey != Key) { _nextToVertexs.Add(edge.FromVertexKey); }

else if (edge.ToVertexKey != Key) {

_nextToVertexs.Add(edge.ToVertexKey); }

}

public void RemoveEdge(Edge edge) { _edges.Remove(edge.Key); if (edge.FromVertexKey != Key) { _nextToVertexs.Remove(edge.FromVertexKey); }

else if (edge.ToVertexKey != Key) {

_nextToVertexs.Remove(edge.ToVertexKey); }

}

public void RemoveAllEdges() {

_edges.Clear(); }

public Edge GetEdge(string fromKey, string toKey) {

if (string.IsNullOrEmpty(fromKey) || string.IsNullOrEmpty(toKey))

throw new ArgumentNullException("Key is not alowed null."); string edgeKey = fromKey + ">" + toKey;

if (_edges.ContainsKey(edgeKey)) return _edges[edgeKey];

else if (Entity.IsDirection == false) {

edgeKey = toKey + ">" + fromKey; return _edges[edgeKey];

}

throw new Exception("Not found edge"); }

public IList<string> GetNextToVertexs() {

return _nextToVertexs; }

 Edge: chứa các thuộc tính về đỉnh thừa kế từ lớp Entity bao gồm 2 thông số: + Cạnh được nối từ FromVertexKey đến ToVertexKey.

+ Trọng số của cạnh (trong trường hợp đồ thị không trọng số thì ta dùng giá trị này để đánh dấu có cạnh nối giữa 2 đỉnh hay không).

public class Edge : Entity {

public override string Key {

get { (adsbygoogle = window.adsbygoogle || []).push({});

return FromVertexKey + ">" + ToVertexKey; } set { base.Key = value; } }

public string FromVertexKey { get; set; } public string ToVertexKey { get; set; }

}

 Một cái “túi” để “đựng” các bước thực hiện theo thuật toán để mô phỏng. public class BagStep

{

private IList<Step> _steps = new List<Step>(); public void AddStep(Step step)

{

_steps.Add(step); }

public IList<Step> Steps {

get { return _steps; } }

}

Một phần của tài liệu LUẬN VĂN:MÔ PHỎNG MỘT SỐ THUẬT TOÁN TRÊN ĐỒ THỊ ppt (Trang 65 - 68)