private const int Infinite = 1000000; // Hopefully. Do not change this to max value // ..because during B-Ford, computing case 2 will fail when Cwv will be added to maxval. /// <summary> /// Returns shortest path edge /// </summary> /// <param name="filePath"></param> /// <returns></returns> public Edge1 Execute(string filePath) // { Edge1 retval; _edges = EdgeBuilder.BuildEdges(filePath); //_edges.Add(new Edge(1, 2, -2)); //_edges.Add(new Edge(2, 3, -1)); //_edges.Add(new Edge(3, 1, 4)); //_edges.Add(new Edge(3, 4, 2)); //_edges.Add(new Edge(3, 5, -3)); //_edges.Add(new Edge(6, 4, 1)); //_edges.Add(new Edge(6, 5, -4)); //_edges.Add(new Edge(4, 1, 0)); // To test -ve cost cycle _vertices = EdgeBuilder.ExtractVertices(_edges); _vertices.OrderBy(v => v); AttachTempVertex_S(); if (!BFord()) { throw new InvalidOperationException("Negative cost cycle found in graph"); } ReWeightG(); retval = RunDijkstras(); return(retval); }
private int KruskalRoutine(List <Edge1> edges) { /* Start picking and adding edges to the UnionFind * without creating cycles. Stop when UnionFind components * become 'k'-clusters. */ // Initialize UnionFind UnionFind uf = new UnionFind(EdgeBuilder.ExtractVertices(edges)); int maxSpacing = 0; foreach (Edge1 edge in edges) { // If the vertices (clusters) are in different components, add. if (uf.Find(edge.U) != uf.Find(edge.V)) { // Check if k-cluster reached if (uf.Components == _k) { maxSpacing = edge.Cost; break; } uf.Union(edge.U, edge.V); } } return(maxSpacing); }
public int Execute(string filePath) { int retval = 0; List <Edge1> edges = EdgeBuilder.BuildEdges(filePath); //edges.Add(new Edge1(1, 2, 1)); //edges.Add(new Edge1(1, 3, 3)); //edges.Add(new Edge1(1, 4, 8)); //edges.Add(new Edge1(1, 5, 12)); //edges.Add(new Edge1(1, 6, 13)); //edges.Add(new Edge1(2, 3, 2)); //edges.Add(new Edge1(2, 4, 14)); //edges.Add(new Edge1(2, 5, 11)); //edges.Add(new Edge1(2, 6, 10)); //edges.Add(new Edge1(3, 4, 15)); //edges.Add(new Edge1(3, 5, 17)); //edges.Add(new Edge1(3, 6, 16)); //edges.Add(new Edge1(4, 5, 7)); //edges.Add(new Edge1(4, 6, 19)); //edges.Add(new Edge1(5, 6, 9)); // Kruskal's: Sort edges in asc order of costs PerformQuickSort(edges, 0, edges.Count - 1); retval = KruskalRoutine(edges); return(retval); }
/// <summary> /// Creates list of vertices with the respective cost(infinity) /// for use with the Priority Queue. /// </summary> /// <param name="edges"></param> private List <KeyValuePair <int, int> > SetVerticesForHeap(List <Edge1> edges) { // Create heap data (vertices with cost) List <int> vertices = EdgeBuilder.ExtractVertices(edges); // Assign 'infinite' cost to the vertices. List <KeyValuePair <int, int> > verticesWithCost = new List <KeyValuePair <int, int> >(); foreach (int vertex in vertices) { verticesWithCost.Add(new KeyValuePair <int, int>(Infinity, vertex)); } return(verticesWithCost); }
public void Compute(string filePath) { // Initialize X and T List <int> _X = new List <int>(); // 1st invariant int mstCost = 0; // Read the file in a dict<E, W> List <Edge1> edges = EdgeBuilder.BuildEdges(filePath); //edges.Add(new Edge(1, 6, 7)); //edges.Add(new Edge(5, 6, 4)); //edges.Add(new Edge(1, 5, 1)); //edges.Add(new Edge(1, 2, 6)); //edges.Add(new Edge(1, 4, 4)); //edges.Add(new Edge(2, 5, 7)); //edges.Add(new Edge(4, 5, 5)); //edges.Add(new Edge(2, 4, 3)); //edges.Add(new Edge(2, 3, 4)); //edges.Add(new Edge(3, 4, 2)); // Create heap data (vertices with cost) List <int> vertices = EdgeBuilder.ExtractVertices(edges); // Assign 'infinity' cost to the vertices. List <KeyValuePair <int, int> > verticesWithCost = new List <KeyValuePair <int, int> >(); foreach (int vertex in vertices) { verticesWithCost.Add(new KeyValuePair <int, int>(Int32.MaxValue, vertex)); } // Initialise the min-heap PriorityQueue <int, int> minHeap = new PriorityQueue <int, int>(verticesWithCost); // Begin Prim's MST sequence // Start at an arbitrary vertex int firstVertex = minHeap.Dequeue().Value; _X.Add(firstVertex); foreach (Edge1 edge in edges) { if (edge.U == firstVertex) { minHeap.DecreaseKey(new KeyValuePair <int, int>(edge.Cost, edge.V)); } else if (edge.V == firstVertex) { minHeap.DecreaseKey(new KeyValuePair <int, int>(edge.Cost, edge.U)); } } for (int j = 1; j < vertices.Count; j++) { // Extract min mstCost += minHeap.Peek().Key; _X.Add(minHeap.Dequeue().Value); // Recompute cost of all vertices incident to this (assuming no parallel edges) for (int i = 0; i < edges.Count; i++) { // If 'w'E V-X if (_X.Exists(x => x == edges[i].U) && !_X.Exists(x => x == edges[i].V)) { // V (or w) is the vertex we want to update minHeap.DecreaseKey(new KeyValuePair <int, int>(edges[i].Cost, edges[i].V)); } else if (_X.Exists(x => x == edges[i].V) && !_X.Exists(x => x == edges[i].U)) { // U is the vertex we want to update minHeap.DecreaseKey(new KeyValuePair <int, int>(edges[i].Cost, edges[i].U)); } else if (_X.Exists(x => x == edges[i].V) && _X.Exists(x => x == edges[i].U)) { //edges.RemoveAt(i); // Already in X } } } string stub = ""; }