public static IEnumerable <TaggedEdge <string, string> > GetEdges(UndirectedGraph <string, TaggedEdge <string, string> > g) { var algorithm = new PrimMinimumSpanningTreeAlgorithm <string, TaggedEdge <string, string> >(g, edgeWeights => double.Parse(edgeWeights.Tag)); var recorder = new EdgeRecorderObserver <string, TaggedEdge <string, string> >(); using (recorder.Attach(algorithm)) { algorithm.Compute(); return(recorder.Edges); } }
public static IEnumerable <Edge <int> > GetEdges(UndirectedGraph <int, Edge <int> > g) { var algorithm = new PrimMinimumSpanningTreeAlgorithm <int, Edge <int> >(g, edgeWeights => 1.0); var recorder = new EdgeRecorderObserver <int, Edge <int> >(); using (recorder.Attach(algorithm)) { algorithm.Compute(); return(recorder.Edges); } }
public static double GetCost(UndirectedGraph <string, TaggedEdge <string, string> > g) { var algorithm = new PrimMinimumSpanningTreeAlgorithm <string, TaggedEdge <string, string> >(g, edgeWeights => double.Parse(edgeWeights.Tag)); var recorder = new EdgeRecorderObserver <string, TaggedEdge <string, string> >(); var distances = new Dictionary <TaggedEdge <string, string>, double>(); foreach (var e in g.Edges) { distances[e] = double.Parse(e.Tag); } using (recorder.Attach(algorithm)) { algorithm.Compute(); return(recorder.Edges.Sum(e => distances[e])); } }
public static double GetCost(UndirectedGraph <int, Edge <int> > g) { var algorithm = new PrimMinimumSpanningTreeAlgorithm <int, Edge <int> >(g, edgeWeights => 1.0); var recorder = new EdgeRecorderObserver <int, Edge <int> >(); var distances = new Dictionary <Edge <int>, double>(); foreach (var edge in g.Edges) { distances[edge] = 1.0; } using (recorder.Attach(algorithm)) { algorithm.Compute(); return(recorder.Edges.Sum(e => distances[e])); } }
public void Compute(IUndirectedGraph<string, Edge<string>> g) { Dictionary<Edge<string>, double> distances = new Dictionary<Edge<string>,double>(); foreach(Edge<string> edge in g.Edges) distances.Add(edge, 1); PrimMinimumSpanningTreeAlgorithm<string, Edge<string>> prim = new PrimMinimumSpanningTreeAlgorithm<string, Edge<string>>(g, distances); VertexPredecessorRecorderObserver<string, Edge<string>> predecessors = new VertexPredecessorRecorderObserver<string, Edge<string>>(); predecessors.Attach(prim); prim.Compute(); foreach (string v in g.Vertices) { Edge<string> edge; if (predecessors.VertexPredecessors.TryGetValue(v, out edge)) Console.WriteLine("{0}: {1}", v, edge); else Console.WriteLine("{0}", v); } }