Created
November 8, 2016 09:14
-
-
Save whoo24/15ce90cc0169862d8808744a750146cb to your computer and use it in GitHub Desktop.
Getting shortest path using Dijkstra algorithm on C#
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
using DijkstraAlgorithm.GraphController; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace DijkstraAlgorithm { | |
public class Dijkstra<T> { | |
readonly int kInfinity = int.MaxValue - 1; | |
public void dijkstra (Graph<T> G, Graph<T>.Node r, out Dictionary<Graph<T>.Node, Graph<T>.Node> result) { | |
List<Graph<T>.Node> S = new List<Graph<T>.Node>(); | |
Dictionary<Graph<T>.Node, int> d = new Dictionary<Graph<T>.Node, int>(); | |
foreach (var u in G.nodes) { | |
d[u] = kInfinity; | |
} | |
d[r] = 0; | |
result = new Dictionary<Graph<T>.Node, Graph<T>.Node>(); | |
while(S.Count != G.n) { | |
var u = extractMin(G.nodes.Except(S).ToList(), d); | |
S.Add(u); | |
foreach(var v in u.GetNeighbors()) { | |
if (G.nodes.Except(S).Contains(v) && (d[u] + u.GetWeight(v) < d[v])) { | |
d[v] = d[u] + u.GetWeight(v); | |
result[v] = u; | |
} | |
} | |
} | |
} | |
public Graph<T>.Node extractMin(List<Graph<T>.Node> Q, Dictionary<Graph<T>.Node, int> d) { | |
// 집합 Q에서 d값이 가장 작은 정점 u를 리턴 | |
int min_weight = kInfinity; | |
Graph<T>.Node min = null; | |
foreach(var n in Q) { | |
if (d[n] <= min_weight) { | |
min_weight = d[n]; | |
min = n; | |
} | |
} | |
return min; | |
} | |
} | |
} |
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
using System.Collections.Generic; | |
namespace DijkstraAlgorithm { | |
public class Graph<T> { | |
private List<Node> nodes_ = new List<Node>(); | |
public class Node { | |
private List<Edge> edges_ = new List<Edge>(); | |
private T context_; | |
public T context { | |
get { | |
return context_; | |
} | |
} | |
public List<Edge> edges { | |
get { | |
return edges_; | |
} | |
} | |
public Node (T context) { | |
context_ = context; | |
} | |
} | |
public class Edge { | |
Node to_; | |
int weight_; | |
public Node to { | |
get { | |
return to_; | |
} | |
} | |
public int weight { | |
get { | |
return weight_; | |
} | |
} | |
public Edge (Node to, int weight) { | |
to_ = to; | |
weight_ = weight; | |
} | |
} | |
public int n { | |
get { | |
return nodes_.Count; | |
} | |
} | |
public List<Node> nodes { | |
get { | |
return nodes_; | |
} | |
} | |
public Graph (IEnumerable<T> initialize_list) { | |
foreach (var i in initialize_list) { | |
AddVertex(i); | |
} | |
} | |
public Graph (Graph<T> othr) { | |
CopyFrom(othr); | |
} | |
public Node AddVertex (T context) { | |
Node a = new Node(context); | |
nodes_.Add(a); | |
return a; | |
} | |
public Node FindVertex (T context) { | |
return nodes_.Find(n => n.context.Equals(context)); | |
} | |
public void CopyFrom (Graph<T> othr) { | |
nodes_.Clear(); | |
nodes_.AddRange(othr.nodes_); | |
} | |
public bool Contains (Node node) { | |
return nodes_.Find(n => n.Equals(node)) != null; | |
} | |
public void Remove (Node node) { | |
nodes_.Remove(node); | |
} | |
} | |
} |
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
using System.Collections.Generic; | |
namespace DijkstraAlgorithm.GraphController { | |
public static class Controller { | |
public static void SetEdge<T> (this Graph<T> self, T a, T b, int weight) { | |
self.FindVertex(a).AddEdge(self.FindVertex(b), weight); | |
} | |
public static void AddEdge<T> (this Graph<T>.Node self, Graph<T>.Node to, int weight) { | |
self.edges.Add(new Graph<T>.Edge(to, weight)); | |
} | |
public static List<Graph<T>.Node> GetNeighbors<T> (this Graph<T>.Node self) { | |
return self.edges.ConvertAll(e => e.to); | |
} | |
public static int GetWeight<T> (this Graph<T>.Node self, Graph<T>.Node b) { | |
foreach (var e in self.edges) { | |
if (e.to == b) { | |
return e.weight; | |
} | |
} | |
return int.MaxValue - 1; | |
} | |
} | |
} |
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
using DijkstraAlgorithm.GraphController; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace DijkstraAlgorithm { | |
class Program { | |
static void Main (string[] args) { | |
Graph<int> W = new Graph<int>(Enumerable.Range(0, 8)); | |
W.SetEdge(0, 1, weight: 8); | |
W.SetEdge(0, 2, weight: 9); | |
W.SetEdge(0, 3, weight: 11); | |
W.SetEdge(1, 4, weight: 10); | |
W.SetEdge(2, 1, weight: 6); | |
W.SetEdge(2, 3, weight: 3); | |
W.SetEdge(2, 4, weight: 1); | |
W.SetEdge(3, 5, weight: 8); | |
W.SetEdge(3, 6, weight: 8); | |
W.SetEdge(4, 7, weight: 2); | |
W.SetEdge(5, 2, weight: 12); | |
W.SetEdge(5, 7, weight: 5); | |
W.SetEdge(6, 5, weight: 7); | |
W.SetEdge(7, 6, weight: 4); | |
Dijkstra<int> dijkstra = new Dijkstra<int>(); | |
Dictionary<Graph<int>.Node, Graph<int>.Node> result; | |
dijkstra.dijkstra(W, W.FindVertex(0), out result); | |
foreach (var kv in result) { | |
Console.WriteLine("{1} => {0}", kv.Key.context, kv.Value.context); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment