-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation with
GetShortestPaths
- Loading branch information
1 parent
d2a69d1
commit ac14a31
Showing
4 changed files
with
190 additions
and
79 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Docs/SuperLinq.Docs/apidoc/SuperLinq.SuperEnumerable.GetShortestPaths.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
uid: SuperLinq.SuperEnumerable.GetShortestPaths``2(``0,System.Func{``0,``1,System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1}}}) | ||
example: [*content] | ||
--- | ||
The following code example demonstrates how to use Dijkstra's algorithm to build a distance map using `GetShortestPaths`. | ||
[!code-csharp[](SuperLinq/GetShortestPaths/GetShortestPaths1.linq#L6-)] | ||
|
||
--- | ||
uid: SuperLinq.SuperEnumerable.GetShortestPaths``2(``0,System.Func{``0,``1,System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1}}},System.Collections.Generic.IEqualityComparer{``0},System.Collections.Generic.IComparer{``1}) | ||
example: [*content] | ||
--- | ||
The following code example demonstrates how to use Dijkstra's algorithm to build a distance map using `GetShortestPaths`. | ||
[!code-csharp[](SuperLinq/GetShortestPaths/GetShortestPaths2.linq#L6-)] |
52 changes: 52 additions & 0 deletions
52
Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths1.linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<Query Kind="Statements"> | ||
<NuGetReference>SuperLinq</NuGetReference> | ||
<Namespace>SuperLinq</Namespace> | ||
</Query> | ||
|
||
var costs = | ||
new[] | ||
{ | ||
(from: "start", to: "a", cost: 1), | ||
(from: "a", to: "b", cost: 2), | ||
(from: "b", to: "c", cost: 3), | ||
(from: "c", to: "d", cost: 4), | ||
(from: "d", to: "end", cost: 5), | ||
(from: "start", to: "A", cost: 10), | ||
(from: "A", to: "B", cost: 20), | ||
(from: "B", to: "C", cost: 30), | ||
(from: "C", to: "D", cost: 40), | ||
(from: "D", to: "end", cost: 50), | ||
(from: "start", to: "END", cost: 10), | ||
(from: "start", to: "END", cost: 1000), | ||
}; | ||
var map = costs | ||
.Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) | ||
.Where(x => | ||
x.to != "start" | ||
&& x.from != "end") | ||
.ToLookup(x => x.from, x => (x.to, x.cost)); | ||
|
||
// Find the shortest path from start to end | ||
var result = SuperEnumerable | ||
.GetShortestPaths<string, int>( | ||
"start", | ||
(state, cost) => map[state] | ||
.Select(x => (x.to, x.cost + cost))); | ||
|
||
foreach (var (key, (from, cost)) in result) | ||
{ | ||
Console.WriteLine($"[{key}] = (from: {from}, totalCost: {cost})"); | ||
} | ||
|
||
// This code produces the following output: | ||
// [start] = (from: , totalCost: 0) | ||
// [a] = (from: start, totalCost: 1) | ||
// [b] = (from: a, totalCost: 3) | ||
// [c] = (from: b, totalCost: 6) | ||
// [END] = (from: start, totalCost: 10) | ||
// [d] = (from: c, totalCost: 10) | ||
// [A] = (from: start, totalCost: 10) | ||
// [end] = (from: d, totalCost: 15) | ||
// [B] = (from: A, totalCost: 30) | ||
// [C] = (from: B, totalCost: 60) | ||
// [D] = (from: C, totalCost: 100) |
49 changes: 49 additions & 0 deletions
49
Docs/SuperLinq.Docs/apidoc/SuperLinq/GetShortestPaths/GetShortestPaths2.linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<Query Kind="Statements"> | ||
<NuGetReference>SuperLinq</NuGetReference> | ||
<Namespace>SuperLinq</Namespace> | ||
</Query> | ||
|
||
var costs = | ||
new[] | ||
{ | ||
(from: "start", to: "a", cost: 1), | ||
(from: "a", to: "b", cost: 2), | ||
(from: "b", to: "c", cost: 3), | ||
(from: "c", to: "d", cost: 4), | ||
(from: "d", to: "end", cost: 5), | ||
(from: "start", to: "A", cost: 10), | ||
(from: "A", to: "B", cost: 20), | ||
(from: "B", to: "C", cost: 30), | ||
(from: "C", to: "D", cost: 40), | ||
(from: "D", to: "end", cost: 50), | ||
(from: "start", to: "END", cost: 10), | ||
(from: "start", to: "END", cost: 1000), | ||
}; | ||
var map = costs | ||
.Concat(costs.Select(x => (from: x.to, to: x.from, x.cost))) | ||
.Where(x => | ||
x.to != "start" | ||
&& x.from != "end") | ||
.ToLookup(x => x.from, x => (x.to, x.cost)); | ||
|
||
// Find the shortest path from start to end | ||
var result = SuperEnumerable | ||
.GetShortestPaths<string, int>( | ||
"start", | ||
(state, cost) => map[state] | ||
.Select(x => (x.to, x.cost + cost)), | ||
StringComparer.OrdinalIgnoreCase, | ||
default); | ||
|
||
foreach (var (key, (from, cost)) in result) | ||
{ | ||
Console.WriteLine($"[{key}] = (from: {from}, totalCost: {cost})"); | ||
} | ||
|
||
// This code produces the following output: | ||
// [start] = (from: , totalCost: 0) | ||
// [a] = (from: start, totalCost: 1) | ||
// [b] = (from: a, totalCost: 3) | ||
// [c] = (from: b, totalCost: 6) | ||
// [END] = (from: start, totalCost: 10) | ||
// [d] = (from: c, totalCost: 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters