using System.Collections.Generic;
using ConcurrentPriorityQueue;
public struct Distance<T> {
public class Graph<T> where T: IComparable<T> {
private readonly Dictionary<T, List<Edge>> _adj;
_adj = new Dictionary<T, List<Edge>>();
public void AddEdge(T source) {
_adj[source] = new List<Edge>();
public void AddEdge(T source, T dest, int weight) {
if(!_adj.TryGetValue(source, out verticies)) {
verticies = new List<Edge>();
verticies.Add(new Edge(dest, weight));
_adj[source] = verticies;
public Dictionary<T, T> PrimMinimalSpanningTree(T start) {
var q = new ConcurrentPriorityQueue<T, int>();
var dist = new Dictionary<T, int>();
var path = new Dictionary<T, T>();
var visited = new HashSet<T>();
foreach(var n in _adj.Keys) {
foreach(var edge in _adj[cur]) {
if(visited.Contains(edge.Node)) {
var newDist = edge.Weight;
if(newDist < dist[edge.Node]) {
q.Enqueue(edge.Node, newDist);
dist[edge.Node] = newDist;
Console.WriteLine("dist=[{0}]", string.Join(",", dist));
public readonly int Weight;
public Edge(T node, int weight) {
public override string ToString() {
return string.Format("To={0} Weight={1}", Node, Weight);
public static void Main()
var graph = new Graph<int>();
Console.WriteLine("MinimalSpanningTree: {0}", string.Join("->", graph.PrimMinimalSpanningTree(0)));