-
Notifications
You must be signed in to change notification settings - Fork 29
Adding Dijkstra #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HenD-11
wants to merge
2
commits into
Sharpach:master
Choose a base branch
from
HenD-11:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adding Dijkstra #26
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,177 @@ | ||
| namespace Dijkstra | ||
| { | ||
| class Dijkstra | ||
| { | ||
| public static Graph SetupExampleGraph() | ||
| { | ||
| Vertex v1 = new Vertex('1'), v2 = new Vertex('2'), v3 = new Vertex('6'); | ||
| Vertex v4 = new Vertex('3'), v5 = new Vertex('5'), v6 = new Vertex('4'); | ||
|
|
||
| #region Setup Edges | ||
|
|
||
| v1.Edges = new List<Edge> | ||
| { | ||
| new Edge(v2, 7), | ||
| new Edge(v3, 14), | ||
| new Edge(v4, 9) | ||
| }; | ||
| v2.Edges = new List<Edge> | ||
| { | ||
| new Edge(v1, 7), | ||
| new Edge(v4, 10), | ||
| new Edge(v6, 15) | ||
| }; | ||
| v3.Edges = new List<Edge> | ||
| { | ||
| new Edge(v1, 14), | ||
| new Edge(v4, 2), | ||
| new Edge(v5, 9), | ||
| }; | ||
| v4.Edges = new List<Edge> | ||
| { | ||
| new Edge(v1, 9), | ||
| new Edge(v2, 10), | ||
| new Edge(v3, 2), | ||
| new Edge(v6, 11) | ||
| }; | ||
| v5.Edges = new List<Edge> | ||
| { | ||
| new Edge(v3, 9), | ||
| new Edge(v6, 6) | ||
| }; | ||
| v6.Edges = new List<Edge> | ||
| { | ||
| new Edge(v2, 15), | ||
| new Edge(v4, 11), | ||
| new Edge(v5, 6) | ||
| }; | ||
|
|
||
| #endregion | ||
|
|
||
| var graph = new Graph(); | ||
| graph.AddVertex(v1); | ||
| graph.AddVertex(v2); | ||
| graph.AddVertex(v3); | ||
| graph.AddVertex(v4); | ||
| graph.AddVertex(v5); | ||
| graph.AddVertex(v6); | ||
|
|
||
| return graph; | ||
| } | ||
|
|
||
| public static void PathPrinter(List<Vertex> path) | ||
| { | ||
| var sb = new StringBuilder(); | ||
| var charsAsStrings = path.Select(v => v.Key.ToString()); | ||
| Console.WriteLine(String.Join(" => ", path.Select(v => v.Key))); | ||
| } | ||
| } | ||
|
|
||
| class Graph | ||
| { | ||
| // Each graph consists of a number of vertices connected by 'edges'. | ||
| private Dictionary<char, Vertex> _vertices; | ||
|
|
||
| public Graph() | ||
| { | ||
| _vertices = new Dictionary<char, Vertex>(); | ||
| } | ||
|
|
||
| public void AddVertex(Vertex vertex) | ||
| { | ||
| _vertices[vertex.Key] = vertex; | ||
| } | ||
|
|
||
| public List<Vertex> GetShortestPath(char start, char finish) | ||
| { | ||
| ResetGraphCost(); | ||
| CalculateCost(start, finish); | ||
|
|
||
| var path = CalculatePath(start, finish); | ||
| // The path is calculated using the "Previous" vertex property. | ||
| // The path is therefore initially the shortest path from finish to start. | ||
| // Reverse the path to get the shortest path from start to finish | ||
| path.Reverse(); | ||
| return path; | ||
| } | ||
|
|
||
| private void ResetGraphCost() | ||
| { | ||
| foreach (var vertex in _vertices.Values) | ||
| { | ||
| vertex.Cost = int.MaxValue; | ||
| } | ||
| } | ||
|
|
||
| private void CalculateCost(char start, char finish) | ||
| { | ||
| // Initialize the start vertex | ||
| _vertices[start].Cost = 0; | ||
| var visitQueue = new Queue<Vertex>(); | ||
| visitQueue.Enqueue(_vertices[start]); | ||
| do | ||
| { | ||
| var currentNode = visitQueue.Dequeue(); | ||
| if (currentNode.Visisted) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Visit each neighboring vertex | ||
| foreach (var edge in currentNode.Edges) | ||
| { | ||
| if (currentNode.Cost + edge.Distance < edge.EndVertex.Cost) | ||
| { | ||
| edge.EndVertex.Cost = currentNode.Cost + edge.Distance; | ||
| edge.EndVertex.Previous = currentNode; | ||
| } | ||
|
|
||
| visitQueue.Enqueue(edge.EndVertex); | ||
| } | ||
| currentNode.Visisted = true; | ||
| } while (visitQueue.Any()); | ||
| } | ||
|
|
||
| private List<Vertex> CalculatePath(char start, char finish) | ||
| { | ||
| var path = new List<Vertex>(); | ||
| var currentVertex = _vertices[finish]; | ||
| path.Add(currentVertex); | ||
| do | ||
| { | ||
| currentVertex = currentVertex.Previous; | ||
| path.Add(currentVertex); | ||
| } while (currentVertex.Previous != null); | ||
|
|
||
| return path; | ||
| } | ||
| } | ||
|
|
||
| class Vertex | ||
| { | ||
| public Vertex(char key) | ||
| { | ||
| Key = key; | ||
| Cost = int.MaxValue; | ||
| Edges = new List<Edge>(); | ||
| } | ||
|
|
||
| public char Key { get; set; } | ||
| public bool Visisted { get; set; } = false; | ||
| public int Cost { get; set; } | ||
| public Vertex Previous { get; set; } | ||
| public List<Edge> Edges { get; set; } | ||
| } | ||
|
|
||
| class Edge | ||
| { | ||
| public Edge(Vertex vertex, int distance) | ||
| { | ||
| EndVertex = vertex; | ||
| Distance = distance; | ||
|
|
||
| } | ||
| public Vertex EndVertex { get; set; } | ||
| public int Distance { get; set; } | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| public class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| var graph = Dijkstra.SetupExampleGraph(); | ||
| var result = graph.GetShortestPath('1', '5'); | ||
| Dijkstra.PathPrinter(result); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide tests with xUnit instead of Program.cs