forked from dotnet/dotnet-console-games
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b7bddf
commit ccf1ab2
Showing
10 changed files
with
219 additions
and
226 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,34 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
public class Graph | ||
{ | ||
public class Node | ||
{ | ||
public int OwnIndex { get; } | ||
public List<int> Connections { get; } | ||
public List<double> Costs { get; } | ||
namespace Prims | ||
{ | ||
public class Graph | ||
{ | ||
public class Node | ||
{ | ||
public int OwnIndex { get; } | ||
public List<int> Connections { get; } | ||
public List<double> Costs { get; } | ||
|
||
public void Add(int other, double cost) | ||
{ | ||
Connections.Add(other); | ||
Costs.Add(cost); | ||
} | ||
public void Add(int other, double cost) | ||
{ | ||
Connections.Add(other); | ||
Costs.Add(cost); | ||
} | ||
|
||
public Node(int ownIndex) | ||
{ | ||
OwnIndex = ownIndex; | ||
Connections = new List<int>(); | ||
Costs = new List<double>(); | ||
} | ||
} | ||
public Node(int ownIndex) | ||
{ | ||
OwnIndex = ownIndex; | ||
Connections = new List<int>(); | ||
Costs = new List<double>(); | ||
} | ||
} | ||
|
||
public Node[] Nodes { get; } | ||
public Node[] Nodes { get; } | ||
|
||
public Graph(Node[] nodes) | ||
{ | ||
Nodes = nodes ?? throw new ArgumentNullException(nameof(nodes)); | ||
} | ||
public Graph(Node[] nodes) | ||
{ | ||
Nodes = nodes ?? throw new ArgumentNullException(nameof(nodes)); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,58 +1,59 @@ | ||
| ||
using System; | ||
using System; | ||
using Towel.DataStructures; | ||
|
||
public static class PrimsAlgorithm | ||
namespace Prims | ||
{ | ||
private readonly struct TwoWayConnection : IComparable<TwoWayConnection> | ||
{ | ||
public readonly int IndexA; | ||
public readonly int IndexB; | ||
public readonly double Cost; | ||
|
||
public TwoWayConnection(int indexA, int indexB, double cost) | ||
{ | ||
IndexA = indexA; | ||
IndexB = indexB; | ||
Cost = cost; | ||
} | ||
|
||
public int CompareTo(TwoWayConnection other) => other.Cost.CompareTo(Cost); // inversed because of how the heap works | ||
} | ||
|
||
public static Graph SimplePrims(Graph graph) | ||
{ | ||
var newGraph = new Graph(new Graph.Node[graph.Nodes.Length]); | ||
var nodes = graph.Nodes; | ||
var current = nodes[0]; | ||
newGraph.Nodes[0] = new Graph.Node(0); | ||
|
||
var heap = new HeapArray<TwoWayConnection>(); | ||
|
||
|
||
while(true) | ||
{ | ||
for (int i = 0; i < current.Connections.Count; i++) | ||
{ | ||
heap.Enqueue(new TwoWayConnection(current.OwnIndex, current.Connections[i], current.Costs[i])); | ||
} | ||
|
||
TwoWayConnection c; | ||
do | ||
{ | ||
if (heap.Count == 0) | ||
return newGraph; | ||
|
||
c = heap.Dequeue(); | ||
} | ||
while (newGraph.Nodes[c.IndexB] != null); | ||
|
||
|
||
newGraph.Nodes[c.IndexA].Add(c.IndexB, c.Cost); | ||
|
||
newGraph.Nodes[c.IndexB] = new Graph.Node(c.IndexB); | ||
current = graph.Nodes[c.IndexB]; | ||
newGraph.Nodes[c.IndexB].Add(c.IndexA, c.Cost); | ||
} | ||
} | ||
public static class PrimsAlgorithm | ||
{ | ||
private readonly struct TwoWayConnection : IComparable<TwoWayConnection> | ||
{ | ||
public readonly int IndexA; | ||
public readonly int IndexB; | ||
public readonly double Cost; | ||
|
||
public TwoWayConnection(int indexA, int indexB, double cost) | ||
{ | ||
IndexA = indexA; | ||
IndexB = indexB; | ||
Cost = cost; | ||
} | ||
|
||
public int CompareTo(TwoWayConnection other) => other.Cost.CompareTo(Cost); // inversed because of how the heap works | ||
} | ||
|
||
public static Graph SimplePrims(Graph graph) | ||
{ | ||
var newGraph = new Graph(new Graph.Node[graph.Nodes.Length]); | ||
var nodes = graph.Nodes; | ||
var current = nodes[0]; | ||
newGraph.Nodes[0] = new Graph.Node(0); | ||
|
||
var heap = new HeapArray<TwoWayConnection>(); | ||
|
||
while (true) | ||
{ | ||
for (int i = 0; i < current.Connections.Count; i++) | ||
{ | ||
heap.Enqueue(new TwoWayConnection(current.OwnIndex, current.Connections[i], current.Costs[i])); | ||
} | ||
|
||
TwoWayConnection c; | ||
do | ||
{ | ||
if (heap.Count == 0) | ||
{ | ||
return newGraph; | ||
} | ||
c = heap.Dequeue(); | ||
} | ||
while (newGraph.Nodes[c.IndexB] != null); | ||
|
||
newGraph.Nodes[c.IndexA].Add(c.IndexB, c.Cost); | ||
|
||
newGraph.Nodes[c.IndexB] = new Graph.Node(c.IndexB); | ||
current = graph.Nodes[c.IndexB]; | ||
newGraph.Nodes[c.IndexB].Add(c.IndexA, c.Cost); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.