Repository navigation
Expand file tree
/
Copy pathGraphAlgorithms.cs
More file actions
131 lines (112 loc) · 3.78 KB
/
Copy pathGraphAlgorithms.cs
File metadata and controls
131 lines (112 loc) · 3.78 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
namespace ProblemSolving.Algorithms;
/// <summary>An outgoing weighted edge in an adjacency-list graph.</summary>
public readonly record struct WeightedEdge<TNode>(TNode Node, double Weight);
/// <summary>Graph traversal and shortest-path algorithms.</summary>
public static class GraphAlgorithms
{
public static List<TNode> BreadthFirstSearch<TNode>(
IReadOnlyDictionary<TNode, IReadOnlyCollection<TNode>> graph,
TNode start)
where TNode : notnull
{
ArgumentNullException.ThrowIfNull(graph);
List<TNode> order = [];
HashSet<TNode> visited = [start];
System.Collections.Generic.Queue<TNode> pending = new();
pending.Enqueue(start);
while (pending.Count > 0)
{
TNode node = pending.Dequeue();
order.Add(node);
if (!graph.TryGetValue(node, out IReadOnlyCollection<TNode>? neighbors))
{
continue;
}
foreach (TNode neighbor in neighbors)
{
if (visited.Add(neighbor))
{
pending.Enqueue(neighbor);
}
}
}
return order;
}
public static List<TNode> DepthFirstSearch<TNode>(
IReadOnlyDictionary<TNode, IReadOnlyCollection<TNode>> graph,
TNode start)
where TNode : notnull
{
ArgumentNullException.ThrowIfNull(graph);
List<TNode> order = [];
HashSet<TNode> visited = [];
System.Collections.Generic.Stack<TNode> pending = new();
pending.Push(start);
while (pending.Count > 0)
{
TNode node = pending.Pop();
if (!visited.Add(node))
{
continue;
}
order.Add(node);
if (!graph.TryGetValue(node, out IReadOnlyCollection<TNode>? neighbors))
{
continue;
}
foreach (TNode neighbor in neighbors.Reverse())
{
pending.Push(neighbor);
}
}
return order;
}
public static Dictionary<TNode, double> Dijkstra<TNode>(
IReadOnlyDictionary<TNode, IReadOnlyCollection<WeightedEdge<TNode>>> graph,
TNode start)
where TNode : notnull
{
ArgumentNullException.ThrowIfNull(graph);
Dictionary<TNode, double> distances = [];
foreach ((TNode node, IReadOnlyCollection<WeightedEdge<TNode>> edges) in graph)
{
distances.TryAdd(node, double.PositiveInfinity);
foreach (WeightedEdge<TNode> edge in edges)
{
if (edge.Weight < 0)
{
throw new ArgumentException(
"Dijkstra's algorithm requires non-negative weights.",
nameof(graph));
}
distances.TryAdd(edge.Node, double.PositiveInfinity);
}
}
distances[start] = 0;
PriorityQueue<TNode, double> pending = new();
pending.Enqueue(start, 0);
while (pending.TryDequeue(out TNode node, out double distance))
{
if (distance > distances[node])
{
continue;
}
if (!graph.TryGetValue(
node,
out IReadOnlyCollection<WeightedEdge<TNode>>? edges))
{
continue;
}
foreach (WeightedEdge<TNode> edge in edges)
{
double candidate = distance + edge.Weight;
if (candidate < distances[edge.Node])
{
distances[edge.Node] = candidate;
pending.Enqueue(edge.Node, candidate);
}
}
}
return distances;
}
}