1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
// C5 example: topological sorting 2005-09-09
|
23
|
|
24
|
// Compile with
|
25
|
// csc /r:C5.dll Toposort.cs
|
26
|
|
27
|
using System;
|
28
|
using System.Text;
|
29
|
using C5;
|
30
|
using SCG = System.Collections.Generic;
|
31
|
using SDD = System.Diagnostics.Debug;
|
32
|
|
33
|
namespace Toposort {
|
34
|
class TestToposort {
|
35
|
public static void Main(String[] args) {
|
36
|
Node<String>
|
37
|
d = new Node<String>("d"),
|
38
|
e = new Node<String>("e"),
|
39
|
c = new Node<String>("c", d, e),
|
40
|
b = new Node<String>("b", d),
|
41
|
a = new Node<String>("a", d, b, c);
|
42
|
foreach (Node<String> n in Toposort0(a))
|
43
|
Console.WriteLine(n);
|
44
|
Console.WriteLine();
|
45
|
foreach (Node<String> n in Toposort1(a))
|
46
|
Console.WriteLine(n);
|
47
|
Console.WriteLine();
|
48
|
foreach (Node<String> n in Toposort2(a))
|
49
|
Console.WriteLine(n);
|
50
|
}
|
51
|
|
52
|
// Toposort 0, adding each node when finished, after its descendants.
|
53
|
// Classic depth-first search. Does not terminate on cyclic graphs.
|
54
|
|
55
|
public static IList<Node<T>> Toposort0<T>(params Node<T>[] starts) {
|
56
|
HashedLinkedList<Node<T>> sorted = new HashedLinkedList<Node<T>>();
|
57
|
foreach (Node<T> start in starts)
|
58
|
if (!sorted.Contains(start))
|
59
|
AddNode0(sorted, start);
|
60
|
return sorted;
|
61
|
}
|
62
|
|
63
|
private static void AddNode0<T>(IList<Node<T>> sorted, Node<T> node) {
|
64
|
SDD.Assert(!sorted.Contains(node));
|
65
|
foreach (Node<T> child in node.children)
|
66
|
if (!sorted.Contains(child))
|
67
|
AddNode0(sorted, child);
|
68
|
sorted.InsertLast(node);
|
69
|
}
|
70
|
|
71
|
// Toposort 1, using hash index to add each node before its descendants.
|
72
|
// Terminates also on cyclic graphs.
|
73
|
|
74
|
public static IList<Node<T>> Toposort1<T>(params Node<T>[] starts) {
|
75
|
HashedLinkedList<Node<T>> sorted = new HashedLinkedList<Node<T>>();
|
76
|
foreach (Node<T> start in starts)
|
77
|
if (!sorted.Contains(start)) {
|
78
|
sorted.InsertLast(start);
|
79
|
AddNode1(sorted, start);
|
80
|
}
|
81
|
return sorted;
|
82
|
}
|
83
|
|
84
|
private static void AddNode1<T>(IList<Node<T>> sorted, Node<T> node) {
|
85
|
SDD.Assert(sorted.Contains(node));
|
86
|
foreach (Node<T> child in node.children)
|
87
|
if (!sorted.Contains(child)) {
|
88
|
sorted.ViewOf(node).InsertFirst(child);
|
89
|
AddNode1(sorted, child);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
// Toposort 2, node rescanning using a view.
|
94
|
// Uses no method call stack and no extra data structures, but slower.
|
95
|
|
96
|
public static IList<Node<T>> Toposort2<T>(params Node<T>[] starts) {
|
97
|
HashedLinkedList<Node<T>> sorted = new HashedLinkedList<Node<T>>();
|
98
|
foreach (Node<T> start in starts)
|
99
|
if (!sorted.Contains(start)) {
|
100
|
sorted.InsertLast(start);
|
101
|
using (IList<Node<T>> cursor = sorted.View(sorted.Count-1,1)) {
|
102
|
do {
|
103
|
Node<T> child;
|
104
|
while (null != (child = PendingChild(sorted, cursor.First))) {
|
105
|
cursor.InsertFirst(child);
|
106
|
cursor.Slide(0,1);
|
107
|
}
|
108
|
} while (cursor.TrySlide(+1));
|
109
|
}
|
110
|
}
|
111
|
return sorted;
|
112
|
}
|
113
|
|
114
|
static Node<T> PendingChild<T>(IList<Node<T>> sorted, Node<T> node) {
|
115
|
foreach (Node<T> child in node.children)
|
116
|
if (!sorted.Contains(child))
|
117
|
return child;
|
118
|
return null;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
class Node<T> {
|
123
|
public readonly T id;
|
124
|
public readonly Node<T>[] children;
|
125
|
|
126
|
public Node(T id, params Node<T>[] children) {
|
127
|
this.id = id; this.children = children;
|
128
|
}
|
129
|
|
130
|
public override String ToString() {
|
131
|
return id.ToString();
|
132
|
}
|
133
|
|
134
|
public Node<T> this[int i] {
|
135
|
set { children[i] = value; }
|
136
|
get { return children[i]; }
|
137
|
}
|
138
|
}
|
139
|
}
|