示例#1
0
        public void TestWDFSFail()
        {
            var graph = new WeightedDirectedGraph <string>();

            var v1 = "One";
            var v2 = "Two";
            var v3 = "Three";
            var v4 = "Four";
            var v5 = "Five";

            graph.AddVertex(v1);
            graph.AddVertex(v2);
            graph.AddVertex(v3);
            graph.AddVertex(v4);
            graph.AddVertex(v5);

            graph.AddEdge(v1, v2, 3);

            graph.AddEdge(v2, v4, 5);
            graph.AddEdge(v3, v5, 7);

            var expected = new LinkedList <string>();

            var actual = DepthFirstSearch <string> .Search(graph, v1, v5);

            Assert.AreEqual(expected, actual);
        }
示例#2
0
        public void TestWBFSShortest()
        {
            var graph = new WeightedDirectedGraph <string>();

            var v1 = "One";
            var v2 = "Two";
            var v3 = "Three";
            var v4 = "Four";
            var v5 = "Five";

            graph.AddVertex(v1);
            graph.AddVertex(v2);
            graph.AddVertex(v3);
            graph.AddVertex(v4);
            graph.AddVertex(v5);

            graph.AddEdge(v1, v2, 3);
            graph.AddEdge(v1, v3, 4);
            graph.AddEdge(v2, v4, 2);
            graph.AddEdge(v3, v5, 1);
            graph.AddEdge(v4, v5, 10);

            var expected = new LinkedList <string>();

            expected.AddLast(v1);
            expected.AddLast(v3);
            expected.AddLast(v5);

            var actual = BreadthFirstSearch <string> .Search(graph, v1, v5);

            Assert.AreEqual(expected, actual);
        }
        public void TestPrint()
        {
            var graph = new WeightedDirectedGraph <string>();

            var v1 = "One";
            var v2 = "Two";
            var v3 = "Three";
            var v4 = "Four";
            var v5 = "Five";

            graph.AddVertex(v1);
            graph.AddVertex(v2);
            graph.AddVertex(v3);
            graph.AddVertex(v4);
            graph.AddVertex(v5);

            graph.AddEdge(v1, v2, 3);
            graph.AddEdge(v2, v4, 5);
            graph.AddEdge(v3, v2, 10);
            graph.AddEdge(v3, v4, 100);
            graph.AddEdge(v3, v5, 40);
            graph.AddEdge(v4, v1, 15);
            graph.AddEdge(v5, v3, 8);

            Console.WriteLine(graph.Print());
        }
        public void TestEdgeAddNegative()
        {
            var graph = new WeightedDirectedGraph <string>();

            var v1 = "One";
            var v2 = "Two";

            Assert.Throws <ArgumentException>(() => graph.AddEdge(v1, v2, -5));
        }
示例#5
0
        static void Main(string[] args)
        {
            var graph = new WeightedDirectedGraph();
            var root  = new Vertice {
                Id = 0
            };

            graph.AddVertice(root);
            graph.AddEdge(new DirectedEdge <Vertice>(root, root, 5));
        }
示例#6
0
    // We use Dijkstra's algorithm from the exit cell. It's like starting at our destination and
    // then running time backwards to see where we could've gotten there from. Connection directions
    // need to be reversed. Like, we can spend forward time to go in one direction, or backwards
    // time to go in the opposite direction. A --> B --> C becomes A <-- B <-- C. As a little
    // optimization we don't initialize the heap w/ all cells. Once the best path time is over our
    // limit, we can just break out of the loop w/o ever putting later cells onto the heap.
    public static int Solve(int cellCount, int exitCell, int timeLimit, int connectionCount, int[,] connections)
    {
        var graph = new WeightedDirectedGraph(cellCount);

        for (int c = 0; c < connectionCount; ++c)
        {
            graph.AddEdge(connections[c, 1], connections[c, 0], connections[c, 2]);
        }

        int mouseCount = 0;
        var pathTimes  = new BinaryHeap(graph.Vertices[exitCell]);

        bool[] visitedCells = new bool[cellCount];

        while (!pathTimes.IsEmpty)
        {
            var closestPath      = pathTimes.Extract();
            var cell             = closestPath.Key;
            int pathTimeFromCell = closestPath.Value;

            if (pathTimeFromCell > timeLimit)
            {
                break;
            }

            ++mouseCount;

            foreach (var neighbor in cell.Neighbors.Where(n => !visitedCells[n.ID]))
            {
                int pathTimeFromNeighborThroughCell = pathTimeFromCell + cell.GetEdgeWeight(neighbor);
                int currentPathTimeFromNeighbor;

                // We know the neighboring cell hasn't been visited yet, so we need to maintain its
                // path cost in the heap. If it's already in the heap, see if a cheaper path exists
                // to it through the cell we're visiting. If it isn't in the heap yet, add it.
                if (pathTimes.TryGetValue(neighbor, out currentPathTimeFromNeighbor))
                {
                    if (pathTimeFromNeighborThroughCell < currentPathTimeFromNeighbor)
                    {
                        pathTimes.Update(neighbor, pathTimeFromNeighborThroughCell);
                    }
                }
                else
                {
                    pathTimes.Add(neighbor, pathTimeFromNeighborThroughCell);
                }
            }

            visitedCells[cell.ID] = true;
        }

        return(mouseCount);
    }
        public static IWeightedDirectedGraph <TVertex, TEdge> ToGraph <TVertex, TEdge>(
            this IEnumerable <IWeightedEndpointPair <TVertex, TEdge> > endpointPairs
            )
        {
            var graph = new WeightedDirectedGraph <TVertex, TEdge>();

            foreach (var endpointPair in endpointPairs)
            {
                graph.AddEdge(endpointPair.Origin, endpointPair.Destination, endpointPair.Edge);
            }

            return(graph);
        }
示例#8
0
        public async Task <IWeightedDirectedGraph <string, Edge> > GetWeightedDirectedGraphAsync()
        {
            var edges = await EdgeService.GetEdgesAsync();

            var graph = new WeightedDirectedGraph <string, Edge>();

            foreach (var edge in edges)
            {
                graph.AddEdge(edge.Origin, edge.Destination, edge);
            }

            return(graph);
        }
        public void TestEdgeAdd()
        {
            var graph = new WeightedDirectedGraph <string>();

            var v1      = "One";
            var v2      = "Two";
            var actual1 = graph.ContainsEdge(v1, v2);

            Assert.AreEqual(false, actual1);

            graph.AddEdge(v1, v2, 5);
            var actual2 = graph.ContainsEdge(v1, v2);

            Assert.AreEqual(true, actual2);
        }
示例#10
0
        public void GetMaxStreams_NoException()
        {
            // Arrange
            var graph    = new WeightedDirectedGraph();
            var vertice0 = new Vertice {
                Id = 0
            };
            var vertice1 = new Vertice {
                Id = 1
            };
            var vertice2 = new Vertice {
                Id = 2
            };
            var vertice3 = new Vertice {
                Id = 3
            };
            var vertice4 = new Vertice {
                Id = 4
            };

            graph.AddVertices(new[] { vertice0, vertice1, vertice2, vertice3, vertice4 });
            graph.AddEdge(new DirectedEdge <Vertice>(vertice0, vertice1, 20));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice0, vertice2, 30));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice0, vertice3, 10));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice1, vertice2, 40));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice1, vertice4, 30));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice2, vertice3, 10));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice2, vertice4, 20));
            graph.AddEdge(new DirectedEdge <Vertice>(vertice3, vertice4, 20));

            // Act
            var streams = graph.GetMaxStream();

            // Assert
            Assert.Equal(60, streams.Sum(x => x.Size));
        }
示例#11
0
        public static WeightedDirectedGraph GetStreamGraph(
            InputDto input,
            int?sizeStorage = null,
            IEnumerable <int> uesdStoreageCustomerId = null)
        {
            var streamGraph = new WeightedDirectedGraph();

            var nextIdVertice = 0;
            var rootVertice   = new Vertice {
                Id = nextIdVertice++
            };

            streamGraph.AddVertice(rootVertice);

            var supplierVartices = new Vertice[input.CountSupplier];

            for (var i = 0; i < input.CountSupplier; i++)
            {
                supplierVartices[i] = new Vertice {
                    Id = nextIdVertice++
                };
                streamGraph.AddVertice(supplierVartices[i]);
                streamGraph.AddEdge(new DirectedEdge <Vertice>(rootVertice, supplierVartices[i], input.MaxSendBySupplier[i]));
            }

            var supplierInTick = new Dictionary <int, Vertice[]>(input.CountSupplier);

            for (var i = 0; i < input.CountSupplier; i++)
            {
                supplierInTick.Add(i, input.MaxSendBySupplierInTick[i]
                                   .Select(x => new Vertice
                {
                    Id = nextIdVertice++
                })
                                   .ToArray());
                streamGraph.AddVertices(supplierInTick[i]);
                for (var j = 0; j < input.CountTicks; j++)
                {
                    streamGraph.AddEdge(new DirectedEdge <Vertice>(supplierVartices[i], supplierInTick[i][j], input.MaxSendBySupplierInTick[i][j]));
                }
            }

            var maxStream      = input.MaxSendBySupplier.Sum();
            var customerInTick = new Dictionary <int, Vertice[]>(input.CountCustomers);

            for (var i = 0; i < input.CountCustomers; i++)
            {
                customerInTick.Add(i, input.MaxGetByCustomerInTick[i]
                                   .Select(x => new Vertice
                {
                    Id = nextIdVertice++
                })
                                   .ToArray());
                streamGraph.AddVertices(customerInTick[i]);
                for (var tick = 0; tick < input.CountTicks; tick++)
                {
                    foreach (var supplierId in input.SupplierIdsForCustomer[i])
                    {
                        streamGraph.AddEdge(new DirectedEdge <Vertice>(supplierInTick[supplierId][tick], customerInTick[i][tick], maxStream));
                    }
                }
            }

            if (sizeStorage != null)
            {
                var storageEdges = new List <DirectedEdge <Vertice> >();
                foreach (var customerId in customerInTick
                         .Where(x => uesdStoreageCustomerId == null ||
                                uesdStoreageCustomerId.Contains(x.Key)))
                {
                    for (var t = 0; t < customerId.Value.Length - 1; t++)
                    {
                        storageEdges.Add(new DirectedEdge <Vertice>(customerId.Value[t], customerId.Value[t + 1], sizeStorage.Value));
                    }
                }
                streamGraph.AddEdges(storageEdges);
            }

            var endVertice = new Vertice {
                Id = nextIdVertice++
            };

            streamGraph.AddVertice(endVertice);
            for (var id = 0; id < input.CountCustomers; id++)
            {
                for (var tick = 0; tick < input.CountTicks; tick++)
                {
                    streamGraph.AddEdge(new DirectedEdge <Vertice>(customerInTick[id][tick], endVertice, input.MaxGetByCustomerInTick[id][tick]));
                }
            }

            return(streamGraph);
        }
示例#12
0
        public void TestSearch3()
        {
            var graph = new WeightedDirectedGraph <string>();

            var a = graph.AddVertex("A");
            var b = graph.AddVertex("B");
            var c = graph.AddVertex("C");
            var d = graph.AddVertex("D");
            var e = graph.AddVertex("E");
            var f = graph.AddVertex("F");
            var g = graph.AddVertex("G");

            graph.AddEdge(a, d, 1);
            graph.AddEdge(a, c, 2);
            graph.AddEdge(b, a, 2);
            graph.AddEdge(c, d, 1);
            graph.AddEdge(c, f, 2);
            graph.AddEdge(d, b, 5);
            graph.AddEdge(d, e, 1);
            graph.AddEdge(d, g, 5);
            graph.AddEdge(d, f, 6);
            graph.AddEdge(e, b, 1);
            graph.AddEdge(f, g, 10);
            graph.AddEdge(g, e, 3);

            var actual = DijkstraSearch <string> .Search(graph, a, g);

            var expected = new string[] { a, d, g };

            Assert.AreEqual(expected, actual);
        }
示例#13
0
        public void TestSearch2()
        {
            var graph = new WeightedDirectedGraph <string>();

            var a = graph.AddVertex("A");
            var b = graph.AddVertex("B");
            var c = graph.AddVertex("C");
            var d = graph.AddVertex("D");
            var e = graph.AddVertex("E");
            var f = graph.AddVertex("F");
            var g = graph.AddVertex("G");
            var h = graph.AddVertex("H");

            graph.AddEdge(a, b, 2);
            graph.AddEdge(a, d, 4);
            graph.AddEdge(a, c, 1);
            graph.AddEdge(b, c, 5);
            graph.AddEdge(b, f, 2);
            graph.AddEdge(b, e, 10);
            graph.AddEdge(c, a, 9);
            graph.AddEdge(c, e, 11);
            graph.AddEdge(d, c, 2);
            graph.AddEdge(e, d, 7);
            graph.AddEdge(e, g, 1);
            graph.AddEdge(f, h, 3);
            graph.AddEdge(g, e, 3);
            graph.AddEdge(g, f, 2);
            graph.AddEdge(h, g, 1);

            var actual = DijkstraSearch <string> .Search(graph, d, e);

            var expected = new string[] { d, c, e };

            Assert.AreEqual(expected, actual);
        }