예제 #1
0
        //https://leetcode.com/problems/reconstruct-itinerary/
        public IList <string> FindItinerary(IList <IList <string> > tickets)
        {
            var nodes = new HashSet <GraphNode <string> >(tickets.Count + 1);
            var edges = new List <DirectedEdge <string> >(tickets.Count);

            foreach (var ticket in tickets)
            {
                var source = new GraphNode <string>(ticket[0]);
                if (!nodes.TryGetValue(source, out var srcNode))
                {
                    nodes.Add(source);
                }
                else
                {
                    source = srcNode;
                }

                var destination = new GraphNode <string>(ticket[1]);
                if (!nodes.TryGetValue(destination, out var destNode))
                {
                    nodes.Add(destination);
                }
                else
                {
                    destination = destNode;
                }

                edges.Add(new DirectedEdge <string>(source, destination));
            }
            var graph = new DirectedGraph <string>(nodes, edges);
            var eular = new EularianPath <string>(graph);

            return(eular.GetEularianPath(StartAirport));
        }
예제 #2
0
        public void Directed_single_edge()
        {
            var nodes = new List <GraphNode <int> >()
            {
                new GraphNode <int>(0),
                new GraphNode <int>(1),
            };

            var edges = new List <DirectedEdge <int> >()
            {
                new DirectedEdge <int>(nodes[0], nodes[1]),
            };

            var graph = new DirectedGraph <int>(nodes, edges);
            var eular = new EularianPath <int>(graph);

            Assert.Equal(new[] { 0, 1 }, eular.GetEularianPath(0));
        }
예제 #3
0
        public void Directed_circuit()
        {
            var nodes = new List <GraphNode <int> >()
            {
                new GraphNode <int>(0),
                new GraphNode <int>(1),
                new GraphNode <int>(2)
            };

            var edges = new List <DirectedEdge <int> >()
            {
                new DirectedEdge <int>(nodes[0], nodes[1]),
                new DirectedEdge <int>(nodes[1], nodes[2]),
                new DirectedEdge <int>(nodes[2], nodes[0])
            };

            var graph = new DirectedGraph <int>(nodes, edges);
            var eular = new EularianPath <int>(graph);

            Assert.Equal(new[] { 0, 1, 2, 0 }, eular.GetEularianPath(0));
        }