region DFS_BFS все ребра обходить или только исходящие summary Стандар

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#region DFS_BFS
//1)все ребра обходить или только исходящие?
/// <summary>
/// Стандартный ДФС
/// </summary>
public IEnumerable<CVDescr<VProperties>> DFS {
get {
Stack<CVDescr<VProperties>> stack = new Stack<CVDescr<VProperties>>();
CVDescr<VProperties>.ResetAllMarks();
foreach (CVDescr<VProperties> vertex in storage.AllVerts) {
if (!vertex.GetMark(CVDescr<VProperties>.Marks.Mark)) {
stack.Push(vertex);
while (stack.Count > 0) {
CVDescr<VProperties> v = stack.Pop();
if (!v.GetMark(CVDescr<VProperties>.Marks.Mark)) {
v.SetMark(CVDescr<VProperties>.Marks.Mark, true);
yield return v;
stack.Push(v);
foreach (CEDescr<VProperties, EProperties> e in this.GetOutEdges(v))
if (!e.VEnd.GetMark(CVDescr<VProperties>.Marks.Mark))
stack.Push(e.VEnd);
}
}
}
}
}
}
/// <summary>
/// Стандартный БФС
/// </summary>
public IEnumerable<CVDescr<VProperties>> BFS {
get {
CVDescr<VProperties>.ResetAllMarks();
Queue<CVDescr<VProperties>> queue = new Queue<CVDescr<VProperties>>();
foreach (CVDescr<VProperties> vertex in storage.AllVerts) {
if (!vertex.GetMark(CVDescr<VProperties>.Marks.Mark)) {
queue.Enqueue(vertex);
while (queue.Count > 0) {
CVDescr<VProperties> v = queue.Dequeue();
yield return v;
foreach (CEDescr<VProperties, EProperties> e in this.GetOutEdges(v)) {
if ( !(e.VEnd.GetMark(CVDescr<VProperties>.Marks.Mark))) {
e.VEnd.SetMark(CVDescr<VProperties>.Marks.Mark, true);
queue.Enqueue(e.VEnd);
}
}
}
}
}
}
}
/// <summary>
/// ДФС от конкретной вершины
/// </summary>
/// <param name="startVertex"></param>
/// <returns></returns>
public IEnumerable<CVDescr<VProperties>> DFS(CVDescr<VProperties> startVertex) {
Stack<CVDescr<VProperties>> stack = new Stack<CVDescr<VProperties>>();
CVDescr<VProperties>.ResetAllMarks();
stack.Push(startVertex);
while (stack.Count > 0) {
CVDescr<VProperties> v = stack.Pop();
if (!v.GetMark(CVDescr<VProperties>.Marks.Mark)) {
v.SetMark(CVDescr<VProperties>.Marks.Mark, true);
yield return v;
stack.Push(v);
foreach (CEDescr<VProperties, EProperties> e in this.GetOutEdges(v))
if (!e.VEnd.GetMark(CVDescr<VProperties>.Marks.Mark))
stack.Push(e.VEnd);
}
}
}
/// <summary>
/// ДФС от конкретной вершины, в порядке: первая - последняя посещенная стандартным дфсом
/// </summary>
/// <param name="startVertex"></param>
/// <returns></returns>
public IEnumerable<CVDescr<VProperties>> DFS_postOrder(CVDescr<VProperties> startVertex) {
Stack<CVDescr<VProperties>> stack = new Stack<CVDescr<VProperties>>();
CVDescr<VProperties>.ResetAllMarks();
stack.Push(startVertex);
while (stack.Count > 0) {
CVDescr<VProperties> v = stack.Pop();
if (!v.GetMark(CVDescr<VProperties>.Marks.Mark)) {
v.SetMark(CVDescr<VProperties>.Marks.Mark, true);
stack.Push(v);
foreach (CEDescr<VProperties, EProperties> e in this.GetOutEdges(v))
if (!e.VEnd.GetMark(CVDescr<VProperties>.Marks.Mark))
stack.Push(e.VEnd);
yield return v;
}
}
}
#endregion