-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
222 lines (215 loc) · 7.81 KB
/
Copy pathcompression.diff
File metadata and controls
222 lines (215 loc) · 7.81 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
--- input/input.cs
+++ output/output.cs
@@ -59,47 +59,10 @@
/// Thrown when the graph contains a cycle (not a DAG).
/// </exception>
public List<Vertex<T>> Sort(IDirectedWeightedGraph<T> graph)
- {
// Stack to store vertices in reverse topological order.
// We use a stack because DFS naturally gives us reverse topological order.
- var stack = new Stack<Vertex<T>>();
-
- // Track visited vertices to avoid reprocessing and detect cycles.
- var visited = new HashSet<Vertex<T>>();
-
- // Track vertices currently in the recursion stack to detect cycles.
- // If we encounter a vertex that's in the recursion stack, we have a cycle.
- var recursionStack = new HashSet<Vertex<T>>();
-
- // Process all vertices in the graph.
- // We need to iterate through all vertices because the graph might be disconnected.
- for (int i = 0; i < graph.Count; i++)
- {
- var vertex = graph.Vertices[i];
-
- // Skip null vertices (shouldn't happen in a well-formed graph).
- if (vertex == null)
- {
- continue;
- }
-
- // If vertex hasn't been visited, perform DFS from it.
- if (!visited.Contains(vertex))
- {
- DfsTopologicalSort(graph, vertex, visited, recursionStack, stack);
- }
- }
-
- // Convert stack to list. The stack contains vertices in reverse topological order,
- // so we need to reverse it to get the correct topological ordering.
- var result = new List<Vertex<T>>(stack.Count);
- while (stack.Count > 0)
- {
- result.Add(stack.Pop());
- }
-
+ { … 36 line(s) … ⟦tj:1e15748d807d72676d016da0b795e205⟧ }
return result;
- }
/// <summary>
/// Performs topological sort using Kahn's Algorithm (BFS-based approach).
@@ -132,21 +95,10 @@
/// Thrown when the graph contains a cycle (not a DAG).
/// </exception>
public List<Vertex<T>> SortKahn(IDirectedWeightedGraph<T> graph)
- {
// Calculate in-degree for each vertex.
var inDegree = CalculateInDegrees(graph);
-
- // Queue to process vertices with in-degree 0.
- var queue = InitializeQueueWithZeroInDegreeVertices(inDegree);
-
- // Process vertices in topological order.
- var result = ProcessVerticesInTopologicalOrder(graph, inDegree, queue);
-
- // Verify all vertices were processed (no cycles).
- ValidateNoCycles(graph, result);
-
+ { … 10 line(s) … ⟦tj:b1ce9d7750afc156393a6509e09e25df⟧ }
return result;
- }
/// <summary>
/// Calculates the in-degree for each vertex in the graph.
@@ -155,31 +107,10 @@
/// <param name="graph">The graph to analyze.</param>
/// <returns>Dictionary mapping each vertex to its in-degree.</returns>
private Dictionary<Vertex<T>, int> CalculateInDegrees(IDirectedWeightedGraph<T> graph)
- {
var inDegree = new Dictionary<Vertex<T>, int>();
- // Initialize in-degree for all vertices to 0.
- for (int i = 0; i < graph.Count; i++)
- {
- var vertex = graph.Vertices[i];
- if (vertex != null)
- {
- inDegree[vertex] = 0;
- }
- }
-
- // Calculate actual in-degrees by examining all edges.
- for (int i = 0; i < graph.Count; i++)
- {
- var vertex = graph.Vertices[i];
- if (vertex != null)
- {
- IncrementNeighborInDegrees(graph, vertex, inDegree);
- }
- }
-
+ { … 20 line(s) … ⟦tj:87ef3cd0fa49e4601f93342a759dd415⟧ }
return inDegree;
- }
/// <summary>
/// Increments the in-degree for all neighbors of a given vertex.
@@ -208,20 +139,8 @@
/// <param name="inDegree">Dictionary mapping vertices to their in-degrees.</param>
/// <returns>Queue containing all vertices with in-degree 0.</returns>
private Queue<Vertex<T>> InitializeQueueWithZeroInDegreeVertices(Dictionary<Vertex<T>, int> inDegree)
- {
- var queue = new Queue<Vertex<T>>();
-
- foreach (var kvp in inDegree)
- {
- if (kvp.Value == 0)
- {
- queue.Enqueue(kvp.Key);
- }
- }
+ { … 13 line(s) … ⟦tj:c238b9e8cefa1d7e178d12c0946650ca⟧ }
- return queue;
- }
-
/// <summary>
/// Processes vertices in topological order using Kahn's algorithm.
/// Dequeues vertices with in-degree 0 and decreases in-degrees of their neighbors.
@@ -234,20 +153,8 @@
IDirectedWeightedGraph<T> graph,
Dictionary<Vertex<T>, int> inDegree,
Queue<Vertex<T>> queue)
- {
- var result = new List<Vertex<T>>();
+ { … 13 line(s) … ⟦tj:9fb17b8ade5178e65e8486688c9fd6c4⟧ }
- while (queue.Count > 0)
- {
- var vertex = queue.Dequeue();
- result.Add(vertex);
-
- ProcessNeighbors(graph, vertex, inDegree, queue);
- }
-
- return result;
- }
-
/// <summary>
/// Processes neighbors of a vertex by decreasing their in-degrees.
/// If a neighbor's in-degree becomes 0, it's added to the queue.
@@ -261,20 +168,10 @@
Vertex<T> vertex,
Dictionary<Vertex<T>, int> inDegree,
Queue<Vertex<T>> queue)
- {
foreach (var neighbor in graph.GetNeighbors(vertex))
{
- if (neighbor != null)
- {
- inDegree[neighbor]--;
-
- if (inDegree[neighbor] == 0)
- {
- queue.Enqueue(neighbor);
- }
- }
+ { … 9 line(s) … ⟦tj:3b2006792d043afe4192034cffc654fd⟧ }
}
- }
/// <summary>
/// Validates that all vertices were processed, ensuring no cycles exist.
@@ -321,43 +218,10 @@
HashSet<Vertex<T>> visited,
HashSet<Vertex<T>> recursionStack,
Stack<Vertex<T>> stack)
- {
// CYCLE DETECTION:
// If the vertex is in the recursion stack, we've encountered it again
- // in the current DFS path, which means there's a cycle.
- if (recursionStack.Contains(vertex))
- {
- throw new InvalidOperationException(
- $"Graph contains a cycle involving vertex: {vertex}. " +
- "Topological sort is only possible for Directed Acyclic Graphs (DAGs).");
- }
-
- // If already visited, no need to process again.
- if (visited.Contains(vertex))
- {
- return;
- }
-
- // Mark vertex as visited and add to recursion stack.
- visited.Add(vertex);
- recursionStack.Add(vertex);
-
- // Recursively visit all neighbors (descendants).
- // This ensures all dependencies are processed first.
- foreach (var neighbor in graph.GetNeighbors(vertex))
- {
- if (neighbor != null)
- {
- DfsTopologicalSort(graph, neighbor, visited, recursionStack, stack);
- }
- }
-
- // Remove from recursion stack as we're done with this DFS path.
- recursionStack.Remove(vertex);
-
- // POST-ORDER: Add vertex to stack after visiting all descendants.
- // This ensures that all vertices that depend on the current vertex
- // are already in the stack (deeper in the stack).
- stack.Push(vertex);
+ { … 35 line(s) … ⟦tj:627919bd67ff548075c3468f6999014c⟧ }
}
-}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (14419 bytes): call tinyjuice_retrieve with token "5dc7491667a2d80f9805a7947293b5f7"]
\ No newline at end of file